// File: /workspace/github_cpp_makefile_star2_O3/akdess[P]BAFExtract/src/main.cpp
// Function: load_compressed_pileups(char*, int&)
unsigned short** load_compressed_pileups(char* cur_comp_allele_fp, int& l_pileup)
{
  FILE* f_comp = NULL;
  if(t_string::ends_with(cur_comp_allele_fp, ".bin"))
  {
    f_comp = open_f(cur_comp_allele_fp, "rb");
  }
  else if(t_string::ends_with(cur_comp_allele_fp, ".bin.gz"))
  {
    char ungzip_cmd[1000];
    sprintf(ungzip_cmd, "gzip -cd %s", cur_comp_allele_fp);
#ifdef WIN32
    f_comp = _popen(ungzip_cmd, "r");
#else 
    f_comp = popen(ungzip_cmd, "r");
#endif
  }

  if(f_comp == NULL)
  {
    fprintf(stderr, "Could not open %s\n", cur_comp_allele_fp);
    exit(0);
  }

  int l_read_sig = 0;
  fread(&l_read_sig, sizeof(int), 1, f_comp);
  int l_sig = l_read_sig;

  l_pileup = l_sig;

  fprintf(stderr, "Loading pileup of length %d\n", l_sig);

  unsigned short** loaded_pileup = allocate_pileup(l_sig);

  // Go over all the positions.
  int i = 1;
  while(i <= l_sig)
  {
    // Read the existence flag.
    unsigned char existence_flag = 0;
    fread(&existence_flag, sizeof(unsigned char), 1, f_comp);


    // Check an RLE case.
    if(existence_flag == 0xFF)
    {
      unsigned int l_RLE = 0;
      fread(&l_RLE, sizeof(unsigned int), 1, f_comp);

if(__DUMP_MAPPED_READ_TOOLS_MSGS__)
      fprintf(stderr, "Loading RLE of length %d @ %d\n", l_RLE, i);

      // When we add this, we move upto the location where a 0-run ends.
      i += l_RLE;
    } // RLE check.
    else
    {
      // Read the first byte, parse the data range and which alleles exist.
      unsigned char drange_flag = ((existence_flag & (1 << 5)) >> 5);

      for(int allele_i = 0; allele_i < 5; allele_i++)
      {
        // Do we have an entry here?
        if((existence_flag & (1 << allele_i)) > 0)
        {
          if(drange_flag == 1)
          { 
            unsigned short current_count_short = 0;
            fread(&(current_count_short), sizeof(unsigned short), 1, f_comp);
            loaded_pileup[allele_i][i] = current_count_short;
          }
          else
          {
            unsigned char current_count_char = 0;
            fread(&(current_count_char), sizeof(unsigned char), 1, f_comp);
            loaded_pileup[allele_i][i] = (unsigned short)current_count_char;
          }
        } // count check for current position.
      } // allele_i loop.

      i++;
    }
  } // i loop.

  if(t_string::ends_with(cur_comp_allele_fp, ".bin"))
  {
    fclose(f_comp);
  }
  else if(t_string::ends_with(cur_comp_allele_fp, ".bin.gz"))
  {
#ifdef WIN32
    _pclose(f_comp);
#else 
    pclose(f_comp);
#endif
  }

  // Free memory.
  return(loaded_pileup);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x408, %rsp            # imm = 0x408
movq %rsi, %rbx
movq %rdi, %r15
leaq 0x7466(%rip), %rsi      # 0xa699
callq 0x848a
testb %al, %al
je 0x324d
leaq 0x6e2b(%rip), %rsi      # 0xa06e
movq %r15, %rdi
callq 0x4b65
jmp 0x328c
leaq 0x76a9(%rip), %rsi      # 0xa8fd
movq %r15, %rdi
callq 0x848a
testb %al, %al
je 0x344b
leaq 0x6e06(%rip), %rsi      # 0xa071
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2060
leaq 0x6db2(%rip), %rsi      # 0xa036
movq %r14, %rdi
callq 0x2080
movq %rax, %r14
testq %rax, %rax
je 0x344b
movq %r15, 0x18(%rsp)
leaq 0x20(%rsp), %r15
movl $0x0, (%r15)
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x2240
movl (%r15), %ebp
movl %ebp, (%rbx)
movq 0xbd16(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x6dbc(%rip), %rsi      # 0xa090
movl %ebp, %edx
xorl %eax, %eax
callq 0x2230
movl %ebp, %edi
callq 0x2ffc
movq %rax, %r15
movl %ebp, 0x14(%rsp)
testl %ebp, %ebp
jle 0x33f9
movl $0x1, %r12d
movb $0x0, 0xf(%rsp)
movl $0x1, %esi
movl $0x1, %edx
leaq 0xf(%rsp), %rdi
movq %r14, %rcx
callq 0x2240
movzbl 0xf(%rsp), %r13d
cmpl $0xff, %r13d
je 0x339c
movslq %r12d, %rbp
xorl %ebx, %ebx
btl %ebx, %r13d
jae 0x338b
testb $0x20, %r13b
jne 0x3360
movb $0x0, 0x10(%rsp)
movl $0x1, %esi
movl $0x1, %edx
leaq 0x10(%rsp), %rdi
movq %r14, %rcx
callq 0x2240
movzbl 0x10(%rsp), %eax
movq (%r15,%rbx,8), %rcx
movw %ax, (%rcx,%rbp,2)
jmp 0x338b
movw $0x0, 0x10(%rsp)
movl $0x2, %esi
movl $0x1, %edx
leaq 0x10(%rsp), %rdi
movq %r14, %rcx
callq 0x2240
movzwl 0x10(%rsp), %eax
movq (%r15,%rbx,8), %rcx
movw %ax, (%rcx,%rbp,2)
incq %rbx
cmpq $0x5, %rbx
jne 0x3329
movl $0x1, %r13d
jmp 0x33c9
movl $0x0, 0x10(%rsp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0x10(%rsp), %rdi
movq %r14, %rcx
callq 0x2240
movl 0x10(%rsp), %r13d
cmpb $0x1, 0xbdca(%rip)      # 0xf191
je 0x33d9
addl %r13d, %r12d
cmpl 0x14(%rsp), %r12d
jle 0x32f9
jmp 0x33f9
movq 0xbc00(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x6cc3(%rip), %rsi      # 0xa0ad
movl %r13d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x2230
jmp 0x33c9
leaq 0x7299(%rip), %rsi      # 0xa699
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x848a
testb %al, %al
je 0x341b
movq %r14, %rdi
callq 0x2150
jmp 0x3436
leaq 0x74db(%rip), %rsi      # 0xa8fd
movq %rbx, %rdi
callq 0x848a
testb %al, %al
je 0x3436
movq %r14, %rdi
callq 0x2220
movq %r15, %rax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x2bd6