// File: /workspace/github_cpp_makefile_star2_O3/akdess[P]BAFExtract/src/main.cpp
// Function: compress_nucleotide_pileup_track(unsigned short**, int, char*)
void compress_nucleotide_pileup_track(unsigned short** pileup, int l_sig, char* op_fp)
{
  FILE* f_op = open_f(op_fp, "wb");
  
  // Write the length of pileup
  fwrite(&l_sig, sizeof(int), 1, f_op);
  int n_4bit_posns = 0;
  int n_8bit_posns = 0;
  int n_12bit_posns = 0;
  int n_14bit_posns = 0;
  int n_16bit_posns = 0;
  int n_0_signal = 0;

  // This is the 0 RLE start position.
  int cur_0_run_start = -1;
  for(int i = 1; i <= l_sig; i++)
  {
    // Dump the current 5 levels:     
    // Generate the existence flag.
    //fprintf(stderr, "Position %d:\n", i);
    unsigned char existence_flag = 0;
    unsigned char drange_flag = 0;
    unsigned short max_val = 0;
    unsigned int total_val = 0;
    for(int allele_i = 0; allele_i < 5; allele_i++)
    {
      if(pileup[allele_i][i] > 0)
      {
        total_val += (unsigned int)(pileup[allele_i][i]);

        if(max_val < pileup[allele_i][i])
        {
          max_val = pileup[allele_i][i];
        }

        existence_flag = existence_flag | (1 << allele_i);
        //fprintf(stderr, "Nuc %d: %d\n", allele_i, pileup[allele_i][i]);
        //getc(stdin);
      } // signal check.
    } // allele_i loop.

    // Write the 1 byte phred-based partitioning flag.
    // Following is an RLE check.
    if(total_val == 0)
    {
      if(cur_0_run_start == -1)
      {
        // Initiate the 0 run.
        cur_0_run_start = i;

if(__DUMP_MAPPED_READ_TOOLS_MSGS__)
        fprintf(stderr, "Initiated a 0-run @ %d\n", i);
      }
      else
      {
        // We are already in a zero run.
      }
    } // total_val 0 check.
    else
    {
      // Check if we will do a RLE dump.
      // Make sure there was a run of zeros before this position.
      if(cur_0_run_start != -1)
      {
        // Check the RLE length and dump if requested.
        bool do_RLE_dump = ((i - cur_0_run_start) > 5);

        if(do_RLE_dump)
        {
          //fprintf(stderr, "RLE dumping a 0-run @ %d till %d\n", cur_0_run_start, i);

          // Do an RLE dump: Note that an RLE dump is guaranteed to be more efficient as long as the length run is longer than 5.
          // Note that the top 2 bits are reserved for RLE, setting the RLE indicator to FF.
          unsigned char RLE_indicator = 0xFF;
          fwrite(&RLE_indicator, sizeof(unsigned char), 1, f_op);

if(__DUMP_MAPPED_READ_TOOLS_MSGS__)
          fprintf(stderr, "Writing: %x\n", RLE_indicator);

          // Write the length of 0 length region: Note that i does not have a 0, it must not be included.
          unsigned int l_RLE = (i - cur_0_run_start);
          fwrite(&l_RLE, sizeof(unsigned int), 1, f_op);

if(__DUMP_MAPPED_READ_TOOLS_MSGS__)
          fprintf(stderr, "Writing l_RLE: %d\n", l_RLE);
        } // RLE dump check.
        else
        {
if(__DUMP_MAPPED_READ_TOOLS_MSGS__)
          fprintf(stderr, "Per element dumping a 0-run @ %d till %d\n", cur_0_run_start, i);

          // Save the position with normal dump where we dump 0's.
          for(int j = cur_0_run_start; j < i; j++)
          {
            // Write 0 to each position.
            unsigned char RLE_indicator = 0;
            fwrite(&RLE_indicator, sizeof(unsigned char), 1, f_op);
          } // j loop.
        } // non-RLE check.

        // Reset the RLE start position.
        cur_0_run_start = -1;
      } // 0-run check. 

      if(max_val == 0)
      {
if(__DUMP_MAPPED_READ_TOOLS_MSGS__)
        fprintf(stderr, "Sanity check failed: Max value is 0 on a non-zero dumping path.\n");
        exit(0);
      }
      
      // Set the data range flag: 1 for short, 0 for char.
      if(max_val >= (1 << 8)-1)
      {
        drange_flag = 1;
      }

      // If val is not char, flag it to make sure we dump the right number of bytes.
      existence_flag = existence_flag | (drange_flag << 5);

      // Write existence flag.
      fwrite(&existence_flag, sizeof(unsigned char), 1, f_op);

      // Dump the numbers.
      for(int allele_i = 0; allele_i < 5; allele_i++)
      {
        if(pileup[allele_i][i] > 0)
        {
          if(drange_flag == 1)
          { 
            unsigned short current_count_short = (unsigned short)(pileup[allele_i][i]);
            fwrite(&(current_count_short), sizeof(unsigned short), 1, f_op);
          }
          else
          {
            unsigned char current_count_char = (unsigned char)(pileup[allele_i][i]);
            fwrite(&(current_count_char), sizeof(unsigned char), 1, f_op);
          }
        } // count check for current position.
      } // allele_i loop.

    } // total value check.

    // Get some simple stats.
    if(max_val == 0)
    {
      n_0_signal++;
    }
    else if(max_val <= ((2<<4)-1))
    {
      n_4bit_posns++;
    }
    else if(max_val <= ((2<<8)-1))
    {
      n_8bit_posns++;
    }
    else if(max_val <= ((2<<12)-1))
    {
      n_12bit_posns++;
    }
    else if(max_val <= ((2<<14)-1))
    {
      n_14bit_posns++;
    }
    else if(max_val <= ((2<<16)-1))
    {
      n_16bit_posns++;
    }
  } // i loop.
  fclose(f_op);
  fprintf(stderr, "%d 0 signal, %d 4-bit, %d 8-bit, %d 12-bit, %d 14-bit, %d 16-bit positions.\n", n_0_signal, n_4bit_posns, n_8bit_posns, n_12bit_posns, n_14bit_posns, n_16bit_posns);

  // Now, load and test if we received the correct values.
  bool loading_check = false;
  if(loading_check)
  {
    fprintf(stderr, "Loading and comparing.\n");

    // Load the compressed pileup; then do sanity check.
    int l_loaded_pileup = 0;
    unsigned short** loaded_pileup = load_compressed_pileups(op_fp, l_loaded_pileup);

    // Compare the loaded and the actual pileups.
    for(int i_allele = 0; i_allele < 5; i_allele++)
    {
      for(int i = 1; i <= l_sig; i++)
      {
        if(loaded_pileup[i_allele][i] != pileup[i_allele][i])
        {
          fprintf(stderr, "Sanity check failed: posn: %d; allele: %d: %d,%d\n", 
            i, i_allele, loaded_pileup[i_allele][i], pileup[i_allele][i]);
          exit(0);
        }
      } // i loop.        
    } // i_allele loop.

    // Free memory for both loaded and dumped pileups.
    for(int i_allele = 0; i_allele < 5; i_allele++)
    {
      delete [] loaded_pileup[i_allele];
    } // i_allele loop.
  }
  else
  {
    fprintf(stderr, "Skipping loading check.\n");
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
leaq 0x34(%rsp), %r12
movl %esi, (%r12)
leaq 0x6b9c(%rip), %rsi      # 0xa0cc
movq %rdx, %rdi
callq 0x4b65
movq %rax, %r14
movl $0x1, %r15d
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %rax, %rcx
callq 0x22e0
movl (%r12), %eax
movq $0x0, 0x20(%rsp)
movl $0x0, %ecx
movq %rcx, 0x18(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, (%rsp)
movl $0x0, 0x4(%rsp)
movl $0x0, 0x8(%rsp)
movq %rax, 0x28(%rsp)
testl %eax, %eax
jg 0x35f9
movq %r14, %rdi
callq 0x2150
movq 0xba3c(%rip), %rbx      # 0xefe0
movq (%rbx), %rdi
leaq 0x6bc4(%rip), %rsi      # 0xa172
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
movl 0x4(%rsp), %r8d
movl (%rsp), %r9d
xorl %eax, %eax
pushq 0x18(%rsp)
pushq 0x28(%rsp)
callq 0x2230
addq $0x10, %rsp
movq (%rbx), %rcx
leaq 0x6be3(%rip), %rdi      # 0xa1bf
movl $0x18, %esi
movl $0x1, %edx
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22e0
incq 0x28(%rsp)
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl $0x0, 0x8(%rsp)
movl $0x0, 0x4(%rsp)
movl $0x0, (%rsp)
movl $0x0, 0xc(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
xorl %ecx, %ecx
xorl %eax, %eax
xorl %ebp, %ebp
xorl %r12d, %r12d
movq (%rbx,%rcx,8), %rdx
movzwl (%rdx,%r15,2), %edx
testl %edx, %edx
je 0x365d
addl %edx, %eax
cmpw %dx, %bp
cmovbel %edx, %ebp
movl $0x1, %edx
shll %cl, %edx
orb %dl, %r12b
incq %rcx
cmpq $0x5, %rcx
jne 0x363e
testl %eax, %eax
je 0x36cd
cmpl $-0x1, %r13d
je 0x3719
movl %r15d, %eax
subl %r13d, %eax
cmpl $0x6, %eax
jl 0x36ec
movl %eax, %r13d
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rsi
callq 0x21e0
cmpb $0x1, 0xbafb(%rip)      # 0xf191
je 0x37f7
movl %r13d, 0x14(%rsp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0x14(%rsp), %rdi
movq %r14, %rcx
callq 0x22e0
cmpb $0x1, 0xbad2(%rip)      # 0xf191
jne 0x3719
leaq 0x14(%rsp), %rdi
callq 0x2c16
jmp 0x3719
cmpl $-0x1, %r13d
jne 0x3792
cmpb $0x1, 0xbab3(%rip)      # 0xf191
je 0x3824
movl %r15d, %r13d
jmp 0x3792
cmpb $0x1, 0xba9e(%rip)      # 0xf191
je 0x3801
movslq %r13d, %rax
cmpq %rax, %r15
jle 0x3719
incl %r13d
xorl %edi, %edi
movq %r14, %rsi
callq 0x21e0
movl %r13d, %eax
incl %r13d
cmpq %rax, %r15
jne 0x3704
testw %bp, %bp
je 0x3844
movzwl %bp, %r13d
cmpl $0xff, %r13d
setae %al
shlb $0x5, %al
orb %r12b, %al
movsbl %al, %edi
movq %r14, %rsi
callq 0x21e0
xorl %r12d, %r12d
movq (%rbx,%r12,8), %rax
movzwl (%rax,%r15,2), %eax
testl %eax, %eax
je 0x3783
cmpl $0xff, %r13d
jb 0x3778
movw %ax, 0x12(%rsp)
movl $0x2, %esi
movl $0x1, %edx
leaq 0x12(%rsp), %rdi
movq %r14, %rcx
callq 0x22e0
jmp 0x3783
movsbl %al, %edi
movq %r14, %rsi
callq 0x21e0
incq %r12
cmpq $0x5, %r12
jne 0x3744
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
testw %bp, %bp
je 0x37a3
cmpw $0x1f, %bp
ja 0x37a9
incl 0x8(%rsp)
jmp 0x37e4
incl 0xc(%rsp)
jmp 0x37e4
movzwl %bp, %eax
cmpl $0x1ff, %eax            # imm = 0x1FF
ja 0x37b9
incl 0x4(%rsp)
jmp 0x37e4
cmpl $0x1fff, %eax           # imm = 0x1FFF
ja 0x37c5
incl (%rsp)
jmp 0x37e4
testw %bp, %bp
js 0x37d8
movq 0x20(%rsp), %rax
incl %eax
movq %rax, 0x20(%rsp)
jmp 0x37e4
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
incq %r15
cmpq 0x28(%rsp), %r15
jne 0x3635
jmp 0x3595
callq 0x2bf9
jmp 0x369c
movq 0xb7d8(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x68f5(%rip), %rsi      # 0xa107
movl %r13d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2230
jmp 0x36f9
movq 0xb7b5(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x689a(%rip), %rsi      # 0xa0cf
movl %r15d, %edx
xorl %eax, %eax
callq 0x2230
jmp 0x36e4
cmpb $0x1, 0xb946(%rip)      # 0xf191
jne 0x3852
callq 0x2c33
xorl %edi, %edi
callq 0x21b0