// File: /workspace/github_c_makefile_star2_O1/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, 0xc(%rsp)
movl %r8d, %r14d
movq %rdx, 0x10(%rsp)
movq %rsi, %r15
movq %rdi, %r13
leaq 0x2f28(%rip), %rdi      # 0x4440
callq 0x1080
movl 0x218(%r13), %eax
addl 0x214(%r13), %eax
shll $0x3, %eax
movl $0x80, %ecx
subl %eax, %ecx
movq $-0x1, %rsi
movq $-0x1, %rax
shrq %cl, %rax
xorl %edi, %edi
movl %ecx, 0x18(%rsp)
testb $0x40, %cl
cmovneq %rax, %rsi
cmoveq %rax, %rdi
movq (%r13), %rax
movq (%rax), %rax
movq (%rax), %rcx
movslq 0x8(%rax), %rax
shlq $0x4, %rax
movq 0x8(%rcx,%rax), %rdx
movq %rdi, 0x40(%rsp)
xorq %rdi, %rdx
movq (%rcx,%rax), %rax
movq %rsi, 0x20(%rsp)
xorq %rsi, %rax
orq %rdx, %rax
jne 0x15e5
testl %r14d, %r14d
je 0x15c5
xorl %edx, %edx
movq $-0x1, %rax
movl 0x18(%rsp), %ecx
shrdq %cl, %rdx, %rax
testb $0x40, %cl
cmovneq %rdx, %rax
movq %r13, %rdi
movq %r15, %rsi
movq 0x20(%rsp), %rdx
movq %rax, %rcx
movl $0x1, %r8d
callq 0x2f4f
movq 0x10(%rsp), %rax
incq (%rax)
leaq 0x2e73(%rip), %rdi      # 0x443f
callq 0x1080
movl $0x1, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movslq 0xc(%rsp), %rax
movq %rax, 0x28(%rsp)
xorl %ebx, %ebx
movl %r14d, 0x1c(%rsp)
movq %r13, %rdi
callq 0x2ef6
movq %rax, %rbp
movq %rdx, %r12
testl %r14d, %r14d
je 0x167b
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movl %r14d, %r8d
callq 0x2f4f
movq 0x10(%rsp), %rax
incq (%rax)
movslq 0x218(%r13), %rax
testl %r14d, %r14d
je 0x1719
leal (,%rax,8), %ecx
movq %rbp, %rdi
shrdq %cl, %r12, %rdi
shlq $0x4, %rax
leaq 0x2a32(%rip), %rdx      # 0x4080
movq (%rax,%rdx), %rsi
movq %r12, %rdx
shrq %cl, %rdx
andq %rbp, %rsi
testb $0x40, %cl
cmoveq %rdi, %rdx
leaq 0x2ab1(%rip), %rdi      # 0x411a
movq %rbp, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x10d0
jmp 0x173c
movslq 0x218(%r13), %rax
leal (,%rax,8), %ecx
movq %rbp, %rdx
shrdq %cl, %r12, %rdx
movq %r12, %rsi
shrq %cl, %rsi
testb $0x40, %cl
cmoveq %rdx, %rsi
cmpl $0x0, 0xc(%rsp)
je 0x1609
leaq -0x1(%rsi), %rcx
cmpq %rcx, %rbx
jge 0x176f
movq %rbp, 0x38(%rsp)
shlq $0x4, %rax
movq %r13, %r14
movq %r15, %r13
movq 0x10(%rsp), %r15
leaq 0x29b0(%rip), %rcx      # 0x4080
movq (%rax,%rcx), %rbp
andq 0x28(%rsp), %rbp
notq %rbx
movq %rsi, 0x30(%rsp)
addq %rsi, %rbx
movq %r14, %rdi
movq %r13, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2f4f
incq (%r15)
decq %rbx
jne 0x16e4
movq 0x30(%rsp), %rbx
movq %r13, %r15
movq %r14, %r13
movl 0x1c(%rsp), %r14d
movq 0x38(%rsp), %rbp
jmp 0x1609
shll $0x3, %eax
movl %eax, %ecx
shrdq %cl, %r12, %rbp
shrq %cl, %r12
testb $0x40, %al
cmoveq %rbp, %r12
leaq 0x29fb(%rip), %rdi      # 0x412d
movq %r12, %rsi
xorl %eax, %eax
callq 0x10d0
movq (%r13), %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 0x20(%rsp), %rax
orq %rdx, %rax
jne 0x15f6
jmp 0x1587
movq %rsi, %rbx
jmp 0x1609