// File: twekkel[P]mzpq/libzpaq.cpp
// Function: libzpaq::LZBuffer::LZBuffer(libzpaq::StringBuffer&, int*, unsigned int const*)
LZBuffer::LZBuffer(StringBuffer& inbuf, int args[], const unsigned* sap):
    ht((args[1]&3)==3 ? (inbuf.size()+1)*!sap      // for BWT suffix array
        : args[5]-args[0]<21 ? 1u<<args[5]         // for LZ77 hash table
        : (inbuf.size()*!sap)+(1u<<17<<args[0])),  // for LZ77 SA and ISA
    in(inbuf.data()),
    checkbits(args[5]-args[0]<21 ? 12-args[0] : 17+args[0]),
    level(args[1]&3),
    htsize(ht.size()),
    n(inbuf.size()),
    i(0),
    minMatch(args[2]),
    minMatch2(args[3]),
    maxMatch(BUFSIZE*3),
    maxLiteral(BUFSIZE/4),
    lookahead(args[6]),
    h1(0), h2(0),
    bucket((1<<args[4])-1), 
    shift1(minMatch>0 ? (args[5]-1)/minMatch+1 : 1),
    shift2(minMatch2>0 ? (args[5]-1)/minMatch2+1 : 0),
    minMatchBoth(MAX(minMatch, minMatch2+lookahead)+4),
    rb(args[0]>4 ? args[0]-4 : 0),
    bits(0), nbits(0), rpos(0), wpos(0),
    idx(0), sa(0), isa(0) {
  assert(args[0]>=0);
  assert(n<=(1u<<20<<args[0]));
  assert(args[1]>=1 && args[1]<=7 && args[1]!=4);
  assert(level>=1 && level<=3);
  if ((minMatch<4 && level==1) || (minMatch<1 && level==2))
    error("match length $3 too small");

  // e8e9 transform
  if (args[1]>4 && !sap) e8e9(inbuf.data(), n);

  // build suffix array if not supplied
  if (args[5]-args[0]>=21 || level==3) {  // LZ77-SA or BWT
    if (sap)
      sa=sap;
    else {
      assert(ht.size()>=n);
      assert(ht.size()>0);
      sa=&ht[0];
      if (n>0) divsufsort((const unsigned char*)in, (int*)sa, n);
    }
    if (level<3) {
      assert(ht.size()>=(n*(sap==0))+(1u<<17<<args[0]));
      isa=&ht[n*(sap==0)];
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x40(%rbp)
callq 0x30e90
movq -0x40(%rbp), %rax
leaq 0x1d7a5(%rip), %rcx     # 0x40bc8
addq $0x10, %rcx
movq %rcx, (%rax)
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x3, %eax
cmpl $0x3, %eax
jne 0x23473
movq -0x10(%rbp), %rdi
callq 0x31600
movq %rax, -0x48(%rbp)
jmp 0x23450
movq -0x48(%rbp), %rax
addq $0x1, %rax
cmpq $0x0, -0x20(%rbp)
setne %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
imulq %rcx, %rax
movq %rax, -0x50(%rbp)
jmp 0x234e3
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x18(%rbp), %rcx
subl (%rcx), %eax
cmpl $0x15, %eax
jge 0x2349b
movq -0x18(%rbp), %rax
movl 0x14(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %eax
movq %rax, -0x58(%rbp)
jmp 0x234db
movq -0x10(%rbp), %rdi
callq 0x31600
movq %rax, -0x60(%rbp)
jmp 0x234aa
movq -0x60(%rbp), %rax
cmpq $0x0, -0x20(%rbp)
setne %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
imulq %rcx, %rax
movq -0x18(%rbp), %rcx
movl (%rcx), %ecx
movl $0x20000, %edx          # imm = 0x20000
shll %cl, %edx
movl %edx, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rdi
movq -0x50(%rbp), %rsi
xorl %edx, %edx
callq 0x30690
jmp 0x234f4
movq -0x40(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rdi
callq 0x313e0
movq %rax, -0x68(%rbp)
jmp 0x2350f
movq -0x40(%rbp), %rax
movq -0x70(%rbp), %rcx
movq -0x68(%rbp), %rdx
movq %rdx, (%rcx)
addq $0x28, %rax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x18(%rbp), %rcx
subl (%rcx), %eax
cmpl $0x15, %eax
jge 0x23548
movq -0x18(%rbp), %rcx
movl $0xc, %eax
subl (%rcx), %eax
movl %eax, -0x7c(%rbp)
jmp 0x23554
movq -0x18(%rbp), %rax
movl (%rax), %eax
addl $0x11, %eax
movl %eax, -0x7c(%rbp)
movq -0x40(%rbp), %rdi
movq -0x78(%rbp), %rax
movl -0x7c(%rbp), %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x3, %eax
movl %eax, 0x2c(%rdi)
movq %rdi, %rax
addq $0x30, %rax
movq %rax, -0x90(%rbp)
addq $0x8, %rdi
callq 0x30c00
movq %rax, -0x88(%rbp)
jmp 0x2358e
movq -0x40(%rbp), %rax
movq -0x90(%rbp), %rcx
movq -0x88(%rbp), %rdx
movl %edx, (%rcx)
addq $0x34, %rax
movq %rax, -0xa0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x31600
movq %rax, -0x98(%rbp)
jmp 0x235bf
movq -0x40(%rbp), %rax
movq -0xa0(%rbp), %rcx
movq -0x98(%rbp), %rdx
movl %edx, (%rcx)
movl $0x0, 0x38(%rax)
movq -0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
movl %ecx, 0x3c(%rax)
movq -0x18(%rbp), %rcx
movl 0xc(%rcx), %ecx
movl %ecx, 0x40(%rax)
movl $0xc000, 0x44(%rax)     # imm = 0xC000
movl $0x1000, 0x48(%rax)     # imm = 0x1000
movq -0x18(%rbp), %rcx
movl 0x18(%rcx), %ecx
movl %ecx, 0x4c(%rax)
movl $0x0, 0x50(%rax)
movl $0x0, 0x54(%rax)
movq -0x18(%rbp), %rcx
movl 0x10(%rcx), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
subl $0x1, %ecx
movl %ecx, 0x58(%rax)
movq %rax, %rcx
addq $0x5c, %rcx
movq %rcx, -0xa8(%rbp)
cmpl $0x0, 0x3c(%rax)
jbe 0x2365c
movq -0x40(%rbp), %rcx
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
subl $0x1, %eax
xorl %edx, %edx
divl 0x3c(%rcx)
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x23669
movl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x23669
movq -0x40(%rbp), %rax
movq -0xa8(%rbp), %rcx
movl -0xac(%rbp), %edx
movl %edx, (%rcx)
movq %rax, %rcx
addq $0x60, %rcx
movq %rcx, -0xb8(%rbp)
cmpl $0x0, 0x40(%rax)
jbe 0x236ae
movq -0x40(%rbp), %rcx
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
subl $0x1, %eax
xorl %edx, %edx
divl 0x40(%rcx)
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x236b8
xorl %eax, %eax
movl %eax, -0xbc(%rbp)
jmp 0x236b8
movq -0x40(%rbp), %rdx
movq -0xb8(%rbp), %rax
movl -0xbc(%rbp), %ecx
movl %ecx, (%rax)
movq %rdx, %rax
addq $0x64, %rax
movq %rax, -0xc8(%rbp)
movl 0x3c(%rdx), %eax
movl 0x40(%rdx), %ecx
addl 0x4c(%rdx), %ecx
cmpl %ecx, %eax
jbe 0x236f5
movq -0x40(%rbp), %rax
movl 0x3c(%rax), %eax
movl %eax, -0xcc(%rbp)
jmp 0x23705
movq -0x40(%rbp), %rcx
movl 0x40(%rcx), %eax
addl 0x4c(%rcx), %eax
movl %eax, -0xcc(%rbp)
movq -0x40(%rbp), %rax
movq -0xc8(%rbp), %rcx
movl -0xcc(%rbp), %edx
addl $0x4, %edx
movl %edx, (%rcx)
addq $0x68, %rax
movq %rax, -0xd8(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x4, (%rax)
jle 0x23740
movq -0x18(%rbp), %rax
movl (%rax), %eax
subl $0x4, %eax
movl %eax, -0xdc(%rbp)
jmp 0x2374a
xorl %eax, %eax
movl %eax, -0xdc(%rbp)
jmp 0x2374a
movq -0x40(%rbp), %rax
movq -0xd8(%rbp), %rcx
movl -0xdc(%rbp), %edx
movl %edx, (%rcx)
movl $0x0, 0x6c(%rax)
movl $0x0, 0x70(%rax)
movl $0x0, 0x74(%rax)
movl $0x0, 0x78(%rax)
movl $0x0, 0x7c(%rax)
movq $0x0, 0x80(%rax)
movq $0x0, 0x88(%rax)
cmpl $0x4, 0x3c(%rax)
jae 0x237a6
movq -0x40(%rbp), %rax
cmpl $0x1, 0x2c(%rax)
je 0x237ba
movq -0x40(%rbp), %rax
cmpl $0x1, 0x3c(%rax)
jae 0x237f5
movq -0x40(%rbp), %rax
cmpl $0x2, 0x2c(%rax)
jne 0x237f5
leaq 0x1505f(%rip), %rdi     # 0x38820
callq 0x5720
jmp 0x237c8
jmp 0x237f5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x23907
movq -0x38(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
callq 0x30820
jmp 0x23907
movq -0x18(%rbp), %rax
cmpl $0x4, 0x4(%rax)
jle 0x2382b
cmpq $0x0, -0x20(%rbp)
jne 0x2382b
movq -0x10(%rbp), %rdi
callq 0x313e0
movq %rax, -0xe8(%rbp)
jmp 0x23818
movq -0xe8(%rbp), %rdi
movq -0x40(%rbp), %rax
movl 0x34(%rax), %esi
callq 0x225a0
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x18(%rbp), %rcx
subl (%rcx), %eax
cmpl $0x15, %eax
jge 0x2384b
movq -0x40(%rbp), %rax
cmpl $0x3, 0x2c(%rax)
jne 0x238fe
cmpq $0x0, -0x20(%rbp)
je 0x23863
movq -0x40(%rbp), %rax
movq -0x20(%rbp), %rcx
movq %rcx, 0x80(%rax)
jmp 0x238b2
movq -0x40(%rbp), %rdi
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x30880
movq %rax, -0xf0(%rbp)
jmp 0x2387d
movq -0x40(%rbp), %rax
movq -0xf0(%rbp), %rcx
movq %rcx, 0x80(%rax)
cmpl $0x0, 0x34(%rax)
jbe 0x238b0
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rdi
movq 0x80(%rax), %rsi
movl 0x34(%rax), %edx
callq 0x212b0
jmp 0x238ae
jmp 0x238b0
jmp 0x238b2
movq -0x40(%rbp), %rax
cmpl $0x3, 0x2c(%rax)
jge 0x238fc
movq -0x40(%rbp), %rax
movq %rax, %rdi
addq $0x8, %rdi
movl 0x34(%rax), %eax
movq -0x20(%rbp), %rcx
testq %rcx, %rcx
sete %cl
movzbl %cl, %ecx
imull %ecx, %eax
movl %eax, %esi
callq 0x30880
movq %rax, -0xf8(%rbp)
jmp 0x238ea
movq -0x40(%rbp), %rax
movq -0xf8(%rbp), %rcx
movq %rcx, 0x88(%rax)
jmp 0x238fe
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x6200
movq -0x28(%rbp), %rdi
callq 0x5490
nopl (%rax)