package kotlinx.coroutines.channels;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import kotlin.coroutines.Continuation;
import kotlinx.coroutines.internal.s;
import kotlinx.coroutines.internal.t;
import kotlinx.coroutines.v1;

/* JADX INFO: loaded from: classes.dex */
public final class a implements v1 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public Object f29302a = c.f29331p;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public kotlinx.coroutines.h f29303b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final /* synthetic */ b f29304c;

    public a(b bVar) {
        this.f29304c = bVar;
    }

    @Override // kotlinx.coroutines.v1
    public final void a(s sVar, int i10) {
        kotlinx.coroutines.h hVar = this.f29303b;
        if (hVar != null) {
            hVar.a(sVar, i10);
        }
    }

    public final Object b(Continuation continuation) throws Throwable {
        i iVar;
        kotlinx.coroutines.h hVar;
        Boolean bool;
        i iVar2;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = b.f29310h;
        b bVar = this.f29304c;
        i iVar3 = (i) atomicReferenceFieldUpdater.get(bVar);
        while (true) {
            bVar.getClass();
            if (bVar.u(b.f29305c.get(bVar), true)) {
                this.f29302a = c.f29327l;
                Throwable thP = bVar.p();
                if (thP == null) {
                    return Boolean.FALSE;
                }
                int i10 = t.f29488a;
                throw thP;
            }
            long andIncrement = b.f29306d.getAndIncrement(bVar);
            long j10 = c.f29317b;
            long j11 = andIncrement / j10;
            int i11 = (int) (andIncrement % j10);
            if (iVar3.f29487c != j11) {
                i iVarO = bVar.o(j11, iVar3);
                if (iVarO == null) {
                    continue;
                } else {
                    iVar = iVarO;
                }
            } else {
                iVar = iVar3;
            }
            Object objE = bVar.E(iVar, i11, andIncrement, null);
            la.e eVar = c.f29328m;
            if (objE == eVar) {
                throw new IllegalStateException("unreachable".toString());
            }
            la.e eVar2 = c.f29330o;
            if (objE != eVar2) {
                if (objE != c.f29329n) {
                    iVar.a();
                    this.f29302a = objE;
                    return Boolean.TRUE;
                }
                b bVar2 = this.f29304c;
                kotlinx.coroutines.h hVarC0 = hb.a.c0(nb.g.F(continuation));
                try {
                    this.f29303b = hVarC0;
                    hVar = hVarC0;
                } catch (Throwable th) {
                    th = th;
                    hVar = hVarC0;
                }
                try {
                    Object objE2 = bVar2.E(iVar, i11, andIncrement, this);
                    if (objE2 == eVar) {
                        a(iVar, i11);
                    } else {
                        sd.l lVarA = null;
                        kotlin.coroutines.h hVar2 = hVar.f29439e;
                        sd.l lVar = bVar2.f29315b;
                        if (objE2 == eVar2) {
                            if (andIncrement < bVar2.s()) {
                                iVar.a();
                            }
                            i iVar4 = (i) b.f29310h.get(bVar2);
                            while (true) {
                                if (bVar2.u(b.f29305c.get(bVar2), true)) {
                                    kotlinx.coroutines.h hVar3 = this.f29303b;
                                    nb.g.f(hVar3);
                                    this.f29303b = null;
                                    this.f29302a = c.f29327l;
                                    Throwable thP2 = bVar.p();
                                    if (thP2 == null) {
                                        hVar3.resumeWith(Boolean.FALSE);
                                    } else {
                                        hVar3.resumeWith(kotlin.a.b(thP2));
                                    }
                                } else {
                                    long andIncrement2 = b.f29306d.getAndIncrement(bVar2);
                                    long j12 = c.f29317b;
                                    long j13 = andIncrement2 / j12;
                                    int i12 = (int) (andIncrement2 % j12);
                                    if (iVar4.f29487c != j13) {
                                        i iVarO2 = bVar2.o(j13, iVar4);
                                        if (iVarO2 != null) {
                                            iVar2 = iVarO2;
                                        }
                                    } else {
                                        iVar2 = iVar4;
                                    }
                                    sd.l lVar2 = lVar;
                                    Object objE3 = bVar2.E(iVar2, i12, andIncrement2, this);
                                    if (objE3 == c.f29328m) {
                                        a(iVar2, i12);
                                        break;
                                    }
                                    if (objE3 == c.f29330o) {
                                        if (andIncrement2 < bVar2.s()) {
                                            iVar2.a();
                                        }
                                        lVar = lVar2;
                                        iVar4 = iVar2;
                                    } else {
                                        if (objE3 == c.f29329n) {
                                            throw new IllegalStateException("unexpected".toString());
                                        }
                                        iVar2.a();
                                        this.f29302a = objE3;
                                        this.f29303b = null;
                                        bool = Boolean.TRUE;
                                        if (lVar2 != null) {
                                            lVarA = kotlinx.coroutines.internal.a.a(lVar2, objE3, hVar2);
                                        }
                                    }
                                }
                            }
                        } else {
                            iVar.a();
                            this.f29302a = objE2;
                            this.f29303b = null;
                            bool = Boolean.TRUE;
                            if (lVar != null) {
                                lVarA = kotlinx.coroutines.internal.a.a(lVar, objE2, hVar2);
                            }
                        }
                        hVar.d(bool, lVarA);
                    }
                    return hVar.s();
                } catch (Throwable th2) {
                    th = th2;
                    hVar.A();
                    throw th;
                }
            }
            if (andIncrement < bVar.s()) {
                iVar.a();
            }
            iVar3 = iVar;
        }
    }
}
