// File: 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:
subq $0x98, %rsp
movq %rdi, 0x90(%rsp)
movl %esi, 0x8c(%rsp)
movq %rdx, 0x80(%rsp)
movq 0x80(%rsp), %rdi
leaq 0x11343(%rip), %rsi     # 0x140a0
callq 0x6f40
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rcx
leaq 0x8c(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2310
movl $0x0, 0x74(%rsp)
movl $0x0, 0x70(%rsp)
movl $0x0, 0x6c(%rsp)
movl $0x0, 0x68(%rsp)
movl $0x0, 0x64(%rsp)
movl $0x0, 0x60(%rsp)
movl $0xffffffff, 0x5c(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x58(%rsp)
movl 0x58(%rsp), %eax
cmpl 0x8c(%rsp), %eax
jg 0x31f4
movb $0x0, 0x57(%rsp)
movb $0x0, 0x56(%rsp)
movw $0x0, 0x54(%rsp)
movl $0x0, 0x50(%rsp)
movl $0x0, 0x4c(%rsp)
cmpl $0x5, 0x4c(%rsp)
jge 0x2ead
movq 0x90(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x58(%rsp), %rcx
movzwl (%rax,%rcx,2), %eax
cmpl $0x0, %eax
jle 0x2e9b
movq 0x90(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x58(%rsp), %rcx
movzwl (%rax,%rcx,2), %eax
addl 0x50(%rsp), %eax
movl %eax, 0x50(%rsp)
movzwl 0x54(%rsp), %eax
movq 0x90(%rsp), %rcx
movslq 0x4c(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq 0x58(%rsp), %rdx
movzwl (%rcx,%rdx,2), %ecx
cmpl %ecx, %eax
jge 0x2e83
movq 0x90(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x58(%rsp), %rcx
movw (%rax,%rcx,2), %ax
movw %ax, 0x54(%rsp)
movzbl 0x57(%rsp), %eax
movl 0x4c(%rsp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
orl %ecx, %eax
movb %al, 0x57(%rsp)
jmp 0x2e9d
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x2df5
cmpl $0x0, 0x50(%rsp)
jne 0x2ef1
cmpl $-0x1, 0x5c(%rsp)
jne 0x2eea
movl 0x58(%rsp), %eax
movl %eax, 0x5c(%rsp)
testb $0x1, 0x182df(%rip)     # 0x1b1a9
je 0x2ee8
movq 0x1810d(%rip), %rax     # 0x1afe0
movq (%rax), %rdi
movl 0x58(%rsp), %edx
leaq 0x111c2(%rip), %rsi     # 0x140a3
movb $0x0, %al
callq 0x2240
jmp 0x2eec
jmp 0x2eec
jmp 0x3145
cmpl $-0x1, 0x5c(%rsp)
je 0x3017
movl 0x58(%rsp), %eax
subl 0x5c(%rsp), %eax
cmpl $0x5, %eax
setg %al
andb $0x1, %al
movb %al, 0x4b(%rsp)
testb $0x1, 0x4b(%rsp)
je 0x2fa9
movb $-0x1, 0x4a(%rsp)
movq 0x78(%rsp), %rcx
leaq 0x4a(%rsp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x2310
testb $0x1, 0x1826b(%rip)     # 0x1b1a9
je 0x2f5d
movq 0x18099(%rip), %rax     # 0x1afe0
movq (%rax), %rdi
movzbl 0x4a(%rsp), %edx
leaq 0x11165(%rip), %rsi     # 0x140bb
movb $0x0, %al
callq 0x2240
movl 0x58(%rsp), %eax
subl 0x5c(%rsp), %eax
movl %eax, 0x44(%rsp)
movq 0x78(%rsp), %rcx
leaq 0x44(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2310
testb $0x1, 0x18220(%rip)     # 0x1b1a9
je 0x2fa7
movq 0x1804e(%rip), %rax     # 0x1afe0
movq (%rax), %rdi
movl 0x44(%rsp), %edx
leaq 0x11128(%rip), %rsi     # 0x140c8
movb $0x0, %al
callq 0x2240
jmp 0x300f
testb $0x1, 0x181f9(%rip)     # 0x1b1a9
je 0x2fd2
movq 0x18027(%rip), %rax     # 0x1afe0
movq (%rax), %rdi
movl 0x5c(%rsp), %edx
movl 0x58(%rsp), %ecx
leaq 0x11110(%rip), %rsi     # 0x140db
movb $0x0, %al
callq 0x2240
movl 0x5c(%rsp), %eax
movl %eax, 0x40(%rsp)
movl 0x40(%rsp), %eax
cmpl 0x58(%rsp), %eax
jge 0x300d
movb $0x0, 0x3f(%rsp)
movq 0x78(%rsp), %rcx
leaq 0x3f(%rsp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x2310
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x2fda
jmp 0x300f
movl $0xffffffff, 0x5c(%rsp) # imm = 0xFFFFFFFF
movzwl 0x54(%rsp), %eax
cmpl $0x0, %eax
jne 0x3049
testb $0x1, 0x18181(%rip)     # 0x1b1a9
je 0x3042
movq 0x17faf(%rip), %rax     # 0x1afe0
movq (%rax), %rdi
leaq 0x110ca(%rip), %rsi     # 0x14105
movb $0x0, %al
callq 0x2240
xorl %edi, %edi
callq 0x21d0
movzwl 0x54(%rsp), %eax
cmpl $0xff, %eax
jl 0x305a
movb $0x1, 0x56(%rsp)
movzbl 0x57(%rsp), %eax
movzbl 0x56(%rsp), %ecx
shll $0x5, %ecx
orl %ecx, %eax
movb %al, 0x57(%rsp)
movq 0x78(%rsp), %rcx
leaq 0x57(%rsp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x2310
movl $0x0, 0x38(%rsp)
cmpl $0x5, 0x38(%rsp)
jge 0x3143
movq 0x90(%rsp), %rax
movslq 0x38(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x58(%rsp), %rcx
movzwl (%rax,%rcx,2), %eax
cmpl $0x0, %eax
jle 0x3131
movzbl 0x56(%rsp), %eax
cmpl $0x1, %eax
jne 0x30fa
movq 0x90(%rsp), %rax
movslq 0x38(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x58(%rsp), %rcx
movw (%rax,%rcx,2), %ax
movw %ax, 0x36(%rsp)
movq 0x78(%rsp), %rcx
leaq 0x36(%rsp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x2310
jmp 0x312f
movq 0x90(%rsp), %rax
movslq 0x38(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x58(%rsp), %rcx
movw (%rax,%rcx,2), %ax
movb %al, 0x35(%rsp)
movq 0x78(%rsp), %rcx
leaq 0x35(%rsp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x2310
jmp 0x3131
jmp 0x3133
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x308c
jmp 0x3145
movzwl 0x54(%rsp), %eax
cmpl $0x0, %eax
jne 0x315f
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x31e2
movzwl 0x54(%rsp), %eax
cmpl $0x1f, %eax
jg 0x3176
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x31e0
movzwl 0x54(%rsp), %eax
cmpl $0x1ff, %eax            # imm = 0x1FF
jg 0x318f
movl 0x70(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x70(%rsp)
jmp 0x31de
movzwl 0x54(%rsp), %eax
cmpl $0x1fff, %eax           # imm = 0x1FFF
jg 0x31a8
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x31dc
movzwl 0x54(%rsp), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jg 0x31c1
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x31da
movzwl 0x54(%rsp), %eax
cmpl $0x1ffff, %eax          # imm = 0x1FFFF
jg 0x31d8
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x31da
jmp 0x31dc
jmp 0x31de
jmp 0x31e0
jmp 0x31e2
jmp 0x31e4
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x2dc3
movq 0x78(%rsp), %rdi
callq 0x2170
movq 0x17ddb(%rip), %rax     # 0x1afe0
movq (%rax), %rdi
movl 0x60(%rsp), %edx
movl 0x74(%rsp), %ecx
movl 0x70(%rsp), %r8d
movl 0x6c(%rsp), %r9d
movl 0x68(%rsp), %r10d
movl 0x64(%rsp), %eax
leaq 0x10f1c(%rip), %rsi     # 0x14146
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x2240
movb $0x0, 0x34(%rsp)
testb $0x1, 0x34(%rsp)
je 0x33a2
movq 0x17d90(%rip), %rax     # 0x1afe0
movq (%rax), %rdi
leaq 0x10f39(%rip), %rsi     # 0x14193
movb $0x0, %al
callq 0x2240
movl $0x0, 0x30(%rsp)
movq 0x80(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x2760
movq %rax, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
cmpl $0x5, 0x24(%rsp)
jge 0x335f
movl $0x1, 0x20(%rsp)
movl 0x20(%rsp), %eax
cmpl 0x8c(%rsp), %eax
jg 0x334d
movq 0x28(%rsp), %rax
movslq 0x24(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x20(%rsp), %rcx
movzwl (%rax,%rcx,2), %eax
movq 0x90(%rsp), %rcx
movslq 0x24(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq 0x20(%rsp), %rdx
movzwl (%rcx,%rdx,2), %ecx
cmpl %ecx, %eax
je 0x333b
movq 0x17cf8(%rip), %rax     # 0x1afe0
movq (%rax), %rdi
movl 0x20(%rsp), %edx
movl 0x24(%rsp), %ecx
movq 0x28(%rsp), %rax
movslq 0x24(%rsp), %rsi
movq (%rax,%rsi,8), %rax
movslq 0x20(%rsp), %rsi
movzwl (%rax,%rsi,2), %r8d
movq 0x90(%rsp), %rax
movslq 0x24(%rsp), %rsi
movq (%rax,%rsi,8), %rax
movslq 0x20(%rsp), %rsi
movzwl (%rax,%rsi,2), %r9d
leaq 0x10e7e(%rip), %rsi     # 0x141ab
movb $0x0, %al
callq 0x2240
xorl %edi, %edi
callq 0x21d0
jmp 0x333d
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x329b
jmp 0x334f
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x3288
movl $0x0, 0x1c(%rsp)
cmpl $0x5, 0x1c(%rsp)
jge 0x33a0
movq 0x28(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x3391
movq 0x10(%rsp), %rdi
callq 0x2200
jmp 0x3393
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x3367
jmp 0x33ba
movq 0x17c37(%rip), %rax     # 0x1afe0
movq (%rax), %rdi
leaq 0x10e2a(%rip), %rsi     # 0x141dd
movb $0x0, %al
callq 0x2240
addq $0x98, %rsp
retq
nopw %cs:(%rax,%rax)