// File: /workspace/github_cpp_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
movq %rdi, %rbx
xorps %xmm1, %xmm1
movups %xmm1, 0xa0(%rdi)
movq $0x1, 0x48(%rdi)
movaps 0x235c3(%rip), %xmm0    # 0x5fa40
movups %xmm0, 0x50(%rdi)
xorl %eax, %eax
movl %eax, 0x98(%rdi)
movl %eax, 0xb0(%rdi)
movq $-0x1, 0xb8(%rdi)
movq %rax, 0x60(%rdi)
movl %eax, 0x68(%rdi)
movups %xmm1, 0x80(%rdi)
movq %rax, 0x90(%rdi)
movl 0x8(%rdi), %ecx
cmpl $0x20, %ecx
movl $0x20, %edx
cmovbl %ecx, %edx
movl %edx, 0x6c(%rdi)
movl %eax, 0x70(%rdi)
movq (%rdi), %rcx
cmpq 0x38(%rdi), %rcx
je 0x3c537
leaq 0x108(%rbx), %r14
xorl %r15d, %r15d
imulq $0x110, %r15, %r12      # imm = 0x110
movq 0x10(%r14,%r12), %rsi
testq %rsi, %rsi
je 0x3c509
addq %r14, %r12
movq $0x0, 0x10(%r12)
movq (%rsi), %r13
movq (%r12), %rax
movq 0x20(%rax), %rdi
callq *0x8(%rdi)
movq %r13, %rsi
testq %r13, %r13
jne 0x3c4f3
incq %r15
cmpq $0x20, %r15
jne 0x3c4d6
movq 0x78(%rbx), %rsi
testq %rsi, %rsi
je 0x3c52a
movq 0x20(%rbx), %rdi
callq *0x8(%rdi)
movq $0x0, 0x78(%rbx)
movq (%rbx), %rax
movq %rax, 0x38(%rbx)
movl 0x70(%rbx), %eax
xorps %xmm1, %xmm1
movq 0x18(%rbx), %rcx
movq %rcx, 0xc0(%rbx)
movl $0x0, 0xc8(%rbx)
movups %xmm1, 0xd0(%rbx)
movq $0x0, 0x40(%rbx)
leal 0x1(%rax), %ecx
movl %ecx, 0x70(%rbx)
movl %eax, %eax
imulq $0x110, %rax, %r15      # imm = 0x110
leaq (%rbx,%r15), %r12
addq $0x1a8, %r12            # imm = 0x1A8
movq %r12, %rdi
callq 0x3da6a
testl %eax, %eax
jne 0x3c5b3
addq %rbx, %r15
addq $0x108, %r15            # imm = 0x108
leaq 0x38(%r15), %r13
movq %r13, %rdi
callq 0x3da6a
testl %eax, %eax
jne 0x3c5b3
movq %r12, %rdi
callq 0x3d7a5
testl %eax, %eax
jne 0x3c5b3
movq %r13, %rdi
callq 0x3d7a5
testl %eax, %eax
je 0x3c5f6
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 0x3c5e3
cmpl $0x0, 0x58(%rbx)
jne 0x3c5e3
cmpl $0x0, 0x54(%rbx)
jne 0x3c5e3
movl $0x0, 0x5c(%rbx)
xorl %eax, %eax
cmpl $0x0, 0x5c(%rbx)
cmovel %ecx, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq %r15, %rdi
callq 0x3c632
movq %rax, %r14
testl %r14d, %r14d
je 0x3c62e
movl $0x0, 0x5c(%rbx)
movl $0x108, %r15d           # imm = 0x108
leaq (%rbx,%r15), %rdi
callq 0x3d45d
addq $0x110, %r15            # imm = 0x110
cmpq $0x2308, %r15           # imm = 0x2308
jne 0x3c613
jmp 0x3c5b6
xorl %ecx, %ecx
jmp 0x3c5ca