// File: /workspace/github_cpp_makefile_star2_O2/twekkel[P]mzpq/libzpaq.cpp
// Function: libzpaq::divbwt(unsigned char const*, unsigned char*, int*, int)
int
divbwt(const unsigned char *T, unsigned char *U, int *A, int n) {
  int *B;
  int *bucket_A, *bucket_B;
  int m, pidx, i;

  /* Check arguments. */
  if((T == NULL) || (U == NULL) || (n < 0)) { return -1; }
  else if(n <= 1) { if(n == 1) { U[0] = T[0]; } return n; }

  if((B = A) == NULL) { B = (int *)malloc((size_t)(n + 1) * sizeof(int)); }
  bucket_A = (int *)malloc(BUCKET_A_SIZE * sizeof(int));
  bucket_B = (int *)malloc(BUCKET_B_SIZE * sizeof(int));

  /* Burrows-Wheeler Transform. */
  if((B != NULL) && (bucket_A != NULL) && (bucket_B != NULL)) {
    m = sort_typeBstar(T, B, bucket_A, bucket_B, n);
    pidx = construct_BWT(T, B, bucket_A, bucket_B, n, m);

    /* Copy to output string. */
    U[0] = T[n - 1];
    for(i = 0; i < pidx; ++i) { U[i + 1] = (unsigned char)B[i]; }
    for(i += 1; i < n; ++i) { U[i] = (unsigned char)B[i]; }
    pidx += 1;
  } else {
    pidx = -2;
  }

  free(bucket_B);
  free(bucket_A);
  if(A == NULL) { free(B); }

  return pidx;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
testq %rdi, %rdi
sete %al
movq %rsi, 0x18(%rsp)
testq %rsi, %rsi
sete %sil
orb %al, %sil
testl %ecx, %ecx
sets %al
orb %sil, %al
je 0x1673a
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %rbp
cmpl $0x1, %ecx
ja 0x16757
xorl %ebx, %ebx
cmpl $0x1, %ecx
jne 0x16729
movb (%rbp), %al
movq 0x18(%rsp), %rcx
movb %al, (%rcx)
pushq $0x1
jmp 0x16728
movq %rcx, 0x10(%rsp)
movl %ecx, %eax
movq %rax, 0x8(%rsp)
movq %rdx, %r12
movq %rdx, 0x20(%rsp)
testq %rdx, %rdx
jne 0x16785
movq 0x8(%rsp), %rax
leaq 0x4(,%rax,4), %rdi
callq 0x4240
movq %rax, %r12
movl $0x400, %edi            # imm = 0x400
callq 0x4240
movq %rax, %r13
movl $0x40000, %edi          # imm = 0x40000
callq 0x4240
movq %rax, %r14
testq %r12, %r12
sete %al
testq %r13, %r13
sete %cl
orb %al, %cl
testq %r14, %r14
sete %al
orb %cl, %al
jne 0x168a4
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %r14, %rcx
movq 0x10(%rsp), %r8
callq 0x14857
testl %eax, %eax
jle 0x168ac
movl $0xfe, %eax
testl %eax, %eax
js 0x168ac
movl %eax, %edx
shll $0x8, %edx
leal 0x1(%rax), %ecx
orl %edx, %ecx
movslq %ecx, %rcx
movslq (%r14,%rcx,4), %rcx
leaq (%r12,%rcx,4), %rsi
movl %eax, %ecx
movslq 0x4(%r13,%rcx,4), %rcx
leaq (%r12,%rcx,4), %rdi
movslq %edx, %r8
xorl %r9d, %r9d
pushq $-0x1
popq %r10
addq $-0x4, %rdi
cmpq %rdi, %rsi
ja 0x1689d
movslq (%rdi), %rcx
testq %rcx, %rcx
jg 0x1682f
testl %ecx, %ecx
je 0x16810
notl %ecx
movl %ecx, (%rdi)
jmp 0x16810
movq %r13, %r15
leal -0x1(%rcx), %r13d
movzbl (%rbp,%r13), %r11d
movl %r11d, %ebx
notl %ebx
movl %ebx, (%rdi)
xorl %ebx, %ebx
cmpl $0x1, %ecx
je 0x1685a
movl %ecx, %ebx
negl %ebx
cmpb %r11b, -0x2(%rbp,%rcx)
cmoval %ebx, %r13d
movl %r13d, %ebx
cmpl %r11d, %r10d
jne 0x16867
movl %r10d, %r11d
movq %r15, %r13
jmp 0x1688e
testl %r10d, %r10d
movq %r15, %r13
js 0x16880
subq %r12, %r9
shrq $0x2, %r9
orl %edx, %r10d
movslq %r10d, %rcx
movl %r9d, (%r14,%rcx,4)
movq %r8, %rcx
orq %r11, %rcx
movslq (%r14,%rcx,4), %rcx
leaq (%r12,%rcx,4), %r9
movl %ebx, (%r9)
addq $-0x4, %r9
movl %r11d, %r10d
jmp 0x16810
decl %eax
jmp 0x167de
pushq $-0x2
popq %rbx
jmp 0x169a6
movq 0x10(%rsp), %rax
leal -0x1(%rax), %ecx
movzbl (%rbp,%rcx), %eax
movslq (%r13,%rax,4), %rdx
movq 0x8(%rsp), %r8
movzbl -0x2(%rbp,%r8), %edi
cmpb %al, %dil
notl %edi
cmovael %ecx, %edi
leaq 0x4(%r12,%rdx,4), %rsi
movl %edi, -0x4(%rsi)
leaq (%r12,%r8,4), %rcx
movq %r12, %rdx
movq %r12, %rbx
cmpq %rcx, %rdx
jae 0x16957
movslq (%rdx), %r9
testq %r9, %r9
jle 0x16941
leal -0x1(%r9), %r10d
movzbl (%rbp,%r10), %edi
movl %edi, (%rdx)
xorl %r8d, %r8d
cmpl $0x1, %r9d
je 0x16919
movzbl -0x2(%rbp,%r9), %r9d
movl %r10d, %r8d
cmpb %dil, %r9b
jae 0x16919
notl %r9d
movl %r9d, %r8d
cmpl %edi, %eax
je 0x16938
subq %r12, %rsi
shrq $0x2, %rsi
movl %eax, %eax
movl %esi, (%r13,%rax,4)
movl %edi, %eax
movslq (%r13,%rax,4), %rax
leaq (%r12,%rax,4), %rsi
movl %edi, %eax
movl %r8d, (%rsi)
addq $0x4, %rsi
jmp 0x16951
testl %r9d, %r9d
je 0x1694e
notl %r9d
movl %r9d, (%rdx)
jmp 0x16951
movq %rdx, %rbx
addq $0x4, %rdx
jmp 0x168e3
subq %r12, %rbx
shrq $0x2, %rbx
movq 0x8(%rsp), %rax
movb -0x1(%rbp,%rax), %al
movq 0x18(%rsp), %rdi
movb %al, (%rdi)
xorl %ecx, %ecx
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
movq 0x10(%rsp), %rsi
cmpq %rcx, %rax
je 0x1699d
movb (%r12,%rcx,4), %dl
movb %dl, 0x1(%rdi,%rcx)
incq %rcx
jmp 0x1697f
movb 0x4(%r12,%rax,4), %cl
movb %cl, 0x1(%rdi,%rax)
incq %rax
leal 0x1(%rax), %ecx
cmpl %esi, %ecx
jl 0x16991
incl %ebx
movq %r14, %rdi
callq 0x4200
movq %r13, %rdi
callq 0x4200
cmpq $0x0, 0x20(%rsp)
jne 0x16729
movq %r12, %rdi
callq 0x4200
jmp 0x16729