// File: /workspace/github_cpp_makefile_star2_O1/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 0x74b5(%rip), %rsi      # 0xa699
callq 0x853c
testb %al, %al
je 0x31fe
leaq 0x6e7a(%rip), %rsi      # 0xa06e
movq %r15, %rdi
callq 0x4b1e
jmp 0x3239
leaq 0x76f8(%rip), %rsi      # 0xa8fd
movq %r15, %rdi
callq 0x853c
testb %al, %al
je 0x323e
leaq 0x6e59(%rip), %rsi      # 0xa071
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2060
leaq 0x6e05(%rip), %rsi      # 0xa036
movq %r14, %rdi
callq 0x2080
movq %rax, %r14
jmp 0x3241
xorl %r14d, %r14d
movq %r15, 0x18(%rsp)
testq %r14, %r14
je 0x33fc
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 0xbd64(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x6e0a(%rip), %rsi      # 0xa090
movl %ebp, %edx
xorl %eax, %eax
callq 0x2230
movl %ebp, %edi
callq 0x2fbc
movq %rax, %r15
movl %ebp, 0x14(%rsp)
testl %ebp, %ebp
jle 0x33aa
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 0x334d
movslq %r12d, %rbp
xorl %ebx, %ebx
btl %ebx, %r13d
jae 0x333d
testb $0x20, %r13b
jne 0x3312
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 0x333d
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 0x32db
movl $0x1, %eax
jmp 0x3379
movl $0x0, 0x10(%rsp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0x10(%rsp), %rdi
movq %r14, %rcx
callq 0x2240
cmpb $0x1, 0xbe1e(%rip)      # 0xf191
je 0x3389
movl 0x10(%rsp), %eax
addl %eax, %r12d
cmpl 0x14(%rsp), %r12d
jle 0x32ab
jmp 0x33aa
movq 0xbc50(%rip), %rax      # 0xefe0
movq (%rax), %rdi
movl 0x10(%rsp), %edx
leaq 0x6d0f(%rip), %rsi      # 0xa0ad
movl %r12d, %ecx
xorl %eax, %eax
callq 0x2230
jmp 0x3375
leaq 0x72e8(%rip), %rsi      # 0xa699
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x853c
testb %al, %al
je 0x33cc
movq %r14, %rdi
callq 0x2150
jmp 0x33e7
leaq 0x752a(%rip), %rsi      # 0xa8fd
movq %rbx, %rdi
callq 0x853c
testb %al, %al
je 0x33e7
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 0x18(%rsp), %rdi
callq 0x2ba0