package kotlinx.coroutines.internal;

import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import p039m1.C1468e;
import p060t1.C1679h;

/* renamed from: kotlinx.coroutines.internal.n */
/* loaded from: classes.dex */
public final class C1373n<E> {
    private volatile /* synthetic */ Object _next = null;
    private volatile /* synthetic */ long _state = 0;

    /* renamed from: a */
    public final int f3053a;

    /* renamed from: b */
    public final boolean f3054b;

    /* renamed from: c */
    public final int f3055c;

    /* renamed from: d */
    public /* synthetic */ AtomicReferenceArray f3056d;

    /* renamed from: e */
    public static final C1374a f3049e = new C1374a(null);

    /* renamed from: h */
    public static final C1384w f3052h = new C1384w("REMOVE_FROZEN");

    /* renamed from: f */
    public static final /* synthetic */ AtomicReferenceFieldUpdater f3050f = AtomicReferenceFieldUpdater.newUpdater(C1373n.class, Object.class, "_next");

    /* renamed from: g */
    public static final /* synthetic */ AtomicLongFieldUpdater f3051g = AtomicLongFieldUpdater.newUpdater(C1373n.class, "_state");

    /* renamed from: kotlinx.coroutines.internal.n$a */
    /* loaded from: classes.dex */
    public static final class C1374a {
        public C1374a() {
        }

        public /* synthetic */ C1374a(C1468e c1468e) {
            this();
        }

        /* renamed from: a */
        public final int m1499a(long j) {
            return (j & 2305843009213693952L) != 0 ? 2 : 1;
        }

        /* renamed from: b */
        public final long m1498b(long j, int i) {
            return m1496d(j, 1073741823L) | (i << 0);
        }

        /* renamed from: c */
        public final long m1497c(long j, int i) {
            return m1496d(j, 1152921503533105152L) | (i << 30);
        }

        /* renamed from: d */
        public final long m1496d(long j, long j2) {
            return j & (j2 ^ (-1));
        }
    }

    /* renamed from: kotlinx.coroutines.internal.n$b */
    /* loaded from: classes.dex */
    public static final class C1375b {

        /* renamed from: a */
        public final int f3057a;

        public C1375b(int i) {
            this.f3057a = i;
        }
    }

    public C1373n(int i, boolean z) {
        this.f3053a = i;
        this.f3054b = z;
        int i2 = i - 1;
        this.f3055c = i2;
        this.f3056d = new AtomicReferenceArray(i);
        if (!(i2 <= 1073741823)) {
            throw new IllegalStateException("Check failed.".toString());
        }
        if (!((i & i2) == 0)) {
            throw new IllegalStateException("Check failed.".toString());
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:17:0x004c, code lost:
        return 1;
     */
    /* renamed from: a */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final int m1510a(E e) {
        while (true) {
            long j = this._state;
            if ((3458764513820540928L & j) != 0) {
                return f3049e.m1499a(j);
            }
            int i = (int) ((1073741823 & j) >> 0);
            int i2 = (int) ((1152921503533105152L & j) >> 30);
            int i3 = this.f3055c;
            if (((i2 + 2) & i3) == (i & i3)) {
                return 1;
            }
            if (!this.f3054b && this.f3056d.get(i2 & i3) != null) {
                int i4 = this.f3053a;
                if (i4 < 1024 || ((i2 - i) & 1073741823) > (i4 >> 1)) {
                    break;
                }
            } else if (f3051g.compareAndSet(this, j, f3049e.m1497c(j, (i2 + 1) & 1073741823))) {
                this.f3056d.set(i2 & i3, e);
                C1373n<E> c1373n = this;
                while ((c1373n._state & 1152921504606846976L) != 0 && (c1373n = c1373n.m1502i().m1506e(i2, e)) != null) {
                }
                return 0;
            }
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* renamed from: b */
    public final C1373n<E> m1509b(long j) {
        C1373n<E> c1373n = new C1373n<>(this.f3053a * 2, this.f3054b);
        int i = (int) ((1073741823 & j) >> 0);
        int i2 = (int) ((1152921503533105152L & j) >> 30);
        while (true) {
            int i3 = this.f3055c;
            if ((i & i3) == (i2 & i3)) {
                c1373n._state = f3049e.m1496d(j, 1152921504606846976L);
                return c1373n;
            }
            Object obj = this.f3056d.get(i3 & i);
            if (obj == null) {
                obj = new C1375b(i);
            }
            c1373n.f3056d.set(c1373n.f3055c & i, obj);
            i++;
        }
    }

    /* renamed from: c */
    public final C1373n<E> m1508c(long j) {
        while (true) {
            C1373n<E> c1373n = (C1373n) this._next;
            if (c1373n != null) {
                return c1373n;
            }
            C1679h.m602a(f3050f, this, null, m1509b(j));
        }
    }

    /* renamed from: d */
    public final boolean m1507d() {
        long j;
        do {
            j = this._state;
            if ((j & 2305843009213693952L) != 0) {
                return true;
            }
            if ((1152921504606846976L & j) != 0) {
                return false;
            }
        } while (!f3051g.compareAndSet(this, j, j | 2305843009213693952L));
        return true;
    }

    /* renamed from: e */
    public final C1373n<E> m1506e(int i, E e) {
        Object obj = this.f3056d.get(this.f3055c & i);
        if ((obj instanceof C1375b) && ((C1375b) obj).f3057a == i) {
            this.f3056d.set(i & this.f3055c, e);
            return this;
        }
        return null;
    }

    /* renamed from: f */
    public final int m1505f() {
        long j = this._state;
        return 1073741823 & (((int) ((j & 1152921503533105152L) >> 30)) - ((int) ((1073741823 & j) >> 0)));
    }

    /* renamed from: g */
    public final boolean m1504g() {
        long j = this._state;
        return ((int) ((1073741823 & j) >> 0)) == ((int) ((j & 1152921503533105152L) >> 30));
    }

    /* renamed from: h */
    public final long m1503h() {
        long j;
        long j2;
        do {
            j = this._state;
            if ((j & 1152921504606846976L) != 0) {
                return j;
            }
            j2 = j | 1152921504606846976L;
        } while (!f3051g.compareAndSet(this, j, j2));
        return j2;
    }

    /* renamed from: i */
    public final C1373n<E> m1502i() {
        return m1508c(m1503h());
    }

    /* renamed from: j */
    public final Object m1501j() {
        while (true) {
            long j = this._state;
            if ((1152921504606846976L & j) != 0) {
                return f3052h;
            }
            int i = (int) ((1073741823 & j) >> 0);
            int i2 = (int) ((1152921503533105152L & j) >> 30);
            int i3 = this.f3055c;
            if ((i2 & i3) == (i & i3)) {
                return null;
            }
            Object obj = this.f3056d.get(i3 & i);
            if (obj == null) {
                if (this.f3054b) {
                    return null;
                }
            } else if (obj instanceof C1375b) {
                return null;
            } else {
                int i4 = (i + 1) & 1073741823;
                if (f3051g.compareAndSet(this, j, f3049e.m1498b(j, i4))) {
                    this.f3056d.set(this.f3055c & i, null);
                    return obj;
                } else if (this.f3054b) {
                    C1373n<E> c1373n = this;
                    do {
                        c1373n = c1373n.m1500k(i, i4);
                    } while (c1373n != null);
                    return obj;
                }
            }
        }
    }

    /* renamed from: k */
    public final C1373n<E> m1500k(int i, int i2) {
        long j;
        int i3;
        do {
            j = this._state;
            i3 = (int) ((1073741823 & j) >> 0);
            if ((1152921504606846976L & j) != 0) {
                return m1502i();
            }
        } while (!f3051g.compareAndSet(this, j, f3049e.m1498b(j, i2)));
        this.f3056d.set(i3 & this.f3055c, null);
        return null;
    }
}