package l1;

import java.util.concurrent.atomic.AtomicReference;
import rm.n0;
import rm.y;
import tp.m0;
import tp.u1;
import tp.y1;
import y.x0;

/* loaded from: classes.dex */
public abstract class p {

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public static final class a {

        /* renamed from: a, reason: collision with root package name */
        private final u1 f27419a;

        /* renamed from: b, reason: collision with root package name */
        private final Object f27420b;

        public a(u1 u1Var, Object obj) {
            this.f27419a = u1Var;
            this.f27420b = obj;
        }

        public final u1 a() {
            return this.f27419a;
        }

        public final Object b() {
            return this.f27420b;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public static final class b extends kotlin.coroutines.jvm.internal.l implements dn.p {

        /* renamed from: p, reason: collision with root package name */
        int f27421p;

        /* renamed from: q, reason: collision with root package name */
        private /* synthetic */ Object f27422q;

        /* renamed from: r, reason: collision with root package name */
        final /* synthetic */ dn.l f27423r;

        /* renamed from: s, reason: collision with root package name */
        final /* synthetic */ AtomicReference f27424s;

        /* renamed from: t, reason: collision with root package name */
        final /* synthetic */ dn.p f27425t;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        b(dn.l lVar, AtomicReference atomicReference, dn.p pVar, vm.d dVar) {
            super(2, dVar);
            this.f27423r = lVar;
            this.f27424s = atomicReference;
            this.f27425t = pVar;
        }

        @Override // kotlin.coroutines.jvm.internal.a
        public final vm.d create(Object obj, vm.d dVar) {
            b bVar = new b(this.f27423r, this.f27424s, this.f27425t, dVar);
            bVar.f27422q = obj;
            return bVar;
        }

        @Override // kotlin.coroutines.jvm.internal.a
        public final Object invokeSuspend(Object obj) {
            Object f10;
            a aVar;
            u1 a10;
            a aVar2;
            f10 = wm.d.f();
            int i10 = this.f27421p;
            try {
                if (i10 == 0) {
                    y.b(obj);
                    m0 m0Var = (m0) this.f27422q;
                    aVar = new a(y1.k(m0Var.getCoroutineContext()), this.f27423r.invoke(m0Var));
                    a aVar3 = (a) this.f27424s.getAndSet(aVar);
                    if (aVar3 != null && (a10 = aVar3.a()) != null) {
                        this.f27422q = aVar;
                        this.f27421p = 1;
                        if (y1.g(a10, this) == f10) {
                            return f10;
                        }
                    }
                } else {
                    if (i10 != 1) {
                        if (i10 != 2) {
                            throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                        }
                        aVar2 = (a) this.f27422q;
                        try {
                            y.b(obj);
                            x0.a(this.f27424s, aVar2, null);
                            return obj;
                        } catch (Throwable th2) {
                            th = th2;
                            x0.a(this.f27424s, aVar2, null);
                            throw th;
                        }
                    }
                    aVar = (a) this.f27422q;
                    y.b(obj);
                }
                dn.p pVar = this.f27425t;
                Object b10 = aVar.b();
                this.f27422q = aVar;
                this.f27421p = 2;
                obj = pVar.invoke(b10, this);
                if (obj == f10) {
                    return f10;
                }
                aVar2 = aVar;
                x0.a(this.f27424s, aVar2, null);
                return obj;
            } catch (Throwable th3) {
                th = th3;
                aVar2 = aVar;
                x0.a(this.f27424s, aVar2, null);
                throw th;
            }
        }

        @Override // dn.p
        public final Object invoke(m0 m0Var, vm.d dVar) {
            return ((b) create(m0Var, dVar)).invokeSuspend(n0.f34904a);
        }
    }

    public static AtomicReference a() {
        return b(new AtomicReference(null));
    }

    private static AtomicReference b(AtomicReference atomicReference) {
        return atomicReference;
    }

    public static final Object c(AtomicReference atomicReference) {
        a aVar = (a) atomicReference.get();
        if (aVar != null) {
            return aVar.b();
        }
        return null;
    }

    public static final Object d(AtomicReference atomicReference, dn.l lVar, dn.p pVar, vm.d dVar) {
        return tp.n0.f(new b(lVar, atomicReference, pVar, null), dVar);
    }
}
