// File: /workspace/github_cpp_makefile_star2_O2/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), %r15
movl %esi, (%r15)
leaq 0x67cd(%rip), %rsi      # 0x90bc
movq %rdx, %rdi
callq 0x471a
movq %rax, %r14
pushq $0x4
popq %rsi
pushq $0x1
popq %r12
movq %r15, %rdi
movq %r12, %rdx
movq %rax, %rcx
callq 0x22f0
movl (%r15), %ecx
xorl %eax, %eax
testl %ecx, %ecx
cmovlel %eax, %ecx
incl %ecx
movq %rcx, 0x28(%rsp)
pushq $-0x1
popq %rdx
pushq $0x1
popq %r13
movq $0x0, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
movl $0x0, 0x10(%rsp)
movl $0x0, 0x4(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
cmpq 0x28(%rsp), %r12
je 0x2b83
xorl %ecx, %ecx
xorl %r15d, %r15d
xorl %ebp, %ebp
xorl %eax, %eax
cmpq $0x5, %rcx
je 0x2995
movq (%rbx,%rcx,8), %rsi
movzwl (%rsi,%r12,2), %esi
testl %esi, %esi
je 0x2990
addl %esi, %eax
cmpw %si, %bp
cmovbel %esi, %ebp
movl %r13d, %esi
shll %cl, %esi
orb %sil, %r15b
incq %rcx
jmp 0x296d
testl %eax, %eax
je 0x2a65
cmpl $-0x1, %edx
je 0x29ef
movl %r12d, %r13d
subl %edx, %r13d
cmpl $0x6, %r13d
jl 0x2a9e
pushq $-0x1
popq %rdi
movq %r14, %rsi
callq 0x21f0
cmpb $0x1, 0xc7d5(%rip)      # 0xf199
je 0x2afa
movl %r13d, 0x14(%rsp)
leaq 0x14(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x22f0
cmpb $0x1, 0xc7b0(%rip)      # 0xf199
je 0x2b1c
testw %bp, %bp
je 0x2be4
cmpw $0xff, %bp
setae %al
shlb $0x5, %al
orb %r15b, %al
movsbl %al, %edi
movq %r14, %rsi
callq 0x21f0
xorl %r15d, %r15d
pushq $0x1
popq %r13
cmpq $0x5, %r15
je 0x2a5c
movq (%rbx,%r15,8), %rax
movzwl (%rax,%r12,2), %eax
testl %eax, %eax
je 0x2a57
cmpw $0xff, %bp
jb 0x2a4c
movw %ax, 0x2(%rsp)
leaq 0x2(%rsp), %rdi
pushq $0x2
popq %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x22f0
jmp 0x2a57
movsbl %al, %edi
movq %r14, %rsi
callq 0x21f0
incq %r15
jmp 0x2a18
pushq $-0x1
popq %rdx
pushq $0x1
popq %r13
jmp 0x2a7a
cmpl $-0x1, %edx
jne 0x2a7a
cmpb $0x1, 0xc728(%rip)      # 0xf199
je 0x2b63
movl %r12d, %edx
testw %bp, %bp
je 0x2a8b
cmpw $0x1f, %bp
ja 0x2a91
incl 0xc(%rsp)
jmp 0x2af2
incl 0x10(%rsp)
jmp 0x2af2
cmpw $0x1ff, %bp             # imm = 0x1FF
ja 0x2ac6
incl 0x8(%rsp)
jmp 0x2af2
cmpb $0x1, 0xc6f4(%rip)      # 0xf199
je 0x2b3d
movslq %edx, %r13
cmpq %r13, %r12
jle 0x29ef
xorl %edi, %edi
movq %r14, %rsi
callq 0x21f0
incq %r13
jmp 0x2aae
cmpw $0x1fff, %bp            # imm = 0x1FFF
ja 0x2ad3
incl 0x4(%rsp)
jmp 0x2af2
testw %bp, %bp
js 0x2ae6
movq 0x20(%rsp), %rax
incl %eax
movq %rax, 0x20(%rsp)
jmp 0x2af2
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
incq %r12
jmp 0x2959
movq 0xc4df(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x65cc(%rip), %rsi      # 0x90d7
movl $0xff, %edx
xorl %eax, %eax
callq 0x2240
jmp 0x29ca
movq 0xc4bd(%rip), %rax      # 0xefe0
movq (%rax), %rdi
movl 0x14(%rsp), %edx
leaq 0x65b3(%rip), %rsi      # 0x90e4
xorl %eax, %eax
callq 0x2240
jmp 0x29ef
movq 0xc49c(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x65a9(%rip), %rsi      # 0x90f7
movl %r12d, %ecx
xorl %eax, %eax
movl %edx, %r13d
callq 0x2240
movl %r13d, %edx
jmp 0x2aab
movq 0xc476(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq 0x654b(%rip), %rsi      # 0x90bf
movl %r12d, %edx
xorl %eax, %eax
callq 0x2240
jmp 0x2a77
movq %r14, %rdi
callq 0x2160
movq 0xc44e(%rip), %rbx      # 0xefe0
movq (%rbx), %rdi
leaq 0x65c6(%rip), %rsi      # 0x9162
movl 0x10(%rsp), %edx
movl 0xc(%rsp), %ecx
movl 0x8(%rsp), %r8d
movl 0x4(%rsp), %r9d
xorl %eax, %eax
pushq 0x18(%rsp)
pushq 0x28(%rsp)
callq 0x2240
addq $0x10, %rsp
movq (%rbx), %rcx
leaq 0x65e4(%rip), %rdi      # 0x91af
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22f0
cmpb $0x1, 0xc5ae(%rip)      # 0xf199
jne 0x2c09
movq 0xc3ec(%rip), %rax      # 0xefe0
movq (%rax), %rcx
leaq 0x6523(%rip), %rdi      # 0x9121
pushq $0x40
popq %rsi
pushq $0x1
popq %rdx
callq 0x22f0
xorl %edi, %edi
callq 0x21c0