// File: kchan1[P]helloworld_test1/./encoder.hh
// Function: encode(double**, int, int, int, double, double, std::basic_ofstream<char, std::char_traits<char>>*)
int encode(double**image,int width,int height,int depth,double min_threshold,double max,std::ofstream*file_out)
{
  int threshold = max;
  //this holds the coding states of all the coefficients
  unsigned char**coded;
  //this colds all the reconstructed coefficients
  double**recon_coeff;
  //this is used to keep track of which coefficients need a subordinate pass
  IntQueue*sub_list;
  //for writing individual bits
  BitWriter*bitstream;
  int i,j;
  //initialization
  sub_list = new IntQueue(width*height);
  coded = new unsigned char*[height];
  recon_coeff = new double*[height];
  bitstream = new BitWriter(file_out);
  for(i=0;i<height;i++)
  {
    coded[i]=new unsigned char[width];
    recon_coeff[i]= new double[width];
    for(j=0;j<width;j++)
    {
      coded[i][j]='U';
    }
  }
  //do a dom and sub pass
  //we're going to be doing this at least once
  do
  {
    dominant_pass(image,width,height,depth,width,height,0,0,threshold,0,coded,sub_list,recon_coeff,bitstream);
    subordinate_pass(image,threshold,sub_list,recon_coeff,bitstream);
    threshold/=2;
  }
  while(threshold>min_threshold);
  for(i=0;i<width;i++)
  {
    delete coded[i];
    delete recon_coeff[i];
  }
  delete coded;
  delete recon_coeff;
  delete sub_list;
  delete bitstream;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdi, -0x20(%rbp)
movl %esi, -0x24(%rbp)
movl %edx, -0x28(%rbp)
movl %ecx, -0x2c(%rbp)
movsd %xmm0, -0x38(%rbp)
movsd %xmm1, -0x40(%rbp)
movq %r8, -0x48(%rbp)
cvttsd2si -0x40(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl $0x18, %edi
callq 0x2180
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x90(%rbp)
movl -0x24(%rbp), %esi
movl -0x28(%rbp), %eax
imull %eax, %esi
callq 0x66b0
jmp 0x300a
movq -0x90(%rbp), %rax
movq %rax, -0x68(%rbp)
movslq -0x28(%rbp), %rax
movl $0x8, %ecx
movq %rcx, -0xa8(%rbp)
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
movq %rax, -0xa0(%rbp)
cmovoq %rax, %rdi
callq 0x2030
movq -0xa8(%rbp), %rcx
movq %rax, -0x58(%rbp)
movslq -0x28(%rbp), %rax
mulq %rcx
movq %rax, %rdi
movq -0xa0(%rbp), %rax
cmovoq %rax, %rdi
callq 0x2030
movq %rax, -0x60(%rbp)
movl $0x10, %edi
callq 0x2180
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x98(%rbp)
movq -0x48(%rbp), %rsi
callq 0x6710
jmp 0x308d
movq -0x98(%rbp), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x317f
movslq -0x24(%rbp), %rdi
callq 0x2030
movq %rax, %rdx
movq -0x58(%rbp), %rax
movslq -0x74(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movslq -0x24(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, %rdx
movq -0x60(%rbp), %rax
movslq -0x74(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x316f
movq -0x58(%rbp), %rax
movslq -0x74(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x78(%rbp), %rcx
movb $0x55, (%rax,%rcx)
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x30fe
movq -0x90(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
movl $0x18, %esi
callq 0x2190
jmp 0x331e
movq -0x98(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
movl $0x10, %esi
callq 0x2190
jmp 0x331e
jmp 0x3171
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x309f
jmp 0x3181
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %esi
movl -0x28(%rbp), %edx
movl -0x2c(%rbp), %ecx
movl -0x24(%rbp), %r8d
movl -0x28(%rbp), %r9d
movl -0x4c(%rbp), %r14d
movq -0x58(%rbp), %rbx
movq -0x68(%rbp), %r11
movq -0x60(%rbp), %r10
movq -0x70(%rbp), %rax
xorl %r15d, %r15d
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
movl %r14d, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
callq 0x2950
movq -0x20(%rbp), %rdi
movl -0x4c(%rbp), %esi
movq -0x68(%rbp), %rdx
movq -0x60(%rbp), %rcx
movq -0x70(%rbp), %r8
callq 0x2eb0
movl -0x4c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x4c(%rbp)
cvtsi2sdl -0x4c(%rbp), %xmm0
ucomisd -0x38(%rbp), %xmm0
ja 0x3181
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x3288
movq -0x58(%rbp), %rax
movslq -0x74(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb0(%rbp)
cmpq $0x0, %rax
je 0x3251
movq -0xb0(%rbp), %rdi
movl $0x1, %esi
callq 0x2190
movq -0x60(%rbp), %rax
movslq -0x74(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb8(%rbp)
cmpq $0x0, %rax
je 0x327b
movq -0xb8(%rbp), %rdi
movl $0x8, %esi
callq 0x2190
jmp 0x327d
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x321f
movq -0x58(%rbp), %rax
movq %rax, -0xc0(%rbp)
cmpq $0x0, %rax
je 0x32aa
movq -0xc0(%rbp), %rdi
movl $0x8, %esi
callq 0x2190
movq -0x60(%rbp), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, %rax
je 0x32cc
movq -0xc8(%rbp), %rdi
movl $0x8, %esi
callq 0x2190
movq -0x68(%rbp), %rax
movq %rax, -0xd0(%rbp)
cmpq $0x0, %rax
je 0x32fa
movq -0xd0(%rbp), %rdi
callq 0x6740
movq -0xd0(%rbp), %rdi
movl $0x18, %esi
callq 0x2190
movq -0x70(%rbp), %rax
movq %rax, -0xd8(%rbp)
cmpq $0x0, %rax
je 0x331c
movq -0xd8(%rbp), %rdi
movl $0x10, %esi
callq 0x2190
ud2
movq -0x80(%rbp), %rdi
callq 0x22f0
nopw (%rax,%rax)