// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
leaq 0x3e5a(%rip), %rdi      # 0x51e4
xorl %eax, %eax
movl %eax, -0xc4(%rbp)
callq 0x10d0
movl -0xc4(%rbp), %edx
movq 0x3e0c(%rip), %rdi      # 0x51b0
movq -0x8(%rbp), %rcx
movl 0x214(%rcx), %eax
movl 0x218(%rcx), %ecx
addl %ecx, %eax
movb $0x10, %sil
subb %al, %sil
shlb $0x3, %sil
movq $-0x1, %rax
movb %sil, %cl
shrdq %cl, %rdi, %rax
movb %sil, %cl
shrq %cl, %rdi
movq %rdi, %rcx
testb $0x40, %sil
cmovneq %rcx, %rax
cmovneq %rdx, %rcx
movq %rcx, -0x38(%rbp)
movq %rax, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rcx
movq (%rcx), %rax
movslq 0x8(%rcx), %rcx
shlq $0x4, %rcx
movaps (%rax,%rcx), %xmm0
movaps -0x40(%rbp), %xmm1
pcmpeqb %xmm1, %xmm0
pmovmskb %xmm0, %eax
subl $0xffff, %eax           # imm = 0xFFFF
je 0x164e
jmp 0x1425
movq -0x8(%rbp), %rdi
callq 0x3b10
movq %rax, -0x70(%rbp)
movq %rdx, -0x68(%rbp)
movq -0x70(%rbp), %rax
movq -0x68(%rbp), %rcx
movq %rcx, -0x58(%rbp)
movq %rax, -0x60(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x152f
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rsi
movq -0x8(%rbp), %rcx
movb 0x218(%rcx), %dl
shlb $0x3, %dl
movb %dl, %cl
movq %rsi, %rdi
shrq %cl, %rdi
movq %rdi, -0xd0(%rbp)
movb %dl, %cl
shrdq %cl, %rsi, %rax
movq -0xd0(%rbp), %rcx
testb $0x40, %dl
cmovneq %rcx, %rax
movq %rax, -0x48(%rbp)
cmpl $0x0, -0x28(%rbp)
je 0x152d
movslq -0x28(%rbp), %rax
movq -0x8(%rbp), %rcx
movslq 0x218(%rcx), %rcx
shlq $0x4, %rcx
leaq 0x3c80(%rip), %rdx      # 0x5130
movq (%rcx,%rdx), %rcx
andq %rcx, %rax
movq %rax, -0x80(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jge 0x1525
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x80(%rbp), %rax
movq -0x78(%rbp), %rcx
movl -0x24(%rbp), %r8d
movq %rcx, -0x88(%rbp)
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdx
movq -0x88(%rbp), %rcx
callq 0x3bc0
movq -0x18(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x14c3
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x152f
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
movl -0x24(%rbp), %r8d
movq %rcx, -0x98(%rbp)
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdx
movq -0x98(%rbp), %rcx
callq 0x3bc0
movq -0x18(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
cmpl $0x0, -0x24(%rbp)
je 0x1603
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %r9
movq %r9, -0xd8(%rbp)
movq -0x8(%rbp), %rcx
movslq 0x218(%rcx), %rcx
movq %rcx, %rdx
shlq $0x4, %rdx
leaq 0x3b8c(%rip), %rsi      # 0x5130
movq (%rdx,%rsi), %rdx
movq %rax, %rsi
andq %rdx, %rsi
movb %cl, %r8b
shlb $0x3, %r8b
movb %r8b, %cl
movq %r9, %rdi
shrq %cl, %rdi
movb %r8b, %cl
movq %rax, %rdx
shrdq %cl, %r9, %rdx
movq -0xd8(%rbp), %rcx
testb $0x40, %r8b
cmovneq %rdi, %rdx
movq %rcx, -0xa8(%rbp)
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rcx
movq -0xa8(%rbp), %r8
leaq 0x3bd0(%rip), %rdi      # 0x51ca
movb $0x0, %al
callq 0x10d0
jmp 0x1649
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x8(%rbp), %rax
movb 0x218(%rax), %cl
shlb $0x3, %cl
movb %cl, -0xd9(%rbp)
movq %rdx, %rax
shrq %cl, %rax
movb -0xd9(%rbp), %cl
shrdq %cl, %rdx, %rsi
movb -0xd9(%rbp), %cl
testb $0x40, %cl
cmovneq %rax, %rsi
leaq 0x3b9b(%rip), %rdi      # 0x51dd
movb $0x0, %al
callq 0x10d0
jmp 0x13f3
cmpl $0x0, -0x24(%rbp)
je 0x1699
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rcx
movq %rcx, -0xb8(%rbp)
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rdx
movq -0xb8(%rbp), %rcx
movl $0x1, %r8d
callq 0x3bc0
movq -0x18(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
leaq 0x3b43(%rip), %rdi      # 0x51e3
movb $0x0, %al
callq 0x10d0
movl $0x1, %eax
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)