package e5;

import I2.C0317a;
import K.R0;
import V4.C0772m;
import V4.E;
import V4.InterfaceC0770l;
import V4.N0;
import Z0.o;
import a.AbstractC0872a;
import a5.AbstractC0988a;
import a5.q;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import w4.C3137A;

/* JADX INFO: loaded from: classes3.dex */
public class h {

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

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

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final /* synthetic */ AtomicIntegerFieldUpdater f16353g = AtomicIntegerFieldUpdater.newUpdater(h.class, "_availablePermits$volatile");
    private volatile /* synthetic */ int _availablePermits$volatile;

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final R0 f16355b;
    private volatile /* synthetic */ long deqIdx$volatile;
    private volatile /* synthetic */ long enqIdx$volatile;
    private volatile /* synthetic */ Object head$volatile;
    private volatile /* synthetic */ Object tail$volatile;

    public h(int i6, int i7) {
        this.f16354a = i6;
        if (i6 <= 0) {
            throw new IllegalArgumentException(o.l("Semaphore should have at least 1 permit, but had ", i6).toString());
        }
        if (i7 < 0 || i7 > i6) {
            throw new IllegalArgumentException(o.l("The number of acquired permits should be in 0..", i6).toString());
        }
        k kVar = new k(0L, null, 2);
        this.head$volatile = kVar;
        this.tail$volatile = kVar;
        this._availablePermits$volatile = i6 - i7;
        this.f16355b = new R0(this, 7);
    }

    /* JADX WARN: Code restructure failed: missing block: B:13:0x0025, code lost:
    
        r5.k(r3, r4.f16355b);
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object a(C4.c cVar) {
        AtomicIntegerFieldUpdater atomicIntegerFieldUpdater;
        int andDecrement;
        int i6;
        do {
            atomicIntegerFieldUpdater = f16353g;
            andDecrement = atomicIntegerFieldUpdater.getAndDecrement(this);
            i6 = this.f16354a;
        } while (andDecrement > i6);
        C3137A c3137a = C3137A.f25453a;
        if (andDecrement <= 0) {
            C0772m c0772mR = E.r(AbstractC0872a.V(cVar));
            try {
                if (!b(c0772mR)) {
                    while (true) {
                        int andDecrement2 = atomicIntegerFieldUpdater.getAndDecrement(this);
                        if (andDecrement2 <= i6) {
                            if (andDecrement2 > 0) {
                                break;
                            }
                            if (b(c0772mR)) {
                                break;
                            }
                        }
                    }
                }
                Object objR = c0772mR.r();
                B4.a aVar = B4.a.f492a;
                if (objR != aVar) {
                    objR = c3137a;
                }
                if (objR == aVar) {
                    return objR;
                }
            } catch (Throwable th) {
                c0772mR.z();
                throw th;
            }
        }
        return c3137a;
    }

    public final boolean b(N0 n02) {
        Object objB;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f16351e;
        k kVar = (k) atomicReferenceFieldUpdater.get(this);
        long andIncrement = f16352f.getAndIncrement(this);
        f fVar = f.f16347a;
        long j4 = andIncrement / ((long) j.f16361f);
        loop0: while (true) {
            objB = AbstractC0988a.b(kVar, j4, fVar);
            if (!AbstractC0988a.e(objB)) {
                q qVarC = AbstractC0988a.c(objB);
                while (true) {
                    q qVar = (q) atomicReferenceFieldUpdater.get(this);
                    if (qVar.f10849c >= qVarC.f10849c) {
                        break loop0;
                    }
                    if (!qVarC.j()) {
                        break;
                    }
                    while (!atomicReferenceFieldUpdater.compareAndSet(this, qVar, qVarC)) {
                        if (atomicReferenceFieldUpdater.get(this) != qVar) {
                            if (qVarC.f()) {
                                qVarC.e();
                            }
                        }
                    }
                    if (qVar.f()) {
                        qVar.e();
                    }
                }
            } else {
                break;
            }
        }
        k kVar2 = (k) AbstractC0988a.c(objB);
        int i6 = (int) (andIncrement % ((long) j.f16361f));
        AtomicReferenceArray atomicReferenceArray = kVar2.f16362e;
        while (!atomicReferenceArray.compareAndSet(i6, null, n02)) {
            if (atomicReferenceArray.get(i6) != null) {
                C0317a c0317a = j.f16357b;
                C0317a c0317a2 = j.f16358c;
                while (!atomicReferenceArray.compareAndSet(i6, c0317a, c0317a2)) {
                    if (atomicReferenceArray.get(i6) != c0317a) {
                        return false;
                    }
                }
                if (n02 instanceof InterfaceC0770l) {
                    ((InterfaceC0770l) n02).k(C3137A.f25453a, this.f16355b);
                    return true;
                }
                throw new IllegalStateException(("unexpected: " + n02).toString());
            }
        }
        n02.a(kVar2, i6);
        return true;
    }

    public final void c() {
        int i6;
        Object objB;
        boolean z6;
        do {
            AtomicIntegerFieldUpdater atomicIntegerFieldUpdater = f16353g;
            int andIncrement = atomicIntegerFieldUpdater.getAndIncrement(this);
            int i7 = this.f16354a;
            if (andIncrement >= i7) {
                do {
                    i6 = atomicIntegerFieldUpdater.get(this);
                    if (i6 <= i7) {
                        break;
                    }
                } while (!atomicIntegerFieldUpdater.compareAndSet(this, i6, i7));
                throw new IllegalStateException(("The number of released permits cannot be greater than " + i7).toString());
            }
            if (andIncrement >= 0) {
                return;
            }
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f16349c;
            k kVar = (k) atomicReferenceFieldUpdater.get(this);
            long andIncrement2 = f16350d.getAndIncrement(this);
            long j4 = andIncrement2 / ((long) j.f16361f);
            g gVar = g.f16348a;
            while (true) {
                objB = AbstractC0988a.b(kVar, j4, gVar);
                if (AbstractC0988a.e(objB)) {
                    break;
                }
                q qVarC = AbstractC0988a.c(objB);
                while (true) {
                    q qVar = (q) atomicReferenceFieldUpdater.get(this);
                    if (qVar.f10849c >= qVarC.f10849c) {
                        break;
                    }
                    if (!qVarC.j()) {
                        break;
                    }
                    while (!atomicReferenceFieldUpdater.compareAndSet(this, qVar, qVarC)) {
                        if (atomicReferenceFieldUpdater.get(this) != qVar) {
                            if (qVarC.f()) {
                                qVarC.e();
                            }
                        }
                    }
                    if (qVar.f()) {
                        qVar.e();
                    }
                }
            }
            k kVar2 = (k) AbstractC0988a.c(objB);
            kVar2.b();
            z6 = false;
            if (kVar2.f10849c <= j4) {
                int i8 = (int) (andIncrement2 % ((long) j.f16361f));
                C0317a c0317a = j.f16357b;
                AtomicReferenceArray atomicReferenceArray = kVar2.f16362e;
                Object andSet = atomicReferenceArray.getAndSet(i8, c0317a);
                if (andSet == null) {
                    int i9 = j.f16356a;
                    for (int i10 = 0; i10 < i9; i10++) {
                        if (atomicReferenceArray.get(i8) == j.f16358c) {
                            z6 = true;
                            break;
                        }
                    }
                    C0317a c0317a2 = j.f16357b;
                    C0317a c0317a3 = j.f16359d;
                    while (true) {
                        if (!atomicReferenceArray.compareAndSet(i8, c0317a2, c0317a3)) {
                            if (atomicReferenceArray.get(i8) != c0317a2) {
                                break;
                            }
                        } else {
                            z6 = true;
                            break;
                        }
                    }
                    z6 = !z6;
                } else if (andSet != j.f16360e) {
                    if (!(andSet instanceof InterfaceC0770l)) {
                        throw new IllegalStateException(("unexpected: " + andSet).toString());
                    }
                    InterfaceC0770l interfaceC0770l = (InterfaceC0770l) andSet;
                    C0317a c0317aB = interfaceC0770l.b(C3137A.f25453a, this.f16355b);
                    if (c0317aB != null) {
                        interfaceC0770l.l(c0317aB);
                        z6 = true;
                        break;
                        break;
                    }
                }
            }
        } while (!z6);
    }
}
