// File: /workspace/github_cpp_makefile_star2_O2/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 0x7075(%rip), %rsi      # 0x9689
callq 0x7320
testb %al, %al
je 0x262e
leaq 0x6a3a(%rip), %rsi      # 0x905e
movq %r15, %rdi
callq 0x471a
jmp 0x266d
leaq 0x72b8(%rip), %rsi      # 0x98ed
movq %r15, %rdi
callq 0x7320
testb %al, %al
je 0x280d
leaq 0x6a15(%rip), %rsi      # 0x9061
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2060
leaq 0x69c1(%rip), %rsi      # 0x9026
movq %r14, %rdi
callq 0x2090
movq %rax, %r14
testq %rax, %rax
je 0x280d
movq %r15, 0x18(%rsp)
leaq 0x20(%rsp), %r15
andl $0x0, (%r15)
pushq $0x4
popq %rsi
pushq $0x1
popq %r13
movq %r15, %rdi
movq %r13, %rdx
movq %r14, %rcx
callq 0x2250
movl (%r15), %ebp
movl %ebp, (%rbx)
movq 0xc938(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x69ce(%rip), %rsi      # 0x9080
movl %ebp, %edx
xorl %eax, %eax
callq 0x2240
movl %ebp, 0x10(%rsp)
movl %ebp, %edi
callq 0x23fc
movq %rax, %r15
pushq $0x1
popq %rbp
cmpl 0x10(%rsp), %ebp
jg 0x27bb
movb $0x0, 0xb(%rsp)
leaq 0xb(%rsp), %rdi
movq %r13, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x2250
movzbl 0xb(%rsp), %ebx
cmpl $0xff, %ebx
je 0x2771
movl %ebp, 0x14(%rsp)
movslq %ebp, %rbp
xorl %r12d, %r12d
cmpq $0x5, %r12
je 0x2768
btl %r12d, %ebx
jae 0x2763
testb $0x20, %bl
jne 0x273d
movb $0x0, 0xc(%rsp)
leaq 0xc(%rsp), %rdi
movq %r13, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x2250
movzbl 0xc(%rsp), %eax
movq (%r15,%r12,8), %rcx
movw %ax, (%rcx,%rbp,2)
jmp 0x2763
andw $0x0, 0xc(%rsp)
leaq 0xc(%rsp), %rdi
pushq $0x2
popq %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x2250
movzwl 0xc(%rsp), %eax
movq (%r15,%r12,8), %rcx
movw %ax, (%rcx,%rbp,2)
incq %r12
jmp 0x2705
pushq $0x1
popq %rbx
movl 0x14(%rsp), %ebp
jmp 0x2796
andl $0x0, 0xc(%rsp)
leaq 0xc(%rsp), %rdi
pushq $0x4
popq %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x2250
movl 0xc(%rsp), %ebx
cmpb $0x1, 0xca05(%rip)      # 0xf199
je 0x279d
addl %ebx, %ebp
jmp 0x26cc
movq 0xc83c(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x68ef(%rip), %rsi      # 0x909d
movl %ebx, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2240
jmp 0x2796
leaq 0x6ec7(%rip), %rsi      # 0x9689
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x7320
testb %al, %al
je 0x27dd
movq %r14, %rdi
callq 0x2160
jmp 0x27f8
leaq 0x7109(%rip), %rsi      # 0x98ed
movq %rbx, %rdi
callq 0x7320
testb %al, %al
je 0x27f8
movq %r14, %rdi
callq 0x2230
movq %r15, %rax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xc7cc(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x684f(%rip), %rsi      # 0x906d
movq %r15, %rdx
xorl %eax, %eax
callq 0x2240
xorl %edi, %edi
callq 0x21c0