// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movq %rdi, %rbx
leaq 0x44(%rsp), %r12
movl %esi, (%r12)
leaq 0x6be8(%rip), %rsi      # 0xa0cc
movq %rdx, %rdi
callq 0x4b1e
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, 0x28(%rsp)
movl $0x0, %ecx
movq %rcx, 0x20(%rsp)
movl $0x0, 0x14(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x10(%rsp)
movq %rax, 0x38(%rsp)
testl %eax, %eax
jg 0x35af
movq %r14, %rdi
callq 0x2150
movq 0xba87(%rip), %rbx      # 0xefe0
movq (%rbx), %rdi
leaq 0x6c0f(%rip), %rsi      # 0xa172
movl 0x14(%rsp), %edx
movl 0x10(%rsp), %ecx
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
xorl %eax, %eax
pushq 0x20(%rsp)
pushq 0x30(%rsp)
callq 0x2230
addq $0x10, %rsp
movq (%rbx), %rcx
leaq 0x6c2d(%rip), %rdi      # 0xa1bf
movl $0x18, %esi
movl $0x1, %edx
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22e0
incq 0x38(%rsp)
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl $0x0, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0x14(%rsp)
movq $0x0, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movq %r14, 0x30(%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 0x3619
addl %edx, %eax
cmpw %dx, %bp
cmovbel %edx, %ebp
movl $0x1, %edx
shll %cl, %edx
orb %dl, %r12b
incq %rcx
cmpq $0x5, %rcx
jne 0x35fa
testl %eax, %eax
je 0x368f
cmpl $-0x1, %r13d
je 0x36e0
movl %r15d, %r14d
subl %r13d, %r14d
cmpl $0x6, %r14d
jl 0x36ae
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq 0x30(%rsp), %r13
movq %r13, %rsi
callq 0x21e0
cmpb $0x1, 0xbb3c(%rip)      # 0xf191
je 0x37bf
movl %r14d, 0x1c(%rsp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0x1c(%rsp), %rdi
movq %r13, %rcx
callq 0x22e0
cmpb $0x1, 0xbb13(%rip)      # 0xf191
movq %r13, %r14
jne 0x36e0
leaq 0x1c(%rsp), %rdi
callq 0x2be0
jmp 0x36e0
cmpl $-0x1, %r13d
jne 0x3759
cmpb $0x1, 0xbaf1(%rip)      # 0xf191
je 0x37ec
movl %r15d, %r13d
jmp 0x3759
cmpb $0x1, 0xbadc(%rip)      # 0xf191
je 0x37c9
movslq %r13d, %rax
cmpq %rax, %r15
movq 0x30(%rsp), %r14
jle 0x36e0
incl %r13d
xorl %edi, %edi
movq %r14, %rsi
callq 0x21e0
movl %r13d, %eax
incl %r13d
cmpq %rax, %r15
jne 0x36cb
testw %bp, %bp
je 0x380c
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 0x374a
cmpl $0xff, %r13d
jb 0x373f
movw %ax, 0x1a(%rsp)
movl $0x2, %esi
movl $0x1, %edx
leaq 0x1a(%rsp), %rdi
movq %r14, %rcx
callq 0x22e0
jmp 0x374a
movsbl %al, %edi
movq %r14, %rsi
callq 0x21e0
incq %r12
cmpq $0x5, %r12
jne 0x370b
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
testw %bp, %bp
je 0x376a
cmpw $0x1f, %bp
ja 0x3770
incl 0x10(%rsp)
jmp 0x37ac
incl 0x14(%rsp)
jmp 0x37ac
movzwl %bp, %eax
cmpl $0x1ff, %eax            # imm = 0x1FF
ja 0x3780
incl 0xc(%rsp)
jmp 0x37ac
cmpl $0x1fff, %eax           # imm = 0x1FFF
ja 0x378d
incl 0x8(%rsp)
jmp 0x37ac
testw %bp, %bp
js 0x37a0
movq 0x28(%rsp), %rax
incl %eax
movq %rax, 0x28(%rsp)
jmp 0x37ac
movq 0x20(%rsp), %rax
incl %eax
movq %rax, 0x20(%rsp)
incq %r15
cmpq 0x38(%rsp), %r15
jne 0x35f1
jmp 0x354a
callq 0x2bc3
jmp 0x365b
movq 0xb810(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x692d(%rip), %rsi      # 0xa107
movl %r13d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2230
jmp 0x36bb
movq 0xb7ed(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x68d2(%rip), %rsi      # 0xa0cf
movl %r15d, %edx
xorl %eax, %eax
callq 0x2230
jmp 0x36a6
cmpb $0x1, 0xb97e(%rip)      # 0xf191
jne 0x381a
callq 0x2bfd
xorl %edi, %edi
callq 0x21b0