// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movl %edi, -0xc(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq %r9, -0x30(%rbp)
movl $0x230, %edi            # imm = 0x230
callq 0x3d60
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x30ca
callq 0x1040
movl $0xc, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0x3307
movl -0xc(%rbp), %eax
addl $0x2, %eax
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x3d60
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movq -0x38(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x3111
movq -0x38(%rbp), %rdi
callq 0x3ed0
callq 0x1040
movl $0xc, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0x3307
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x313e
movq -0x38(%rbp), %rax
movq (%rax), %rax
movslq -0x3c(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x3118
movq -0x38(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x38(%rbp), %rdi
addq $0xc, %rdi
movq -0x18(%rbp), %rsi
callq 0x1060
movq -0x38(%rbp), %rdi
addq $0xc, %rdi
leaq 0x2308(%rip), %rsi      # 0x5471
callq 0x1160
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x200(%rax)
movq -0x38(%rbp), %rax
cmpq $0x0, 0x200(%rax)
jne 0x31a0
leaq 0x2527(%rip), %rdi      # 0x56b8
callq 0x1170
movl $0x1, %edi
callq 0x11d0
movaps 0x1e89(%rip), %xmm1     # 0x5030
movq -0x30(%rbp), %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x1e88(%rip), %xmm1     # 0x5040
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x24c9(%rip), %xmm1     # 0x5698
mulsd %xmm1, %xmm0
movsd 0x24c5(%rip), %xmm1     # 0x56a0
mulsd %xmm1, %xmm0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
movsd 0x24b2(%rip), %xmm1     # 0x56a8
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
andq %rdx, %rax
orq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x228(%rax)
movq -0x38(%rbp), %rax
movq 0x228(%rax), %rdi
addq $0x1, %rdi
shlq $0x4, %rdi
callq 0x3d60
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x208(%rax)
movq -0x38(%rbp), %rax
cmpq $0x0, 0x208(%rax)
jne 0x325a
leaq 0x247f(%rip), %rdi      # 0x56ca
callq 0x1170
movl $0x1, %edi
callq 0x11d0
movq -0x38(%rbp), %rax
movl $0x0, 0x210(%rax)
movl -0x20(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x214(%rax)
movl -0x24(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x218(%rax)
movq -0x30(%rbp), %rax
movslq -0xc(%rbp), %rcx
xorl %edx, %edx
divq %rcx
movq %rax, %xmm0
movaps 0x1d95(%rip), %xmm1     # 0x5030
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x1d99(%rip), %xmm1     # 0x5040
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x23f2(%rip), %xmm1     # 0x56b0
mulsd %xmm1, %xmm0
movsd 0x23d6(%rip), %xmm1     # 0x56a0
mulsd %xmm1, %xmm0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
movsd 0x23c3(%rip), %xmm1     # 0x56a8
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
andq %rdx, %rax
orq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x220(%rax)
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)