package defpackage;

import androidx.media3.container.NalUnitUtil;
import java.io.IOException;
import java.io.OutputStream;
import kotlin.Metadata;
import kotlin.coroutines.jvm.internal.Boxing;
import kotlin.coroutines.jvm.internal.ContinuationImpl;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\u0002\n\u0000¨\u0006\u0000"}, d2 = {"ktor-io"}, k = 2, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@vx8
public final class jwa {
    /* JADX WARN: Removed duplicated region for block: B:19:0x004e  */
    /* JADX WARN: Removed duplicated region for block: B:29:0x0098  */
    /* JADX WARN: Removed duplicated region for block: B:44:0x00de  */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0017  */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:20:0x0056 -> B:26:0x0075). Please report as a decompilation issue!!! */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:24:0x0067 -> B:25:0x006e). Please report as a decompilation issue!!! */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final Object a(bn0 bn0Var, OutputStream outputStream, long j, ContinuationImpl continuationImpl) throws IOException {
        iwa iwaVar;
        bn0 bn0Var2;
        OutputStream outputStream2;
        iwa iwaVar2;
        long j2;
        if (continuationImpl instanceof iwa) {
            iwaVar = (iwa) continuationImpl;
            int i = iwaVar.e;
            if ((i & Integer.MIN_VALUE) != 0) {
                iwaVar.e = i - Integer.MIN_VALUE;
            } else {
                iwaVar = new iwa(continuationImpl);
            }
        }
        Object obj = iwaVar.d;
        ck1 ck1Var = ck1.a;
        int i2 = iwaVar.e;
        long j3 = 0;
        if (i2 == 0) {
            s38.b(obj);
            if (j < 0) {
                throw new IllegalArgumentException(n41.j(j, "Limit shouldn't be negative: ").toString());
            }
            bn0Var2 = bn0Var;
            outputStream2 = outputStream;
            iwaVar2 = iwaVar;
            j2 = 0;
            if (bn0Var2.i()) {
            }
        } else {
            if (i2 != 1) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            long j4 = iwaVar.c;
            OutputStream outputStream3 = iwaVar.b;
            bn0 bn0Var3 = iwaVar.a;
            s38.b(obj);
            OutputStream outputStream4 = outputStream3;
            iwaVar2 = iwaVar;
            j2 = j4;
            outputStream2 = outputStream4;
            bn0Var2 = bn0Var3;
            j2 += bn0Var2.g().getC().c;
            mg0 c = bn0Var2.g().getC();
            long j5 = c.c;
            cr4.g(outputStream2, "out");
            lxa.b(c.c, j5);
            while (j5 > j3) {
                if (c.j()) {
                    throw new IllegalArgumentException("Buffer is empty");
                }
                le8 le8Var = c.a;
                cr4.d(le8Var);
                byte[] bArr = le8Var.a;
                int i3 = le8Var.b;
                int iMin = (int) Math.min(j5, le8Var.c - i3);
                outputStream2.write(bArr, i3, iMin);
                long j6 = iMin;
                j5 -= j6;
                if (iMin != 0) {
                    if (iMin < 0) {
                        throw new IllegalStateException("Returned negative read bytes count");
                    }
                    if (iMin > le8Var.b()) {
                        throw new IllegalStateException("Returned too many bytes");
                    }
                    c.skip(j6);
                }
                j3 = 0;
            }
            if (bn0Var2.i()) {
                return Boxing.boxLong(j2);
            }
            if (bn0Var2.g().j()) {
                iwaVar2.a = bn0Var2;
                iwaVar2.b = outputStream2;
                iwaVar2.c = j2;
                iwaVar2.e = 1;
                if (bn0Var2.h(1, iwaVar2) == ck1Var) {
                    return ck1Var;
                }
                bn0Var3 = bn0Var2;
                iwa iwaVar3 = iwaVar2;
                outputStream3 = outputStream2;
                j4 = j2;
                iwaVar = iwaVar3;
                OutputStream outputStream42 = outputStream3;
                iwaVar2 = iwaVar;
                j2 = j4;
                outputStream2 = outputStream42;
                bn0Var2 = bn0Var3;
            }
            j2 += bn0Var2.g().getC().c;
            mg0 c2 = bn0Var2.g().getC();
            long j52 = c2.c;
            cr4.g(outputStream2, "out");
            lxa.b(c2.c, j52);
            while (j52 > j3) {
            }
            if (bn0Var2.i()) {
            }
        }
    }
}
