// File: /workspace/github_c_makefile_star2_O1/felipelouza[P]egap/mergegap.c
// Function: init_arrays
static void init_arrays(g_data *g)
{
  customInt i=0; // position inside Z newZ and B  
  FILE *fnewmerge=NULL, *fmerge=NULL;
  if(g->extMem) {
    fnewmerge = fopen(g->newmerge_fname,"wb");
    if(!fnewmerge) die("mergegap:init_arrays:fnewmerge open");
    fmerge = fopen(g->merge_fname,"wb");
    if(!fmerge) die("mergegap:init_arrays:fmerge open");
  }
  for(int j=0;j<g->sizeOfAlpha;j++) {
    if(!g->lcpMerge) tba_or_m(g->bitB,i,1);
    else g->blockBeginsAt[i]=1;  // start of symbol j, correct lcp is 0
    g->firstColumn[j] = i;  // symbol j starts at position i
    for(int b=0;b<g->numBwt;b++) {
      for(customInt t=0;t<g->bwtOcc[b][j];t++) { 
        if(j==0) { // zero chars are all different, Z, Znew never change 
          if(!g->lcpMerge) tba_or_m(g->bitB,i,1);
          else g->blockBeginsAt[i]=1;
           //for 0-chars write b also to newmerge aka newZ 
          if(g->extMem)  fwrite_color(b,fnewmerge);
          else           g->newMergeColor[i]=b;          
        }
        // all colors are written to merge aka Z
        if(g->extMem) fwrite_color(b,fmerge);
        else g->mergeColor[i] = b;
        i++;
      } // end for t
    } // end for b 
  } // end for j
  assert(i==g->mergeLen); 
  // extra check on mergeColor, can be commented out
  if(g->extMem) {
    assert(ftell(fmerge)==g->mergeLen*sizeof(palette));
    if(fclose(fmerge)!=0) die("init_arrays:fmerge close"); 
    if(fclose(fnewmerge)!=0) die("mergegap:init_arrays:fnewmerge close"); 
  }
  else {
    #ifndef NDEBUG
    customInt cnt[MAX_NUMBER_OF_BWTS] = {0};
    for(i=0;i<g->mergeLen;i++) cnt[g->mergeColor[i]]++;
    for(int i=0; i<g->numBwt; i++)
      assert(cnt[i]==g->bwtLen[i]);
    #endif
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r14
cmpb $0x1, 0x69(%rdi)
jne 0xaf3f
movq 0x50e0(%r14), %rdi
leaq 0x3a35(%rip), %rsi      # 0xe92e
callq 0x21e0
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0xaf14
leaq 0x523d(%rip), %rdi      # 0x1014c
callq 0x3973
movq 0x50d8(%r14), %rdi
leaq 0x3a0c(%rip), %rsi      # 0xe92e
callq 0x21e0
movq %rax, 0x8(%rsp)
testq %rax, %rax
jne 0xaf51
leaq 0x5238(%rip), %rdi      # 0x10170
callq 0x3973
jmp 0xaf48
movq $0x0, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
cmpl $0x0, 0x38(%r14)
jle 0xb08e
xorl %r13d, %r13d
xorl %ebp, %ebp
cmpb $0x0, 0x3d(%r14)
je 0xaf75
movq 0x98(%r14), %rax
movb $0x1, (%rax,%rbp)
jmp 0xaf96
movq 0x98(%r14), %rax
movq %rbp, %rdx
leal (,%rbp,2), %ecx
movl $0x1, %esi
shlq %cl, %rsi
shrq $0x5, %rdx
orq %rsi, (%rax,%rdx,8)
movq 0x5108(%r14), %rax
movq %rbp, (%rax,%r13,8)
cmpl $0x0, 0x20(%r14)
jle 0xb07e
xorl %r12d, %r12d
movq 0x18(%r14), %rax
movq (%rax,%r12,8), %rax
cmpq $0x0, (%rax,%r13,8)
je 0xb06e
leaq (,%rbp,2), %rbx
xorl %r15d, %r15d
testq %r13, %r13
jne 0xb02e
cmpb $0x0, 0x3d(%r14)
je 0xafea
movq 0x98(%r14), %rax
addq %rbp, %rax
movb $0x1, (%r15,%rax)
jmp 0xb00a
leaq (%r15,%rbp), %rax
movq 0x98(%r14), %rdx
movl %ebx, %ecx
andb $0x3e, %cl
movl $0x1, %esi
shlq %cl, %rsi
shrq $0x5, %rax
orq %rsi, (%rdx,%rax,8)
cmpb $0x1, 0x69(%r14)
jne 0xb020
movl %r12d, %edi
movq 0x10(%rsp), %rsi
callq 0xbcb2
jmp 0xb02e
movq 0x50f0(%r14), %rax
addq %rbp, %rax
movb %r12b, (%r15,%rax)
cmpb $0x1, 0x69(%r14)
jne 0xb044
movl %r12d, %edi
movq 0x8(%rsp), %rsi
callq 0xbcb2
jmp 0xb052
movq 0x50e8(%r14), %rax
addq %rbp, %rax
movb %r12b, (%r15,%rax)
incq %r15
movq 0x18(%r14), %rax
movq (%rax,%r12,8), %rax
addq $0x2, %rbx
cmpq (%rax,%r13,8), %r15
jb 0xafcd
addq %r15, %rbp
incq %r12
movslq 0x20(%r14), %rax
cmpq %rax, %r12
jl 0xafaf
incq %r13
movslq 0x38(%r14), %rax
cmpq %rax, %r13
jl 0xaf61
cmpb $0x0, 0x69(%r14)
je 0xb0d7
movq 0x8(%rsp), %rdi
callq 0x20e0
testl %eax, %eax
je 0xb0af
leaq 0x50e7(%rip), %rdi      # 0x10191
callq 0x3973
movq 0x10(%rsp), %rdi
callq 0x20e0
testl %eax, %eax
je 0xb0d7
leaq 0x50e6(%rip), %rdi      # 0x101aa
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3973
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq