// File: /workspace/github_c_makefile_star2_O0/felipelouza[P]egap/util.c
// Function: readBWTsingle
bool readBWTsingle(char *path, g_data *g)
{
  char filename[Filename_size];
  customInt n=0;
  
  // open .size file and get number of (multi) BWT
  snprintf(filename,Filename_size,"%s.%s",path,LEN_EXT);
  FILE *f = fopen(filename,"r");
  if(f==NULL) die(__func__);
  if(fseek(f, 0, SEEK_END) != 0) {
    perror("fseek error");
    die(__func__);
  }
  off_t flen = ftello(f);
  rewind(f);
  if(flen < 0) {
    perror("ftello error");
    die(__func__);
  }
  else if(flen%8!=0) 
    die("Invalid format of ."LEN_EXT" file");
  g->numBwt = flen/8;
  assert(g->numBwt>0);

  // single BWT with no lcp or dbInfo computation: nothing to do
  if(g->numBwt==1 && !g->lcpCompute && g->dbOrder==0){ 
    if(g->outputSA) { // if there is a single (multi)-bwt the DA does not change
      char tmp1[Filename_size];
      char tmp2[Filename_size];
      snprintf(tmp1,Filename_size,"%s.%d.%s",path,g->outputSA, SA_BL_EXT);
      snprintf(tmp2,Filename_size,"%s.%d.%s",path,g->outputSA, SA_EXT);
      if(rename(tmp1,tmp2)!=0)
        die("Cannot rename SA file");
    }
    if(g->outputDA) { // if there is a single (multi)-bwt the DA does not change
      char tmp1[Filename_size];
      char tmp2[Filename_size];
      snprintf(tmp1,Filename_size,"%s.%d.%s",path,g->outputDA, DA_BL_EXT);
      snprintf(tmp2,Filename_size,"%s.%d.%s",path,g->outputDA, DA_EXT);
      if(rename(tmp1,tmp2)!=0)
        die("Cannot rename DA file");
    }
    if(g->outputQS) { // if there is a single (multi)-bwt the QS does not change
      char tmp1[Filename_size];
      char tmp2[Filename_size];
      snprintf(tmp1,Filename_size,"%s.%s",path,QS_BL_EXT);
      snprintf(tmp2,Filename_size,"%s.%s",path,QS_EXT);
      if(rename(tmp1,tmp2)!=0)
        die("Cannot rename QS file");
    }
    if(g->outputSL) { // if there is a single (multi)-bwt the SL does not change
      char tmp1[Filename_size];
      char tmp2[Filename_size];
      snprintf(tmp1,Filename_size,"%s.%d.%s",path,g->outputSL, SL_BL_EXT);
      snprintf(tmp2,Filename_size,"%s.%d.%s",path,g->outputSL, SL_EXT);
      if(rename(tmp1,tmp2)!=0)
        die("Cannot rename SL file");
    }
    size_t size;
    size_t r = fread(&size, 8, 1, f);// sizes are stored in 64 bits 
    if(r!=1) die(__func__);
    g->mergeLen = size;
    fclose(f);
    return false;
  }

  if(g->outputDA) { 
    g->bwtDocs = (customInt *) malloc (g->numBwt * sizeof(customInt));
    snprintf(filename,Filename_size,"%s.%s",path,DOCS_EXT);
    FILE *f = fopen(filename,"r");
    if(f==NULL) die(__func__);
    size_t sum=0;
    for (int i = 0; i < g->numBwt; ++i) {
      customInt docs;
      size_t r = fread(&docs, 8, 1, f);// docs are stored in 64 bits 
      if(r!=1) die(__func__);
      g->bwtDocs[i] = sum;
      sum+=docs;
      //printf("--> %zu\n",  docs);
    }
    fclose(f);
    f = fopen(filename,"wb");
    size_t e = fwrite(&sum,8,1,f);
    if(e!=1) die(__func__);
    fclose(f);
  }

  // we have the number of (multi)BWTs: allocate g->bwtLen and g->bws  
  g->bwtLen = (customInt *) malloc (g->numBwt * sizeof(customInt));
  g->bws = (symbol **) malloc (g->numBwt * sizeof(symbol *));
  if(!g->bwtLen || !g->bws) die(__func__);
  // read and store sizes of multibwt in g->bwtLen 
  for (int i = 0; i < g->numBwt; ++i) {
    size_t size;
    size_t r = fread(&size, 8, 1, f);// sizes are stored in 64 bits 
    if(r!=1) die(__func__);
    if(size>MAX_OUTPUT_SIZE) {
      fprintf(stderr,"Bwt len %zu larger than maximum sequence size: %llu\n",
      size,MAX_OUTPUT_SIZE);
      die(__func__);
    }
    g->bwtLen[i] = size;
    n += size;
  }
  g->mergeLen = n;
  fclose(f);
  
  // We now have g->mergeLen: open and read concatenated bwt file 
  snprintf(g->bwfname,Filename_size,"%s.%s",path,BWT_EXT);
  int fd = open(g->bwfname,O_RDWR);
  if(fd == -1) die(__func__);
  // allocate/mmap memory for BWT concatenation and read whole file
  if(g->mmapBWT || g->extMem) { // mmap input file to RAM modifications are written back to the input file 
    g->bws[0] = mmap(NULL,n*sizeof(symbol),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    if(g->bws[0] == MAP_FAILED) die(__func__);
    #ifdef USE_MMAP_ADVISE
    // the first access is sequential to remap alphabet
    madvise(g->bws[0], g->mergeLen*sizeof(symbol), MADV_SEQUENTIAL);
    #endif
  }
  else { // allocate memory and read from file descriptor
    uint8_t *b = malloc(n*sizeof(symbol));
    if(!b) die(__func__);  
    huge_pread(fd,b,sizeof(symbol)*n,0);
    g->bws[0] = (symbol *) b;
  } 
  if(close(fd)!=0) die(__func__);

  // init g->bws[1..]: we do this even for g->extMem, since g->bws[i]-g->bws[0] is the starting point in the file of the i-th bwt
  for (int i = 0; i < g->numBwt -1; ++i)  
    g->bws[i+1] = g->bws[i] + g->bwtLen[i]; 
    
  // remap BWTs and also init g->sizeOfAlpha etc   
  remap_bwts(g);    
  if(g->extMem) { // if we are working in external memory don't keep the BWTs mmaped 
    int e = munmap(g->bws[0],g->mergeLen*sizeof(symbol));
    if(e) die("main (unmap bws)");
  }
  return true;
}

// Assembly:
subq $0x90a8, %rsp           # imm = 0x90A8
movq %rdi, 0x9098(%rsp)
movq %rsi, 0x9090(%rsp)
movq $0x0, 0x8088(%rsp)
leaq 0x8090(%rsp), %rdi
movq 0x9098(%rsp), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0x11740(%rip), %rdx     # 0x14adf
leaq 0x11da8(%rip), %r8      # 0x1514e
movb $0x0, %al
callq 0x2130
leaq 0x8090(%rsp), %rdi
leaq 0x12ebd(%rip), %rsi     # 0x16279
callq 0x21d0
movq %rax, 0x8080(%rsp)
cmpq $0x0, 0x8080(%rsp)
jne 0x33e0
leaq 0x1173e(%rip), %rdi     # 0x14b19
callq 0x3ce0
movq 0x8080(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x2250
cmpl $0x0, %eax
je 0x3413
leaq 0x11725(%rip), %rdi     # 0x14b27
callq 0x2310
leaq 0x1170b(%rip), %rdi     # 0x14b19
callq 0x3ce0
movq 0x8080(%rsp), %rdi
callq 0x2180
movq %rax, 0x8078(%rsp)
movq 0x8080(%rsp), %rdi
callq 0x2110
cmpq $0x0, 0x8078(%rsp)
jge 0x345a
leaq 0x116ec(%rip), %rdi     # 0x14b33
callq 0x2310
leaq 0x116c6(%rip), %rdi     # 0x14b19
callq 0x3ce0
jmp 0x3480
movq 0x8078(%rsp), %rax
movl $0x8, %ecx
cqto
idivq %rcx
cmpq $0x0, %rdx
je 0x347e
leaq 0x116c7(%rip), %rdi     # 0x14b40
callq 0x3ce0
jmp 0x3480
movq 0x8078(%rsp), %rax
movl $0x8, %ecx
cqto
idivq %rcx
movl %eax, %ecx
movq 0x9090(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0x9090(%rsp), %rax
cmpl $0x1, 0x20(%rax)
jne 0x37ab
movq 0x9090(%rsp), %rax
testb $0x1, 0x3e(%rax)
jne 0x37ab
movq 0x9090(%rsp), %rax
cmpl $0x0, 0x40(%rax)
jne 0x37ab
movq 0x9090(%rsp), %rax
cmpl $0x0, 0x4c(%rax)
je 0x357b
leaq 0x7070(%rsp), %rdi
movq 0x9098(%rsp), %rcx
movq 0x9090(%rsp), %rax
movl 0x4c(%rax), %r8d
movl $0x1000, %esi           # imm = 0x1000
leaq 0x1164e(%rip), %rdx     # 0x14b5d
leaq 0x11650(%rip), %r9      # 0x14b66
movb $0x0, %al
callq 0x2130
leaq 0x6070(%rsp), %rdi
movq 0x9098(%rsp), %rcx
movq 0x9090(%rsp), %rax
movl 0x4c(%rax), %r8d
movl $0x1000, %esi           # imm = 0x1000
leaq 0x11618(%rip), %rdx     # 0x14b5d
leaq 0x10e06(%rip), %r9      # 0x14352
movb $0x0, %al
callq 0x2130
leaq 0x7070(%rsp), %rdi
leaq 0x6070(%rsp), %rsi
callq 0x2320
cmpl $0x0, %eax
je 0x3579
leaq 0x115f8(%rip), %rdi     # 0x14b6c
callq 0x3ce0
jmp 0x357b
movq 0x9090(%rsp), %rax
cmpl $0x0, 0x44(%rax)
je 0x3621
leaq 0x5070(%rsp), %rdi
movq 0x9098(%rsp), %rcx
movq 0x9090(%rsp), %rax
movl 0x44(%rax), %r8d
movl $0x1000, %esi           # imm = 0x1000
leaq 0x115a8(%rip), %rdx     # 0x14b5d
leaq 0x115c6(%rip), %r9      # 0x14b82
movb $0x0, %al
callq 0x2130
leaq 0x4070(%rsp), %rdi
movq 0x9098(%rsp), %rcx
movq 0x9090(%rsp), %rax
movl 0x44(%rax), %r8d
movl $0x1000, %esi           # imm = 0x1000
leaq 0x11572(%rip), %rdx     # 0x14b5d
leaq 0x10d23(%rip), %r9      # 0x14315
movb $0x0, %al
callq 0x2130
leaq 0x5070(%rsp), %rdi
leaq 0x4070(%rsp), %rsi
callq 0x2320
cmpl $0x0, %eax
je 0x361f
leaq 0x1156e(%rip), %rdi     # 0x14b88
callq 0x3ce0
jmp 0x3621
movq 0x9090(%rsp), %rax
cmpl $0x0, 0x54(%rax)
je 0x36ab
leaq 0x3070(%rsp), %rdi
movq 0x9098(%rsp), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0x11494(%rip), %rdx     # 0x14adf
leaq 0x1154c(%rip), %r8      # 0x14b9e
movb $0x0, %al
callq 0x2130
leaq 0x2070(%rsp), %rdi
movq 0x9098(%rsp), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0x1146a(%rip), %rdx     # 0x14adf
leaq 0x10d20(%rip), %r8      # 0x1439c
movb $0x0, %al
callq 0x2130
leaq 0x3070(%rsp), %rdi
leaq 0x2070(%rsp), %rsi
callq 0x2320
cmpl $0x0, %eax
je 0x36a9
leaq 0x11504(%rip), %rdi     # 0x14ba8
callq 0x3ce0
jmp 0x36ab
movq 0x9090(%rsp), %rax
cmpl $0x0, 0x50(%rax)
je 0x374b
leaq 0x1070(%rsp), %rdi
movq 0x9098(%rsp), %rcx
movq 0x9090(%rsp), %rax
movl 0x50(%rax), %r8d
movl $0x1000, %esi           # imm = 0x1000
leaq 0x11478(%rip), %rdx     # 0x14b5d
leaq 0x114d2(%rip), %r9      # 0x14bbe
movb $0x0, %al
callq 0x2130
leaq 0x70(%rsp), %rdi
movq 0x9098(%rsp), %rcx
movq 0x9090(%rsp), %rax
movl 0x50(%rax), %r8d
movl $0x1000, %esi           # imm = 0x1000
leaq 0x11445(%rip), %rdx     # 0x14b5d
leaq 0x114a5(%rip), %r9      # 0x14bc4
movb $0x0, %al
callq 0x2130
leaq 0x1070(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x2320
cmpl $0x0, %eax
je 0x3749
leaq 0x11483(%rip), %rdi     # 0x14bc7
callq 0x3ce0
jmp 0x374b
movq 0x8080(%rsp), %rcx
leaq 0x68(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x2090
movq %rax, 0x60(%rsp)
cmpq $0x1, 0x60(%rsp)
je 0x3780
leaq 0x1139e(%rip), %rdi     # 0x14b19
callq 0x3ce0
movq 0x68(%rsp), %rcx
movq 0x9090(%rsp), %rax
movq %rcx, 0x28(%rax)
movq 0x8080(%rsp), %rdi
callq 0x20d0
movb $0x0, 0x90a7(%rsp)
jmp 0x3cc3
movq 0x9090(%rsp), %rax
cmpl $0x0, 0x44(%rax)
je 0x3921
movq 0x9090(%rsp), %rax
movslq 0x20(%rax), %rdi
shlq $0x3, %rdi
callq 0x127b0
movq %rax, %rcx
movq 0x9090(%rsp), %rax
movq %rcx, 0x10(%rax)
leaq 0x8090(%rsp), %rdi
movq 0x9098(%rsp), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0x112e2(%rip), %rdx     # 0x14adf
leaq 0x113d9(%rip), %r8      # 0x14bdd
movb $0x0, %al
callq 0x2130
leaq 0x8090(%rsp), %rdi
leaq 0x12a5f(%rip), %rsi     # 0x16279
callq 0x21d0
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x58(%rsp)
jne 0x3838
leaq 0x112e6(%rip), %rdi     # 0x14b19
callq 0x3ce0
movq $0x0, 0x50(%rsp)
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
movq 0x9090(%rsp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x38c2
movq 0x58(%rsp), %rcx
leaq 0x40(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x2090
movq %rax, 0x38(%rsp)
cmpq $0x1, 0x38(%rsp)
je 0x388c
leaq 0x11292(%rip), %rdi     # 0x14b19
callq 0x3ce0
movq 0x50(%rsp), %rdx
movq 0x9090(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x4c(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x40(%rsp), %rax
addq 0x50(%rsp), %rax
movq %rax, 0x50(%rsp)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x3849
movq 0x58(%rsp), %rdi
callq 0x20d0
leaq 0x8090(%rsp), %rdi
leaq 0x11214(%rip), %rsi     # 0x14aef
callq 0x21d0
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rcx
leaq 0x50(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x2380
movq %rax, 0x30(%rsp)
cmpq $0x1, 0x30(%rsp)
je 0x3917
leaq 0x11207(%rip), %rdi     # 0x14b19
callq 0x3ce0
movq 0x58(%rsp), %rdi
callq 0x20d0
movq 0x9090(%rsp), %rax
movslq 0x20(%rax), %rdi
shlq $0x3, %rdi
callq 0x127b0
movq %rax, %rcx
movq 0x9090(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x9090(%rsp), %rax
movslq 0x20(%rax), %rdi
shlq $0x3, %rdi
callq 0x127b0
movq %rax, %rcx
movq 0x9090(%rsp), %rax
movq %rcx, (%rax)
movq 0x9090(%rsp), %rax
cmpq $0x0, 0x8(%rax)
je 0x3985
movq 0x9090(%rsp), %rax
cmpq $0x0, (%rax)
jne 0x3991
leaq 0x1118d(%rip), %rdi     # 0x14b19
callq 0x3ce0
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movq 0x9090(%rsp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x3a5a
movq 0x8080(%rsp), %rcx
leaq 0x20(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x2090
movq %rax, 0x18(%rsp)
cmpq $0x1, 0x18(%rsp)
je 0x39e3
leaq 0x1113b(%rip), %rdi     # 0x14b19
callq 0x3ce0
cmpq $-0x2, 0x20(%rsp)
jbe 0x3a1b
movq 0x15606(%rip), %rax     # 0x18ff8
movq (%rax), %rdi
movq 0x20(%rsp), %rdx
leaq 0x111e1(%rip), %rsi     # 0x14be2
movq $-0x2, %rcx
movb $0x0, %al
callq 0x21b0
leaq 0x11103(%rip), %rdi     # 0x14b19
callq 0x3ce0
movq 0x20(%rsp), %rdx
movq 0x9090(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x2c(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x20(%rsp), %rax
addq 0x8088(%rsp), %rax
movq %rax, 0x8088(%rsp)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x3999
movq 0x8088(%rsp), %rcx
movq 0x9090(%rsp), %rax
movq %rcx, 0x28(%rax)
movq 0x8080(%rsp), %rdi
callq 0x20d0
movq 0x9090(%rsp), %rdi
addq $0xa0, %rdi
movq 0x9098(%rsp), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0x11041(%rip), %rdx     # 0x14adf
leaq 0x11046(%rip), %r8      # 0x14aeb
movb $0x0, %al
callq 0x2130
movq 0x9090(%rsp), %rdi
addq $0xa0, %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x22e0
movl %eax, 0x14(%rsp)
cmpl $-0x1, 0x14(%rsp)
jne 0x3ade
leaq 0x11040(%rip), %rdi     # 0x14b19
callq 0x3ce0
movq 0x9090(%rsp), %rax
testb $0x1, 0x72(%rax)
jne 0x3afe
movq 0x9090(%rsp), %rax
testb $0x1, 0x69(%rax)
je 0x3b84
movq 0x8088(%rsp), %rsi
shlq $0x0, %rsi
movl 0x14(%rsp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x2220
movq %rax, %rcx
movq 0x9090(%rsp), %rax
movq (%rax), %rax
movq %rcx, (%rax)
movq 0x9090(%rsp), %rax
movq (%rax), %rax
movq $-0x1, %rcx
cmpq %rcx, (%rax)
jne 0x3b5a
leaq 0x10fc4(%rip), %rdi     # 0x14b19
callq 0x3ce0
movq 0x9090(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rdi
movq 0x9090(%rsp), %rax
movq 0x28(%rax), %rsi
shlq $0x0, %rsi
movl $0x2, %edx
callq 0x22d0
jmp 0x3bdf
movq 0x8088(%rsp), %rdi
shlq $0x0, %rdi
callq 0x127b0
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
jne 0x3bae
leaq 0x10f70(%rip), %rdi     # 0x14b19
callq 0x3ce0
movl 0x14(%rsp), %edi
movq 0x8(%rsp), %rsi
movq 0x8088(%rsp), %rdx
shlq $0x0, %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x78c0
movq 0x8(%rsp), %rcx
movq 0x9090(%rsp), %rax
movq (%rax), %rax
movq %rcx, (%rax)
movl 0x14(%rsp), %edi
callq 0x2150
cmpl $0x0, %eax
je 0x3bf9
leaq 0x10f25(%rip), %rdi     # 0x14b19
callq 0x3ce0
movl $0x0, 0x4(%rsp)
movl 0x4(%rsp), %eax
movq 0x9090(%rsp), %rcx
movl 0x20(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x3c66
movq 0x9090(%rsp), %rax
movq (%rax), %rax
movslq 0x4(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
movq 0x9090(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x4(%rsp), %rcx
addq (%rax,%rcx,8), %rdx
movq 0x9090(%rsp), %rax
movq (%rax), %rax
movl 0x4(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x3c01
movq 0x9090(%rsp), %rdi
callq 0x12500
movq 0x9090(%rsp), %rax
testb $0x1, 0x69(%rax)
je 0x3cbb
movq 0x9090(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rdi
movq 0x9090(%rsp), %rax
movq 0x28(%rax), %rsi
shlq $0x0, %rsi
callq 0x2280
movl %eax, (%rsp)
cmpl $0x0, (%rsp)
je 0x3cb9
leaq 0x10d36(%rip), %rdi     # 0x149ea
callq 0x3ce0
jmp 0x3cbb
movb $0x1, 0x90a7(%rsp)
movb 0x90a7(%rsp), %al
andb $0x1, %al
addq $0x90a8, %rsp           # imm = 0x90A8
retq
nopw %cs:(%rax,%rax)