package Z6;

import e7.AbstractC0892a;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/* JADX INFO: renamed from: Z6.i, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public class C0369i extends H implements InterfaceC0367g, G6.d, B0 {

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

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

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f7515x = AtomicReferenceFieldUpdater.newUpdater(C0369i.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: t, reason: collision with root package name */
    public final E6.d f7516t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final E6.i f7517u;

    public C0369i(int i, E6.d dVar) {
        super(i);
        this.f7516t = dVar;
        this.f7517u = dVar.h();
        this._decisionAndIndex$volatile = 536870911;
        this._state$volatile = C0361b.f7497q;
    }

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

    public static Object F(p0 p0Var, Object obj, int i, O6.q qVar) {
        if (obj instanceof C0377q) {
            return obj;
        }
        if (i != 1 && i != 2) {
            return obj;
        }
        if (qVar != null || (p0Var instanceof InterfaceC0366f)) {
            return new C0376p(obj, p0Var instanceof InterfaceC0366f ? (InterfaceC0366f) p0Var : null, qVar, (CancellationException) null, 16);
        }
        return obj;
    }

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

    public final void C() throws IllegalAccessException, F, InvocationTargetException {
        E6.d dVar = this.f7516t;
        Throwable th = null;
        e7.f fVar = dVar instanceof e7.f ? (e7.f) dVar : null;
        if (fVar != null) {
            loop0: while (true) {
                AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = e7.f.f12545x;
                Object obj = atomicReferenceFieldUpdater.get(fVar);
                B3.f fVar2 = AbstractC0892a.f12535c;
                if (obj == fVar2) {
                    while (!atomicReferenceFieldUpdater.compareAndSet(fVar, fVar2, this)) {
                        if (atomicReferenceFieldUpdater.get(fVar) != fVar2) {
                            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;
            }
            r();
            q(th);
        }
    }

    public final void D(Object obj, int i, O6.q qVar) throws IllegalAccessException, F, InvocationTargetException {
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f7514w;
            Object obj2 = atomicReferenceFieldUpdater.get(this);
            if (obj2 instanceof p0) {
                Object objF = F((p0) obj2, obj, i, qVar);
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj2, objF)) {
                    if (atomicReferenceFieldUpdater.get(this) != obj2) {
                        break;
                    }
                }
                if (!z()) {
                    r();
                }
                s(i);
                return;
            }
            if (obj2 instanceof C0370j) {
                C0370j c0370j = (C0370j) obj2;
                c0370j.getClass();
                if (C0370j.f7522c.compareAndSet(c0370j, 0, 1)) {
                    if (qVar != null) {
                        n(qVar, c0370j.f7541a, obj);
                        return;
                    }
                    return;
                }
            }
            throw new IllegalStateException(("Already resumed, but proposed with update " + obj).toString());
        }
    }

    public final void E(AbstractC0378s abstractC0378s) {
        A6.n nVar = A6.n.f457a;
        E6.d dVar = this.f7516t;
        e7.f fVar = dVar instanceof e7.f ? (e7.f) dVar : null;
        D(nVar, (fVar != null ? fVar.f12546t : null) == abstractC0378s ? 4 : this.f7472s, null);
    }

    @Override // Z6.B0
    public final void a(e7.r rVar, int i) {
        AtomicIntegerFieldUpdater atomicIntegerFieldUpdater;
        int i5;
        do {
            atomicIntegerFieldUpdater = f7513v;
            i5 = atomicIntegerFieldUpdater.get(this);
            if ((i5 & 536870911) != 536870911) {
                throw new IllegalStateException("invokeOnCancellation should be called at most once");
            }
        } while (!atomicIntegerFieldUpdater.compareAndSet(this, i5, ((i5 >> 29) << 29) + i));
        y(rVar);
    }

    @Override // Z6.H
    public final void b(CancellationException cancellationException) throws IllegalAccessException, InvocationTargetException {
        CancellationException cancellationException2;
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f7514w;
            Object obj = atomicReferenceFieldUpdater.get(this);
            if (obj instanceof p0) {
                throw new IllegalStateException("Not completed");
            }
            if (obj instanceof C0377q) {
                return;
            }
            if (!(obj instanceof C0376p)) {
                cancellationException2 = cancellationException;
                C0376p c0376p = new C0376p(obj, (InterfaceC0366f) null, (O6.q) null, cancellationException2, 14);
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, c0376p)) {
                    if (atomicReferenceFieldUpdater.get(this) != obj) {
                        break;
                    }
                }
                return;
            }
            C0376p c0376p2 = (C0376p) obj;
            if (c0376p2.f7539e != null) {
                throw new IllegalStateException("Must be called at most once");
            }
            C0376p c0376pA = C0376p.a(c0376p2, null, cancellationException, 15);
            while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, c0376pA)) {
                if (atomicReferenceFieldUpdater.get(this) != obj) {
                    cancellationException2 = cancellationException;
                }
            }
            InterfaceC0366f interfaceC0366f = c0376p2.f7536b;
            if (interfaceC0366f != null) {
                m(interfaceC0366f, cancellationException);
            }
            O6.q qVar = c0376p2.f7537c;
            if (qVar != null) {
                n(qVar, cancellationException, c0376p2.f7535a);
                return;
            }
            return;
            cancellationException = cancellationException2;
        }
    }

    @Override // Z6.InterfaceC0367g
    public final void c(Object obj, O6.q qVar) throws IllegalAccessException, F, InvocationTargetException {
        D(obj, this.f7472s, qVar);
    }

    @Override // Z6.InterfaceC0367g
    public final B3.f d(Object obj, O6.q qVar) {
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f7514w;
            Object obj2 = atomicReferenceFieldUpdater.get(this);
            boolean z8 = obj2 instanceof p0;
            B3.f fVar = AbstractC0385z.f7564a;
            if (!z8) {
                boolean z9 = obj2 instanceof C0376p;
                return null;
            }
            Object objF = F((p0) obj2, obj, this.f7472s, qVar);
            while (!atomicReferenceFieldUpdater.compareAndSet(this, obj2, objF)) {
                if (atomicReferenceFieldUpdater.get(this) != obj2) {
                    break;
                }
            }
            if (!z()) {
                r();
            }
            return fVar;
        }
    }

    @Override // Z6.H
    public final E6.d e() {
        return this.f7516t;
    }

    @Override // G6.d
    public final G6.d f() {
        E6.d dVar = this.f7516t;
        if (dVar instanceof G6.d) {
            return (G6.d) dVar;
        }
        return null;
    }

    @Override // Z6.H
    public final Throwable g(Object obj) {
        Throwable thG = super.g(obj);
        if (thG != null) {
            return thG;
        }
        return null;
    }

    @Override // E6.d
    public final E6.i h() {
        return this.f7517u;
    }

    @Override // E6.d
    public final void i(Object obj) throws IllegalAccessException, F, InvocationTargetException {
        Throwable thA = A6.j.a(obj);
        if (thA != null) {
            obj = new C0377q(thA, false);
        }
        D(obj, this.f7472s, null);
    }

    @Override // Z6.H
    public final Object j(Object obj) {
        return obj instanceof C0376p ? ((C0376p) obj).f7535a : obj;
    }

    @Override // Z6.H
    public final Object l() {
        return f7514w.get(this);
    }

    public final void m(InterfaceC0366f interfaceC0366f, Throwable th) throws IllegalAccessException, InvocationTargetException {
        try {
            interfaceC0366f.b(th);
        } catch (Throwable th2) {
            AbstractC0385z.p(this.f7517u, new A6.b("Exception in invokeOnCancellation handler for " + this, th2));
        }
    }

    public final void n(O6.q qVar, Throwable th, Object obj) throws IllegalAccessException, InvocationTargetException {
        E6.i iVar = this.f7517u;
        try {
            qVar.j(th, obj, iVar);
        } catch (Throwable th2) {
            AbstractC0385z.p(iVar, new A6.b("Exception in resume onCancellation handler for " + this, th2));
        }
    }

    @Override // Z6.InterfaceC0367g
    public final void o(Object obj) throws F {
        s(this.f7472s);
    }

    public final void p(e7.r rVar, Throwable th) throws IllegalAccessException, InvocationTargetException {
        E6.i iVar = this.f7517u;
        int i = f7513v.get(this) & 536870911;
        if (i == 536870911) {
            throw new IllegalStateException("The index for Segment.onCancellation(..) is broken");
        }
        try {
            rVar.h(i, iVar);
        } catch (Throwable th2) {
            AbstractC0385z.p(iVar, new A6.b("Exception in invokeOnCancellation handler for " + this, th2));
        }
    }

    public final boolean q(Throwable th) throws IllegalAccessException, F, InvocationTargetException {
        Throwable cancellationException;
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f7514w;
            Object obj = atomicReferenceFieldUpdater.get(this);
            if (!(obj instanceof p0)) {
                return false;
            }
            boolean z8 = (obj instanceof InterfaceC0366f) || (obj instanceof e7.r);
            if (th == null) {
                cancellationException = new CancellationException("Continuation " + this + " was cancelled normally");
            } else {
                cancellationException = th;
            }
            C0370j c0370j = new C0370j(cancellationException, z8);
            while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, c0370j)) {
                if (atomicReferenceFieldUpdater.get(this) != obj) {
                    break;
                }
            }
            p0 p0Var = (p0) obj;
            if (p0Var instanceof InterfaceC0366f) {
                m((InterfaceC0366f) obj, th);
            } else if (p0Var instanceof e7.r) {
                p((e7.r) obj, th);
            }
            if (!z()) {
                r();
            }
            s(this.f7472s);
            return true;
        }
    }

    public final void r() {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f7515x;
        L l8 = (L) atomicReferenceFieldUpdater.get(this);
        if (l8 == null) {
            return;
        }
        l8.b();
        atomicReferenceFieldUpdater.set(this, o0.f7534q);
    }

    public final void s(int i) throws F {
        AtomicIntegerFieldUpdater atomicIntegerFieldUpdater;
        int i5;
        do {
            atomicIntegerFieldUpdater = f7513v;
            i5 = atomicIntegerFieldUpdater.get(this);
            int i8 = i5 >> 29;
            if (i8 != 0) {
                if (i8 != 1) {
                    throw new IllegalStateException("Already resumed");
                }
                E6.d dVar = this.f7516t;
                boolean z8 = i == 4;
                if (!z8 && (dVar instanceof e7.f)) {
                    boolean z9 = i == 1 || i == 2;
                    int i9 = this.f7472s;
                    if (z9 == (i9 == 1 || i9 == 2)) {
                        e7.f fVar = (e7.f) dVar;
                        AbstractC0378s abstractC0378s = fVar.f12546t;
                        E6.i iVarH = fVar.f12547u.h();
                        if (AbstractC0892a.j(abstractC0378s, iVarH)) {
                            AbstractC0892a.i(abstractC0378s, iVarH, this);
                            return;
                        }
                        U uA = t0.a();
                        if (uA.f7489s < 4294967296L) {
                            uA.o0(true);
                            try {
                                AbstractC0385z.u(this, dVar, true);
                                do {
                                } while (uA.q0());
                            } finally {
                                try {
                                } finally {
                                }
                            }
                            return;
                        }
                        B6.i iVar = uA.f7491u;
                        if (iVar == null) {
                            iVar = new B6.i();
                            uA.f7491u = iVar;
                        }
                        iVar.addLast(this);
                        return;
                    }
                }
                AbstractC0385z.u(this, dVar, z8);
                return;
            }
        } while (!atomicIntegerFieldUpdater.compareAndSet(this, i5, 1073741824 + (536870911 & i5)));
    }

    public Throwable t(k0 k0Var) {
        return k0Var.r();
    }

    public final String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(B());
        sb.append('(');
        sb.append(AbstractC0385z.y(this.f7516t));
        sb.append("){");
        Object obj = f7514w.get(this);
        sb.append(obj instanceof p0 ? "Active" : obj instanceof C0370j ? "Cancelled" : "Completed");
        sb.append("}@");
        sb.append(AbstractC0385z.m(this));
        return sb.toString();
    }

    public final Object u() {
        AtomicIntegerFieldUpdater atomicIntegerFieldUpdater;
        int i;
        boolean z8 = z();
        do {
            atomicIntegerFieldUpdater = f7513v;
            i = atomicIntegerFieldUpdater.get(this);
            int i5 = i >> 29;
            if (i5 != 0) {
                if (i5 != 2) {
                    throw new IllegalStateException("Already suspended");
                }
                if (z8) {
                    C();
                }
                Object obj = f7514w.get(this);
                if (obj instanceof C0377q) {
                    throw ((C0377q) obj).f7541a;
                }
                int i8 = this.f7472s;
                if (i8 == 1 || i8 == 2) {
                    d0 d0Var = (d0) this.f7517u.K(C0379t.f7546r);
                    if (d0Var != null && !d0Var.a()) {
                        CancellationException cancellationExceptionR = d0Var.r();
                        b(cancellationExceptionR);
                        throw cancellationExceptionR;
                    }
                }
                return j(obj);
            }
        } while (!atomicIntegerFieldUpdater.compareAndSet(this, i, 536870912 + (536870911 & i)));
        if (((L) f7515x.get(this)) == null) {
            w();
        }
        if (z8) {
            C();
        }
        return F6.a.f2580q;
    }

    public final void v() {
        L lW = w();
        if (lW == null || (f7514w.get(this) instanceof p0)) {
            return;
        }
        lW.b();
        f7515x.set(this, o0.f7534q);
    }

    public final L w() {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
        d0 d0Var = (d0) this.f7517u.K(C0379t.f7546r);
        if (d0Var == null) {
            return null;
        }
        L lQ = AbstractC0385z.q(d0Var, true, new C0371k(this, 0));
        do {
            atomicReferenceFieldUpdater = f7515x;
            if (atomicReferenceFieldUpdater.compareAndSet(this, null, lQ)) {
                break;
            }
        } while (atomicReferenceFieldUpdater.get(this) == null);
        return lQ;
    }

    public final void x(O6.l lVar) {
        y(new C0365e(1, lVar));
    }

    /* JADX WARN: Code restructure failed: missing block: B:64:0x00ac, code lost:
    
        A(r8, r2);
     */
    /* JADX WARN: Code restructure failed: missing block: B:65:0x00af, code lost:
    
        throw null;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void y(p0 p0Var) {
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f7514w;
            Object obj = atomicReferenceFieldUpdater.get(this);
            if (obj instanceof C0361b) {
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, p0Var)) {
                    if (atomicReferenceFieldUpdater.get(this) != obj) {
                        break;
                    }
                }
                return;
            }
            if ((obj instanceof InterfaceC0366f) || (obj instanceof e7.r)) {
                break;
            }
            if (obj instanceof C0377q) {
                C0377q c0377q = (C0377q) obj;
                c0377q.getClass();
                if (!C0377q.f7540b.compareAndSet(c0377q, 0, 1)) {
                    A(p0Var, obj);
                    throw null;
                }
                if (obj instanceof C0370j) {
                    if (((C0377q) obj) == null) {
                        c0377q = null;
                    }
                    Throwable th = c0377q != null ? c0377q.f7541a : null;
                    if (p0Var instanceof InterfaceC0366f) {
                        m((InterfaceC0366f) p0Var, th);
                        return;
                    } else {
                        p((e7.r) p0Var, th);
                        return;
                    }
                }
                return;
            }
            if (!(obj instanceof C0376p)) {
                if (p0Var instanceof e7.r) {
                    return;
                }
                C0376p c0376p = new C0376p(obj, (InterfaceC0366f) p0Var, (O6.q) null, (CancellationException) null, 28);
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, c0376p)) {
                    if (atomicReferenceFieldUpdater.get(this) != obj) {
                        break;
                    }
                }
                return;
            }
            C0376p c0376p2 = (C0376p) obj;
            if (c0376p2.f7536b != null) {
                A(p0Var, obj);
                throw null;
            }
            if (p0Var instanceof e7.r) {
                return;
            }
            InterfaceC0366f interfaceC0366f = (InterfaceC0366f) p0Var;
            Throwable th2 = c0376p2.f7539e;
            if (th2 != null) {
                m(interfaceC0366f, th2);
                return;
            }
            C0376p c0376pA = C0376p.a(c0376p2, interfaceC0366f, null, 29);
            while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, c0376pA)) {
                if (atomicReferenceFieldUpdater.get(this) != obj) {
                    break;
                }
            }
            return;
        }
    }

    public final boolean z() {
        if (this.f7472s != 2) {
            return false;
        }
        E6.d dVar = this.f7516t;
        P6.g.c(dVar, "null cannot be cast to non-null type kotlinx.coroutines.internal.DispatchedContinuation<*>");
        return e7.f.f12545x.get((e7.f) dVar) != null;
    }
}
