// File: /workspace/github_c_makefile_star2_O1/felipelouza[P]egap/tools/heap.c
// Function: heap_alloc
heap* heap_alloc(int heap_size, char* file_name, int level, int pos_size, int lcp_size, size_t RAM) {
   
  heap* h = (heap*) malloc(sizeof(heap));

  if (!h) {
    errno = ENOMEM;
    return 0;
  }
  
  h->heap = (heap_node**) malloc((heap_size+2)*sizeof(heap_node*));
  if (!h->heap) {
    free(h);
    errno = ENOMEM;
    return 0;
  }
  
  int i;
  for(i=0; i<heap_size; i++)
    h->heap[i] = NULL;
   
  h->size = 0;
  
  //input file for all heap nodes
  strcpy(h->file_name, file_name);
 
  h->f_in = fopen(h->file_name, "rb"); 
  if (!h->f_in) {perror ("fopen(heap_alloc)");  exit(EXIT_FAILURE);}

//h->output_size = OUTPUT_SIZE;
  h->output_size = 0.3*RAM/sizeof(pair);

  #if OUTPUT_BUFFER
    h->out_buffer = (pair*) malloc((h->output_size+1)*sizeof(pair));
    if(!h->out_buffer) {perror("malloc(heap_alloc)");   exit(EXIT_FAILURE);}
    h->out_idx = 0;
  #endif

  h->pos_size = pos_size;
  h->lcp_size = lcp_size;

  h->input_size = 0.7*(RAM/heap_size)/sizeof(pair);
//h->input_size = INPUT_SIZE;

  return h;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %r9, %r14
movl %r8d, %ebp
movl %ecx, %r15d
movq %rsi, %r12
movl %edi, %r13d
movl $0x230, %edi            # imm = 0x230
callq 0x2fb6
testq %rax, %rax
je 0x29cd
movq %rax, %rbx
movl %ebp, 0x4(%rsp)
movslq %r13d, %rbp
leaq 0x10(,%rbp,8), %rdi
callq 0x2fb6
movq %rax, (%rbx)
testq %rax, %rax
je 0x29c5
testl %r13d, %r13d
jle 0x289d
movl %r13d, %eax
xorl %ecx, %ecx
movq (%rbx), %rdx
movq $0x0, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x288a
movl $0x0, 0x8(%rbx)
movq %rbx, %r13
addq $0xc, %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x1070
leaq 0x1ade(%rip), %rsi      # 0x439b
movq %r13, %rdi
callq 0x1160
movq %rax, 0x200(%rbx)
testq %rax, %rax
je 0x29ec
movq %r14, %xmm0
punpckldq 0x172e(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1736(%rip), %xmm0     # 0x4020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x1c12(%rip), %xmm1     # 0x4510
mulsd 0x1c12(%rip), %xmm1     # 0x4518
cvttsd2si %xmm1, %rax
movq %rax, %rcx
subsd 0x1c0a(%rip), %xmm1     # 0x4520
sarq $0x3f, %rcx
cvttsd2si %xmm1, %rdi
andq %rcx, %rdi
orq %rax, %rdi
movq %rdi, 0x228(%rbx)
shlq $0x4, %rdi
addq $0x10, %rdi
callq 0x2fb6
movq %rax, 0x208(%rbx)
testq %rax, %rax
je 0x29f1
movl $0x0, 0x210(%rbx)
movl %r15d, 0x214(%rbx)
movl 0x4(%rsp), %eax
movl %eax, 0x218(%rbx)
movq %r14, %rax
xorl %edx, %edx
divq %rbp
movq %rax, %xmm0
punpckldq 0x1697(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x169f(%rip), %xmm0     # 0x4020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x1b93(%rip), %xmm1     # 0x4528
mulsd 0x1b7b(%rip), %xmm1     # 0x4518
cvttsd2si %xmm1, %rax
movq %rax, %rcx
subsd 0x1b73(%rip), %xmm1     # 0x4520
cvttsd2si %xmm1, %rdx
sarq $0x3f, %rcx
andq %rcx, %rdx
orq %rax, %rdx
movq %rdx, 0x220(%rbx)
jmp 0x29da
movq %rbx, %rdi
callq 0x306d
callq 0x1050
movl $0xc, (%rax)
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x12f3
callq 0x12de