// File: /workspace/github_c_makefile_star2_O3/felipelouza[P]egap/unbwt.c
// Function: invert_bwt
void invert_bwt(char *bnam, char *tnam)
{
  FILE *b, *t;
  uint64_t occ[_BW_ALPHA_SIZE];      // emulation of first column of bwt matrix 
  uint64_t start_sa_range[_BW_ALPHA_SIZE+1];
  uint64_t i,tot;
  uint32_t *rankprev, *bwt;
  uint8_t *high8 = NULL;

  b = fopen(bnam,"r");
  if(b==NULL) perror_die("Error opening infile");
  t = fopen(tnam,"w");
  if(t==NULL) perror_die("Error opening outfile");

  // get BWT size
  long bsize = fseek(b,0,SEEK_END);
  if(bsize<0) perror_die("fseek error");
  bsize = ftell(b);
  if(bsize<0) perror_die("ftell error");
  rewind(b);
  
  if(bsize>0xFFFFFFFFFF) 
    die("BWT too large for internal memory inversion");// maximum limit is 2^40 -1
  if(bsize>0xFFFFFFFF) {
    fprintf(stderr,"Large file (>4GB): using 5n bytes ram\n");
    // we need an extra array to store the highest 8 bits 
    high8 = malloc(bsize*sizeof(*high8));
    if(high8==NULL) die("Out of mem");
  }
  bwt = rankprev = (uint32_t *) malloc(bsize*sizeof(*rankprev));
  if(rankprev==NULL) die("Out of mem");

  // clear occ
  for(i=0;i<_BW_ALPHA_SIZE;i++) occ[i]=0;  
  // read bwt from file and compute occ
  for(i=0;i<bsize;i++) {
    int e = fread(bwt+i,1,1,b);
    if(e!=1) perror_die("Error reading BWT");
    occ[bwt[i]]++; // increment count
  }
  if(fclose(b)!=0) perror_die("Error closing BWT file");
  
  // compute start sa_range  
  for(i=0,tot=0;i<_BW_ALPHA_SIZE;i++) {
    start_sa_range[i] = tot; tot+= occ[i];
  }
  start_sa_range[_BW_ALPHA_SIZE] = tot;  
  assert(tot==bsize);

  fprintf(stderr,"Computing rankprev\n");
  // bwt -> rankprev inplace
  for(i=0;i<bsize;i++) {
      // if(i%10000==0) fprintf(stderr,"%ld outof %ld\n",i,bsize);
      uint64_t rp = (start_sa_range[bwt[i]]++);
      assert(rp<bsize);
      rankprev[i] = (uint32_t) rp;
      if(high8) high8[i]=(uint8_t)(rp>>32);
  } 
  assert(start_sa_range[_BW_ALPHA_SIZE-1]==start_sa_range[_BW_ALPHA_SIZE]);
  fprintf(stderr,"Done\n");  
  // recover the original start_sa_range values
  tot=0;
  for(i=0;i<_BW_ALPHA_SIZE;i++) {
    uint64_t temp = start_sa_range[i];
    start_sa_range[i]=tot;
    tot=temp;
  }
  assert(tot==start_sa_range[_BW_ALPHA_SIZE]);

  // start recovering of sequences
  // init buffer
  long s_size=1024; long s_step = 1024;
  char *s = malloc(s_size*sizeof(*s));
  if(!s) die("out of mem");
  long s_pos = 0;
  for(int i=0;i<occ[0];i++) {
    if(i%10000==0) fprintf(stderr,"Recovering sequecence %d\n",i);
    s_pos=0;
    long old_rank = i;
    while(true) {
      // move to first column
      uint64_t rank = rankprev[old_rank];
      if(high8!=NULL) rank += ((uint64_t) high8[old_rank]) << 32;
      int32_t c = get_char_from_first_column64(rank, start_sa_range);
      //fprintf(stderr,"%ld --> %d\n",rank,c); 
      if(c==0) break; //end of sequence
      if(s_pos>=s_size) {
        assert(s_pos==s_size);
        s_size += s_step;
        s = realloc(s,s_size*sizeof(*s));
      }
      assert(s_pos<s_size);  
      s[s_pos++] = c;
      old_rank = rank;
    }
    if(s_pos==0)
      die("Empty string in BWT file");
    // ---- reverse string ----
    for(long k=0;k<s_pos/2;k++) {
     char c = s[k]; s[k] = s[s_pos-1-k]; s[s_pos-1-k]=c;
    }
    // write string
    // fprintf(t,">\n");
    int e = fwrite(s,1,s_pos,t);
    if(e!=s_pos) perror_die("Error writing ouput sequence");
    fprintf(t,"\n");
  }
  fprintf(stderr,"Recovered %zu sequences\n",occ[0]);
  if(fclose(t)!=0) perror_die("Error closing output file");      
  free(rankprev);
  if(high8) free(high8);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1038, %rsp           # imm = 0x1038
movq %rsi, %rbx
leaq 0xbc2(%rip), %rsi       # 0x203f
callq 0x10f0
testq %rax, %rax
je 0x1964
movq %rax, %r12
leaq 0xb88(%rip), %rsi       # 0x201d
movq %rbx, %rdi
callq 0x10f0
testq %rax, %rax
je 0x1969
movq %rax, %r15
movq %r12, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x10d0
testl %eax, %eax
js 0x196e
movq %r12, %rdi
callq 0x10b0
testq %rax, %rax
js 0x1973
movq %rax, %rbx
movq %r12, %rdi
callq 0x1060
movq %rbx, %rax
shrq $0x28, %rax
jne 0x1978
movq %rbx, %rax
shrq $0x20, %rax
je 0x150c
leaq 0x20(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1140
movq (%r14), %rax
movq %rax, 0x8(%rsp)
jmp 0x1515
movq $0x0, 0x8(%rsp)
leaq (,%rbx,4), %rdi
callq 0x10c0
movq %rax, (%rsp)
testq %rax, %rax
je 0x197d
leaq 0x830(%rsp), %rdi
movl $0x800, %edx            # imm = 0x800
xorl %esi, %esi
callq 0x1080
testq %rbx, %rbx
je 0x1582
movq (%rsp), %r13
movq %rbx, %r14
movl $0x1, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r12, %rcx
callq 0x1040
cmpl $0x1, %eax
jne 0x1936
movl (%r13), %eax
incq 0x830(%rsp,%rax,8)
addq $0x4, %r13
decq %r14
jne 0x154f
movq %r12, %rdi
callq 0x1050
testl %eax, %eax
jne 0x1982
xorl %eax, %eax
xorl %ecx, %ecx
movq %rcx, 0x20(%rsp,%rax,8)
addq 0x830(%rsp,%rax,8), %rcx
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x1596
movq %rcx, 0x820(%rsp)
cmpq %rbx, %rcx
jne 0x1987
movq 0x2a32(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb37(%rip), %rdi       # 0x2107
movl $0x13, %esi
movl $0x1, %edx
callq 0x1120
xorl %eax, %eax
testq %rbx, %rbx
je 0x162a
movq 0x8(%rsp), %rdi
movq (%rsp), %r8
movl (%r8,%rax,4), %edx
movq 0x20(%rsp,%rdx,8), %rcx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x20(%rsp,%rdx,8)
cmpq %rbx, %rcx
jae 0x193b
movl %ecx, (%r8,%rax,4)
testq %rdi, %rdi
je 0x161a
shrq $0x20, %rcx
movb %cl, (%rdi,%rax)
incq %rax
cmpq %rax, %rbx
jne 0x15ef
movq 0x820(%rsp), %rax
cmpq %rax, 0x818(%rsp)
jne 0x19a6
movq 0x29b9(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb1c(%rip), %rdi       # 0x2165
movl $0x5, %esi
movl $0x1, %edx
callq 0x1120
xorps %xmm0, %xmm0
xorl %eax, %eax
movaps %xmm0, %xmm1
movaps 0x20(%rsp,%rax,8), %xmm0
shufps $0x4e, %xmm0, %xmm1     # xmm1 = xmm1[2,3],xmm0[0,1]
movaps %xmm1, 0x20(%rsp,%rax,8)
addq $0x2, %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x165d
pshufd $0xee, %xmm0, %xmm0     # xmm0 = xmm0[2,3,2,3]
movq %xmm0, %rax
cmpq 0x820(%rsp), %rax
jne 0x19c5
movl $0x400, %edi            # imm = 0x400
callq 0x10c0
testq %rax, %rax
je 0x19e4
cmpq $0x0, 0x830(%rsp)
movq (%rsp), %rdi
jne 0x170b
xorl %edx, %edx
movq 0x293b(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xb3e(%rip), %rsi       # 0x2205
xorl %eax, %eax
callq 0x10a0
movq %r15, %rdi
callq 0x1050
testl %eax, %eax
jne 0x19e9
movq (%rsp), %rdi
callq 0x1030
movq 0x8(%rsp), %rdi
addq $0x1038, %rsp           # imm = 0x1038
testq %rdi, %rdi
je 0x18af
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1030
movq %rax, %r12
movl $0x400, %r13d           # imm = 0x400
xorl %ecx, %ecx
movq %r15, 0x18(%rsp)
imull $0x3afb7e91, %ecx, %eax # imm = 0x3AFB7E91
rorl $0x4, %eax
cmpl $0x68db8, %eax          # imm = 0x68DB8
movq %rcx, 0x10(%rsp)
jbe 0x1884
movq $-0x1, %rbp
xorl %ebx, %ebx
movq %rcx, %r15
movq 0x8(%rsp), %rsi
movl (%rdi,%r15,4), %eax
testq %rsi, %rsi
je 0x175c
movzbl (%rsi,%r15), %r15d
shlq $0x20, %r15
orq %rax, %r15
jmp 0x175f
movq %rax, %r15
cmpq %r15, 0x20(%rsp)
ja 0x18d9
cmpq %r15, 0x820(%rsp)
jbe 0x18d9
xorl %r14d, %r14d
movl $0xff, %eax
leal (%rax,%r14), %ecx
leal (%rax,%r14), %edx
incl %edx
shrl $0x1f, %edx
addl %edx, %ecx
incl %ecx
sarl %ecx
movslq %ecx, %rdx
cmpq %r15, 0x20(%rsp,%rdx,8)
ja 0x17a2
movl %ecx, %r14d
jmp 0x17a6
decl %ecx
movl %ecx, %eax
cmpl %eax, %r14d
jl 0x1780
jne 0x18ba
testl %r14d, %r14d
je 0x17f7
cmpq %r13, %rbx
jl 0x17df
jne 0x1917
addq $0x400, %r13            # imm = 0x400
movq %r12, %rdi
movq %r13, %rsi
callq 0x10e0
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rax, %r12
cmpq %r13, %rbx
jge 0x18f8
movb %r14b, (%r12,%rbx)
incq %rbx
incq %rbp
jmp 0x1745
cmpq $0x1, %rbx
je 0x1832
testq %rbx, %rbx
je 0x195f
movq %rbx, %rax
shrq %rax
negq %rax
addq %r12, %rbp
movq %r12, %rcx
xorl %edx, %edx
movb (%rcx), %sil
movb (%rbp,%rdx), %dil
movb %dil, (%rcx)
movb %sil, (%rbp,%rdx)
decq %rdx
incq %rcx
cmpq %rdx, %rax
jne 0x1817
movl $0x1, %esi
movq %r12, %rdi
movq %rbx, %rdx
movq 0x18(%rsp), %r15
movq %r15, %rcx
callq 0x1120
cltq
cmpq %rbx, %rax
jne 0x195a
movl $0xa, %edi
movq %r15, %rsi
callq 0x1090
movq 0x10(%rsp), %rcx
incq %rcx
movq 0x830(%rsp), %rdx
cmpq %rcx, %rdx
movq (%rsp), %rdi
ja 0x171b
jmp 0x16b6
movq 0x276d(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x905(%rip), %rsi       # 0x219a
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x10a0
movq 0x10(%rsp), %rcx
movq (%rsp), %rdi
jmp 0x1734
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa8f(%rip), %rdi       # 0x2350
leaq 0x817(%rip), %rsi       # 0x20df
leaq 0xa46(%rip), %rcx       # 0x2315
movl $0xb9, %edx
callq 0x1070
leaq 0xa07(%rip), %rdi       # 0x22e7
leaq 0x7f8(%rip), %rsi       # 0x20df
leaq 0xa27(%rip), %rcx       # 0x2315
movl $0xb1, %edx
callq 0x1070
leaq 0x8c3(%rip), %rdi       # 0x21c2
leaq 0x7d9(%rip), %rsi       # 0x20df
leaq 0x7da(%rip), %rcx       # 0x20e7
movl $0x93, %edx
callq 0x1070
leaq 0x896(%rip), %rdi       # 0x21b4
leaq 0x7ba(%rip), %rsi       # 0x20df
leaq 0x7bb(%rip), %rcx       # 0x20e7
movl $0x8f, %edx
callq 0x1070
callq 0x119d
leaq 0x7d9(%rip), %rdi       # 0x211b
leaq 0x796(%rip), %rsi       # 0x20df
leaq 0x797(%rip), %rcx       # 0x20e7
movl $0x6e, %edx
callq 0x1070
callq 0x11ef
callq 0x11c7
callq 0x12d0
callq 0x12bb
callq 0x12a6
callq 0x1291
callq 0x1269
callq 0x1241
callq 0x11b2
leaq 0x746(%rip), %rdi       # 0x20d4
leaq 0x74a(%rip), %rsi       # 0x20df
leaq 0x74b(%rip), %rcx       # 0x20e7
movl $0x67, %edx
callq 0x1070
leaq 0x777(%rip), %rdi       # 0x2124
leaq 0x72b(%rip), %rsi       # 0x20df
leaq 0x72c(%rip), %rcx       # 0x20e7
movl $0x72, %edx
callq 0x1070
leaq 0x79f(%rip), %rdi       # 0x216b
leaq 0x70c(%rip), %rsi       # 0x20df
leaq 0x70d(%rip), %rcx       # 0x20e7
movl $0x7b, %edx
callq 0x1070
callq 0x1219
callq 0x1204