// File: dgboy2000[P]compress/compress.cpp
// Function: DATCompression::burrows_wheeler_encode(std::vector<int, std::allocator<int>>)
vector<int> DATCompression::burrows_wheeler_encode(vector<int> data) {
    int n = data.size();
    SuffixArray sa(&data);
    
    vector<int> bwt;
    bwt.resize(n+4);
    vector<int>::iterator bwt_iter = bwt.begin();
    
    long int dollar_ind;
    *(bwt_iter++) = data.back();
    
    for (int i=0; i<n; ++i) {
        if (sa[i] == 0) {
            *(bwt_iter++) = 0;
            dollar_ind = i + 1;
        }
        else *(bwt_iter++) = data[sa[i] - 1];
    }
    
    *(bwt_iter++) = dollar_ind / (256*256);
    *(bwt_iter++) = (dollar_ind / 256) % 256;
    *(bwt_iter++) = dollar_ind % 256;
    
    return bwt;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdx, -0xa8(%rbp)
movq %rdi, %rax
movq -0xa8(%rbp), %rdi
movq %rax, -0xa0(%rbp)
movq %rax, %rcx
movq %rcx, -0xb0(%rbp)
movq %rax, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
callq 0x90a0
movq -0xa8(%rbp), %rsi
movl %eax, -0x1c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x65e0
movq -0xa0(%rbp), %rdi
movb $0x0, -0x39(%rbp)
callq 0x8d20
movq -0xa0(%rbp), %rdi
movl -0x1c(%rbp), %eax
addl $0x4, %eax
movslq %eax, %rsi
callq 0x92b0
jmp 0x3068
movq -0xa0(%rbp), %rdi
callq 0x8dd0
movq -0xa8(%rbp), %rdi
movq %rax, -0x58(%rbp)
callq 0x9340
movl (%rax), %eax
movl %eax, -0xb4(%rbp)
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0x8e00
movq %rax, -0x68(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x8eb0
movl -0xb4(%rbp), %ecx
movl %ecx, (%rax)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x3194
movl -0x6c(%rbp), %esi
leaq -0x38(%rbp), %rdi
callq 0x9260
movl %eax, -0xb8(%rbp)
jmp 0x30d3
movl -0xb8(%rbp), %eax
cmpl $0x0, %eax
jne 0x3130
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0x8e00
movq %rax, -0x78(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x8eb0
movl $0x0, (%rax)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
cltq
movq %rax, -0x60(%rbp)
jmp 0x3184
movq -0xa0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
callq 0x8f20
leaq -0x38(%rbp), %rdi
callq 0x9280
jmp 0x327f
movl -0x6c(%rbp), %esi
leaq -0x38(%rbp), %rdi
callq 0x9260
movl %eax, -0xbc(%rbp)
jmp 0x3144
movq -0xa8(%rbp), %rdi
movl -0xbc(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x8db0
movl (%rax), %eax
movl %eax, -0xc0(%rbp)
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0x8e00
movq %rax, -0x80(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x8eb0
movl -0xc0(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x3186
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x30b3
movq -0x60(%rbp), %rax
movl $0x10000, %ecx          # imm = 0x10000
cqto
idivq %rcx
movl %eax, -0xcc(%rbp)
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0x8e00
movq %rax, -0x88(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x8eb0
movl -0xcc(%rbp), %ecx
movl %ecx, (%rax)
movq -0x60(%rbp), %rax
movl $0x100, %ecx            # imm = 0x100
cqto
idivq %rcx
movl $0x100, %ecx            # imm = 0x100
cqto
idivq %rcx
movl %edx, %eax
movl %eax, -0xc8(%rbp)
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0x8e00
movq %rax, -0x90(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x8eb0
movl -0xc8(%rbp), %ecx
movl %ecx, (%rax)
movq -0x60(%rbp), %rax
movl $0x100, %ecx            # imm = 0x100
cqto
idivq %rcx
movl %edx, %eax
movl %eax, -0xc4(%rbp)
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0x8e00
movq %rax, -0x98(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x8eb0
movl -0xc4(%rbp), %ecx
movl %ecx, (%rax)
movb $0x1, -0x39(%rbp)
testb $0x1, -0x39(%rbp)
jne 0x3266
movq -0xa0(%rbp), %rdi
callq 0x8f20
leaq -0x38(%rbp), %rdi
callq 0x9280
movq -0xb0(%rbp), %rax
addq $0xd0, %rsp
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x2380
nopl (%rax,%rax)