package o;

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

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.sg, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public class C2448sg extends AbstractC1618js implements InterfaceC2259qg, InterfaceC0117En, Ik0 {
    public static final /* synthetic */ AtomicIntegerFieldUpdater q = AtomicIntegerFieldUpdater.newUpdater(C2448sg.class, "_decisionAndIndex$volatile");
    public static final /* synthetic */ AtomicReferenceFieldUpdater r = AtomicReferenceFieldUpdater.newUpdater(C2448sg.class, Object.class, "_state$volatile");
    public static final /* synthetic */ AtomicReferenceFieldUpdater s = AtomicReferenceFieldUpdater.newUpdater(C2448sg.class, Object.class, "_parentHandle$volatile");
    private volatile /* synthetic */ int _decisionAndIndex$volatile;
    private volatile /* synthetic */ Object _parentHandle$volatile;
    private volatile /* synthetic */ Object _state$volatile;

    /* renamed from: o, reason: collision with root package name */
    public final InterfaceC0220Im f718o;
    public final InterfaceC2747vn p;

    public C2448sg(int i, InterfaceC0220Im interfaceC0220Im) {
        super(i);
        this.f718o = interfaceC0220Im;
        this.p = interfaceC0220Im.h();
        this._decisionAndIndex$volatile = 536870911;
        this._state$volatile = C2013o1.a;
    }

    public static Object E(InterfaceC2998yS interfaceC2998yS, Object obj, int i, TC tc) {
        if (obj instanceof C2075oj) {
            return obj;
        }
        if (i != 1 && i != 2) {
            return obj;
        }
        if (tc != null || (interfaceC2998yS instanceof InterfaceC1594jg)) {
            return new C1885mj(obj, interfaceC2998yS instanceof InterfaceC1594jg ? (InterfaceC1594jg) interfaceC2998yS : null, tc, (Throwable) null, 16);
        }
        return obj;
    }

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

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

    public final void B() {
        InterfaceC0220Im interfaceC0220Im = this.f718o;
        Throwable th = null;
        C1428hs c1428hs = interfaceC0220Im instanceof C1428hs ? (C1428hs) interfaceC0220Im : null;
        if (c1428hs != null) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = C1428hs.s;
            loop0: while (true) {
                Object obj = atomicReferenceFieldUpdater.get(c1428hs);
                C2943xq c2943xq = C1884mi0.e;
                if (obj == c2943xq) {
                    while (!atomicReferenceFieldUpdater.compareAndSet(c1428hs, c2943xq, this)) {
                        if (atomicReferenceFieldUpdater.get(c1428hs) != c2943xq) {
                            break;
                        }
                    }
                    break loop0;
                } else {
                    if (!(obj instanceof Throwable)) {
                        throw new IllegalStateException(("Inconsistent state " + obj).toString());
                    }
                    while (!atomicReferenceFieldUpdater.compareAndSet(c1428hs, obj, null)) {
                        if (atomicReferenceFieldUpdater.get(c1428hs) != obj) {
                            throw new IllegalArgumentException("Failed requirement.");
                        }
                    }
                    th = (Throwable) obj;
                }
            }
            if (th == null) {
                return;
            }
            n();
            k(th);
        }
    }

    public final void C(Object obj, int i, TC tc) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = r;
            Object obj2 = atomicReferenceFieldUpdater.get(this);
            if (obj2 instanceof InterfaceC2998yS) {
                Object objE = E((InterfaceC2998yS) obj2, obj, i, tc);
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj2, objE)) {
                    if (atomicReferenceFieldUpdater.get(this) != obj2) {
                        break;
                    }
                }
                if (!y()) {
                    n();
                }
                o(i);
                return;
            }
            if (obj2 instanceof C2733vg) {
                C2733vg c2733vg = (C2733vg) obj2;
                if (C2733vg.c.compareAndSet(c2733vg, 0, 1)) {
                    if (tc != null) {
                        l(tc, c2733vg.a, obj);
                        return;
                    }
                    return;
                }
            }
            throw new IllegalStateException(("Already resumed, but proposed with update " + obj).toString());
        }
    }

    public final void D(AbstractC2842wn abstractC2842wn) {
        InterfaceC0220Im interfaceC0220Im = this.f718o;
        C1428hs c1428hs = interfaceC0220Im instanceof C1428hs ? (C1428hs) interfaceC0220Im : null;
        C(Oh0.a, (c1428hs != null ? c1428hs.f571o : null) == abstractC2842wn ? 4 : this.n, null);
    }

    public final C2943xq F(Object obj, TC tc) {
        C2943xq c2943xq = C80.c;
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = r;
            Object obj2 = atomicReferenceFieldUpdater.get(this);
            if (!(obj2 instanceof InterfaceC2998yS)) {
                return null;
            }
            Object objE = E((InterfaceC2998yS) obj2, obj, this.n, tc);
            while (!atomicReferenceFieldUpdater.compareAndSet(this, obj2, objE)) {
                if (atomicReferenceFieldUpdater.get(this) != obj2) {
                    break;
                }
            }
            if (!y()) {
                n();
            }
            return c2943xq;
        }
    }

    @Override // o.Ik0
    public final void a(AbstractC2591u50 abstractC2591u50, int i) {
        AtomicIntegerFieldUpdater atomicIntegerFieldUpdater;
        int i2;
        do {
            atomicIntegerFieldUpdater = q;
            i2 = atomicIntegerFieldUpdater.get(this);
            if ((i2 & 536870911) != 536870911) {
                throw new IllegalStateException("invokeOnCancellation should be called at most once");
            }
        } while (!atomicIntegerFieldUpdater.compareAndSet(this, i2, ((i2 >> 29) << 29) + i));
        x(abstractC2591u50);
    }

    @Override // o.AbstractC1618js
    public final void b(CancellationException cancellationException) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        CancellationException cancellationException2;
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = r;
            Object obj = atomicReferenceFieldUpdater.get(this);
            if (obj instanceof InterfaceC2998yS) {
                throw new IllegalStateException("Not completed");
            }
            if (obj instanceof C2075oj) {
                return;
            }
            if (!(obj instanceof C1885mj)) {
                cancellationException2 = cancellationException;
                C1885mj c1885mj = new C1885mj(obj, (InterfaceC1594jg) null, (TC) null, cancellationException2, 14);
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, c1885mj)) {
                    if (atomicReferenceFieldUpdater.get(this) != obj) {
                        break;
                    }
                }
                return;
            }
            C1885mj c1885mj2 = (C1885mj) obj;
            if (c1885mj2.e != null) {
                throw new IllegalStateException("Must be called at most once");
            }
            C1885mj c1885mjA = C1885mj.a(c1885mj2, null, cancellationException, 15);
            while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, c1885mjA)) {
                if (atomicReferenceFieldUpdater.get(this) != obj) {
                    cancellationException2 = cancellationException;
                }
            }
            InterfaceC1594jg interfaceC1594jg = c1885mj2.b;
            if (interfaceC1594jg != null) {
                j(interfaceC1594jg, cancellationException);
            }
            TC tc = c1885mj2.c;
            if (tc != null) {
                l(tc, cancellationException, c1885mj2.a);
                return;
            }
            return;
            cancellationException = cancellationException2;
        }
    }

    @Override // o.AbstractC1618js
    public final InterfaceC0220Im c() {
        return this.f718o;
    }

    @Override // o.AbstractC1618js
    public final Throwable d(Object obj) {
        Throwable thD = super.d(obj);
        if (thD != null) {
            return thD;
        }
        return null;
    }

    @Override // o.InterfaceC0117En
    public final InterfaceC0117En e() {
        InterfaceC0220Im interfaceC0220Im = this.f718o;
        if (interfaceC0220Im instanceof InterfaceC0117En) {
            return (InterfaceC0117En) interfaceC0220Im;
        }
        return null;
    }

    @Override // o.AbstractC1618js
    public final Object f(Object obj) {
        return obj instanceof C1885mj ? ((C1885mj) obj).a : obj;
    }

    @Override // o.InterfaceC0220Im
    public final InterfaceC2747vn h() {
        return this.p;
    }

    @Override // o.AbstractC1618js
    public final Object i() {
        return r.get(this);
    }

    public final void j(InterfaceC1594jg interfaceC1594jg, Throwable th) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        try {
            interfaceC1594jg.a(th);
        } catch (Throwable th2) {
            AbstractC1155f00.t(new C2170pj("Exception in invokeOnCancellation handler for " + this, th2), this.p);
        }
    }

    @Override // o.InterfaceC2259qg
    public final boolean k(Throwable th) {
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = r;
            Object obj = atomicReferenceFieldUpdater.get(this);
            if (!(obj instanceof InterfaceC2998yS)) {
                return false;
            }
            C2733vg c2733vg = new C2733vg(this, th, (obj instanceof InterfaceC1594jg) || (obj instanceof AbstractC2591u50));
            while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, c2733vg)) {
                if (atomicReferenceFieldUpdater.get(this) != obj) {
                    break;
                }
            }
            InterfaceC2998yS interfaceC2998yS = (InterfaceC2998yS) obj;
            if (interfaceC2998yS instanceof InterfaceC1594jg) {
                j((InterfaceC1594jg) obj, th);
            } else if (interfaceC2998yS instanceof AbstractC2591u50) {
                m((AbstractC2591u50) obj, th);
            }
            if (!y()) {
                n();
            }
            o(this.n);
            return true;
        }
    }

    public final void l(TC tc, Throwable th, Object obj) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        InterfaceC2747vn interfaceC2747vn = this.p;
        try {
            tc.d(th, obj, interfaceC2747vn);
        } catch (Throwable th2) {
            AbstractC1155f00.t(new C2170pj("Exception in resume onCancellation handler for " + this, th2), interfaceC2747vn);
        }
    }

    public final void m(AbstractC2591u50 abstractC2591u50, Throwable th) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        InterfaceC2747vn interfaceC2747vn = this.p;
        int i = q.get(this) & 536870911;
        if (i == 536870911) {
            throw new IllegalStateException("The index for Segment.onCancellation(..) is broken");
        }
        try {
            abstractC2591u50.h(i, interfaceC2747vn);
        } catch (Throwable th2) {
            AbstractC1155f00.t(new C2170pj("Exception in invokeOnCancellation handler for " + this, th2), interfaceC2747vn);
        }
    }

    public final void n() {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = s;
        InterfaceC2567ts interfaceC2567ts = (InterfaceC2567ts) atomicReferenceFieldUpdater.get(this);
        if (interfaceC2567ts == null) {
            return;
        }
        interfaceC2567ts.a();
        atomicReferenceFieldUpdater.set(this, C2713vS.l);
    }

    public final void o(int i) {
        AtomicIntegerFieldUpdater atomicIntegerFieldUpdater;
        int i2;
        do {
            atomicIntegerFieldUpdater = q;
            i2 = atomicIntegerFieldUpdater.get(this);
            int i3 = i2 >> 29;
            if (i3 != 0) {
                if (i3 != 1) {
                    throw new IllegalStateException("Already resumed");
                }
                boolean z = i == 4;
                InterfaceC0220Im interfaceC0220Im = this.f718o;
                if (!z && (interfaceC0220Im instanceof C1428hs)) {
                    boolean z2 = i == 1 || i == 2;
                    int i4 = this.n;
                    if (z2 == (i4 == 1 || i4 == 2)) {
                        C1428hs c1428hs = (C1428hs) interfaceC0220Im;
                        AbstractC2842wn abstractC2842wn = c1428hs.f571o;
                        InterfaceC2747vn interfaceC2747vnH = c1428hs.p.h();
                        if (abstractC2842wn.V(interfaceC2747vnH)) {
                            abstractC2842wn.T(interfaceC2747vnH, this);
                            return;
                        }
                        AbstractC0437Qw abstractC0437QwA = AbstractC1688kf0.a();
                        if (abstractC0437QwA.m >= 4294967296L) {
                            abstractC0437QwA.Y(this);
                            return;
                        }
                        abstractC0437QwA.a0(true);
                        try {
                            C2887xC.B(this, interfaceC0220Im, true);
                            do {
                            } while (abstractC0437QwA.c0());
                        } finally {
                            try {
                                return;
                            } finally {
                            }
                        }
                        return;
                    }
                }
                C2887xC.B(this, interfaceC0220Im, z);
                return;
            }
        } while (!atomicIntegerFieldUpdater.compareAndSet(this, i2, 1073741824 + (536870911 & i2)));
    }

    public Throwable p(C1375hJ c1375hJ) {
        return c1375hJ.z();
    }

    public final Object q() {
        AtomicIntegerFieldUpdater atomicIntegerFieldUpdater;
        int i;
        TI ti;
        boolean zY = y();
        do {
            atomicIntegerFieldUpdater = q;
            i = atomicIntegerFieldUpdater.get(this);
            int i2 = i >> 29;
            if (i2 != 0) {
                if (i2 != 2) {
                    throw new IllegalStateException("Already suspended");
                }
                if (zY) {
                    B();
                }
                Object obj = r.get(this);
                if (obj instanceof C2075oj) {
                    throw ((C2075oj) obj).a;
                }
                int i3 = this.n;
                if ((i3 != 1 && i3 != 2) || (ti = (TI) this.p.m(C0669Zu.K)) == null || ti.b()) {
                    return f(obj);
                }
                CancellationException cancellationExceptionZ = ti.z();
                b(cancellationExceptionZ);
                throw cancellationExceptionZ;
            }
        } while (!atomicIntegerFieldUpdater.compareAndSet(this, i, 536870912 + (536870911 & i)));
        if (((InterfaceC2567ts) s.get(this)) == null) {
            t();
        }
        if (zY) {
            B();
        }
        return EnumC0091Dn.l;
    }

    public final void r() {
        InterfaceC2567ts interfaceC2567tsT = t();
        if (interfaceC2567tsT == null || (r.get(this) instanceof InterfaceC2998yS)) {
            return;
        }
        interfaceC2567tsT.a();
        s.set(this, C2713vS.l);
    }

    @Override // o.InterfaceC0220Im
    public final void resumeWith(Object obj) {
        Throwable thA = I20.a(obj);
        if (thA != null) {
            obj = new C2075oj(thA, false);
        }
        C(obj, this.n, null);
    }

    @Override // o.InterfaceC2259qg
    public final void s(Object obj, TC tc) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        C(obj, this.n, tc);
    }

    public final InterfaceC2567ts t() {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
        TI ti = (TI) this.p.m(C0669Zu.K);
        if (ti == null) {
            return null;
        }
        InterfaceC2567ts interfaceC2567tsQ = AbstractC0752ap.Q(ti, true, new C0111Eh(this, 0));
        do {
            atomicReferenceFieldUpdater = s;
            if (atomicReferenceFieldUpdater.compareAndSet(this, null, interfaceC2567tsQ)) {
                break;
            }
        } while (atomicReferenceFieldUpdater.get(this) == null);
        return interfaceC2567tsQ;
    }

    public final String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(A());
        sb.append('(');
        sb.append(AbstractC0249Jp.f0(this.f718o));
        sb.append("){");
        Object obj = r.get(this);
        sb.append(obj instanceof InterfaceC2998yS ? "Active" : obj instanceof C2733vg ? "Cancelled" : "Completed");
        sb.append("}@");
        sb.append(AbstractC0249Jp.K(this));
        return sb.toString();
    }

    @Override // o.InterfaceC2259qg
    public final C2943xq u(Object obj, TC tc) {
        return F(obj, tc);
    }

    @Override // o.InterfaceC2259qg
    public final void v(Object obj) {
        o(this.n);
    }

    public final void w(OC oc) {
        x(new C1499ig(1, oc));
    }

    /* JADX WARN: Code restructure failed: missing block: B:58:0x00a0, code lost:
    
        z(r8, r2);
     */
    /* JADX WARN: Code restructure failed: missing block: B:59:0x00a3, code lost:
    
        throw null;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final void x(o.InterfaceC2998yS r8) {
        /*
            r7 = this;
        L0:
            java.util.concurrent.atomic.AtomicReferenceFieldUpdater r0 = o.C2448sg.r
            java.lang.Object r2 = r0.get(r7)
            boolean r1 = r2 instanceof o.C2013o1
            if (r1 == 0) goto L19
        La:
            boolean r1 = r0.compareAndSet(r7, r2, r8)
            if (r1 == 0) goto L12
            goto L97
        L12:
            java.lang.Object r1 = r0.get(r7)
            if (r1 == r2) goto La
            goto L0
        L19:
            boolean r1 = r2 instanceof o.InterfaceC1594jg
            r3 = 0
            if (r1 != 0) goto La0
            boolean r1 = r2 instanceof o.AbstractC2591u50
            if (r1 != 0) goto La0
            boolean r1 = r2 instanceof o.C2075oj
            if (r1 == 0) goto L4d
            r0 = r2
            o.oj r0 = (o.C2075oj) r0
            java.util.concurrent.atomic.AtomicIntegerFieldUpdater r1 = o.C2075oj.b
            r4 = 0
            r5 = 1
            boolean r1 = r1.compareAndSet(r0, r4, r5)
            if (r1 == 0) goto L49
            boolean r1 = r2 instanceof o.C2733vg
            if (r1 == 0) goto L97
            java.lang.Throwable r0 = r0.a
            boolean r1 = r8 instanceof o.InterfaceC1594jg
            if (r1 == 0) goto L43
            o.jg r8 = (o.InterfaceC1594jg) r8
            r7.j(r8, r0)
            return
        L43:
            o.u50 r8 = (o.AbstractC2591u50) r8
            r7.m(r8, r0)
            return
        L49:
            z(r8, r2)
            throw r3
        L4d:
            boolean r1 = r2 instanceof o.C1885mj
            if (r1 == 0) goto L80
            r1 = r2
            o.mj r1 = (o.C1885mj) r1
            o.jg r4 = r1.b
            if (r4 != 0) goto L7c
            boolean r4 = r8 instanceof o.AbstractC2591u50
            if (r4 == 0) goto L5d
            return
        L5d:
            r4 = r8
            o.jg r4 = (o.InterfaceC1594jg) r4
            java.lang.Throwable r5 = r1.e
            if (r5 == 0) goto L68
            r7.j(r4, r5)
            return
        L68:
            r5 = 29
            o.mj r1 = o.C1885mj.a(r1, r4, r3, r5)
        L6e:
            boolean r3 = r0.compareAndSet(r7, r2, r1)
            if (r3 == 0) goto L75
            goto L97
        L75:
            java.lang.Object r3 = r0.get(r7)
            if (r3 == r2) goto L6e
            goto L0
        L7c:
            z(r8, r2)
            throw r3
        L80:
            boolean r1 = r8 instanceof o.AbstractC2591u50
            if (r1 == 0) goto L85
            return
        L85:
            r3 = r8
            o.jg r3 = (o.InterfaceC1594jg) r3
            o.mj r1 = new o.mj
            r5 = 0
            r6 = 28
            r4 = 0
            r1.<init>(r2, r3, r4, r5, r6)
        L91:
            boolean r3 = r0.compareAndSet(r7, r2, r1)
            if (r3 == 0) goto L98
        L97:
            return
        L98:
            java.lang.Object r3 = r0.get(r7)
            if (r3 == r2) goto L91
            goto L0
        La0:
            z(r8, r2)
            throw r3
        */
        throw new UnsupportedOperationException("Method not decompiled: o.C2448sg.x(o.yS):void");
    }

    public final boolean y() {
        if (this.n != 2) {
            return false;
        }
        C1428hs c1428hs = (C1428hs) this.f718o;
        c1428hs.getClass();
        return C1428hs.s.get(c1428hs) != null;
    }
}
