// File: /workspace/github_c_makefile_star2_O3/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 0x3092
testq %rax, %rax
je 0x2a5e
movq %rax, %rbx
movl %ebp, 0x4(%rsp)
movslq %r13d, %rbp
leaq 0x10(,%rbp,8), %rdi
callq 0x3092
movq %rax, (%rbx)
testq %rax, %rax
je 0x2a56
testl %r13d, %r13d
jle 0x292e
movl %r13d, %ecx
incl %r13d
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl %r13d
shlq $0x4, %r13
movdqa 0x1c49(%rip), %xmm1     # 0x4500
xorl %ecx, %ecx
movdqa 0x1c4f(%rip), %xmm2     # 0x4510
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x1c4f(%rip), %xmm4     # 0x4520
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %edx
notl %edx
testb $0x1, %dl
je 0x290a
movq $0x0, (%rax,%rcx)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %edx
testb $0x1, %dl
je 0x2921
movq $0x0, 0x8(%rax,%rcx)
paddq %xmm4, %xmm1
addq $0x10, %rcx
cmpq %rcx, %r13
jne 0x28d1
movl $0x0, 0x8(%rbx)
movq %rbx, %r13
addq $0xc, %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x1070
leaq 0x1a4d(%rip), %rsi      # 0x439b
movq %r13, %rdi
callq 0x1160
movq %rax, 0x200(%rbx)
testq %rax, %rax
je 0x2a7d
movq %r14, %xmm0
punpckldq 0x169d(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x16a5(%rip), %xmm0     # 0x4020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x1bb1(%rip), %xmm1     # 0x4540
mulsd 0x1bb1(%rip), %xmm1     # 0x4548
cvttsd2si %xmm1, %rax
movq %rax, %rcx
subsd 0x1ba9(%rip), %xmm1     # 0x4550
sarq $0x3f, %rcx
cvttsd2si %xmm1, %rdi
andq %rcx, %rdi
orq %rax, %rdi
movq %rdi, 0x228(%rbx)
shlq $0x4, %rdi
addq $0x10, %rdi
callq 0x3092
movq %rax, 0x208(%rbx)
testq %rax, %rax
je 0x2a82
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 0x1606(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x160e(%rip), %xmm0     # 0x4020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x1b32(%rip), %xmm1     # 0x4558
mulsd 0x1b1a(%rip), %xmm1     # 0x4548
cvttsd2si %xmm1, %rax
movq %rax, %rcx
subsd 0x1b12(%rip), %xmm1     # 0x4550
cvttsd2si %xmm1, %rdx
sarq $0x3f, %rcx
andq %rcx, %rdx
orq %rax, %rdx
movq %rdx, 0x220(%rbx)
jmp 0x2a6b
movq %rbx, %rdi
callq 0x3149
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