// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]egap/tools/mergelcp.c
// Function: heap_sort_level
int heap_sort_level(heap *h, FILE *f_lcp, size_t *sum, char* c_file, int level, int k){
  
  #if CHECK == 2
    char c_pos[PATH_MAX];
    FILE* f_pos=NULL;
    if(!level){
      sprintf(c_pos, "%s.pos.lcp", c_file);
      f_pos = file_open(c_pos, "wb"); 
    }
  #endif
  
  #if DEBUG
    printf("**\n");
  #endif
  
  //pair sentinel; sentinel.pos=I_MAX; sentinel.lcp=I_MAX;
  pair sentinel; sentinel=MAX_KEY; //sentinel.lcp=I_MAX;
  int64_t pos, curr=0;

  while(key(0)!=sentinel){
  
    pair tmp = heap_delete_min(h);
  
    //complete LCP empty entries
    /**/
    if(level==0){
      pos=pos(tmp);
      if(k){
        pair aux=lcp(k); //k-truncated LCP-values
        for(;curr<pos-1;curr++){
          #if CHECK == 1
            fprintf(stderr,"%lu, %lu (lcp = %lu)\n", pos, curr, lcp(aux));
          #endif
          heap_write(h, f_lcp, aux, level);
          (*sum)++;
        }
        curr=pos;
      }
    }
    /**/

    heap_write(h, f_lcp, tmp, level);
    (*sum)++;
    #if CHECK == 2
      uint64_t out = pos(tmp);
      if(!level) fwrite(&out, h->pos_size, 1, f_pos);
    #endif    
    
    #if CHECK == 1
      if(!level){
        if(pos(tmp)!=curr){
          printf("%lu != %lu\n", pos(tmp), curr);
          printf("isNotSorted!!\n");
          return 0;
        }
        curr++;
      }
    #endif
    
    #if DEBUG
      if(level) printf("<%lu, %lu [%llu]> ", lcp(tmp), pos(tmp), tmp);
      else printf("%lu, ", pos(tmp));
    #endif
  }
  
  if(level){
    heap_write(h, f_lcp, sentinel, 1);
    (*sum)++;
  }
  
  #if DEBUG
    printf("\n**\n");
  #endif
  
  #if CHECK == 2
    if(!level) fclose(f_pos);
  #endif

return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r9d, %ebp
movl %r8d, %ebx
movq %rdx, 0x10(%rsp)
movq %rsi, %r14
movq %rdi, %r15
leaq 0x30fb(%rip), %rdi      # 0x4440
callq 0x1080
movl 0x218(%r15), %eax
addl 0x214(%r15), %eax
shll $0x3, %eax
movl $0x80, %ecx
subl %eax, %ecx
movl %ecx, 0x18(%rsp)
movzbl %cl, %edx
pushq $-0x1
popq %rdi
movq %rdi, %rsi
callq 0x30f0
movq %rax, 0x28(%rsp)
movq %rdx, 0x40(%rsp)
movl %ebp, 0x1c(%rsp)
movslq %ebp, %rax
movq %rax, 0x30(%rsp)
xorl %r13d, %r13d
movl %ebx, 0xc(%rsp)
movq (%r15), %rax
movq (%rax), %rax
movq (%rax), %rcx
movslq 0x8(%rax), %rax
shlq $0x4, %rax
movq 0x8(%rcx,%rax), %rdx
xorq 0x40(%rsp), %rdx
movq (%rcx,%rax), %rax
xorq 0x28(%rsp), %rax
orq %rdx, %rax
je 0x14f8
movq %r15, %rdi
callq 0x2c80
movq %rax, %r12
movq %rdx, %rbp
testl %ebx, %ebx
jne 0x145a
movslq 0x218(%r15), %rbx
leal (,%rbx,8), %eax
movzbl %al, %edx
movq %r12, 0x20(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x30f0
cmpl $0x0, 0x1c(%rsp)
je 0x1451
movq %rax, %r12
movq %rbp, 0x38(%rsp)
shlq $0x4, %rbx
leaq 0x2c71(%rip), %rax      # 0x4080
movq (%rbx,%rax), %rbp
andq 0x30(%rsp), %rbp
movq %r12, %rbx
decq %rbx
cmpq %rbx, %r13
jge 0x1443
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2cd6
movq 0x10(%rsp), %rax
incq (%rax)
incq %r13
jmp 0x141e
movq %r12, %r13
movl 0xc(%rsp), %ebx
movq 0x38(%rsp), %rbp
jmp 0x1455
movl 0xc(%rsp), %ebx
movq 0x20(%rsp), %r12
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %rbp, %rcx
movl %ebx, %r8d
callq 0x2cd6
movq 0x10(%rsp), %rax
incq (%rax)
movslq 0x218(%r15), %rax
testl %ebx, %ebx
je 0x14d1
movq %rax, %rcx
shlq $0x4, %rcx
leaq 0x2bf1(%rip), %rdx      # 0x4080
movq %rbp, %rbx
movq (%rcx,%rdx), %rbp
andq %r12, %rbp
leal (,%rax,8), %eax
movzbl %al, %edx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x30f0
leaq 0x2c65(%rip), %rdi      # 0x411a
movq %rbp, %rsi
movq %rax, %rdx
movq %r12, %rcx
movq %rbx, %r8
movl 0xc(%rsp), %ebx
xorl %eax, %eax
callq 0x10d0
jmp 0x1391
shll $0x3, %eax
movzbl %al, %edx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x30f0
leaq 0x2c44(%rip), %rdi      # 0x412d
movq %rax, %rsi
xorl %eax, %eax
callq 0x10d0
jmp 0x1391
testl %ebx, %ebx
je 0x152a
movzbl 0x18(%rsp), %edx
pushq $-0x1
popq %rdi
xorl %esi, %esi
callq 0x30f0
pushq $0x1
popq %r8
movq %r15, %rdi
movq %r14, %rsi
movq 0x28(%rsp), %rdx
movq %rax, %rcx
callq 0x2cd6
movq 0x10(%rsp), %rax
incq (%rax)
leaq 0x2f0e(%rip), %rdi      # 0x443f
callq 0x1080
pushq $0x1
popq %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq