// File: sisong[P]ApkDiffPatch/lzma/C/Lzma2Enc.c
// Function: Lzma2EncInt_EncodeSubblock
static SRes Lzma2EncInt_EncodeSubblock(CLzma2EncInt *p, Byte *outBuf,
    size_t *packSizeRes, ISeqOutStreamPtr outStream)
{
  size_t packSizeLimit = *packSizeRes;
  size_t packSize = packSizeLimit;
  UInt32 unpackSize = LZMA2_UNPACK_SIZE_MAX;
  unsigned lzHeaderSize = 5 + (p->needInitProp ? 1 : 0);
  BoolInt useCopyBlock;
  SRes res;

  *packSizeRes = 0;
  if (packSize < lzHeaderSize)
    return SZ_ERROR_OUTPUT_EOF;
  packSize -= lzHeaderSize;
  
  LzmaEnc_SaveState(p->enc);
  res = LzmaEnc_CodeOneMemBlock(p->enc, p->needInitState,
      outBuf + lzHeaderSize, &packSize, LZMA2_PACK_SIZE_MAX, &unpackSize);
  
  PRF(printf("\npackSize = %7d unpackSize = %7d  ", packSize, unpackSize));

  if (unpackSize == 0)
    return res;

  if (res == SZ_OK)
    useCopyBlock = (packSize + 2 >= unpackSize || packSize > (1 << 16));
  else
  {
    if (res != SZ_ERROR_OUTPUT_EOF)
      return res;
    res = SZ_OK;
    useCopyBlock = True;
  }

  if (useCopyBlock)
  {
    size_t destPos = 0;
    PRF(printf("################# COPY           "));

    while (unpackSize > 0)
    {
      const UInt32 u = (unpackSize < LZMA2_COPY_CHUNK_SIZE) ? unpackSize : LZMA2_COPY_CHUNK_SIZE;
      if (packSizeLimit - destPos < u + 3)
        return SZ_ERROR_OUTPUT_EOF;
      outBuf[destPos++] = (Byte)(p->srcPos == 0 ? LZMA2_CONTROL_COPY_RESET_DIC : LZMA2_CONTROL_COPY_NO_RESET);
      outBuf[destPos++] = (Byte)((u - 1) >> 8);
      outBuf[destPos++] = (Byte)(u - 1);
      memcpy(outBuf + destPos, LzmaEnc_GetCurBuf(p->enc) - unpackSize, u);
      unpackSize -= u;
      destPos += u;
      p->srcPos += u;
      
      if (outStream)
      {
        *packSizeRes += destPos;
        if (ISeqOutStream_Write(outStream, outBuf, destPos) != destPos)
          return SZ_ERROR_WRITE;
        destPos = 0;
      }
      else
        *packSizeRes = destPos;
      /* needInitState = True; */
    }
    
    LzmaEnc_RestoreState(p->enc);
    return SZ_OK;
  }

  {
    size_t destPos = 0;
    const UInt32 u = unpackSize - 1;
    const UInt32 pm = (UInt32)(packSize - 1);
    const unsigned mode = (p->srcPos == 0) ? 3 : (p->needInitState ? (p->needInitProp ? 2 : 1) : 0);

    PRF(printf("               "));

    outBuf[destPos++] = (Byte)(LZMA2_CONTROL_LZMA | (mode << 5) | ((u >> 16) & 0x1F));
    outBuf[destPos++] = (Byte)(u >> 8);
    outBuf[destPos++] = (Byte)u;
    outBuf[destPos++] = (Byte)(pm >> 8);
    outBuf[destPos++] = (Byte)pm;
    
    if (p->needInitProp)
      outBuf[destPos++] = p->propsByte;
    
    p->needInitProp = False;
    p->needInitState = False;
    destPos += packSize;
    p->srcPos += unpackSize;

    if (outStream)
      if (ISeqOutStream_Write(outStream, outBuf, destPos) != destPos)
        return SZ_ERROR_WRITE;
    
    *packSizeRes = destPos;
    return SZ_OK;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movl $0x200000, -0x3c(%rbp)  # imm = 0x200000
movq -0x10(%rbp), %rax
movzbl 0xb(%rax), %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
addl $0x5, %eax
movl %eax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movq $0x0, (%rax)
movq -0x38(%rbp), %rax
movl -0x40(%rbp), %ecx
cmpq %rcx, %rax
jae 0x66c63
movl $0x7, -0x4(%rbp)
jmp 0x67062
movl -0x40(%rbp), %eax
movl %eax, %ecx
movq -0x38(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x673d0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movzbl 0xa(%rax), %esi
movq -0x18(%rbp), %rdx
movl -0x40(%rbp), %eax
addq %rax, %rdx
leaq -0x38(%rbp), %rcx
movl $0x10000, %r8d          # imm = 0x10000
leaq -0x3c(%rbp), %r9
callq 0x67d80
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x66cbf
movl -0x48(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x67062
cmpl $0x0, -0x48(%rbp)
jne 0x66cf7
movq -0x38(%rbp), %rcx
addq $0x2, %rcx
movl -0x3c(%rbp), %eax
movl %eax, %edx
movb $0x1, %al
cmpq %rdx, %rcx
movb %al, -0x6d(%rbp)
jae 0x66cea
cmpq $0x10000, -0x38(%rbp)   # imm = 0x10000
seta %al
movb %al, -0x6d(%rbp)
movb -0x6d(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x44(%rbp)
jmp 0x66d16
cmpl $0x7, -0x48(%rbp)
je 0x66d08
movl -0x48(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x67062
movl $0x0, -0x48(%rbp)
movl $0x1, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x66eaf
movq $0x0, -0x50(%rbp)
cmpl $0x0, -0x3c(%rbp)
jbe 0x66e97
cmpl $0x10000, -0x3c(%rbp)   # imm = 0x10000
jae 0x66d43
movl -0x3c(%rbp), %eax
movl %eax, -0x74(%rbp)
jmp 0x66d4d
movl $0x10000, %eax          # imm = 0x10000
movl %eax, -0x74(%rbp)
jmp 0x66d4d
movl -0x74(%rbp), %eax
movl %eax, -0x54(%rbp)
movq -0x30(%rbp), %rax
subq -0x50(%rbp), %rax
movl -0x54(%rbp), %ecx
addl $0x3, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x66d74
movl $0x7, -0x4(%rbp)
jmp 0x67062
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
movl $0x2, %eax
movl $0x1, %ecx
cmpq $0x0, %rdx
cmovel %ecx, %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movq -0x50(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x50(%rbp)
movb %dl, (%rax,%rcx)
movl -0x54(%rbp), %eax
subl $0x1, %eax
shrl $0x8, %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movq -0x50(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x50(%rbp)
movb %dl, (%rax,%rcx)
movl -0x54(%rbp), %eax
subl $0x1, %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movq -0x50(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x50(%rbp)
movb %dl, (%rax,%rcx)
movq -0x18(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x67d40
movq -0x80(%rbp), %rdi
movq %rax, %rsi
movl -0x3c(%rbp), %eax
movl %eax, %ecx
xorl %eax, %eax
subq %rcx, %rax
addq %rax, %rsi
movl -0x54(%rbp), %eax
movl %eax, %edx
callq 0x4320
movl -0x54(%rbp), %ecx
movl -0x3c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x3c(%rbp)
movl -0x54(%rbp), %eax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0x54(%rbp), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
addq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
cmpq $0x0, -0x28(%rbp)
je 0x66e87
movq -0x50(%rbp), %rcx
movq -0x20(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq *%rax
cmpq -0x50(%rbp), %rax
je 0x66e7d
movl $0x9, -0x4(%rbp)
jmp 0x67062
movq $0x0, -0x50(%rbp)
jmp 0x66e92
movq -0x50(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
jmp 0x66d28
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x675e0
movl $0x0, -0x4(%rbp)
jmp 0x67062
movq $0x0, -0x60(%rbp)
movl -0x3c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x64(%rbp)
movq -0x38(%rbp), %rax
subq $0x1, %rax
movl %eax, -0x68(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x66ee3
movl $0x3, %eax
movl %eax, -0x84(%rbp)
jmp 0x66f26
movq -0x10(%rbp), %rax
movzbl 0xa(%rax), %eax
cmpl $0x0, %eax
je 0x66f10
movq -0x10(%rbp), %rax
movzbl 0xb(%rax), %edx
movl $0x1, %eax
movl $0x2, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x88(%rbp)
jmp 0x66f1a
xorl %eax, %eax
movl %eax, -0x88(%rbp)
jmp 0x66f1a
movl -0x88(%rbp), %eax
movl %eax, -0x84(%rbp)
movl -0x84(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
shll $0x5, %eax
orl $0x80, %eax
movl -0x64(%rbp), %ecx
shrl $0x10, %ecx
andl $0x1f, %ecx
orl %ecx, %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movq -0x60(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x60(%rbp)
movb %dl, (%rax,%rcx)
movl -0x64(%rbp), %eax
shrl $0x8, %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movq -0x60(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x60(%rbp)
movb %dl, (%rax,%rcx)
movl -0x64(%rbp), %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movq -0x60(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x60(%rbp)
movb %dl, (%rax,%rcx)
movl -0x68(%rbp), %eax
shrl $0x8, %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movq -0x60(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x60(%rbp)
movb %dl, (%rax,%rcx)
movl -0x68(%rbp), %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movq -0x60(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x60(%rbp)
movb %dl, (%rax,%rcx)
movq -0x10(%rbp), %rax
cmpb $0x0, 0xb(%rax)
je 0x66ff6
movq -0x10(%rbp), %rax
movb 0x9(%rax), %dl
movq -0x18(%rbp), %rax
movq -0x60(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x60(%rbp)
movb %dl, (%rax,%rcx)
movq -0x10(%rbp), %rax
movb $0x0, 0xb(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0xa(%rax)
movq -0x38(%rbp), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
addq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
cmpq $0x0, -0x28(%rbp)
je 0x67050
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq *%rax
cmpq -0x60(%rbp), %rax
je 0x6704e
movl $0x9, -0x4(%rbp)
jmp 0x67062
jmp 0x67050
movq -0x60(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nop