// File: /workspace/github_cpp_makefile_star2_O2/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 $0xa8, %rsp
movq %r8, %r14
movq %rcx, %rbp
movq %rdx, 0x40(%rsp)
movq %rsi, %r13
movq %rdi, 0x8(%rsp)
testq %rcx, %rcx
je 0x2a8f3
movq (%r14), %rax
movq %rax, 0x38(%rsp)
andq $0x0, (%r14)
jmp 0x2a8fc
movq $0x0, 0x38(%rsp)
cmpq $0x0, (%r13)
pushq $0x2
popq %r15
jne 0x2a92d
movb $0x0, 0x8(%r13)
movq 0x8(%rsp), %rax
movq 0x78(%rax), %rdi
movq %r9, %rbx
callq 0x2b457
movq %rbx, %r9
movq %rax, (%r13)
testq %rax, %rax
je 0x2aea6
movq %r9, 0x88(%rsp)
testq %r9, %r9
je 0x2a949
leaq 0x582(%rip), %rax       # 0x2aec3
movq %rax, 0x80(%rsp)
testq %rbp, %rbp
jne 0x2a97f
movq 0x8(%rsp), %rax
cmpq $0x0, 0x70(%rax)
jne 0x2a97f
movq 0x8(%rsp), %r12
movq 0x78(%r12), %rdi
movl $0x10010, %esi          # imm = 0x10010
movq %r9, %rbx
callq *(%rdi)
movq %rbx, %r9
movq %rax, 0x70(%r12)
testq %rax, %rax
je 0x2aea6
cmpb $0x0, 0x8(%r13)
je 0x2ae0f
xorl %r12d, %r12d
xorl %esi, %esi
movq 0x8(%rsp), %rbx
movq %r14, 0x30(%rsp)
movq %rbp, 0x28(%rsp)
movq %r9, 0x68(%rsp)
movq %r13, 0x18(%rsp)
andq $0x0, 0x10(%r13)
movw $0x101, 0xa(%r13)       # imm = 0x101
andq $0x0, 0x98(%rsp)
andl $0x0, 0xa0(%rsp)
movq 0x50(%rbx), %rax
movq %rax, 0x90(%rsp)
testq %r9, %r9
movq %rsi, 0x50(%rsp)
je 0x2aa4b
movq 0x68(%rbx), %rcx
movq %rcx, %rdx
subq %rsi, %rdx
setb %sil
testb $0x1, %sil
pushq $-0x1
popq %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 (%r13), %rdi
callq 0x2b44f
movq (%r13), %rdi
movq 0x78(%rbx), %rcx
movq 0x80(%rbx), %r8
leaq 0x80(%rsp), %rsi
movl $0x200000, %edx         # imm = 0x200000
callq 0x2b5d6
testl %eax, %eax
jne 0x2ae46
movq $0x0, 0x48(%rsp)
jmp 0x2aa99
movq 0xe8(%rsp), %rcx
subq %rsi, %rcx
cmpq %rax, %rcx
movq %rax, %rdx
cmovbq %rcx, %rdx
cmpq $-0x1, %rax
cmoveq %rcx, %rdx
movq (%r13), %rdi
movq 0xe0(%rsp), %rax
addq %rax, %rsi
movq 0x78(%rbx), %r8
movq 0x80(%rbx), %r9
movq %rdx, 0x48(%rsp)
movl $0x200000, %ecx         # imm = 0x200000
callq 0x2b815
testl %eax, %eax
jne 0x2ae46
movq %r12, %r15
movq 0x38(%rsp), %rcx
subq %r15, %rcx
testq %rbp, %rbp
movl $0x10010, %eax          # imm = 0x10010
cmoveq %rax, %rcx
movq %rcx, 0x58(%rsp)
movq %r15, 0x10(%rsp)
je 0x2aac5
leaq (%r15,%rbp), %rbx
xorl %r12d, %r12d
jmp 0x2aad3
movq 0x8(%rsp), %rax
movq 0x70(%rax), %rbx
movq 0x40(%rsp), %r12
movl $0x200000, (%rsp)       # imm = 0x200000
cmpb $0x1, 0xb(%r13)
pushq $0x6
popq %r15
sbbq $0x0, %r15
movq 0x58(%rsp), %rax
subq %r15, %rax
jb 0x2adf3
movq %rax, 0x20(%rsp)
movq (%r13), %rdi
callq 0x2b089
movq (%r13), %rdi
movzbl 0xa(%r13), %esi
addq %rbx, %r15
movq %r15, %rdx
leaq 0x20(%rsp), %rcx
movl $0x10000, %r8d          # imm = 0x10000
movq %rsp, %r9
callq 0x2b86e
movl %eax, %r15d
movl (%rsp), %r13d
testq %r13, %r13
je 0x2ab99
cmpl $0x7, %r15d
je 0x2abea
testl %r15d, %r15d
jne 0x2ab99
movq 0x20(%rsp), %rax
leaq 0x2(%rax), %rcx
cmpq %r13, %rcx
setb %cl
cmpq $0x10001, %rax          # imm = 0x10001
setb %dl
testb %cl, %dl
je 0x2abea
movq 0x18(%rsp), %rdi
cmpq $0x0, 0x10(%rdi)
je 0x2acea
cmpb $0x0, 0xa(%rdi)
movq 0x10(%rsp), %r15
movq %rbx, %r8
je 0x2acf9
xorl %ecx, %ecx
cmpb $0x0, 0xb(%rdi)
setne %cl
shll $0x5, %ecx
addl $0xa0, %ecx
jmp 0x2acfe
testl %r15d, %r15d
jne 0x2ad7c
xorl %ebx, %ebx
movq 0x18(%rsp), %r13
movq 0x10(%rsp), %r15
addq %rbx, %r15
testq %rbp, %rbp
movq 0xf8(%rsp), %rdi
je 0x2abc1
movq %r15, (%r14)
testq %rdi, %rdi
je 0x2abdc
movq 0x10(%r13), %rsi
addq 0x50(%rsp), %rsi
movq %r15, %rdx
callq *(%rdi)
testl %eax, %eax
jne 0x2ae01
testq %rbx, %rbx
jne 0x2aa9c
jmp 0x2adf7
xorl %r15d, %r15d
xorl %ebp, %ebp
movq %rbx, 0x78(%rsp)
movq %r12, 0x70(%rsp)
movq %r15, 0x60(%rsp)
testl %r13d, %r13d
je 0x2acc3
movl $0x10000, %eax          # imm = 0x10000
cmpl %eax, %r13d
cmovael %eax, %r13d
movq 0x58(%rsp), %rax
subq %rbp, %rax
leal 0x3(%r13), %ecx
cmpq %rcx, %rax
jb 0x2ad6a
movq 0x18(%rsp), %r12
cmpq $0x1, 0x10(%r12)
movb $0x2, %al
sbbb $0x0, %al
leaq (%rbx,%rbp), %r15
addq $0x3, %r15
movb %al, -0x3(%r15)
leal -0x1(%r13), %eax
movb %ah, 0x1(%rbx,%rbp)
movb %al, -0x1(%r15)
movq (%r12), %rdi
callq 0x2b85c
movl (%rsp), %r14d
subq %r14, %rax
movl %r13d, %ebx
movq %r15, %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x42c0
subl %r13d, %r14d
movl %r14d, (%rsp)
addq %rbx, 0x10(%r12)
movq 0x70(%rsp), %r12
leaq (%rbx,%rbp), %r15
addq $0x3, %r15
movq 0x78(%rsp), %rbx
movl %r14d, %r13d
movq %r15, %rbp
testq %r12, %r12
je 0x2abf9
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq *(%r12)
cmpq %r15, %rax
jne 0x2ad6e
addq 0x60(%rsp), %r15
movl (%rsp), %r13d
xorl %ebp, %ebp
jmp 0x2abf9
movq 0x18(%rsp), %r13
movq (%r13), %rdi
callq 0x2b1dc
movq 0x30(%rsp), %r14
movq 0x28(%rsp), %rbp
movq 0x10(%rsp), %r15
movq 0x60(%rsp), %rbx
jmp 0x2abae
movq %rbx, %r8
movl $0xe0, %ecx
movq 0x10(%rsp), %r15
jmp 0x2acfe
movl $0x80, %ecx
leal -0x1(%r13), %ebx
leal -0x1(%rax), %edx
movl %ebx, %esi
shrl $0x10, %esi
andl $0x1f, %esi
orl %ecx, %esi
movq %r8, %rcx
movb %sil, (%r8)
movb %bh, 0x1(%rcx)
movb %bl, 0x2(%r8)
movq %r8, %rsi
movb %dh, 0x3(%rsi)
movb %dl, 0x4(%r8)
cmpb $0x0, 0xb(%rdi)
pushq $0x5
popq %rbx
je 0x2ad38
movb 0x9(%rdi), %cl
movb %cl, 0x5(%rsi)
pushq $0x6
popq %rbx
andw $0x0, 0xa(%rdi)
addq %rax, %rbx
addq %r13, 0x10(%rdi)
testq %r12, %r12
movq %rdi, %r13
je 0x2abae
movq %r12, %rdi
movq %rbx, %rdx
callq *(%r12)
cmpq %rbx, %rax
je 0x2abae
pushq $0x9
jmp 0x2ae08
pushq $0x7
jmp 0x2ad70
pushq $0x9
popq %r15
movq 0x30(%rsp), %r14
movq 0x28(%rsp), %rbp
movq 0x18(%rsp), %r13
movq (%r13), %rdi
callq 0x2b846
testl %r15d, %r15d
movq 0x68(%rsp), %r9
jne 0x2aea6
testq %r9, %r9
movq 0x48(%rsp), %rcx
je 0x2adaa
movq 0x98(%rsp), %rcx
movq 0x10(%r13), %rax
cmpq %rcx, %rax
movq 0x10(%rsp), %r12
movq 0x50(%rsp), %rsi
jne 0x2ae4b
addq %rax, %rsi
testq %r9, %r9
je 0x2adde
cmpl $0x0, 0xa0(%rsp)
movq 0x8(%rsp), %rbx
je 0x2a9a8
jmp 0x2ae51
cmpq 0xe8(%rsp), %rsi
movq 0x8(%rsp), %rbx
jne 0x2a9a8
jmp 0x2ae51
pushq $0x7
jmp 0x2ae08
movq %r15, 0x10(%rsp)
xorl %r15d, %r15d
jmp 0x2ad81
movq %r15, 0x10(%rsp)
pushq $0xa
popq %r15
jmp 0x2ad81
movq %r9, %rbx
movq 0x8(%rsp), %rax
leaq 0x8(%rax), %rsi
movq $0x5, 0x20(%rsp)
movq (%r13), %rdi
callq 0x2b32f
testl %eax, %eax
jne 0x2ae46
movq (%r13), %rdi
movq %rsp, %rsi
leaq 0x20(%rsp), %rdx
callq 0x2e0ba
testl %eax, %eax
je 0x2ae77
movl %eax, %r15d
jmp 0x2aea6
pushq $0xb
popq %r15
jmp 0x2aea6
cmpl $0x0, 0xf0(%rsp)
je 0x2aea3
testq %rbp, %rbp
je 0x2ae8b
movq (%r14), %rax
cmpq 0x38(%rsp), %rax
jae 0x2aebb
movb $0x0, (%rbp,%rax)
incq %rax
movq %rax, (%r14)
jmp 0x2aea3
movb (%rsp), %al
movb %al, 0x9(%r13)
movb $0x1, 0x8(%r13)
movq %rbx, %r9
jmp 0x2a98a
leaq 0x20(%rsp), %rsi
movb $0x0, (%rsi)
pushq $0x1
popq %rdx
movq 0x40(%rsp), %rdi
callq *(%rdi)
cmpq $0x1, %rax
jne 0x2aebf
xorl %r15d, %r15d
movl %r15d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x7
jmp 0x2ae4d
pushq $0x9
jmp 0x2ae4d