// File: 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 %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, 0xa0(%rax)
movq -0x10(%rbp), %rax
movq $0x1, 0x48(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x50(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x54(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x58(%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0x5c(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x98(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0xb0(%rax)
movq -0x10(%rbp), %rax
movq $-0x1, 0xb8(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x90(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x60(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x64(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x68(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x80(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x88(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0xa8(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0xac(%rax)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x18(%rbp)
cmpl $0x20, -0x18(%rbp)
jbe 0x79cdf
movl $0x20, -0x18(%rbp)
movl -0x18(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x6c(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x70(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x38(%rcx), %rax
je 0x79d96
movl $0x0, -0x14(%rbp)
cmpl $0x20, -0x14(%rbp)
jae 0x79d52
movq -0x10(%rbp), %rax
addq $0x108, %rax            # imm = 0x108
movl -0x14(%rbp), %ecx
imulq $0x110, %rcx, %rcx      # imm = 0x110
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x79d45
movq -0x20(%rbp), %rdi
callq 0x79560
jmp 0x79d47
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x79d10
movq -0x10(%rbp), %rax
cmpq $0x0, 0x78(%rax)
je 0x79d87
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rdi
movq -0x10(%rbp), %rcx
movq 0x78(%rcx), %rsi
callq *%rax
movq -0x10(%rbp), %rax
movq $0x0, 0x78(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rdi
addq $0xc0, %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x79370
movq -0x10(%rbp), %rax
movl $0x0, 0x40(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x44(%rax)
movq -0x10(%rbp), %rax
addq $0x108, %rax            # imm = 0x108
movq -0x10(%rbp), %rdx
movl 0x70(%rdx), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0x70(%rdx)
movl %ecx, %ecx
imulq $0x110, %rcx, %rcx      # imm = 0x110
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x79ef0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x79e62
movq -0x30(%rbp), %rdi
addq $0xa0, %rdi
callq 0x7b600
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x79e60
movq -0x30(%rbp), %rdi
addq $0x38, %rdi
callq 0x7b600
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x79e5e
movq -0x30(%rbp), %rdi
callq 0x79f50
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x79e5c
movq -0x10(%rbp), %rax
movl $0x0, 0x5c(%rax)
movq -0x10(%rbp), %rdi
callq 0x79f70
jmp 0x79e5e
jmp 0x79e60
jmp 0x79e62
cmpl $0x0, -0x24(%rbp)
jg 0x79e70
movl -0x24(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x79e85
movl -0x24(%rbp), %eax
andl $0xffff, %eax           # imm = 0xFFFF
orl $0x8000000, %eax        # imm = 0x8000000
orl $0x80000000, %eax       # imm = 0x80000000
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
je 0x79e9b
movl -0x28(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x58(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x50(%rax)
jne 0x79eb9
movq -0x10(%rbp), %rax
cmpl $0x0, 0x58(%rax)
jne 0x79eb9
movq -0x10(%rbp), %rax
cmpl $0x0, 0x54(%rax)
je 0x79ebb
jmp 0x79ec6
movq -0x10(%rbp), %rax
movl $0x0, 0x5c(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x5c(%rax)
je 0x79ed9
movl $0x0, -0x4(%rbp)
jmp 0x79edf
movl -0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)