package V4;

import I2.C0317a;
import a5.AbstractC0988a;
import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import w4.C3137A;

/* JADX INFO: renamed from: V4.m, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes3.dex */
public class C0772m extends M implements InterfaceC0770l, C4.d, N0 {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final /* synthetic */ AtomicIntegerFieldUpdater f8312f = AtomicIntegerFieldUpdater.newUpdater(C0772m.class, "_decisionAndIndex$volatile");

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f8313j = AtomicReferenceFieldUpdater.newUpdater(C0772m.class, Object.class, "_state$volatile");

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f8314k = AtomicReferenceFieldUpdater.newUpdater(C0772m.class, Object.class, "_parentHandle$volatile");
    private volatile /* synthetic */ int _decisionAndIndex$volatile;
    private volatile /* synthetic */ Object _parentHandle$volatile;
    private volatile /* synthetic */ Object _state$volatile;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final A4.d f8315d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final A4.j f8316e;

    public C0772m(int i6, A4.d dVar) {
        super(i6);
        this.f8315d = dVar;
        this.f8316e = dVar.getContext();
        this._decisionAndIndex$volatile = 536870911;
        this._state$volatile = C0750b.f8281a;
    }

    public static Object C(z0 z0Var, Object obj, int i6, J4.f fVar) {
        if (obj instanceof C0784w) {
            return obj;
        }
        if (i6 != 1 && i6 != 2) {
            return obj;
        }
        if (fVar != null || (z0Var instanceof InterfaceC0768k)) {
            return new C0783v(obj, z0Var instanceof InterfaceC0768k ? (InterfaceC0768k) z0Var : null, fVar, (CancellationException) null, 16);
        }
        return obj;
    }

    public static void x(z0 z0Var, Object obj) {
        throw new IllegalStateException(("It's prohibited to register multiple handlers, tried to register " + z0Var + ", already has " + obj).toString());
    }

    public final void A(Object obj, int i6, J4.f fVar) {
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f8313j;
            Object obj2 = atomicReferenceFieldUpdater.get(this);
            if (obj2 instanceof z0) {
                Object objC = C((z0) obj2, obj, i6, fVar);
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj2, objC)) {
                    if (atomicReferenceFieldUpdater.get(this) != obj2) {
                        break;
                    }
                }
                if (!w()) {
                    o();
                }
                p(i6);
                return;
            }
            if (obj2 instanceof C0774n) {
                C0774n c0774n = (C0774n) obj2;
                c0774n.getClass();
                if (C0774n.f8317c.compareAndSet(c0774n, 0, 1)) {
                    if (fVar != null) {
                        m(fVar, c0774n.f8346a, obj);
                        return;
                    }
                    return;
                }
            }
            throw new IllegalStateException(("Already resumed, but proposed with update " + obj).toString());
        }
    }

    public final void B(AbstractC0786y abstractC0786y) {
        C3137A c3137a = C3137A.f25453a;
        A4.d dVar = this.f8315d;
        a5.f fVar = dVar instanceof a5.f ? (a5.f) dVar : null;
        A(c3137a, (fVar != null ? fVar.f10822d : null) == abstractC0786y ? 4 : this.f8257c, null);
    }

    public final C0317a D(Object obj, J4.f fVar) {
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f8313j;
            Object obj2 = atomicReferenceFieldUpdater.get(this);
            boolean z6 = obj2 instanceof z0;
            C0317a c0317a = E.f8230a;
            if (!z6) {
                boolean z7 = obj2 instanceof C0783v;
                return null;
            }
            Object objC = C((z0) obj2, obj, this.f8257c, fVar);
            while (!atomicReferenceFieldUpdater.compareAndSet(this, obj2, objC)) {
                if (atomicReferenceFieldUpdater.get(this) != obj2) {
                    break;
                }
            }
            if (!w()) {
                o();
            }
            return c0317a;
        }
    }

    @Override // V4.N0
    public final void a(a5.q qVar, int i6) {
        AtomicIntegerFieldUpdater atomicIntegerFieldUpdater;
        int i7;
        do {
            atomicIntegerFieldUpdater = f8312f;
            i7 = atomicIntegerFieldUpdater.get(this);
            if ((i7 & 536870911) != 536870911) {
                throw new IllegalStateException("invokeOnCancellation should be called at most once");
            }
        } while (!atomicIntegerFieldUpdater.compareAndSet(this, i7, ((i7 >> 29) << 29) + i6));
        v(qVar);
    }

    @Override // V4.InterfaceC0770l
    public final C0317a b(Object obj, J4.f fVar) {
        return D(obj, fVar);
    }

    @Override // V4.M
    public final void c(CancellationException cancellationException) {
        CancellationException cancellationException2;
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f8313j;
            Object obj = atomicReferenceFieldUpdater.get(this);
            if (obj instanceof z0) {
                throw new IllegalStateException("Not completed");
            }
            if (obj instanceof C0784w) {
                return;
            }
            if (!(obj instanceof C0783v)) {
                cancellationException2 = cancellationException;
                C0783v c0783v = new C0783v(obj, (InterfaceC0768k) null, (J4.f) null, cancellationException2, 14);
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, c0783v)) {
                    if (atomicReferenceFieldUpdater.get(this) != obj) {
                        break;
                    }
                }
                return;
            }
            C0783v c0783v2 = (C0783v) obj;
            if (c0783v2.f8343e != null) {
                throw new IllegalStateException("Must be called at most once");
            }
            C0783v c0783vA = C0783v.a(c0783v2, null, cancellationException, 15);
            while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, c0783vA)) {
                if (atomicReferenceFieldUpdater.get(this) != obj) {
                    cancellationException2 = cancellationException;
                }
            }
            InterfaceC0768k interfaceC0768k = c0783v2.f8340b;
            if (interfaceC0768k != null) {
                j(interfaceC0768k, cancellationException);
            }
            J4.f fVar = c0783v2.f8341c;
            if (fVar != null) {
                m(fVar, cancellationException, c0783v2.f8339a);
                return;
            }
            return;
            cancellationException = cancellationException2;
        }
    }

    @Override // V4.M
    public final A4.d d() {
        return this.f8315d;
    }

    @Override // V4.M
    public final Throwable e(Object obj) {
        Throwable thE = super.e(obj);
        if (thE != null) {
            return thE;
        }
        return null;
    }

    @Override // V4.M
    public final Object f(Object obj) {
        return obj instanceof C0783v ? ((C0783v) obj).f8339a : obj;
    }

    @Override // C4.d
    public final C4.d getCallerFrame() {
        A4.d dVar = this.f8315d;
        if (dVar instanceof C4.d) {
            return (C4.d) dVar;
        }
        return null;
    }

    @Override // A4.d
    public final A4.j getContext() {
        return this.f8316e;
    }

    @Override // V4.M
    public final Object h() {
        return f8313j.get(this);
    }

    @Override // V4.InterfaceC0770l
    public final boolean i(Throwable th) {
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f8313j;
            Object obj = atomicReferenceFieldUpdater.get(this);
            if (!(obj instanceof z0)) {
                return false;
            }
            C0774n c0774n = new C0774n(this, th, (obj instanceof InterfaceC0768k) || (obj instanceof a5.q));
            while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, c0774n)) {
                if (atomicReferenceFieldUpdater.get(this) != obj) {
                    break;
                }
            }
            z0 z0Var = (z0) obj;
            if (z0Var instanceof InterfaceC0768k) {
                j((InterfaceC0768k) obj, th);
            } else if (z0Var instanceof a5.q) {
                n((a5.q) obj, th);
            }
            if (!w()) {
                o();
            }
            p(this.f8257c);
            return true;
        }
    }

    @Override // V4.InterfaceC0770l
    public final boolean isActive() {
        return f8313j.get(this) instanceof z0;
    }

    public final void j(InterfaceC0768k interfaceC0768k, Throwable th) {
        try {
            interfaceC0768k.a(th);
        } catch (Throwable th2) {
            E.s(this.f8316e, new E3.v("Exception in invokeOnCancellation handler for " + this, th2));
        }
    }

    @Override // V4.InterfaceC0770l
    public final void k(Object obj, J4.f fVar) {
        A(obj, this.f8257c, fVar);
    }

    @Override // V4.InterfaceC0770l
    public final void l(Object obj) {
        p(this.f8257c);
    }

    public final void m(J4.f fVar, Throwable th, Object obj) {
        A4.j jVar = this.f8316e;
        try {
            fVar.invoke(th, obj, jVar);
        } catch (Throwable th2) {
            E.s(jVar, new E3.v("Exception in resume onCancellation handler for " + this, th2));
        }
    }

    public final void n(a5.q qVar, Throwable th) {
        A4.j jVar = this.f8316e;
        int i6 = f8312f.get(this) & 536870911;
        if (i6 == 536870911) {
            throw new IllegalStateException("The index for Segment.onCancellation(..) is broken");
        }
        try {
            qVar.h(i6, jVar);
        } catch (Throwable th2) {
            E.s(jVar, new E3.v("Exception in invokeOnCancellation handler for " + this, th2));
        }
    }

    public final void o() {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f8314k;
        P p6 = (P) atomicReferenceFieldUpdater.get(this);
        if (p6 == null) {
            return;
        }
        p6.dispose();
        atomicReferenceFieldUpdater.set(this, y0.f8351a);
    }

    public final void p(int i6) {
        AtomicIntegerFieldUpdater atomicIntegerFieldUpdater;
        int i7;
        do {
            atomicIntegerFieldUpdater = f8312f;
            i7 = atomicIntegerFieldUpdater.get(this);
            int i8 = i7 >> 29;
            if (i8 != 0) {
                if (i8 != 1) {
                    throw new IllegalStateException("Already resumed");
                }
                A4.d dVar = this.f8315d;
                boolean z6 = i6 == 4;
                if (!z6 && (dVar instanceof a5.f)) {
                    boolean z7 = i6 == 1 || i6 == 2;
                    int i9 = this.f8257c;
                    if (z7 == (i9 == 1 || i9 == 2)) {
                        a5.f fVar = (a5.f) dVar;
                        AbstractC0786y abstractC0786y = fVar.f10822d;
                        A4.j context = fVar.f10823e.getContext();
                        if (abstractC0786y.g(context)) {
                            abstractC0786y.c(context, this);
                            return;
                        }
                        Y yA = F0.a();
                        if (yA.f8276b >= 4294967296L) {
                            yA.y(this);
                            return;
                        }
                        yA.A(true);
                        try {
                            E.B(this, dVar, true);
                            do {
                            } while (yA.C());
                        } finally {
                            try {
                            } finally {
                            }
                        }
                        return;
                    }
                }
                E.B(this, dVar, z6);
                return;
            }
        } while (!atomicIntegerFieldUpdater.compareAndSet(this, i7, 1073741824 + (536870911 & i7)));
    }

    public Throwable q(u0 u0Var) {
        return u0Var.getCancellationException();
    }

    public final Object r() {
        AtomicIntegerFieldUpdater atomicIntegerFieldUpdater;
        int i6;
        boolean zW = w();
        do {
            atomicIntegerFieldUpdater = f8312f;
            i6 = atomicIntegerFieldUpdater.get(this);
            int i7 = i6 >> 29;
            if (i7 != 0) {
                if (i7 != 2) {
                    throw new IllegalStateException("Already suspended");
                }
                if (zW) {
                    z();
                }
                Object obj = f8313j.get(this);
                if (obj instanceof C0784w) {
                    throw ((C0784w) obj).f8346a;
                }
                int i8 = this.f8257c;
                if (i8 == 1 || i8 == 2) {
                    InterfaceC0765i0 interfaceC0765i0 = (InterfaceC0765i0) this.f8316e.get(C0763h0.f8306a);
                    if (interfaceC0765i0 != null && !interfaceC0765i0.isActive()) {
                        CancellationException cancellationException = interfaceC0765i0.getCancellationException();
                        c(cancellationException);
                        throw cancellationException;
                    }
                }
                return f(obj);
            }
        } while (!atomicIntegerFieldUpdater.compareAndSet(this, i6, androidx.media3.common.C.BUFFER_FLAG_LAST_SAMPLE + (536870911 & i6)));
        if (((P) f8314k.get(this)) == null) {
            t();
        }
        if (zW) {
            z();
        }
        return B4.a.f492a;
    }

    @Override // A4.d
    public final void resumeWith(Object obj) {
        Throwable thA = w4.n.a(obj);
        if (thA != null) {
            obj = new C0784w(thA, false);
        }
        A(obj, this.f8257c, null);
    }

    public final void s() {
        P pT = t();
        if (pT == null || (f8313j.get(this) instanceof z0)) {
            return;
        }
        pT.dispose();
        f8314k.set(this, y0.f8351a);
    }

    public final P t() {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
        InterfaceC0765i0 interfaceC0765i0 = (InterfaceC0765i0) this.f8316e.get(C0763h0.f8306a);
        if (interfaceC0765i0 == null) {
            return null;
        }
        P pT = E.t(interfaceC0765i0, true, new C0776o(this, 0));
        do {
            atomicReferenceFieldUpdater = f8314k;
            if (atomicReferenceFieldUpdater.compareAndSet(this, null, pT)) {
                break;
            }
        } while (atomicReferenceFieldUpdater.get(this) == null);
        return pT;
    }

    public final String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(y());
        sb.append('(');
        sb.append(E.E(this.f8315d));
        sb.append("){");
        Object obj = f8313j.get(this);
        sb.append(obj instanceof z0 ? "Active" : obj instanceof C0774n ? "Cancelled" : "Completed");
        sb.append("}@");
        sb.append(E.p(this));
        return sb.toString();
    }

    public final void u(J4.c cVar) {
        v(new C0766j(cVar, 1));
    }

    /* JADX WARN: Code restructure failed: missing block: B:64:0x00b9, code lost:
    
        x(r8, r2);
     */
    /* JADX WARN: Code restructure failed: missing block: B:65:0x00bc, code lost:
    
        throw null;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void v(z0 z0Var) {
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f8313j;
            Object obj = atomicReferenceFieldUpdater.get(this);
            if (obj instanceof C0750b) {
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, z0Var)) {
                    if (atomicReferenceFieldUpdater.get(this) != obj) {
                        break;
                    }
                }
                return;
            }
            if ((obj instanceof InterfaceC0768k) || (obj instanceof a5.q)) {
                break;
            }
            if (obj instanceof C0784w) {
                C0784w c0784w = (C0784w) obj;
                c0784w.getClass();
                if (!C0784w.f8345b.compareAndSet(c0784w, 0, 1)) {
                    x(z0Var, obj);
                    throw null;
                }
                if (obj instanceof C0774n) {
                    if (!(obj instanceof C0784w)) {
                        c0784w = null;
                    }
                    Throwable th = c0784w != null ? c0784w.f8346a : null;
                    if (z0Var instanceof InterfaceC0768k) {
                        j((InterfaceC0768k) z0Var, th);
                        return;
                    } else {
                        kotlin.jvm.internal.o.f(z0Var, "null cannot be cast to non-null type kotlinx.coroutines.internal.Segment<*>");
                        n((a5.q) z0Var, th);
                        return;
                    }
                }
                return;
            }
            if (!(obj instanceof C0783v)) {
                if (z0Var instanceof a5.q) {
                    return;
                }
                kotlin.jvm.internal.o.f(z0Var, "null cannot be cast to non-null type kotlinx.coroutines.CancelHandler");
                C0783v c0783v = new C0783v(obj, (InterfaceC0768k) z0Var, (J4.f) null, (CancellationException) null, 28);
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, c0783v)) {
                    if (atomicReferenceFieldUpdater.get(this) != obj) {
                        break;
                    }
                }
                return;
            }
            C0783v c0783v2 = (C0783v) obj;
            if (c0783v2.f8340b != null) {
                x(z0Var, obj);
                throw null;
            }
            if (z0Var instanceof a5.q) {
                return;
            }
            kotlin.jvm.internal.o.f(z0Var, "null cannot be cast to non-null type kotlinx.coroutines.CancelHandler");
            InterfaceC0768k interfaceC0768k = (InterfaceC0768k) z0Var;
            Throwable th2 = c0783v2.f8343e;
            if (th2 != null) {
                j(interfaceC0768k, th2);
                return;
            }
            C0783v c0783vA = C0783v.a(c0783v2, interfaceC0768k, null, 29);
            while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, c0783vA)) {
                if (atomicReferenceFieldUpdater.get(this) != obj) {
                    break;
                }
            }
            return;
        }
    }

    public final boolean w() {
        if (this.f8257c != 2) {
            return false;
        }
        A4.d dVar = this.f8315d;
        kotlin.jvm.internal.o.f(dVar, "null cannot be cast to non-null type kotlinx.coroutines.internal.DispatchedContinuation<*>");
        return a5.f.f10821k.get((a5.f) dVar) != null;
    }

    public String y() {
        return "CancellableContinuation";
    }

    public final void z() {
        A4.d dVar = this.f8315d;
        Throwable th = null;
        a5.f fVar = dVar instanceof a5.f ? (a5.f) dVar : null;
        if (fVar != null) {
            loop0: while (true) {
                AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = a5.f.f10821k;
                Object obj = atomicReferenceFieldUpdater.get(fVar);
                C0317a c0317a = AbstractC0988a.f10811c;
                if (obj == c0317a) {
                    while (!atomicReferenceFieldUpdater.compareAndSet(fVar, c0317a, this)) {
                        if (atomicReferenceFieldUpdater.get(fVar) != c0317a) {
                            break;
                        }
                    }
                    break loop0;
                } else {
                    if (!(obj instanceof Throwable)) {
                        throw new IllegalStateException(("Inconsistent state " + obj).toString());
                    }
                    while (!atomicReferenceFieldUpdater.compareAndSet(fVar, obj, null)) {
                        if (atomicReferenceFieldUpdater.get(fVar) != obj) {
                            throw new IllegalArgumentException("Failed requirement.");
                        }
                    }
                    th = (Throwable) obj;
                }
            }
            if (th == null) {
                return;
            }
            o();
            i(th);
        }
    }
}
