package kotlinx.coroutines.internal;

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

/* JADX INFO: compiled from: LockFreeTaskQueue.kt */
/* JADX INFO: loaded from: classes.dex */
public final class q<E> {
    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 f5229a;

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

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

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

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

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

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

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

    /* JADX INFO: compiled from: LockFreeTaskQueue.kt */
    public static final class a {
        private a() {
        }

        public /* synthetic */ a(w1.e eVar) {
            this();
        }

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

        public final long b(long j3, int i3) {
            return d(j3, 1073741823L) | (((long) i3) << 0);
        }

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

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

    /* JADX INFO: compiled from: LockFreeTaskQueue.kt */
    public static final class b {

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

        public b(int i3) {
            this.f5233a = i3;
        }
    }

    public q(int i3, boolean z2) {
        this.f5229a = i3;
        this.f5230b = z2;
        int i4 = i3 - 1;
        this.f5231c = i4;
        this.f5232d = new AtomicReferenceArray(i3);
        if (!(i4 <= 1073741823)) {
            throw new IllegalStateException("Check failed.".toString());
        }
        if (!((i3 & i4) == 0)) {
            throw new IllegalStateException("Check failed.".toString());
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    private final q<E> b(long j3) {
        q<E> qVar = new q<>(this.f5229a * 2, this.f5230b);
        int i3 = (int) ((1073741823 & j3) >> 0);
        int i4 = (int) ((1152921503533105152L & j3) >> 30);
        while (true) {
            int i5 = this.f5231c;
            if ((i3 & i5) == (i4 & i5)) {
                qVar._state = f5225e.d(j3, 1152921504606846976L);
                return qVar;
            }
            Object bVar = this.f5232d.get(i5 & i3);
            if (bVar == null) {
                bVar = new b(i3);
            }
            qVar.f5232d.set(qVar.f5231c & i3, bVar);
            i3++;
        }
    }

    private final q<E> c(long j3) {
        while (true) {
            q<E> qVar = (q) this._next;
            if (qVar != null) {
                return qVar;
            }
            f5226f.compareAndSet(this, null, b(j3));
        }
    }

    private final q<E> e(int i3, E e3) {
        Object obj = this.f5232d.get(this.f5231c & i3);
        if (!(obj instanceof b) || ((b) obj).f5233a != i3) {
            return null;
        }
        this.f5232d.set(i3 & this.f5231c, e3);
        return this;
    }

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

    private final q<E> k(int i3, int i4) {
        long j3;
        a aVar;
        int i5;
        do {
            j3 = this._state;
            aVar = f5225e;
            i5 = (int) ((1073741823 & j3) >> 0);
            if (d2.c0.a()) {
                if (!(i5 == i3)) {
                    throw new AssertionError();
                }
            }
            if ((1152921504606846976L & j3) != 0) {
                return i();
            }
        } while (!f5227g.compareAndSet(this, j3, aVar.b(j3, i4)));
        this.f5232d.set(this.f5231c & i5, null);
        return null;
    }

    /* JADX WARN: Code restructure failed: missing block: B:17:0x004e, 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(E r14) {
        /*
            r13 = this;
        L0:
            long r2 = r13._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.q$a r13 = kotlinx.coroutines.internal.q.f5225e
            int r13 = r13.a(r2)
            return r13
        L12:
            kotlinx.coroutines.internal.q$a r0 = kotlinx.coroutines.internal.q.f5225e
            r4 = 1073741823(0x3fffffff, double:5.304989472E-315)
            long r4 = r4 & r2
            r8 = 0
            long r4 = r4 >> r8
            int r1 = (int) r4
            r4 = 1152921503533105152(0xfffffffc0000000, double:1.2882296003504729E-231)
            long r4 = r4 & r2
            r9 = 30
            long r4 = r4 >> r9
            int r9 = (int) r4
            int r10 = r13.f5231c
            int r4 = r9 + 2
            r4 = r4 & r10
            r5 = r1 & r10
            r11 = 1
            if (r4 != r5) goto L30
            return r11
        L30:
            boolean r4 = r13.f5230b
            r5 = 1073741823(0x3fffffff, float:1.9999999)
            if (r4 != 0) goto L4f
            java.util.concurrent.atomic.AtomicReferenceArray r4 = r13.f5232d
            r12 = r9 & r10
            java.lang.Object r4 = r4.get(r12)
            if (r4 == 0) goto L4f
            int r0 = r13.f5229a
            r2 = 1024(0x400, float:1.435E-42)
            if (r0 < r2) goto L4e
            int r9 = r9 - r1
            r1 = r9 & r5
            int r0 = r0 >> 1
            if (r1 <= r0) goto L0
        L4e:
            return r11
        L4f:
            int r1 = r9 + 1
            r1 = r1 & r5
            java.util.concurrent.atomic.AtomicLongFieldUpdater r4 = kotlinx.coroutines.internal.q.f5227g
            long r11 = r0.c(r2, r1)
            r0 = r4
            r1 = r13
            r4 = r11
            boolean r0 = r0.compareAndSet(r1, r2, r4)
            if (r0 == 0) goto L0
            java.util.concurrent.atomic.AtomicReferenceArray r0 = r13.f5232d
            r1 = r9 & r10
            r0.set(r1, r14)
        L68:
            long r0 = r13._state
            r2 = 1152921504606846976(0x1000000000000000, double:1.2882297539194267E-231)
            long r0 = r0 & r2
            int r0 = (r0 > r6 ? 1 : (r0 == r6 ? 0 : -1))
            if (r0 != 0) goto L72
            goto L7c
        L72:
            kotlinx.coroutines.internal.q r13 = r13.i()
            kotlinx.coroutines.internal.q r13 = r13.e(r9, r14)
            if (r13 != 0) goto L68
        L7c:
            return r8
        */
        throw new UnsupportedOperationException("Method not decompiled: kotlinx.coroutines.internal.q.a(java.lang.Object):int");
    }

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

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

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

    public final q<E> i() {
        return c(h());
    }

    public final Object j() {
        while (true) {
            long j3 = this._state;
            if ((1152921504606846976L & j3) != 0) {
                return f5228h;
            }
            a aVar = f5225e;
            int i3 = (int) ((1073741823 & j3) >> 0);
            int i4 = (int) ((1152921503533105152L & j3) >> 30);
            int i5 = this.f5231c;
            if ((i4 & i5) == (i3 & i5)) {
                return null;
            }
            Object obj = this.f5232d.get(i5 & i3);
            if (obj == null) {
                if (this.f5230b) {
                    return null;
                }
            } else {
                if (obj instanceof b) {
                    return null;
                }
                int i6 = (i3 + 1) & 1073741823;
                if (f5227g.compareAndSet(this, j3, aVar.b(j3, i6))) {
                    this.f5232d.set(this.f5231c & i3, null);
                    return obj;
                }
                if (this.f5230b) {
                    do {
                        this = this.k(i3, i6);
                    } while (this != null);
                    return obj;
                }
            }
        }
    }
}
