// File: /workspace/github_cpp_makefile_star2_O2/sisong[P]ApkDiffPatch/lzma/C/MtDec.c
// Function: MtDec_Code
SRes MtDec_Code(CMtDec *p)
{
  unsigned i;

  p->inProcessed = 0;

  p->blockIndex = 1; // it must be larger than not_defined index (0)
  p->isAllocError = False;
  p->overflow = False;
  p->threadingErrorSRes = SZ_OK;

  p->needContinue = True;

  p->readWasFinished = False;
  p->needInterrupt = False;
  p->interruptIndex = (UInt64)(Int64)-1;

  p->readProcessed = 0;
  p->readRes = SZ_OK;
  p->codeRes = SZ_OK;
  p->wasInterrupted = False;

  p->crossStart = 0;
  p->crossEnd = 0;

  p->filledThreadStart = 0;
  p->numFilledThreads = 0;

  {
    unsigned numThreads = p->numThreadsMax;
    if (numThreads > MTDEC_THREADS_MAX)
      numThreads = MTDEC_THREADS_MAX;
    p->numStartedThreads_Limit = numThreads;
    p->numStartedThreads = 0;
  }

  if (p->inBufSize != p->allocatedBufsSize)
  {
    for (i = 0; i < MTDEC_THREADS_MAX; i++)
    {
      CMtDecThread *t = &p->threads[i];
      if (t->inBuf)
        MtDecThread_FreeInBufs(t);
    }
    if (p->crossBlock)
    {
      ISzAlloc_Free(p->alloc, p->crossBlock);
      p->crossBlock = NULL;
    }

    p->allocatedBufsSize = p->inBufSize;
  }

  MtProgress_Init(&p->mtProgress, p->progress);

  // RINOK_THREAD(AutoResetEvent_OptCreate_And_Reset(&p->finishedEvent))
  p->exitThread = False;
  p->exitThreadWRes = 0;

  {
    WRes wres;
    SRes sres;
    CMtDecThread *nextThread = &p->threads[p->numStartedThreads++];
    // wres = MtDecThread_CreateAndStart(nextThread);
    wres = MtDecThread_CreateEvents(nextThread);
    if (wres == 0) { wres = Event_Set(&nextThread->canWrite);
    if (wres == 0) { wres = Event_Set(&nextThread->canRead);
    if (wres == 0) { THREAD_FUNC_RET_TYPE res = MtDec_ThreadFunc(nextThread);
    wres = (WRes)(MY_uintptr_t)res;
    if (wres != 0)
    {
      p->needContinue = False;
      MtDec_CloseThreads(p);
    }}}}

    // wres = 17; // for test
    // wres = Event_Wait(&p->finishedEvent);

    sres = MY_SRes_HRESULT_FROM_WRes(wres);

    if (sres != 0)
      p->threadingErrorSRes = sres;

    if (
        // wres == 0
        // wres != 0
        // || p->mtc.codeRes == SZ_ERROR_MEM
        p->isAllocError
        || p->threadingErrorSRes != SZ_OK
        || p->overflow)
    {
      // p->needContinue = True;
    }
    else
      p->needContinue = False;
    
    if (p->needContinue)
      return SZ_OK;

    // if (sres != SZ_OK)
    return sres;
    // return SZ_ERROR_FAIL;
  }
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movq %rdi, %rbx
xorps %xmm1, %xmm1
movups %xmm1, 0xa0(%rdi)
movq $0x1, 0x48(%rdi)
movaps 0x20c32(%rip), %xmm0    # 0x54ae0
movups %xmm0, 0x50(%rdi)
andl $0x0, 0x98(%rdi)
andl $0x0, 0xb0(%rdi)
orq $-0x1, 0xb8(%rdi)
andq $0x0, 0x60(%rdi)
andl $0x0, 0x68(%rdi)
movups %xmm1, 0x80(%rdi)
andq $0x0, 0x90(%rdi)
movl 0x8(%rdi), %eax
cmpl $0x20, %eax
pushq $0x20
popq %rcx
cmovbl %eax, %ecx
movl %ecx, 0x6c(%rdi)
andl $0x0, 0x70(%rdi)
movq (%rdi), %rcx
xorl %eax, %eax
cmpq 0x38(%rdi), %rcx
je 0x33f46
leaq 0x108(%rbx), %r14
pushq $0x20
popq %r15
subq $0x1, %r15
jb 0x33f27
cmpq $0x0, 0x10(%r14)
je 0x33f1e
movq %r14, %rdi
callq 0x33b99
addq $0x110, %r14            # imm = 0x110
jmp 0x33f09
movq 0x78(%rbx), %rsi
testq %rsi, %rsi
je 0x33f3c
movq 0x20(%rbx), %rdi
callq *0x8(%rdi)
andq $0x0, 0x78(%rbx)
movq (%rbx), %rax
movq %rax, 0x38(%rbx)
movl 0x70(%rbx), %eax
movq 0x18(%rbx), %rcx
movq %rcx, 0xc0(%rbx)
andl $0x0, 0xc8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0xd0(%rbx)
andq $0x0, 0x40(%rbx)
leal 0x1(%rax), %ecx
movl %ecx, 0x70(%rbx)
movl %eax, %eax
imulq $0x110, %rax, %rax      # imm = 0x110
leaq (%rbx,%rax), %r15
addq $0x108, %r15            # imm = 0x108
movq %r15, %rdi
callq 0x3401f
testl %eax, %eax
jne 0x33faa
leaq 0xa0(%r15), %rdi
callq 0x34f8b
testl %eax, %eax
jne 0x33faa
leaq 0x38(%r15), %rdi
callq 0x34f8b
testl %eax, %eax
je 0x33fe6
movl %eax, %r14d
movzwl %r14w, %ecx
orl $0x88000000, %ecx       # imm = 0x88000000
testl %r14d, %r14d
cmovlel %r14d, %ecx
movl %ecx, 0x58(%rbx)
cmpl $0x0, 0x50(%rbx)
jne 0x33fd7
cmpl $0x0, 0x58(%rbx)
jne 0x33fd7
cmpl $0x0, 0x54(%rbx)
jne 0x33fd7
andl $0x0, 0x5c(%rbx)
xorl %eax, %eax
cmpl $0x0, 0x5c(%rbx)
cmovel %ecx, %eax
popq %rbx
popq %r14
popq %r15
retq
movq %r15, %rdi
callq 0x34042
movq %rax, %r14
testl %r14d, %r14d
je 0x3401b
andl $0x0, 0x5c(%rbx)
movl $0x108, %r15d           # imm = 0x108
cmpq $0x2308, %r15           # imm = 0x2308
je 0x33fad
leaq (%rbx,%r15), %rdi
callq 0x34c73
addq $0x110, %r15            # imm = 0x110
jmp 0x34000
xorl %ecx, %ecx
jmp 0x33fc1