// File: /workspace/github_c_makefile_star2_O1/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 0x1948
movq %rax, %r12
leaq 0xb88(%rip), %rsi       # 0x201d
movq %rbx, %rdi
callq 0x10f0
testq %rax, %rax
je 0x194d
movq %rax, %rbp
movq %r12, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x10d0
testl %eax, %eax
js 0x1952
movq %r12, %rdi
callq 0x10b0
testq %rax, %rax
js 0x1957
movq %rax, %rbx
movq %r12, %rdi
callq 0x1060
movabsq $0x10000000000, %rax    # imm = 0x10000000000
cmpq %rax, %rbx
jge 0x195c
movabsq $0x100000000, %rax      # imm = 0x100000000
cmpq %rax, %rbx
jl 0x1513
leaq 0x20(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1140
movq (%r14), %r14
jmp 0x1516
xorl %r14d, %r14d
leaq (,%rbx,4), %rdi
callq 0x10c0
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x1961
leaq 0x830(%rsp), %rdi
movl $0x800, %edx            # imm = 0x800
xorl %esi, %esi
callq 0x1080
testq %rbx, %rbx
je 0x1585
movq 0x8(%rsp), %r13
movq %rbx, %r15
movl $0x1, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r12, %rcx
callq 0x1040
cmpl $0x1, %eax
jne 0x191a
movl (%r13), %eax
incq 0x830(%rsp,%rax,8)
addq $0x4, %r13
decq %r15
jne 0x1552
movq %r12, %rdi
callq 0x1050
testl %eax, %eax
jne 0x1966
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 0x1599
movq %rcx, 0x820(%rsp)
cmpq %rbx, %rcx
jne 0x196b
movq 0x2a2f(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb34(%rip), %rdi       # 0x2107
movl $0x13, %esi
movl $0x1, %edx
callq 0x1120
testq %rbx, %rbx
movq 0x8(%rsp), %rdi
je 0x1620
xorl %eax, %eax
movl (%rdi,%rax,4), %edx
movq 0x20(%rsp,%rdx,8), %rcx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x20(%rsp,%rdx,8)
cmpq %rbx, %rcx
jae 0x191f
movl %ecx, (%rdi,%rax,4)
testq %r14, %r14
je 0x1618
shrq $0x20, %rcx
movb %cl, (%r14,%rax)
incq %rax
cmpq %rax, %rbx
jne 0x15ee
movq 0x818(%rsp), %rax
cmpq 0x820(%rsp), %rax
jne 0x198a
movq 0x29bb(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xb1e(%rip), %rdi       # 0x2165
movl $0x5, %esi
movl $0x1, %edx
callq 0x1120
xorl %eax, %eax
xorl %ecx, %ecx
movq %rcx, %rdx
movq 0x20(%rsp,%rax,8), %rcx
movq %rdx, 0x20(%rsp,%rax,8)
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x165a
cmpq 0x820(%rsp), %rcx
jne 0x19a9
movl $0x400, %edi            # imm = 0x400
callq 0x10c0
testq %rax, %rax
je 0x19c8
movq 0x830(%rsp), %rdx
testq %rdx, %rdx
jne 0x16f4
movq 0x2951(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xb54(%rip), %rsi       # 0x2205
xorl %eax, %eax
callq 0x10a0
movq %rbp, %rdi
callq 0x1050
testl %eax, %eax
jne 0x19cd
movq 0x8(%rsp), %rdi
callq 0x1030
testq %r14, %r14
je 0x188c
movq %r14, %rdi
addq $0x1038, %rsp           # imm = 0x1038
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 %rbp, 0x18(%rsp)
imull $0x3afb7e91, %ecx, %eax # imm = 0x3AFB7E91
rorl $0x4, %eax
cmpl $0x68db8, %eax          # imm = 0x68DB8
movq %rcx, 0x10(%rsp)
jbe 0x1865
xorl %ebx, %ebx
movq %rcx, %rax
movq 0x8(%rsp), %rdi
movl (%rdi,%rax,4), %ebp
testq %r14, %r14
je 0x173b
movzbl (%r14,%rax), %ecx
shlq $0x20, %rcx
orq %rcx, %rbp
cmpq %rbp, 0x20(%rsp)
ja 0x189e
cmpq %rbp, 0x820(%rsp)
jbe 0x189e
xorl %r15d, %r15d
movl $0xff, %ecx
leal (%rcx,%r15), %edx
leal (%rcx,%r15), %esi
incl %esi
shrl $0x1f, %esi
addl %esi, %edx
incl %edx
sarl %edx
movslq %edx, %rsi
cmpq %rbp, 0x20(%rsp,%rsi,8)
ja 0x177e
movl %edx, %r15d
jmp 0x1782
decl %edx
movl %edx, %ecx
cmpl %ecx, %r15d
jl 0x175c
jne 0x18bd
testl %r15d, %r15d
je 0x17ca
cmpq %r13, %rbx
jl 0x17b7
jne 0x18fb
addq $0x400, %r13            # imm = 0x400
movq %r12, %rdi
movq %r13, %rsi
callq 0x10e0
movq 0x8(%rsp), %rdi
movq %rax, %r12
cmpq %r13, %rbx
jge 0x18dc
movb %r15b, (%r12,%rbx)
incq %rbx
movq %rbp, %rax
testl %r15d, %r15d
jne 0x1727
testq %rbx, %rbx
je 0x193e
cmpq $0x2, %rbx
jl 0x1817
movq %rbx, %rax
shrq $0x3f, %rax
addq %rbx, %rax
sarq %rax
negq %rax
leaq (%r12,%rbx), %rcx
decq %rcx
movq %r12, %rdx
xorl %esi, %esi
movb (%rdx), %dil
movb (%rcx,%rsi), %r8b
movb %r8b, (%rdx)
movb %dil, (%rcx,%rsi)
decq %rsi
incq %rdx
cmpq %rsi, %rax
jne 0x17fe
movl $0x1, %esi
movq %r12, %rdi
movq %rbx, %rdx
movq 0x18(%rsp), %rbp
movq %rbp, %rcx
callq 0x1120
cltq
cmpq %rbx, %rax
jne 0x1943
movl $0xa, %edi
movq %rbp, %rsi
callq 0x1090
movq 0x10(%rsp), %rcx
incq %rcx
movq 0x830(%rsp), %rdx
cmpq %rcx, %rdx
ja 0x1704
jmp 0x16a0
movq 0x278c(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x924(%rip), %rsi       # 0x219a
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x10a0
movq 0x10(%rsp), %rcx
jmp 0x171d
addq $0x1038, %rsp           # imm = 0x1038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa42(%rip), %rdi       # 0x22e7
leaq 0x833(%rip), %rsi       # 0x20df
leaq 0xa62(%rip), %rcx       # 0x2315
movl $0xb1, %edx
callq 0x1070
leaq 0xa8c(%rip), %rdi       # 0x2350
leaq 0x814(%rip), %rsi       # 0x20df
leaq 0xa43(%rip), %rcx       # 0x2315
movl $0xb9, %edx
callq 0x1070
leaq 0x8df(%rip), %rdi       # 0x21c2
leaq 0x7f5(%rip), %rsi       # 0x20df
leaq 0x7f6(%rip), %rcx       # 0x20e7
movl $0x93, %edx
callq 0x1070
leaq 0x8b2(%rip), %rdi       # 0x21b4
leaq 0x7d6(%rip), %rsi       # 0x20df
leaq 0x7d7(%rip), %rcx       # 0x20e7
movl $0x8f, %edx
callq 0x1070
callq 0x119d
leaq 0x7f5(%rip), %rdi       # 0x211b
leaq 0x7b2(%rip), %rsi       # 0x20df
leaq 0x7b3(%rip), %rcx       # 0x20e7
movl $0x6e, %edx
callq 0x1070
callq 0x11dc
callq 0x11c7
callq 0x12d0
callq 0x12bb
callq 0x12a6
callq 0x1291
callq 0x1269
callq 0x1241
callq 0x11b2
leaq 0x762(%rip), %rdi       # 0x20d4
leaq 0x766(%rip), %rsi       # 0x20df
leaq 0x767(%rip), %rcx       # 0x20e7
movl $0x67, %edx
callq 0x1070
leaq 0x793(%rip), %rdi       # 0x2124
leaq 0x747(%rip), %rsi       # 0x20df
leaq 0x748(%rip), %rcx       # 0x20e7
movl $0x72, %edx
callq 0x1070
leaq 0x7bb(%rip), %rdi       # 0x216b
leaq 0x728(%rip), %rsi       # 0x20df
leaq 0x729(%rip), %rcx       # 0x20e7
movl $0x7b, %edx
callq 0x1070
callq 0x1219
callq 0x1204