package kotlinx.coroutines.channels;

import com.google.android.gms.measurement.internal.u1;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.intrinsics.CoroutineSingletons;
import kotlinx.coroutines.internal.UndeliveredElementException;
import kotlinx.coroutines.internal.s;
import kotlinx.coroutines.internal.t;
import kotlinx.coroutines.v1;
import kotlinx.coroutines.y;

/* JADX INFO: loaded from: classes.dex */
public class b implements e {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final /* synthetic */ AtomicLongFieldUpdater f29305c = AtomicLongFieldUpdater.newUpdater(b.class, "sendersAndCloseStatus$volatile");

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final /* synthetic */ AtomicLongFieldUpdater f29306d = AtomicLongFieldUpdater.newUpdater(b.class, "receivers$volatile");

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final /* synthetic */ AtomicLongFieldUpdater f29307e = AtomicLongFieldUpdater.newUpdater(b.class, "bufferEnd$volatile");

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final /* synthetic */ AtomicLongFieldUpdater f29308f = AtomicLongFieldUpdater.newUpdater(b.class, "completedExpandBuffersAndPauseFlag$volatile");

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f29309g = AtomicReferenceFieldUpdater.newUpdater(b.class, Object.class, "sendSegment$volatile");

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f29310h = AtomicReferenceFieldUpdater.newUpdater(b.class, Object.class, "receiveSegment$volatile");

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f29311i = AtomicReferenceFieldUpdater.newUpdater(b.class, Object.class, "bufferEndSegment$volatile");

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f29313k = AtomicReferenceFieldUpdater.newUpdater(b.class, Object.class, "closeHandler$volatile");
    private volatile /* synthetic */ Object _closeCause$volatile;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final int f29314a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final sd.l f29315b;
    private volatile /* synthetic */ long bufferEnd$volatile;
    private volatile /* synthetic */ Object bufferEndSegment$volatile;
    private volatile /* synthetic */ Object closeHandler$volatile;
    private volatile /* synthetic */ long completedExpandBuffersAndPauseFlag$volatile;
    private volatile /* synthetic */ Object receiveSegment$volatile;
    private volatile /* synthetic */ long receivers$volatile;
    private volatile /* synthetic */ Object sendSegment$volatile;
    private volatile /* synthetic */ long sendersAndCloseStatus$volatile;

    public b(int i10, sd.l lVar) {
        this.f29314a = i10;
        this.f29315b = lVar;
        if (i10 < 0) {
            throw new IllegalArgumentException(defpackage.e.e("Invalid channel capacity: ", i10, ", should be >=0").toString());
        }
        i iVar = c.f29316a;
        this.bufferEnd$volatile = i10 != 0 ? i10 != Integer.MAX_VALUE ? i10 : Long.MAX_VALUE : 0L;
        this.completedExpandBuffersAndPauseFlag$volatile = f29307e.get(this);
        i iVar2 = new i(0L, null, this, 3);
        this.sendSegment$volatile = iVar2;
        this.receiveSegment$volatile = iVar2;
        if (w()) {
            iVar2 = c.f29316a;
            nb.g.g(iVar2, "null cannot be cast to non-null type kotlinx.coroutines.channels.ChannelSegment<E of kotlinx.coroutines.channels.BufferedChannel>");
        }
        this.bufferEndSegment$volatile = iVar2;
        this._closeCause$volatile = c.f29334s;
    }

    public static boolean D(Object obj) {
        if (obj instanceof kotlinx.coroutines.g) {
            nb.g.g(obj, "null cannot be cast to non-null type kotlinx.coroutines.CancellableContinuation<kotlin.Unit>");
            return c.a((kotlinx.coroutines.g) obj, kd.e.f29109a, null);
        }
        throw new IllegalStateException(("Unexpected waiter: " + obj).toString());
    }

    public static final i d(b bVar, long j10, i iVar) {
        Object objC;
        AtomicLongFieldUpdater atomicLongFieldUpdater;
        long j11;
        long j12;
        bVar.getClass();
        i iVar2 = c.f29316a;
        BufferedChannelKt$createSegmentFunction$1 bufferedChannelKt$createSegmentFunction$1 = BufferedChannelKt$createSegmentFunction$1.f29301c;
        loop0: while (true) {
            objC = kotlinx.coroutines.internal.a.c(iVar, j10, bufferedChannelKt$createSegmentFunction$1);
            if (!y.I(objC)) {
                s sVarC = y.C(objC);
                while (true) {
                    AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29309g;
                    s sVar = (s) atomicReferenceFieldUpdater.get(bVar);
                    if (sVar.f29487c >= sVarC.f29487c) {
                        break loop0;
                    }
                    if (!sVarC.i()) {
                        break;
                    }
                    while (!atomicReferenceFieldUpdater.compareAndSet(bVar, sVar, sVarC)) {
                        if (atomicReferenceFieldUpdater.get(bVar) != sVar) {
                            if (sVarC.e()) {
                                sVarC.d();
                            }
                        }
                    }
                    if (sVar.e()) {
                        sVar.d();
                    }
                }
            } else {
                break;
            }
        }
        boolean zI = y.I(objC);
        AtomicLongFieldUpdater atomicLongFieldUpdater2 = f29306d;
        if (zI) {
            bVar.j();
            if (iVar.f29487c * ((long) c.f29317b) >= atomicLongFieldUpdater2.get(bVar)) {
                return null;
            }
            iVar.a();
            return null;
        }
        i iVar3 = (i) y.C(objC);
        long j13 = iVar3.f29487c;
        if (j13 <= j10) {
            return iVar3;
        }
        long j14 = ((long) c.f29317b) * j13;
        do {
            atomicLongFieldUpdater = f29305c;
            j11 = atomicLongFieldUpdater.get(bVar);
            j12 = 1152921504606846975L & j11;
            if (j12 >= j14) {
                break;
            }
        } while (!atomicLongFieldUpdater.compareAndSet(bVar, j11, j12 + (((long) ((int) (j11 >> 60))) << 60)));
        if (j13 * ((long) c.f29317b) >= atomicLongFieldUpdater2.get(bVar)) {
            return null;
        }
        iVar3.a();
        return null;
    }

    public static final void f(b bVar, Object obj, kotlinx.coroutines.h hVar) {
        UndeliveredElementException undeliveredElementExceptionB;
        sd.l lVar = bVar.f29315b;
        if (lVar != null && (undeliveredElementExceptionB = kotlinx.coroutines.internal.a.b(lVar, obj, null)) != null) {
            hb.a.g0(hVar.f29439e, undeliveredElementExceptionB);
        }
        hVar.resumeWith(kotlin.a.b(bVar.r()));
    }

    public static final int g(b bVar, i iVar, int i10, Object obj, long j10, Object obj2, boolean z10) {
        bVar.getClass();
        iVar.m(i10, obj);
        if (z10) {
            return bVar.F(iVar, i10, obj, j10, obj2, z10);
        }
        Object objK = iVar.k(i10);
        if (objK == null) {
            if (bVar.h(j10)) {
                if (iVar.j(i10, null, c.f29319d)) {
                    return 1;
                }
            } else {
                if (obj2 == null) {
                    return 3;
                }
                if (iVar.j(i10, null, obj2)) {
                    return 2;
                }
            }
        } else if (objK instanceof v1) {
            iVar.m(i10, null);
            if (bVar.C(objK, obj)) {
                iVar.n(i10, c.f29324i);
                return 0;
            }
            la.e eVar = c.f29326k;
            if (iVar.f29340f.getAndSet((i10 * 2) + 1, eVar) != eVar) {
                iVar.l(i10, true);
            }
            return 5;
        }
        return bVar.F(iVar, i10, obj, j10, obj2, z10);
    }

    public static void t(b bVar) {
        bVar.getClass();
        AtomicLongFieldUpdater atomicLongFieldUpdater = f29308f;
        if ((atomicLongFieldUpdater.addAndGet(bVar, 1L) & 4611686018427387904L) != 0) {
            while ((atomicLongFieldUpdater.get(bVar) & 4611686018427387904L) != 0) {
            }
        }
    }

    public final void A(v1 v1Var, boolean z10) {
        if (v1Var instanceof kotlinx.coroutines.g) {
            ((Continuation) v1Var).resumeWith(kotlin.a.b(z10 ? q() : r()));
            return;
        }
        if (!(v1Var instanceof a)) {
            throw new IllegalStateException(("Unexpected waiter: " + v1Var).toString());
        }
        a aVar = (a) v1Var;
        kotlinx.coroutines.h hVar = aVar.f29303b;
        nb.g.f(hVar);
        aVar.f29303b = null;
        aVar.f29302a = c.f29327l;
        Throwable thP = aVar.f29304c.p();
        if (thP == null) {
            hVar.resumeWith(Boolean.FALSE);
        } else {
            hVar.resumeWith(kotlin.a.b(thP));
        }
    }

    public final Object B() {
        AtomicLongFieldUpdater atomicLongFieldUpdater = f29306d;
        long j10 = atomicLongFieldUpdater.get(this);
        AtomicLongFieldUpdater atomicLongFieldUpdater2 = f29305c;
        long j11 = atomicLongFieldUpdater2.get(this);
        if (u(j11, true)) {
            return new f(p());
        }
        long j12 = j11 & 1152921504606846975L;
        Object obj = h.f29338a;
        if (j10 >= j12) {
            return obj;
        }
        Object obj2 = c.f29326k;
        i iVar = (i) f29310h.get(this);
        while (!u(atomicLongFieldUpdater2.get(this), true)) {
            long andIncrement = atomicLongFieldUpdater.getAndIncrement(this);
            long j13 = c.f29317b;
            long j14 = andIncrement / j13;
            int i10 = (int) (andIncrement % j13);
            if (iVar.f29487c != j14) {
                i iVarO = o(j14, iVar);
                if (iVarO == null) {
                    continue;
                } else {
                    iVar = iVarO;
                }
            }
            Object objE = E(iVar, i10, andIncrement, obj2);
            if (objE == c.f29328m) {
                v1 v1Var = obj2 instanceof v1 ? (v1) obj2 : null;
                if (v1Var != null) {
                    v1Var.a(iVar, i10);
                }
                G(andIncrement);
                iVar.h();
            } else if (objE == c.f29330o) {
                if (andIncrement < s()) {
                    iVar.a();
                }
            } else {
                if (objE == c.f29329n) {
                    throw new IllegalStateException("unexpected".toString());
                }
                iVar.a();
                obj = objE;
            }
            return obj;
        }
        return new f(p());
    }

    public final boolean C(Object obj, Object obj2) {
        if (!(obj instanceof a)) {
            if (!(obj instanceof kotlinx.coroutines.g)) {
                throw new IllegalStateException(("Unexpected receiver type: " + obj).toString());
            }
            nb.g.g(obj, "null cannot be cast to non-null type kotlinx.coroutines.CancellableContinuation<E of kotlinx.coroutines.channels.BufferedChannel>");
            kotlinx.coroutines.g gVar = (kotlinx.coroutines.g) obj;
            sd.l lVar = this.f29315b;
            return c.a(gVar, obj2, lVar != null ? kotlinx.coroutines.internal.a.a(lVar, obj2, gVar.getContext()) : null);
        }
        nb.g.g(obj, "null cannot be cast to non-null type kotlinx.coroutines.channels.BufferedChannel.BufferedChannelIterator<E of kotlinx.coroutines.channels.BufferedChannel>");
        a aVar = (a) obj;
        kotlinx.coroutines.h hVar = aVar.f29303b;
        nb.g.f(hVar);
        aVar.f29303b = null;
        aVar.f29302a = obj2;
        Boolean bool = Boolean.TRUE;
        sd.l lVar2 = aVar.f29304c.f29315b;
        return c.a(hVar, bool, lVar2 != null ? kotlinx.coroutines.internal.a.a(lVar2, obj2, hVar.f29439e) : null);
    }

    public final Object E(i iVar, int i10, long j10, Object obj) {
        Object objK = iVar.k(i10);
        AtomicReferenceArray atomicReferenceArray = iVar.f29340f;
        AtomicLongFieldUpdater atomicLongFieldUpdater = f29305c;
        if (objK == null) {
            if (j10 >= (atomicLongFieldUpdater.get(this) & 1152921504606846975L)) {
                if (obj == null) {
                    return c.f29329n;
                }
                if (iVar.j(i10, objK, obj)) {
                    n();
                    return c.f29328m;
                }
            }
        } else if (objK == c.f29319d && iVar.j(i10, objK, c.f29324i)) {
            n();
            Object obj2 = atomicReferenceArray.get(i10 * 2);
            iVar.m(i10, null);
            return obj2;
        }
        while (true) {
            Object objK2 = iVar.k(i10);
            if (objK2 == null || objK2 == c.f29320e) {
                if (j10 < (atomicLongFieldUpdater.get(this) & 1152921504606846975L)) {
                    if (iVar.j(i10, objK2, c.f29323h)) {
                        n();
                        return c.f29330o;
                    }
                } else {
                    if (obj == null) {
                        return c.f29329n;
                    }
                    if (iVar.j(i10, objK2, obj)) {
                        n();
                        return c.f29328m;
                    }
                }
            } else if (objK2 != c.f29319d) {
                la.e eVar = c.f29325j;
                if (objK2 == eVar) {
                    return c.f29330o;
                }
                if (objK2 == c.f29323h) {
                    return c.f29330o;
                }
                if (objK2 == c.f29327l) {
                    n();
                    return c.f29330o;
                }
                if (objK2 != c.f29322g && iVar.j(i10, objK2, c.f29321f)) {
                    boolean z10 = objK2 instanceof o;
                    if (z10) {
                        objK2 = ((o) objK2).f29343a;
                    }
                    if (D(objK2)) {
                        iVar.n(i10, c.f29324i);
                        n();
                        Object obj3 = atomicReferenceArray.get(i10 * 2);
                        iVar.m(i10, null);
                        return obj3;
                    }
                    iVar.n(i10, eVar);
                    iVar.l(i10, false);
                    if (z10) {
                        n();
                    }
                    return c.f29330o;
                }
            } else if (iVar.j(i10, objK2, c.f29324i)) {
                n();
                Object obj4 = atomicReferenceArray.get(i10 * 2);
                iVar.m(i10, null);
                return obj4;
            }
        }
    }

    public final int F(i iVar, int i10, Object obj, long j10, Object obj2, boolean z10) {
        while (true) {
            Object objK = iVar.k(i10);
            if (objK == null) {
                if (!h(j10) || z10) {
                    if (z10) {
                        if (iVar.j(i10, null, c.f29325j)) {
                            iVar.l(i10, false);
                            return 4;
                        }
                    } else {
                        if (obj2 == null) {
                            return 3;
                        }
                        if (iVar.j(i10, null, obj2)) {
                            return 2;
                        }
                    }
                } else if (iVar.j(i10, null, c.f29319d)) {
                    return 1;
                }
            } else {
                if (objK != c.f29320e) {
                    la.e eVar = c.f29326k;
                    if (objK == eVar) {
                        iVar.m(i10, null);
                        return 5;
                    }
                    if (objK == c.f29323h) {
                        iVar.m(i10, null);
                        return 5;
                    }
                    if (objK == c.f29327l) {
                        iVar.m(i10, null);
                        j();
                        return 4;
                    }
                    iVar.m(i10, null);
                    if (objK instanceof o) {
                        objK = ((o) objK).f29343a;
                    }
                    if (C(objK, obj)) {
                        iVar.n(i10, c.f29324i);
                        return 0;
                    }
                    if (iVar.f29340f.getAndSet((i10 * 2) + 1, eVar) != eVar) {
                        iVar.l(i10, true);
                    }
                    return 5;
                }
                if (iVar.j(i10, objK, c.f29319d)) {
                    return 1;
                }
            }
        }
    }

    public final void G(long j10) {
        AtomicLongFieldUpdater atomicLongFieldUpdater;
        long j11;
        long j12;
        if (w()) {
            return;
        }
        do {
            atomicLongFieldUpdater = f29307e;
        } while (atomicLongFieldUpdater.get(this) <= j10);
        int i10 = c.f29318c;
        int i11 = 0;
        while (true) {
            AtomicLongFieldUpdater atomicLongFieldUpdater2 = f29308f;
            if (i11 >= i10) {
                do {
                    j11 = atomicLongFieldUpdater2.get(this);
                } while (!atomicLongFieldUpdater2.compareAndSet(this, j11, 4611686018427387904L + (j11 & 4611686018427387903L)));
                while (true) {
                    long j13 = atomicLongFieldUpdater.get(this);
                    long j14 = atomicLongFieldUpdater2.get(this);
                    long j15 = j14 & 4611686018427387903L;
                    boolean z10 = (j14 & 4611686018427387904L) != 0;
                    if (j13 == j15 && j13 == atomicLongFieldUpdater.get(this)) {
                        break;
                    } else if (!z10) {
                        atomicLongFieldUpdater2.compareAndSet(this, j14, j15 + 4611686018427387904L);
                    }
                }
                do {
                    j12 = atomicLongFieldUpdater2.get(this);
                } while (!atomicLongFieldUpdater2.compareAndSet(this, j12, j12 & 4611686018427387903L));
                return;
            }
            long j16 = atomicLongFieldUpdater.get(this);
            if (j16 == (atomicLongFieldUpdater2.get(this) & 4611686018427387903L) && j16 == atomicLongFieldUpdater.get(this)) {
                return;
            } else {
                i11++;
            }
        }
    }

    @Override // kotlinx.coroutines.channels.n
    public final void a(sd.l lVar) {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
        do {
            atomicReferenceFieldUpdater = f29313k;
            if (atomicReferenceFieldUpdater.compareAndSet(this, null, lVar)) {
                return;
            }
        } while (atomicReferenceFieldUpdater.get(this) == null);
        while (true) {
            Object obj = atomicReferenceFieldUpdater.get(this);
            la.e eVar = c.f29332q;
            if (obj != eVar) {
                if (obj == c.f29333r) {
                    throw new IllegalStateException("Another handler was already registered and successfully invoked".toString());
                }
                throw new IllegalStateException(("Another handler is already registered: " + obj).toString());
            }
            la.e eVar2 = c.f29333r;
            while (!atomicReferenceFieldUpdater.compareAndSet(this, eVar, eVar2)) {
                if (atomicReferenceFieldUpdater.get(this) != eVar) {
                    break;
                }
            }
            ((ProduceKt$awaitClose$4$1) lVar).invoke(p());
            return;
        }
    }

    @Override // kotlinx.coroutines.channels.n
    public final boolean b(Throwable th) {
        return k(th, false);
    }

    /* JADX WARN: Code restructure failed: missing block: B:65:?, code lost:
    
        return r1;
     */
    @Override // kotlinx.coroutines.channels.n
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Object c(Object obj) {
        i iVar;
        i iVar2;
        AtomicLongFieldUpdater atomicLongFieldUpdater = f29305c;
        boolean zU = u(atomicLongFieldUpdater.get(this), false);
        g gVar = h.f29338a;
        if (zU || !(!h(r0 & 1152921504606846975L))) {
            Object obj2 = c.f29325j;
            i iVar3 = (i) f29309g.get(this);
            while (true) {
                long andIncrement = atomicLongFieldUpdater.getAndIncrement(this);
                long j10 = andIncrement & 1152921504606846975L;
                boolean zU2 = u(andIncrement, false);
                int i10 = c.f29317b;
                long j11 = i10;
                long j12 = j10 / j11;
                int i11 = (int) (j10 % j11);
                if (iVar3.f29487c != j12) {
                    i iVarD = d(this, j12, iVar3);
                    if (iVarD != null) {
                        iVar = iVarD;
                    } else if (zU2) {
                        return new f(r());
                    }
                } else {
                    iVar = iVar3;
                }
                i iVar4 = iVar;
                int iG = g(this, iVar, i11, obj, j10, obj2, zU2);
                kd.e eVar = kd.e.f29109a;
                if (iG == 0) {
                    iVar4.a();
                    break;
                }
                if (iG == 1) {
                    break;
                }
                if (iG == 2) {
                    if (zU2) {
                        iVar4.h();
                        return new f(r());
                    }
                    v1 v1Var = obj2 instanceof v1 ? (v1) obj2 : null;
                    if (v1Var != null) {
                        iVar2 = iVar4;
                        v1Var.a(iVar2, i11 + i10);
                    } else {
                        iVar2 = iVar4;
                    }
                    iVar2.h();
                    return gVar;
                }
                if (iG == 3) {
                    throw new IllegalStateException("unexpected".toString());
                }
                if (iG == 4) {
                    if (j10 < f29306d.get(this)) {
                        iVar4.a();
                    }
                    return new f(r());
                }
                if (iG == 5) {
                    iVar4.a();
                }
                iVar3 = iVar4;
            }
        } else {
            return gVar;
        }
    }

    @Override // kotlinx.coroutines.channels.m
    public final void e(CancellationException cancellationException) {
        if (cancellationException == null) {
            cancellationException = new CancellationException("Channel was cancelled");
        }
        k(cancellationException, true);
    }

    public final boolean h(long j10) {
        return j10 < f29307e.get(this) || j10 < f29306d.get(this) + ((long) this.f29314a);
    }

    /* JADX WARN: Code restructure failed: missing block: B:153:?, code lost:
    
        return r0;
     */
    /* JADX WARN: Code restructure failed: missing block: B:154:?, code lost:
    
        return r3;
     */
    /* JADX WARN: Code restructure failed: missing block: B:50:0x00eb, code lost:
    
        r5 = r27;
     */
    /* JADX WARN: Code restructure failed: missing block: B:51:0x00ed, code lost:
    
        f(r25, r26, r5);
     */
    /* JADX WARN: Code restructure failed: missing block: B:52:0x00f0, code lost:
    
        r2 = r5;
     */
    /* JADX WARN: Code restructure failed: missing block: B:53:0x00f1, code lost:
    
        r3 = r3;
     */
    /* JADX WARN: Code restructure failed: missing block: B:54:0x00f5, code lost:
    
        r0 = th;
     */
    /* JADX WARN: Code restructure failed: missing block: B:55:0x00f6, code lost:
    
        r2 = r5;
     */
    /* JADX WARN: Code restructure failed: missing block: B:77:0x0142, code lost:
    
        if (r18 >= r5.get(r25)) goto L79;
     */
    /* JADX WARN: Code restructure failed: missing block: B:78:0x0144, code lost:
    
        r27.a();
     */
    /* JADX WARN: Code restructure failed: missing block: B:79:0x0147, code lost:
    
        r2 = r23;
     */
    /* JADX WARN: Code restructure failed: missing block: B:80:0x0149, code lost:
    
        f(r25, r26, r2);
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:107:0x01c7  */
    /* JADX WARN: Removed duplicated region for block: B:155:? A[RETURN, SYNTHETIC] */
    /* JADX WARN: Type inference failed for: r2v25 */
    /* JADX WARN: Type inference failed for: r2v34 */
    /* JADX WARN: Type inference failed for: r2v38 */
    /* JADX WARN: Type inference failed for: r2v8 */
    /* JADX WARN: Type inference failed for: r2v9, types: [kotlinx.coroutines.h] */
    @Override // kotlinx.coroutines.channels.n
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Object i(Object obj, Continuation continuation) throws Throwable {
        i iVar;
        Object objY;
        ?? r22;
        int iG;
        kotlinx.coroutines.h hVar;
        kd.e eVar;
        kotlinx.coroutines.h hVar2;
        i iVar2;
        i iVar3;
        kotlinx.coroutines.h hVar3;
        int i10;
        Object obj2 = null;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29309g;
        i iVar4 = (i) atomicReferenceFieldUpdater.get(this);
        while (true) {
            AtomicLongFieldUpdater atomicLongFieldUpdater = f29305c;
            long andIncrement = atomicLongFieldUpdater.getAndIncrement(this);
            long j10 = andIncrement & 1152921504606846975L;
            boolean zU = u(andIncrement, false);
            int i11 = c.f29317b;
            long j11 = i11;
            long j12 = j10 / j11;
            int i12 = (int) (j10 % j11);
            long j13 = iVar4.f29487c;
            kd.e eVar2 = kd.e.f29109a;
            CoroutineSingletons coroutineSingletons = CoroutineSingletons.f29226a;
            if (j13 != j12) {
                i iVarD = d(this, j12, iVar4);
                if (iVarD != null) {
                    iVar = iVarD;
                } else if (zU) {
                    objY = y(obj, continuation);
                    if (objY != coroutineSingletons) {
                        return eVar2;
                    }
                }
            } else {
                iVar = iVar4;
            }
            int iG2 = g(this, iVar, i12, obj, j10, obj2, zU);
            if (iG2 == 0) {
                iVar.a();
                return eVar2;
            }
            if (iG2 == 1) {
                break;
            }
            if (iG2 != 2) {
                AtomicLongFieldUpdater atomicLongFieldUpdater2 = f29306d;
                if (iG2 == 3) {
                    kotlinx.coroutines.h hVarC0 = hb.a.c0(nb.g.F(continuation));
                    i iVar5 = iVar;
                    try {
                        iG = g(this, iVar5, i12, obj, j10, hVarC0, false);
                        try {
                        } catch (Throwable th) {
                            th = th;
                            r22 = iVar5;
                        }
                    } catch (Throwable th2) {
                        th = th2;
                        r22 = hVarC0;
                    }
                    if (iG != 0) {
                        if (iG == 1) {
                            hVar = hVarC0;
                            eVar = eVar2;
                            hVar.resumeWith(eVar);
                        } else if (iG == 2) {
                            hVar = hVarC0;
                            eVar = eVar2;
                            hVar.a(iVar, i12 + i11);
                        } else {
                            if (iG != 4) {
                                int i13 = 5;
                                if (iG != 5) {
                                    throw new IllegalStateException("unexpected".toString());
                                }
                                iVar.a();
                                i iVar6 = (i) atomicReferenceFieldUpdater.get(this);
                                while (true) {
                                    long andIncrement2 = atomicLongFieldUpdater.getAndIncrement(this);
                                    long j14 = andIncrement2 & 1152921504606846975L;
                                    boolean zU2 = u(andIncrement2, false);
                                    int i14 = c.f29317b;
                                    long j15 = i14;
                                    long j16 = j14 / j15;
                                    int i15 = (int) (j14 % j15);
                                    try {
                                        if (iVar6.f29487c != j16) {
                                            try {
                                                i iVarD2 = d(this, j16, iVar6);
                                                if (iVarD2 != null) {
                                                    hVar2 = hVarC0;
                                                    iVar2 = iVarD2;
                                                } else if (zU2) {
                                                    break;
                                                }
                                            } catch (Throwable th3) {
                                                th = th3;
                                                kotlinx.coroutines.h hVar4 = hVarC0;
                                            }
                                        } else {
                                            hVar2 = hVarC0;
                                            iVar2 = iVar6;
                                        }
                                        int iG3 = g(this, iVar2, i15, obj, j14, hVar3, zU2);
                                        if (iG3 == 0) {
                                            eVar = eVar2;
                                            hVar = hVar3;
                                            iVar3.a();
                                            break;
                                        }
                                        if (iG3 == 1) {
                                            eVar = eVar2;
                                            hVar = hVar3;
                                            hVar.resumeWith(eVar);
                                            break;
                                        }
                                        if (iG3 == 2) {
                                            hVar = hVar3;
                                            if (zU2) {
                                                iVar3.h();
                                            } else {
                                                hVar.a(iVar3, i15 + i14);
                                            }
                                        } else {
                                            if (iG3 == 3) {
                                                throw new IllegalStateException("unexpected".toString());
                                            }
                                            if (iG3 == 4) {
                                                break;
                                            }
                                            if (iG3 == i10) {
                                                iVar3.a();
                                            }
                                            iVar6 = iVar3;
                                            i13 = i10;
                                            hVarC0 = hVar3;
                                        }
                                    } catch (Throwable th4) {
                                        th = th4;
                                        r22 = hVar3;
                                    }
                                    iVar3 = iVar2;
                                    hVar3 = hVar2;
                                    i10 = i13;
                                }
                                r22.A();
                                throw th;
                            }
                            hVar = hVarC0;
                            eVar = eVar2;
                            if (j10 < atomicLongFieldUpdater2.get(this)) {
                                iVar.a();
                            }
                            f(this, obj, hVar);
                        }
                        objY = hVar.s();
                        if (objY != coroutineSingletons) {
                            objY = eVar;
                        }
                        if (objY != coroutineSingletons) {
                            return eVar;
                        }
                    } else {
                        hVar = hVarC0;
                        eVar = eVar2;
                        iVar.a();
                    }
                    hVar.resumeWith(eVar);
                    objY = hVar.s();
                    if (objY != coroutineSingletons) {
                    }
                    if (objY != coroutineSingletons) {
                    }
                } else if (iG2 != 4) {
                    if (iG2 == 5) {
                        iVar.a();
                    }
                    iVar4 = iVar;
                    obj2 = null;
                } else {
                    if (j10 < atomicLongFieldUpdater2.get(this)) {
                        iVar.a();
                    }
                    Object objY2 = y(obj, continuation);
                    if (objY2 == coroutineSingletons) {
                        return objY2;
                    }
                }
            } else {
                if (!zU) {
                    return eVar2;
                }
                iVar.h();
                objY = y(obj, continuation);
                if (objY != coroutineSingletons) {
                    return eVar2;
                }
            }
        }
    }

    @Override // kotlinx.coroutines.channels.m
    public final a iterator() {
        return new a(this);
    }

    @Override // kotlinx.coroutines.channels.n
    public final boolean j() {
        return u(f29305c.get(this), false);
    }

    public final boolean k(Throwable th, boolean z10) {
        boolean z11;
        long j10;
        long j11;
        long j12;
        Object obj;
        long j13;
        long j14;
        AtomicLongFieldUpdater atomicLongFieldUpdater = f29305c;
        if (z10) {
            do {
                j14 = atomicLongFieldUpdater.get(this);
                if (((int) (j14 >> 60)) != 0) {
                    break;
                }
                i iVar = c.f29316a;
            } while (!atomicLongFieldUpdater.compareAndSet(this, j14, (((long) 1) << 60) + (j14 & 1152921504606846975L)));
        }
        la.e eVar = c.f29334s;
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29312j;
            if (atomicReferenceFieldUpdater.compareAndSet(this, eVar, th)) {
                z11 = true;
                break;
            }
            if (atomicReferenceFieldUpdater.get(this) != eVar) {
                z11 = false;
                break;
            }
        }
        if (z10) {
            do {
                j13 = atomicLongFieldUpdater.get(this);
            } while (!atomicLongFieldUpdater.compareAndSet(this, j13, (((long) 3) << 60) + (j13 & 1152921504606846975L)));
        } else {
            do {
                j10 = atomicLongFieldUpdater.get(this);
                int i10 = (int) (j10 >> 60);
                if (i10 == 0) {
                    j11 = j10 & 1152921504606846975L;
                    j12 = 2;
                } else {
                    if (i10 != 1) {
                        break;
                    }
                    j11 = j10 & 1152921504606846975L;
                    j12 = 3;
                }
            } while (!atomicLongFieldUpdater.compareAndSet(this, j10, (j12 << 60) + j11));
        }
        j();
        if (z11) {
            loop3: while (true) {
                AtomicReferenceFieldUpdater atomicReferenceFieldUpdater2 = f29313k;
                obj = atomicReferenceFieldUpdater2.get(this);
                la.e eVar2 = obj == null ? c.f29332q : c.f29333r;
                while (!atomicReferenceFieldUpdater2.compareAndSet(this, obj, eVar2)) {
                    if (atomicReferenceFieldUpdater2.get(this) != obj) {
                        break;
                    }
                }
            }
            if (obj != null) {
                hb.a.c(1, obj);
                ((sd.l) obj).invoke(p());
            }
        }
        return z11;
    }

    /* JADX WARN: Code restructure failed: missing block: B:37:0x008f, code lost:
    
        r1 = (kotlinx.coroutines.channels.i) ((kotlinx.coroutines.internal.d) kotlinx.coroutines.internal.d.f29457b.get(r1));
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final i l(long j10) {
        Object objJ;
        long j11;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
        Object obj = f29311i.get(this);
        i iVar = (i) f29309g.get(this);
        if (iVar.f29487c > ((i) obj).f29487c) {
            obj = iVar;
        }
        i iVar2 = (i) f29310h.get(this);
        if (iVar2.f29487c > ((i) obj).f29487c) {
            obj = iVar2;
        }
        kotlinx.coroutines.internal.d dVar = (kotlinx.coroutines.internal.d) obj;
        loop0: while (true) {
            dVar.getClass();
            Object obj2 = kotlinx.coroutines.internal.d.f29456a.get(dVar);
            la.e eVar = kotlinx.coroutines.internal.a.f29450b;
            objJ = null;
            if (obj2 == eVar) {
                break;
            }
            kotlinx.coroutines.internal.d dVar2 = (kotlinx.coroutines.internal.d) obj2;
            if (dVar2 == null) {
                do {
                    atomicReferenceFieldUpdater = kotlinx.coroutines.internal.d.f29456a;
                    if (atomicReferenceFieldUpdater.compareAndSet(dVar, null, eVar)) {
                        break loop0;
                    }
                } while (atomicReferenceFieldUpdater.get(dVar) == null);
            } else {
                dVar = dVar2;
            }
        }
        i iVar3 = (i) dVar;
        if (v()) {
            i iVar4 = iVar3;
            loop2: do {
                int i10 = c.f29317b - 1;
                while (true) {
                    if (-1 >= i10) {
                        break;
                    }
                    j11 = (iVar4.f29487c * ((long) c.f29317b)) + ((long) i10);
                    if (j11 < f29306d.get(this)) {
                        break loop2;
                    }
                    while (true) {
                        Object objK = iVar4.k(i10);
                        if (objK != null && objK != c.f29320e) {
                            if (objK == c.f29319d) {
                                break loop2;
                            }
                        } else {
                            if (iVar4.j(i10, objK, c.f29327l)) {
                                iVar4.h();
                                break;
                            }
                        }
                    }
                    i10--;
                }
            } while (iVar4 != null);
            j11 = -1;
            if (j11 != -1) {
                m(j11);
            }
        }
        loop5: for (i iVar5 = iVar3; iVar5 != null; iVar5 = (i) ((kotlinx.coroutines.internal.d) kotlinx.coroutines.internal.d.f29457b.get(iVar5))) {
            for (int i11 = c.f29317b - 1; -1 < i11; i11--) {
                if ((iVar5.f29487c * ((long) c.f29317b)) + ((long) i11) < j10) {
                    break loop5;
                }
                while (true) {
                    Object objK2 = iVar5.k(i11);
                    if (objK2 != null && objK2 != c.f29320e) {
                        if (!(objK2 instanceof o)) {
                            if (!(objK2 instanceof v1)) {
                                break;
                            }
                            if (iVar5.j(i11, objK2, c.f29327l)) {
                                objJ = kotlinx.coroutines.flow.h.J(objJ, objK2);
                                iVar5.l(i11, true);
                                break;
                            }
                        } else {
                            if (iVar5.j(i11, objK2, c.f29327l)) {
                                objJ = kotlinx.coroutines.flow.h.J(objJ, ((o) objK2).f29343a);
                                iVar5.l(i11, true);
                                break;
                            }
                        }
                    } else {
                        if (iVar5.j(i11, objK2, c.f29327l)) {
                            iVar5.h();
                            break;
                        }
                    }
                }
            }
        }
        if (objJ != null) {
            if (objJ instanceof ArrayList) {
                ArrayList arrayList = (ArrayList) objJ;
                for (int size = arrayList.size() - 1; -1 < size; size--) {
                    A((v1) arrayList.get(size), true);
                }
            } else {
                A((v1) objJ, true);
            }
        }
        return iVar3;
    }

    public final void m(long j10) {
        UndeliveredElementException undeliveredElementExceptionB;
        i iVar = (i) f29310h.get(this);
        while (true) {
            AtomicLongFieldUpdater atomicLongFieldUpdater = f29306d;
            long j11 = atomicLongFieldUpdater.get(this);
            if (j10 < Math.max(((long) this.f29314a) + j11, f29307e.get(this))) {
                return;
            }
            if (atomicLongFieldUpdater.compareAndSet(this, j11, j11 + 1)) {
                long j12 = c.f29317b;
                long j13 = j11 / j12;
                int i10 = (int) (j11 % j12);
                if (iVar.f29487c != j13) {
                    i iVarO = o(j13, iVar);
                    if (iVarO == null) {
                        continue;
                    } else {
                        iVar = iVarO;
                    }
                }
                Object objE = E(iVar, i10, j11, null);
                if (objE != c.f29330o) {
                    iVar.a();
                    sd.l lVar = this.f29315b;
                    if (lVar != null && (undeliveredElementExceptionB = kotlinx.coroutines.internal.a.b(lVar, objE, null)) != null) {
                        throw undeliveredElementExceptionB;
                    }
                } else if (j11 < s()) {
                    iVar.a();
                }
            }
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:102:0x0196, code lost:
    
        t(r16);
     */
    /* JADX WARN: Code restructure failed: missing block: B:103:0x0199, code lost:
    
        return;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void n() {
        Object objC;
        if (w()) {
            return;
        }
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29311i;
        i iVar = (i) atomicReferenceFieldUpdater.get(this);
        loop0: while (true) {
            long andIncrement = f29307e.getAndIncrement(this);
            long j10 = andIncrement / ((long) c.f29317b);
            if (s() <= andIncrement) {
                if (iVar.f29487c < j10 && iVar.b() != null) {
                    x(j10, iVar);
                }
                t(this);
                return;
            }
            if (iVar.f29487c != j10) {
                BufferedChannelKt$createSegmentFunction$1 bufferedChannelKt$createSegmentFunction$1 = BufferedChannelKt$createSegmentFunction$1.f29301c;
                while (true) {
                    objC = kotlinx.coroutines.internal.a.c(iVar, j10, bufferedChannelKt$createSegmentFunction$1);
                    if (!y.I(objC)) {
                        s sVarC = y.C(objC);
                        while (true) {
                            s sVar = (s) atomicReferenceFieldUpdater.get(this);
                            if (sVar.f29487c >= sVarC.f29487c) {
                                break;
                            }
                            if (!sVarC.i()) {
                                break;
                            }
                            while (!atomicReferenceFieldUpdater.compareAndSet(this, sVar, sVarC)) {
                                if (atomicReferenceFieldUpdater.get(this) != sVar) {
                                    if (sVarC.e()) {
                                        sVarC.d();
                                    }
                                }
                            }
                            if (sVar.e()) {
                                sVar.d();
                            }
                        }
                    } else {
                        break;
                    }
                }
                i iVar2 = null;
                if (y.I(objC)) {
                    j();
                    x(j10, iVar);
                    t(this);
                } else {
                    i iVar3 = (i) y.C(objC);
                    long j11 = iVar3.f29487c;
                    if (j11 > j10) {
                        long j12 = j11 * ((long) c.f29317b);
                        if (f29307e.compareAndSet(this, andIncrement + 1, j12)) {
                            AtomicLongFieldUpdater atomicLongFieldUpdater = f29308f;
                            if ((atomicLongFieldUpdater.addAndGet(this, j12 - andIncrement) & 4611686018427387904L) != 0) {
                                while ((atomicLongFieldUpdater.get(this) & 4611686018427387904L) != 0) {
                                }
                            }
                        } else {
                            t(this);
                        }
                    } else {
                        iVar2 = iVar3;
                    }
                }
                if (iVar2 == null) {
                    continue;
                } else {
                    iVar = iVar2;
                }
            }
            int i10 = (int) (andIncrement % ((long) c.f29317b));
            Object objK = iVar.k(i10);
            boolean z10 = objK instanceof v1;
            AtomicLongFieldUpdater atomicLongFieldUpdater2 = f29306d;
            if (!z10 || andIncrement < atomicLongFieldUpdater2.get(this) || !iVar.j(i10, objK, c.f29322g)) {
                while (true) {
                    Object objK2 = iVar.k(i10);
                    if (!(objK2 instanceof v1)) {
                        if (objK2 != c.f29325j) {
                            if (objK2 != null) {
                                if (objK2 == c.f29319d || objK2 == c.f29323h || objK2 == c.f29324i || objK2 == c.f29326k || objK2 == c.f29327l) {
                                    break loop0;
                                }
                                if (objK2 != c.f29321f) {
                                    throw new IllegalStateException(("Unexpected cell state: " + objK2).toString());
                                }
                            } else if (iVar.j(i10, objK2, c.f29320e)) {
                                break loop0;
                            }
                        } else {
                            break;
                        }
                    } else if (andIncrement < atomicLongFieldUpdater2.get(this)) {
                        if (iVar.j(i10, objK2, new o((v1) objK2))) {
                            break loop0;
                        }
                    } else if (iVar.j(i10, objK2, c.f29322g)) {
                        if (D(objK2)) {
                            iVar.n(i10, c.f29319d);
                            break;
                        } else {
                            iVar.n(i10, c.f29325j);
                            iVar.l(i10, false);
                        }
                    }
                }
            } else if (D(objK)) {
                iVar.n(i10, c.f29319d);
                break;
            } else {
                iVar.n(i10, c.f29325j);
                iVar.l(i10, false);
                t(this);
            }
        }
    }

    public final i o(long j10, i iVar) {
        Object objC;
        AtomicLongFieldUpdater atomicLongFieldUpdater;
        long j11;
        i iVar2 = c.f29316a;
        BufferedChannelKt$createSegmentFunction$1 bufferedChannelKt$createSegmentFunction$1 = BufferedChannelKt$createSegmentFunction$1.f29301c;
        loop0: while (true) {
            objC = kotlinx.coroutines.internal.a.c(iVar, j10, bufferedChannelKt$createSegmentFunction$1);
            if (!y.I(objC)) {
                s sVarC = y.C(objC);
                while (true) {
                    AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29310h;
                    s sVar = (s) atomicReferenceFieldUpdater.get(this);
                    if (sVar.f29487c >= sVarC.f29487c) {
                        break loop0;
                    }
                    if (!sVarC.i()) {
                        break;
                    }
                    while (!atomicReferenceFieldUpdater.compareAndSet(this, sVar, sVarC)) {
                        if (atomicReferenceFieldUpdater.get(this) != sVar) {
                            if (sVarC.e()) {
                                sVarC.d();
                            }
                        }
                    }
                    if (sVar.e()) {
                        sVar.d();
                    }
                }
            } else {
                break;
            }
        }
        if (y.I(objC)) {
            j();
            if (iVar.f29487c * ((long) c.f29317b) >= s()) {
                return null;
            }
            iVar.a();
            return null;
        }
        i iVar3 = (i) y.C(objC);
        boolean zW = w();
        long j12 = iVar3.f29487c;
        if (!zW && j10 <= f29307e.get(this) / ((long) c.f29317b)) {
            while (true) {
                AtomicReferenceFieldUpdater atomicReferenceFieldUpdater2 = f29311i;
                s sVar2 = (s) atomicReferenceFieldUpdater2.get(this);
                if (sVar2.f29487c >= j12 || !iVar3.i()) {
                    break;
                }
                while (!atomicReferenceFieldUpdater2.compareAndSet(this, sVar2, iVar3)) {
                    if (atomicReferenceFieldUpdater2.get(this) != sVar2) {
                        if (iVar3.e()) {
                            iVar3.d();
                        }
                    }
                }
                if (sVar2.e()) {
                    sVar2.d();
                }
            }
        }
        if (j12 <= j10) {
            return iVar3;
        }
        long j13 = ((long) c.f29317b) * j12;
        do {
            atomicLongFieldUpdater = f29306d;
            j11 = atomicLongFieldUpdater.get(this);
            if (j11 >= j13) {
                break;
            }
        } while (!atomicLongFieldUpdater.compareAndSet(this, j11, j13));
        if (j12 * ((long) c.f29317b) >= s()) {
            return null;
        }
        iVar3.a();
        return null;
    }

    public final Throwable p() {
        return (Throwable) f29312j.get(this);
    }

    public final Throwable q() {
        Throwable thP = p();
        return thP == null ? new ClosedReceiveChannelException("Channel was closed") : thP;
    }

    public final Throwable r() {
        Throwable thP = p();
        return thP == null ? new ClosedSendChannelException("Channel was closed") : thP;
    }

    public final long s() {
        return f29305c.get(this) & 1152921504606846975L;
    }

    /* JADX WARN: Code restructure failed: missing block: B:75:0x0196, code lost:
    
        r3 = (kotlinx.coroutines.channels.i) r3.b();
     */
    /* JADX WARN: Code restructure failed: missing block: B:76:0x019d, code lost:
    
        if (r3 != null) goto L86;
     */
    /* JADX WARN: Multi-variable type inference failed */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final String toString() {
        String string;
        StringBuilder sb = new StringBuilder();
        int i10 = (int) (f29305c.get(this) >> 60);
        if (i10 == 2) {
            sb.append("closed,");
        } else if (i10 == 3) {
            sb.append("cancelled,");
        }
        sb.append("capacity=" + this.f29314a + ',');
        sb.append("data=[");
        int i11 = 0;
        List listM0 = hb.a.m0(f29310h.get(this), f29309g.get(this), f29311i.get(this));
        ArrayList arrayList = new ArrayList();
        for (Object obj : listM0) {
            if (((i) obj) != c.f29316a) {
                arrayList.add(obj);
            }
        }
        Iterator it = arrayList.iterator();
        if (!it.hasNext()) {
            throw new NoSuchElementException();
        }
        Object next = it.next();
        if (it.hasNext()) {
            long j10 = ((i) next).f29487c;
            do {
                Object next2 = it.next();
                long j11 = ((i) next2).f29487c;
                if (j10 > j11) {
                    next = next2;
                    j10 = j11;
                }
            } while (it.hasNext());
        }
        i iVar = (i) next;
        long j12 = f29306d.get(this);
        long jS = s();
        loop2: while (true) {
            int i12 = c.f29317b;
            int i13 = i11;
            while (true) {
                if (i13 >= i12) {
                    break;
                }
                long j13 = (iVar.f29487c * ((long) c.f29317b)) + ((long) i13);
                if (j13 >= jS && j13 >= j12) {
                    break loop2;
                }
                Object objK = iVar.k(i13);
                Object obj2 = iVar.f29340f.get(i13 * 2);
                if (objK instanceof kotlinx.coroutines.g) {
                    string = (j13 >= j12 || j13 < jS) ? (j13 >= jS || j13 < j12) ? "cont" : "send" : "receive";
                } else if (objK instanceof o) {
                    string = "EB(" + objK + ')';
                } else if (nb.g.c(objK, c.f29321f) || nb.g.c(objK, c.f29322g)) {
                    string = "resuming_sender";
                } else if (objK == null || nb.g.c(objK, c.f29320e) || nb.g.c(objK, c.f29324i) || nb.g.c(objK, c.f29323h) || nb.g.c(objK, c.f29326k) || nb.g.c(objK, c.f29325j) || nb.g.c(objK, c.f29327l)) {
                    i13++;
                } else {
                    string = objK.toString();
                }
                if (obj2 != null) {
                    sb.append("(" + string + ',' + obj2 + "),");
                } else {
                    sb.append(string + ',');
                }
                i13++;
            }
            i11 = 0;
        }
        if (sb.length() == 0) {
            throw new NoSuchElementException("Char sequence is empty.");
        }
        if (sb.charAt(kotlin.text.i.m0(sb)) == ',') {
            nb.g.h(sb.deleteCharAt(sb.length() - 1), "deleteCharAt(...)");
        }
        sb.append("]");
        return sb.toString();
    }

    /* JADX WARN: Code restructure failed: missing block: B:58:0x00c6, code lost:
    
        r0 = (kotlinx.coroutines.channels.i) ((kotlinx.coroutines.internal.d) kotlinx.coroutines.internal.d.f29457b.get(r0));
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean u(long j10, boolean z10) {
        int i10 = (int) (j10 >> 60);
        if (i10 == 0 || i10 == 1) {
            return false;
        }
        AtomicLongFieldUpdater atomicLongFieldUpdater = f29306d;
        if (i10 == 2) {
            l(j10 & 1152921504606846975L);
            if (z10) {
                while (true) {
                    AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29310h;
                    i iVarO = (i) atomicReferenceFieldUpdater.get(this);
                    long j11 = atomicLongFieldUpdater.get(this);
                    if (s() <= j11) {
                        break;
                    }
                    long j12 = c.f29317b;
                    long j13 = j11 / j12;
                    if (iVarO.f29487c != j13 && (iVarO = o(j13, iVarO)) == null) {
                        if (((i) atomicReferenceFieldUpdater.get(this)).f29487c < j13) {
                            break;
                        }
                    } else {
                        iVarO.a();
                        int i11 = (int) (j11 % j12);
                        while (true) {
                            Object objK = iVarO.k(i11);
                            if (objK == null || objK == c.f29320e) {
                                if (iVarO.j(i11, objK, c.f29323h)) {
                                    n();
                                    break;
                                }
                            } else {
                                if (objK == c.f29319d) {
                                    return false;
                                }
                                if (objK != c.f29325j && objK != c.f29327l && objK != c.f29324i && objK != c.f29323h) {
                                    if (objK == c.f29322g) {
                                        return false;
                                    }
                                    if (objK != c.f29321f && j11 == atomicLongFieldUpdater.get(this)) {
                                        return false;
                                    }
                                }
                            }
                        }
                        f29306d.compareAndSet(this, j11, 1 + j11);
                    }
                }
            }
        } else {
            if (i10 != 3) {
                throw new IllegalStateException(u1.g("unexpected close status: ", i10).toString());
            }
            i iVarL = l(j10 & 1152921504606846975L);
            UndeliveredElementException undeliveredElementExceptionB = null;
            Object objJ = null;
            loop0: do {
                int i12 = c.f29317b - 1;
                while (true) {
                    if (-1 >= i12) {
                        break;
                    }
                    long j14 = (iVarL.f29487c * ((long) c.f29317b)) + ((long) i12);
                    while (true) {
                        Object objK2 = iVarL.k(i12);
                        if (objK2 == c.f29324i) {
                            break loop0;
                        }
                        la.e eVar = c.f29319d;
                        AtomicReferenceArray atomicReferenceArray = iVarL.f29340f;
                        sd.l lVar = this.f29315b;
                        if (objK2 == eVar) {
                            if (j14 < atomicLongFieldUpdater.get(this)) {
                                break loop0;
                            }
                            if (iVarL.j(i12, objK2, c.f29327l)) {
                                if (lVar != null) {
                                    undeliveredElementExceptionB = kotlinx.coroutines.internal.a.b(lVar, atomicReferenceArray.get(i12 * 2), undeliveredElementExceptionB);
                                }
                                iVarL.m(i12, null);
                                iVarL.h();
                            }
                        } else if (objK2 == c.f29320e || objK2 == null) {
                            if (iVarL.j(i12, objK2, c.f29327l)) {
                                iVarL.h();
                                break;
                            }
                        } else if (!(objK2 instanceof v1) && !(objK2 instanceof o)) {
                            la.e eVar2 = c.f29322g;
                            if (objK2 == eVar2 || objK2 == c.f29321f) {
                                break loop0;
                            }
                            if (objK2 != eVar2) {
                                break;
                            }
                        } else {
                            if (j14 < atomicLongFieldUpdater.get(this)) {
                                break loop0;
                            }
                            v1 v1Var = objK2 instanceof o ? ((o) objK2).f29343a : (v1) objK2;
                            if (iVarL.j(i12, objK2, c.f29327l)) {
                                if (lVar != null) {
                                    undeliveredElementExceptionB = kotlinx.coroutines.internal.a.b(lVar, atomicReferenceArray.get(i12 * 2), undeliveredElementExceptionB);
                                }
                                objJ = kotlinx.coroutines.flow.h.J(objJ, v1Var);
                                iVarL.m(i12, null);
                                iVarL.h();
                            }
                        }
                    }
                    i12--;
                }
            } while (iVarL != null);
            if (objJ != null) {
                if (objJ instanceof ArrayList) {
                    ArrayList arrayList = (ArrayList) objJ;
                    for (int size = arrayList.size() - 1; -1 < size; size--) {
                        A((v1) arrayList.get(size), false);
                    }
                } else {
                    A((v1) objJ, false);
                }
            }
            if (undeliveredElementExceptionB != null) {
                throw undeliveredElementExceptionB;
            }
        }
        return true;
    }

    public boolean v() {
        return false;
    }

    public final boolean w() {
        long j10 = f29307e.get(this);
        return j10 == 0 || j10 == Long.MAX_VALUE;
    }

    /* JADX WARN: Code restructure failed: missing block: B:39:0x0011, code lost:
    
        continue;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void x(long j10, i iVar) {
        i iVar2;
        i iVar3;
        while (iVar.f29487c < j10 && (iVar3 = (i) iVar.b()) != null) {
            iVar = iVar3;
        }
        while (true) {
            if (!iVar.c() || (iVar2 = (i) iVar.b()) == null) {
                while (true) {
                    AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29311i;
                    s sVar = (s) atomicReferenceFieldUpdater.get(this);
                    if (sVar.f29487c >= iVar.f29487c) {
                        return;
                    }
                    if (!iVar.i()) {
                        break;
                    }
                    while (!atomicReferenceFieldUpdater.compareAndSet(this, sVar, iVar)) {
                        if (atomicReferenceFieldUpdater.get(this) != sVar) {
                            if (iVar.e()) {
                                iVar.d();
                            }
                        }
                    }
                    if (sVar.e()) {
                        sVar.d();
                        return;
                    }
                    return;
                }
            }
            iVar = iVar2;
        }
    }

    public final Object y(Object obj, Continuation continuation) throws Throwable {
        UndeliveredElementException undeliveredElementExceptionB;
        kotlinx.coroutines.h hVar = new kotlinx.coroutines.h(1, nb.g.F(continuation));
        hVar.t();
        sd.l lVar = this.f29315b;
        if (lVar == null || (undeliveredElementExceptionB = kotlinx.coroutines.internal.a.b(lVar, obj, null)) == null) {
            hVar.resumeWith(kotlin.a.b(r()));
        } else {
            kotlin.a.a(undeliveredElementExceptionB, r());
            hVar.resumeWith(kotlin.a.b(undeliveredElementExceptionB));
        }
        Object objS = hVar.s();
        return objS == CoroutineSingletons.f29226a ? objS : kd.e.f29109a;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:48:0x00e3 A[Catch: all -> 0x008f, TryCatch #1 {all -> 0x008f, blocks: (B:26:0x0076, B:30:0x0083, B:32:0x008b, B:35:0x0092, B:36:0x0098, B:38:0x00a3, B:39:0x00b0, B:41:0x00c4, B:45:0x00ce, B:47:0x00df, B:48:0x00e3, B:50:0x00e7, B:52:0x00ef, B:54:0x00f5, B:56:0x00f9, B:58:0x00fe, B:60:0x0104, B:61:0x0108, B:62:0x0111, B:63:0x0112, B:65:0x0119), top: B:81:0x0072 }] */
    /* JADX WARN: Removed duplicated region for block: B:91:0x00df A[SYNTHETIC] */
    /* JADX WARN: Type inference failed for: r10v2 */
    /* JADX WARN: Type inference failed for: r10v3, types: [kotlinx.coroutines.h] */
    /* JADX WARN: Type inference failed for: r10v7 */
    /* JADX WARN: Type inference failed for: r18v0, types: [java.lang.Object, kotlinx.coroutines.channels.b] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object z(Continuation continuation) throws Throwable {
        i iVar;
        ?? r10;
        Object objE;
        kotlinx.coroutines.h hVar;
        sd.l lVarA;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f29310h;
        i iVar2 = (i) atomicReferenceFieldUpdater.get(this);
        while (true) {
            AtomicLongFieldUpdater atomicLongFieldUpdater = f29305c;
            if (u(atomicLongFieldUpdater.get(this), true)) {
                Throwable thQ = q();
                int i10 = t.f29488a;
                throw thQ;
            }
            AtomicLongFieldUpdater atomicLongFieldUpdater2 = f29306d;
            long andIncrement = atomicLongFieldUpdater2.getAndIncrement(this);
            long j10 = c.f29317b;
            long j11 = andIncrement / j10;
            int i11 = (int) (andIncrement % j10);
            if (iVar2.f29487c != j11) {
                i iVarO = o(j11, iVar2);
                if (iVarO == null) {
                    continue;
                } else {
                    iVar = iVarO;
                }
            } else {
                iVar = iVar2;
            }
            Object objE2 = E(iVar, i11, andIncrement, null);
            la.e eVar = c.f29328m;
            if (objE2 == eVar) {
                throw new IllegalStateException("unexpected".toString());
            }
            la.e eVar2 = c.f29330o;
            if (objE2 != eVar2) {
                if (objE2 != c.f29329n) {
                    iVar.a();
                    return objE2;
                }
                kotlinx.coroutines.h hVarC0 = hb.a.c0(nb.g.F(continuation));
                la.e eVar3 = eVar;
                try {
                    objE = E(iVar, i11, andIncrement, hVarC0);
                } catch (Throwable th) {
                    th = th;
                    r10 = hVarC0;
                }
                try {
                    if (objE == eVar3) {
                        hVar = hVarC0;
                        hVar.a(iVar, i11);
                    } else {
                        hVar = hVarC0;
                        sd.l lVar = this.f29315b;
                        kotlin.coroutines.h hVar2 = hVar.f29439e;
                        if (objE == eVar2) {
                            if (andIncrement < s()) {
                                iVar.a();
                            }
                            i iVar3 = (i) atomicReferenceFieldUpdater.get(this);
                            while (true) {
                                if (u(atomicLongFieldUpdater.get(this), true)) {
                                    hVar.resumeWith(kotlin.a.b(q()));
                                    break;
                                }
                                long andIncrement2 = atomicLongFieldUpdater2.getAndIncrement(this);
                                long j12 = c.f29317b;
                                AtomicLongFieldUpdater atomicLongFieldUpdater3 = atomicLongFieldUpdater;
                                long j13 = andIncrement2 / j12;
                                int i12 = (int) (andIncrement2 % j12);
                                if (iVar3.f29487c == j13) {
                                    kotlin.coroutines.h hVar3 = hVar2;
                                    sd.l lVar2 = lVar;
                                    objE = E(iVar3, i12, andIncrement2, hVar);
                                    if (objE != c.f29328m) {
                                        hVar.a(iVar3, i12);
                                        break;
                                    }
                                    if (objE == c.f29330o) {
                                        if (andIncrement2 < s()) {
                                            iVar3.a();
                                        }
                                        hVar2 = hVar3;
                                        lVar = lVar2;
                                        atomicLongFieldUpdater = atomicLongFieldUpdater3;
                                    } else {
                                        if (objE == c.f29329n) {
                                            throw new IllegalStateException("unexpected".toString());
                                        }
                                        iVar3.a();
                                        lVarA = lVar2 != null ? kotlinx.coroutines.internal.a.a(lVar2, objE, hVar3) : null;
                                    }
                                } else {
                                    i iVarO2 = o(j13, iVar3);
                                    if (iVarO2 == null) {
                                        continue;
                                        atomicLongFieldUpdater = atomicLongFieldUpdater3;
                                    } else {
                                        iVar3 = iVarO2;
                                        kotlin.coroutines.h hVar32 = hVar2;
                                        sd.l lVar22 = lVar;
                                        objE = E(iVar3, i12, andIncrement2, hVar);
                                        if (objE != c.f29328m) {
                                        }
                                    }
                                }
                            }
                        } else {
                            iVar.a();
                            lVarA = lVar != null ? kotlinx.coroutines.internal.a.a(lVar, objE, hVar2) : null;
                        }
                        hVar.d(objE, lVarA);
                    }
                    return hVar.s();
                } catch (Throwable th2) {
                    th = th2;
                    r10 = eVar3;
                    r10.A();
                    throw th;
                }
            }
            if (andIncrement < s()) {
                iVar.a();
            }
            iVar2 = iVar;
        }
    }
}
