// File: /workspace/github_c_makefile_star2_O2/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 0x2d64
testq %rax, %rax
je 0x276a
movq %rax, %rbx
movl %ebp, 0x4(%rsp)
movslq %r13d, %rbp
leaq 0x10(,%rbp,8), %rdi
callq 0x2d64
movq %rax, (%rbx)
testq %rax, %rax
je 0x2762
xorl %ecx, %ecx
testl %r13d, %r13d
cmovlel %ecx, %r13d
cmpq %rcx, %r13
je 0x2640
andq $0x0, (%rax,%rcx,8)
incq %rcx
jmp 0x2631
andl $0x0, 0x8(%rbx)
movq %rbx, %r13
addq $0xc, %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x1070
leaq 0x1d3e(%rip), %rsi      # 0x439b
movq %r13, %rdi
callq 0x1160
movq %rax, 0x200(%rbx)
testq %rax, %rax
je 0x2789
movq %r14, %xmm0
punpckldq 0x198e(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1996(%rip), %xmm0     # 0x4020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x1e72(%rip), %xmm1     # 0x4510
mulsd 0x1e72(%rip), %xmm1     # 0x4518
cvttsd2si %xmm1, %rax
movq %rax, %rcx
subsd 0x1e6a(%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 0x2d64
movq %rax, 0x208(%rbx)
testq %rax, %rax
je 0x2792
andl $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 0x18fa(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1902(%rip), %xmm0     # 0x4020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x1df6(%rip), %xmm1     # 0x4528
mulsd 0x1dde(%rip), %xmm1     # 0x4518
cvttsd2si %xmm1, %rax
movq %rax, %rcx
subsd 0x1dd6(%rip), %xmm1     # 0x4520
cvttsd2si %xmm1, %rdx
sarq $0x3f, %rcx
andq %rcx, %rdx
orq %rax, %rdx
movq %rdx, 0x220(%rbx)
jmp 0x2777
movq %rbx, %rdi
callq 0x2e3a
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
leaq 0x1da0(%rip), %rdi      # 0x4530
jmp 0x2799
leaq 0x1da9(%rip), %rdi      # 0x4542
callq 0x1170
pushq $0x1
popq %rdi
callq 0x11c0