// File: /workspace/github_cpp_makefile_star2_O3/sisong[P]ApkDiffPatch/lzma/C/Lzma2Enc.c
// Function: Lzma2Enc_EncodeMt1
static SRes Lzma2Enc_EncodeMt1(
    CLzma2Enc *me,
    CLzma2EncInt *p,
    ISeqOutStreamPtr outStream,
    Byte *outBuf, size_t *outBufSize,
    ISeqInStreamPtr inStream,
    const Byte *inData, size_t inDataSize,
    int finished,
    ICompressProgressPtr progress)
{
  UInt64 unpackTotal = 0;
  UInt64 packTotal = 0;
  size_t outLim = 0;
  CLimitedSeqInStream limitedInStream;

  if (outBuf)
  {
    outLim = *outBufSize;
    *outBufSize = 0;
  }

  if (!p->enc)
  {
    p->propsAreSet = False;
    p->enc = LzmaEnc_Create(me->alloc);
    if (!p->enc)
      return SZ_ERROR_MEM;
  }

  limitedInStream.realStream = inStream;
  if (inStream)
  {
    limitedInStream.vt.Read = LimitedSeqInStream_Read;
  }
  
  if (!outBuf)
  {
    // outStream version works only in one thread. So we use CLzma2Enc::tempBufLzma
    if (!me->tempBufLzma)
    {
      me->tempBufLzma = (Byte *)ISzAlloc_Alloc(me->alloc, LZMA2_CHUNK_SIZE_COMPRESSED_MAX);
      if (!me->tempBufLzma)
        return SZ_ERROR_MEM;
    }
  }

  RINOK(Lzma2EncInt_InitStream(p, &me->props))

  for (;;)
  {
    SRes res = SZ_OK;
    SizeT inSizeCur = 0;

    Lzma2EncInt_InitBlock(p);
    
    LimitedSeqInStream_Init(&limitedInStream);
    limitedInStream.limit = me->props.blockSize;

    if (inStream)
    {
      UInt64 expected = (UInt64)(Int64)-1;
      // inStream version works only in one thread. So we use CLzma2Enc::expectedDataSize
      if (me->expectedDataSize != (UInt64)(Int64)-1
          && me->expectedDataSize >= unpackTotal)
        expected = me->expectedDataSize - unpackTotal;
      if (me->props.blockSize != LZMA2_ENC_PROPS_BLOCK_SIZE_SOLID
          && expected > me->props.blockSize)
        expected = (size_t)me->props.blockSize;

      LzmaEnc_SetDataSize(p->enc, expected);

      RINOK(LzmaEnc_PrepareForLzma2(p->enc,
          &limitedInStream.vt,
          LZMA2_KEEP_WINDOW_SIZE,
          me->alloc,
          me->allocBig))
    }
    else
    {
      inSizeCur = (SizeT)(inDataSize - (size_t)unpackTotal);
      if (me->props.blockSize != LZMA2_ENC_PROPS_BLOCK_SIZE_SOLID
          && inSizeCur > me->props.blockSize)
        inSizeCur = (SizeT)(size_t)me->props.blockSize;
    
      // LzmaEnc_SetDataSize(p->enc, inSizeCur);
      
      RINOK(LzmaEnc_MemPrepare(p->enc,
          inData + (size_t)unpackTotal, inSizeCur,
          LZMA2_KEEP_WINDOW_SIZE,
          me->alloc,
          me->allocBig))
    }

    for (;;)
    {
      size_t packSize = LZMA2_CHUNK_SIZE_COMPRESSED_MAX;
      if (outBuf)
        packSize = outLim - (size_t)packTotal;
      
      res = Lzma2EncInt_EncodeSubblock(p,
          outBuf ? outBuf + (size_t)packTotal : me->tempBufLzma, &packSize,
          outBuf ? NULL : outStream);
      
      if (res != SZ_OK)
        break;

      packTotal += packSize;
      if (outBuf)
        *outBufSize = (size_t)packTotal;
      
      res = Progress(progress, unpackTotal + p->srcPos, packTotal);
      if (res != SZ_OK)
        break;

      /*
      if (LzmaEnc_GetNumAvailableBytes(p->enc) == 0)
        break;
      */

      if (packSize == 0)
        break;
    }
    
    LzmaEnc_Finish(p->enc);
    
    unpackTotal += p->srcPos;
    
    RINOK(res)

    if (p->srcPos != (inStream ? limitedInStream.processed : inSizeCur))
      return SZ_ERROR_FAIL;
    
    if (inStream ? limitedInStream.finished : (unpackTotal == inDataSize))
    {
      if (finished)
      {
        if (outBuf)
        {
          const size_t destPos = *outBufSize;
          if (destPos >= outLim)
            return SZ_ERROR_OUTPUT_EOF;
          outBuf[destPos] = LZMA2_CONTROL_EOF; // 0
          *outBufSize = destPos + 1;
        }
        else
        {
          const Byte b = LZMA2_CONTROL_EOF; // 0;
          if (ISeqOutStream_Write(outStream, &b, 1) != 1)
            return SZ_ERROR_WRITE;
        }
      }
      return SZ_OK;
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r8, %r14
movq %rcx, %r12
movq %rdx, 0x60(%rsp)
movq %rsi, %rbx
movq %rdi, %rbp
testq %rcx, %rcx
je 0x32641
movq (%r14), %r13
movq $0x0, (%r14)
jmp 0x32644
xorl %r13d, %r13d
cmpq $0x0, (%rbx)
movq %rbx, 0x18(%rsp)
jne 0x32673
movb $0x0, 0x8(%rbx)
movq 0x78(%rbp), %rdi
movq %r9, %rbx
callq 0x3320b
movq %rbx, %r9
movq 0x18(%rsp), %rbx
movq %rax, (%rbx)
testq %rax, %rax
je 0x32bdc
movq %r9, 0x98(%rsp)
testq %r9, %r9
je 0x3268f
leaq 0x5d9(%rip), %rax       # 0x32c60
movq %rax, 0x90(%rsp)
testq %r12, %r12
jne 0x326be
cmpq $0x0, 0x70(%rbp)
jne 0x326be
movq 0x78(%rbp), %rdi
movl $0x10010, %esi          # imm = 0x10010
movq %r9, %rbx
callq *(%rdi)
movq %rbx, %r9
movq 0x18(%rsp), %rbx
movq %rax, 0x70(%rbp)
testq %rax, %rax
je 0x32bdc
cmpb $0x0, 0x8(%rbx)
je 0x32ba3
xorl %r15d, %r15d
xorl %esi, %esi
movq %r14, 0x20(%rsp)
movq %r12, 0x50(%rsp)
movq %r13, 0x48(%rsp)
movq %r9, 0x80(%rsp)
movq %rbp, 0x28(%rsp)
xorl %eax, %eax
movq %rax, 0x10(%rbx)
movw $0x101, 0xa(%rbx)       # imm = 0x101
movq %rax, 0xa8(%rsp)
movl $0x0, 0xb0(%rsp)
movq 0x50(%rbp), %rax
movq %rax, 0xa0(%rsp)
testq %r9, %r9
movq %rsi, 0x70(%rsp)
je 0x32793
movq 0x68(%rbp), %rcx
movq %rcx, %rdx
subq %rsi, %rdx
setb %sil
testb $0x1, %sil
movq $-0x1, %rsi
cmovneq %rsi, %rdx
cmpq $-0x1, %rcx
cmoveq %rcx, %rdx
cmpq %rax, %rdx
movq %rax, %rsi
cmovbq %rdx, %rsi
cmpq $-0x1, %rax
cmoveq %rdx, %rsi
movq (%rbx), %rdi
callq 0x33203
movq (%rbx), %rdi
movq 0x78(%rbp), %rcx
movq 0x80(%rbp), %r8
leaq 0x90(%rsp), %rsi
movl $0x200000, %edx         # imm = 0x200000
callq 0x333b9
testl %eax, %eax
jne 0x32bd8
movq $0x0, 0x68(%rsp)
movq %r15, %rdx
jmp 0x327e3
movq 0xf8(%rsp), %rcx
subq %rsi, %rcx
cmpq %rax, %rcx
movq %rax, %rdx
cmovbq %rcx, %rdx
cmpq $-0x1, %rax
cmoveq %rcx, %rdx
movq (%rbx), %rdi
movq 0xf0(%rsp), %rax
addq %rax, %rsi
movq 0x78(%rbp), %r8
movq 0x80(%rbp), %r9
movq %rdx, 0x68(%rsp)
movl $0x200000, %ecx         # imm = 0x200000
callq 0x3361f
testl %eax, %eax
movq %r15, %rdx
jne 0x32bd8
movq %r13, %rcx
subq %rdx, %rcx
testq %r12, %r12
movl $0x10010, %eax          # imm = 0x10010
cmoveq %rax, %rcx
movq %rcx, 0x78(%rsp)
je 0x3280b
leaq (%r12,%rdx), %rcx
movq $0x0, 0x58(%rsp)
jmp 0x32819
movq 0x70(%rbp), %rcx
movq 0x60(%rsp), %rax
movq %rax, 0x58(%rsp)
movq %rdx, 0x30(%rsp)
movl $0x200000, 0x10(%rsp)   # imm = 0x200000
cmpb $0x1, 0xb(%rbx)
movq %rbx, %r15
movl $0x6, %ebx
sbbq $0x0, %rbx
movl $0x7, 0xc(%rsp)
movq 0x78(%rsp), %rax
subq %rbx, %rax
jb 0x32b71
movq %rax, 0x40(%rsp)
movq (%r15), %rdi
movq %rcx, %r14
callq 0x32e33
movq (%r15), %rdi
movzbl 0xa(%r15), %esi
movq %r14, 0x38(%rsp)
addq %r14, %rbx
movq %rbx, %rdx
leaq 0x40(%rsp), %rcx
movl $0x10000, %r8d          # imm = 0x10000
leaq 0x10(%rsp), %r9
callq 0x3367b
movl 0x10(%rsp), %ebp
testq %rbp, %rbp
je 0x328e8
cmpl $0x7, %eax
je 0x3290c
testl %eax, %eax
jne 0x328e8
movq 0x40(%rsp), %rax
leaq 0x2(%rax), %rcx
cmpq %rbp, %rcx
setb %cl
cmpq $0x10001, %rax          # imm = 0x10001
setb %dl
testb %cl, %dl
je 0x3290c
movq 0x18(%rsp), %rdi
cmpq $0x0, 0x10(%rdi)
je 0x329f6
cmpb $0x0, 0xa(%rdi)
movq 0x20(%rsp), %r14
je 0x32a02
xorl %ecx, %ecx
cmpb $0x0, 0xb(%rdi)
setne %cl
shll $0x5, %ecx
addl $0xa0, %ecx
jmp 0x32a07
testl %eax, %eax
movq 0x30(%rsp), %rdx
jne 0x32b7b
xorl %r15d, %r15d
movq 0x20(%rsp), %r14
movq 0x18(%rsp), %rbx
movq 0x28(%rsp), %rbp
jmp 0x32a8b
xorl %r15d, %r15d
xorl %ebx, %ebx
movl $0x10000, %eax          # imm = 0x10000
cmpl %eax, %ebp
cmovael %eax, %ebp
movq 0x78(%rsp), %rax
subq %rbx, %rax
leal 0x3(%rbp), %ecx
cmpq %rcx, %rax
jb 0x32ae8
movq %r15, 0x88(%rsp)
movq 0x18(%rsp), %r15
cmpq $0x1, 0x10(%r15)
movb $0x2, %al
sbbb $0x0, %al
movq 0x38(%rsp), %rcx
leaq (%rcx,%rbx), %r14
addq $0x3, %r14
movb %al, -0x3(%r14)
leal -0x1(%rbp), %eax
movb %ah, 0x1(%rcx,%rbx)
movb %al, -0x1(%r14)
movq (%r15), %rdi
callq 0x33669
movl 0x10(%rsp), %r12d
subq %r12, %rax
movl %ebp, %r13d
movq %r14, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x4290
subl %ebp, %r12d
movl %r12d, 0x10(%rsp)
addq %r13, 0x10(%r15)
addq %r13, %rbx
addq $0x3, %rbx
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x329c6
movq 0x38(%rsp), %rsi
movq %rbx, %rdx
callq *(%rdi)
cmpq %rbx, %rax
jne 0x32ae0
movq 0x88(%rsp), %r15
addq %rbx, %r15
movl 0x10(%rsp), %r12d
xorl %ebx, %ebx
jmp 0x329c9
movq %rbx, %r15
movl %r12d, %ebp
testl %r12d, %r12d
jne 0x32911
movq 0x18(%rsp), %rbx
movq (%rbx), %rdi
callq 0x32f86
movq 0x20(%rsp), %r14
movq 0x50(%rsp), %r12
movq 0x48(%rsp), %r13
jmp 0x32a81
movl $0xe0, %ecx
movq 0x20(%rsp), %r14
jmp 0x32a07
movl $0x80, %ecx
leal -0x1(%rbp), %ebx
leal -0x1(%rax), %edx
movl %ebx, %r8d
shrl $0x10, %r8d
andl $0x1f, %r8d
orl %ecx, %r8d
movq 0x38(%rsp), %rsi
movb %r8b, (%rsi)
movb %bh, 0x1(%rsi)
movb %bl, 0x2(%rsi)
movb %dh, 0x3(%rsi)
movb %dl, 0x4(%rsi)
cmpb $0x0, 0xb(%rdi)
movq %rdi, %rbx
je 0x32a50
movb 0x9(%rbx), %cl
movq 0x38(%rsp), %rdx
movb %cl, 0x5(%rdx)
movq 0x38(%rsp), %rsi
movl $0x6, %r15d
jmp 0x32a56
movl $0x5, %r15d
movw $0x0, 0xa(%rbx)
addq %rax, %r15
addq %rbp, 0x10(%rbx)
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x32a81
movq %r15, %rdx
callq *(%rdi)
cmpq %r15, %rax
movq 0x28(%rsp), %rbp
je 0x32a86
jmp 0x32b96
movq 0x28(%rsp), %rbp
movq 0x30(%rsp), %rdx
addq %r15, %rdx
testq %r12, %r12
movq 0x108(%rsp), %rdi
je 0x32a9e
movq %rdx, (%r14)
testq %rdi, %rdi
je 0x32ac8
movq 0x10(%rbx), %rsi
addq 0x70(%rsp), %rsi
movq 0x108(%rsp), %rax
movq %rdx, 0x30(%rsp)
callq *(%rax)
movq 0x30(%rsp), %rdx
testl %eax, %eax
jne 0x32b89
testq %r15, %r15
jne 0x327e3
movq %rdx, 0x30(%rsp)
movl $0x0, 0xc(%rsp)
jmp 0x32b01
movl $0x9, 0xc(%rsp)
movq 0x20(%rsp), %r14
movq 0x50(%rsp), %r12
movq 0x48(%rsp), %r13
movq 0x18(%rsp), %rbx
movq 0x28(%rsp), %rbp
movq (%rbx), %rdi
callq 0x33653
movl 0xc(%rsp), %ecx
testl %ecx, %ecx
jne 0x32c4c
movq 0x80(%rsp), %r9
testq %r9, %r9
movq 0x68(%rsp), %rcx
je 0x32b2f
movq 0xa8(%rsp), %rcx
movq 0x10(%rbx), %rax
cmpq %rcx, %rax
movq 0x30(%rsp), %r15
movq 0x70(%rsp), %rsi
jne 0x32be3
addq %rax, %rsi
testq %r9, %r9
je 0x32b61
cmpl $0x0, 0xb0(%rsp)
je 0x326e9
jmp 0x32bea
cmpq 0xf8(%rsp), %rsi
jne 0x326e9
jmp 0x32bea
movq 0x20(%rsp), %r14
movq %r15, %rbx
jmp 0x32b01
movl %eax, 0xc(%rsp)
movq 0x20(%rsp), %r14
jmp 0x32af7
movl $0xa, 0xc(%rsp)
jmp 0x32b01
movl $0x9, 0xc(%rsp)
jmp 0x32b01
movq %rbx, %r15
movq %r9, %rbx
leaq 0x8(%rbp), %rsi
movq $0x5, 0x40(%rsp)
movq (%r15), %rdi
callq 0x330d9
testl %eax, %eax
jne 0x32bd8
movq (%r15), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x3630f
testl %eax, %eax
je 0x32c13
movl %eax, %ecx
jmp 0x32c4c
movl $0x2, %ecx
jmp 0x32c4c
movl $0xb, %ecx
jmp 0x32c4c
cmpl $0x0, 0x100(%rsp)
je 0x32c4a
testq %r12, %r12
je 0x32c2b
movq (%r14), %rax
movl $0x7, %ecx
cmpq %r13, %rax
jae 0x32c4c
movb $0x0, (%r12,%rax)
incq %rax
movq %rax, (%r14)
jmp 0x32c4a
movb 0x10(%rsp), %al
movb %al, 0x9(%r15)
movb $0x1, 0x8(%r15)
movq %rbx, %r9
movq %r15, %rbx
jmp 0x326c8
leaq 0x40(%rsp), %rsi
movb $0x0, (%rsi)
movl $0x1, %edx
movq 0x60(%rsp), %rdi
callq *(%rdi)
movl $0x9, %ecx
cmpq $0x1, %rax
jne 0x32c4c
xorl %ecx, %ecx
movl %ecx, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq