package kotlinx.coroutines.internal;

import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/* JADX INFO: loaded from: classes.dex */
public final class k {
    private volatile /* synthetic */ Object _next = null;
    private volatile /* synthetic */ long _state = 0;

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final boolean f5166b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final int f5167c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private /* synthetic */ AtomicReferenceArray f5168d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final a f5161e = new a(null);

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final r f5164h = new r("REMOVE_FROZEN");

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private static final /* synthetic */ AtomicReferenceFieldUpdater f5162f = AtomicReferenceFieldUpdater.newUpdater(k.class, Object.class, "_next");

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private static final /* synthetic */ AtomicLongFieldUpdater f5163g = AtomicLongFieldUpdater.newUpdater(k.class, "_state");

    public static final class a {
        public /* synthetic */ a(kotlin.jvm.internal.g gVar) {
            this();
        }

        public final int a(long j2) {
            return (j2 & 2305843009213693952L) != 0 ? 2 : 1;
        }

        public final long b(long j2, int i2) {
            return d(j2, 1073741823L) | ((long) i2);
        }

        public final long c(long j2, int i2) {
            return d(j2, 1152921503533105152L) | (((long) i2) << 30);
        }

        public final long d(long j2, long j3) {
            return j2 & (~j3);
        }

        private a() {
        }
    }

    public static final class b {

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

        public b(int i2) {
            this.f5169a = i2;
        }
    }

    public k(int i2, boolean z2) {
        this.f5165a = i2;
        this.f5166b = z2;
        int i3 = i2 - 1;
        this.f5167c = i3;
        this.f5168d = new AtomicReferenceArray(i2);
        if (!(i3 <= 1073741823)) {
            throw new IllegalStateException("Check failed.");
        }
        if (!((i2 & i3) == 0)) {
            throw new IllegalStateException("Check failed.");
        }
    }

    private final k b(long j2) {
        k kVar = new k(this.f5165a * 2, this.f5166b);
        int i2 = (int) (1073741823 & j2);
        int i3 = (int) ((1152921503533105152L & j2) >> 30);
        while (true) {
            int i4 = this.f5167c;
            if ((i2 & i4) == (i3 & i4)) {
                kVar._state = f5161e.d(j2, 1152921504606846976L);
                return kVar;
            }
            Object bVar = this.f5168d.get(i4 & i2);
            if (bVar == null) {
                bVar = new b(i2);
            }
            kVar.f5168d.set(kVar.f5167c & i2, bVar);
            i2++;
        }
    }

    private final k c(long j2) {
        while (true) {
            k kVar = (k) this._next;
            if (kVar != null) {
                return kVar;
            }
            androidx.concurrent.futures.a.a(f5162f, this, null, b(j2));
        }
    }

    private final k e(int i2, Object obj) {
        Object obj2 = this.f5168d.get(this.f5167c & i2);
        if (!(obj2 instanceof b) || ((b) obj2).f5169a != i2) {
            return null;
        }
        this.f5168d.set(i2 & this.f5167c, obj);
        return this;
    }

    private final long h() {
        long j2;
        long j3;
        do {
            j2 = this._state;
            if ((j2 & 1152921504606846976L) != 0) {
                return j2;
            }
            j3 = j2 | 1152921504606846976L;
        } while (!f5163g.compareAndSet(this, j2, j3));
        return j3;
    }

    private final k k(int i2, int i3) {
        long j2;
        int i4;
        do {
            j2 = this._state;
            i4 = (int) (1073741823 & j2);
            if ((1152921504606846976L & j2) != 0) {
                return i();
            }
        } while (!f5163g.compareAndSet(this, j2, f5161e.b(j2, i3)));
        this.f5168d.set(i4 & this.f5167c, null);
        return null;
    }

    /* JADX WARN: Code restructure failed: missing block: B:17:0x004a, code lost:
    
        return 1;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final int a(java.lang.Object r12) {
        /*
            r11 = this;
        L0:
            long r2 = r11._state
            r0 = 3458764513820540928(0x3000000000000000, double:1.727233711018889E-77)
            long r0 = r0 & r2
            r6 = 0
            int r0 = (r0 > r6 ? 1 : (r0 == r6 ? 0 : -1))
            if (r0 == 0) goto L12
            kotlinx.coroutines.internal.k$a r12 = kotlinx.coroutines.internal.k.f5161e
            int r12 = r12.a(r2)
            return r12
        L12:
            r0 = 1073741823(0x3fffffff, double:5.304989472E-315)
            long r0 = r0 & r2
            int r0 = (int) r0
            r4 = 1152921503533105152(0xfffffffc0000000, double:1.2882296003504729E-231)
            long r4 = r4 & r2
            r1 = 30
            long r4 = r4 >> r1
            int r8 = (int) r4
            int r9 = r11.f5167c
            int r1 = r8 + 2
            r1 = r1 & r9
            r4 = r0 & r9
            r5 = 1
            if (r1 != r4) goto L2c
            return r5
        L2c:
            boolean r1 = r11.f5166b
            r4 = 1073741823(0x3fffffff, float:1.9999999)
            if (r1 != 0) goto L4b
            java.util.concurrent.atomic.AtomicReferenceArray r1 = r11.f5168d
            r10 = r8 & r9
            java.lang.Object r1 = r1.get(r10)
            if (r1 == 0) goto L4b
            int r1 = r11.f5165a
            r2 = 1024(0x400, float:1.435E-42)
            if (r1 < r2) goto L4a
            int r8 = r8 - r0
            r0 = r8 & r4
            int r1 = r1 >> 1
            if (r0 <= r1) goto L0
        L4a:
            return r5
        L4b:
            int r0 = r8 + 1
            r0 = r0 & r4
            java.util.concurrent.atomic.AtomicLongFieldUpdater r1 = kotlinx.coroutines.internal.k.f5163g
            kotlinx.coroutines.internal.k$a r4 = kotlinx.coroutines.internal.k.f5161e
            long r4 = r4.c(r2, r0)
            r0 = r1
            r1 = r11
            boolean r0 = r0.compareAndSet(r1, r2, r4)
            if (r0 == 0) goto L0
            java.util.concurrent.atomic.AtomicReferenceArray r0 = r11.f5168d
            r1 = r8 & r9
            r0.set(r1, r12)
            r0 = r11
        L66:
            long r1 = r0._state
            r3 = 1152921504606846976(0x1000000000000000, double:1.2882297539194267E-231)
            long r1 = r1 & r3
            int r1 = (r1 > r6 ? 1 : (r1 == r6 ? 0 : -1))
            if (r1 != 0) goto L70
            goto L7a
        L70:
            kotlinx.coroutines.internal.k r0 = r0.i()
            kotlinx.coroutines.internal.k r0 = r0.e(r8, r12)
            if (r0 != 0) goto L66
        L7a:
            r12 = 0
            return r12
        */
        throw new UnsupportedOperationException("Method not decompiled: kotlinx.coroutines.internal.k.a(java.lang.Object):int");
    }

    public final boolean d() {
        long j2;
        do {
            j2 = this._state;
            if ((j2 & 2305843009213693952L) != 0) {
                return true;
            }
            if ((1152921504606846976L & j2) != 0) {
                return false;
            }
        } while (!f5163g.compareAndSet(this, j2, j2 | 2305843009213693952L));
        return true;
    }

    public final int f() {
        long j2 = this._state;
        return (((int) ((j2 & 1152921503533105152L) >> 30)) - ((int) (1073741823 & j2))) & 1073741823;
    }

    public final boolean g() {
        long j2 = this._state;
        return ((int) (1073741823 & j2)) == ((int) ((j2 & 1152921503533105152L) >> 30));
    }

    public final k i() {
        return c(h());
    }

    public final Object j() {
        while (true) {
            long j2 = this._state;
            if ((1152921504606846976L & j2) != 0) {
                return f5164h;
            }
            int i2 = (int) (1073741823 & j2);
            int i3 = this.f5167c;
            if ((((int) ((1152921503533105152L & j2) >> 30)) & i3) == (i2 & i3)) {
                return null;
            }
            Object obj = this.f5168d.get(i3 & i2);
            if (obj == null) {
                if (this.f5166b) {
                    return null;
                }
            } else {
                if (obj instanceof b) {
                    return null;
                }
                int i4 = (i2 + 1) & 1073741823;
                if (f5163g.compareAndSet(this, j2, f5161e.b(j2, i4))) {
                    this.f5168d.set(this.f5167c & i2, null);
                    return obj;
                }
                if (this.f5166b) {
                    k kVarK = this;
                    do {
                        kVarK = kVarK.k(i2, i4);
                    } while (kVarK != null);
                    return obj;
                }
            }
        }
    }
}
