// File: /workspace/github_c_makefile_star2_O3/archer1357[P]Offline-Raytracer/jo_jpeg.c
// Function: jo_processDU
static int jo_processDU(FILE *fp,
                        int *bitBuf, int *bitCnt,
                        float *CDU, float *fdtbl, int DC,
                        const unsigned short HTDC[256][2],
                        const unsigned short HTAC[256][2]) {
  const unsigned short EOB[2] = { HTAC[0x00][0], HTAC[0x00][1] };
  const unsigned short M16zeroes[2] = { HTAC[0xF0][0], HTAC[0xF0][1] };
  int i,dataOff,  nrmarker;
  // DCT rows
  for(  dataOff=0; dataOff<64; dataOff+=8) {
    jo_DCT(&CDU[dataOff], &CDU[dataOff+1], &CDU[dataOff+2], &CDU[dataOff+3], &CDU[dataOff+4], &CDU[dataOff+5], &CDU[dataOff+6], &CDU[dataOff+7]);
  }
  // DCT columns
  for(  dataOff=0; dataOff<8; ++dataOff) {
    jo_DCT(&CDU[dataOff], &CDU[dataOff+8], &CDU[dataOff+16], &CDU[dataOff+24], &CDU[dataOff+32], &CDU[dataOff+40], &CDU[dataOff+48], &CDU[dataOff+56]);
  }
  // Quantize/descale/zigzag the coefficients
  int DU[64];
  for(  i=0; i<64; ++i) {
    float v = CDU[i]*fdtbl[i];
    DU[s_jo_ZigZag[i]] = (int)(v < 0 ? ceilf(v - 0.5f) : floorf(v + 0.5f));
  }

  // Encode DC
  int diff = DU[0] - DC;
  if (diff == 0) {
    jo_writeBits(fp, bitBuf, bitCnt, HTDC[0]);
  } else {
    unsigned short bits[2];
    jo_calcBits(diff, bits);
    jo_writeBits(fp, bitBuf, bitCnt, HTDC[bits[1]]);
    jo_writeBits(fp, bitBuf, bitCnt, bits);
  }
  // Encode ACs
  int end0pos = 63;
  for(; (end0pos>0)&&(DU[end0pos]==0); --end0pos) {
  }
  // end0pos = first element in reverse order !=0
  if(end0pos == 0) {
    jo_writeBits(fp, bitBuf, bitCnt, EOB);
    return DU[0];
  }

  for(  i = 1; i <= end0pos; ++i) {
    int startpos = i;
    for (; DU[i]==0 && i<=end0pos; ++i) {
    }
    int nrzeroes = i-startpos;
    if ( nrzeroes >= 16 ) {
      int lng = nrzeroes>>4;
      for (  nrmarker=1; nrmarker <= lng; ++nrmarker)
        jo_writeBits(fp, bitBuf, bitCnt, M16zeroes);
      nrzeroes &= 15;
    }
    unsigned short bits[2];
    jo_calcBits(DU[i], bits);
    jo_writeBits(fp, bitBuf, bitCnt, HTAC[(nrzeroes<<4)+bits[1]]);
    jo_writeBits(fp, bitBuf, bitCnt, bits);
  }
  if(end0pos != 63) {
    jo_writeBits(fp, bitBuf, bitCnt, EOB);
  }
  return DU[0];
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movl %r9d, 0x8(%rsp)
movq %r8, %r15
movq %rcx, %r13
movq %rdx, 0x10(%rsp)
movq %rsi, %rbp
movq %rdi, (%rsp)
movq 0x198(%rsp), %rcx
movzwl (%rcx), %eax
movl %eax, 0x20(%rsp)
movzwl 0x2(%rcx), %eax
movl %eax, 0x1c(%rsp)
movzwl 0x3c0(%rcx), %r14d
movzwl 0x3c2(%rcx), %eax
movl %eax, 0x24(%rsp)
movq $-0x8, %rbx
movq %r13, %r12
leaq 0x4(%r12), %rsi
leaq 0x8(%r12), %rdx
leaq 0xc(%r12), %rcx
leaq 0x10(%r12), %r8
leaq 0x14(%r12), %r9
leaq 0x18(%r12), %rax
leaq 0x1c(%r12), %r10
movq %r12, %rdi
pushq %r10
pushq %rax
callq 0x405785
addq $0x10, %rsp
addq $0x8, %rbx
addq $0x20, %r12
cmpq $0x38, %rbx
jb 0x4053e6
movl %r14d, 0x28(%rsp)
xorl %ebx, %ebx
leaq (%rbx,%r13), %rdi
leaq (%rbx,%r13), %rsi
addq $0x20, %rsi
leaq 0x40(%r13,%rbx), %rdx
leaq 0x60(%r13,%rbx), %rcx
leaq 0x80(%r13,%rbx), %r8
leaq 0xa0(%r13,%rbx), %r9
leaq 0xc0(%r13,%rbx), %rax
leaq 0xe0(%r13,%rbx), %r10
pushq %r10
pushq %rax
callq 0x405785
addq $0x10, %rsp
addq $0x4, %rbx
cmpq $0x20, %rbx
jne 0x40542d
xorl %ebx, %ebx
leaq 0xd240e(%rip), %r14     # 0x4d7890
movss (%r13,%rbx,4), %xmm0
mulss (%r15,%rbx,4), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jbe 0x4054a6
addss 0xd0ff5(%rip), %xmm0    # 0x4d6494
callq 0x401120
jmp 0x4054b3
addss 0xd0fda(%rip), %xmm0    # 0x4d6488
callq 0x401148
cvttss2si %xmm0, %eax
movzbl (%rbx,%r14), %ecx
movl %eax, 0x50(%rsp,%rcx,4)
incq %rbx
cmpq $0x40, %rbx
jne 0x405482
movl 0x50(%rsp), %r14d
movl %r14d, %eax
subl 0x8(%rsp), %eax
movq %rbp, 0x8(%rsp)
jne 0x405500
movq 0x190(%rsp), %rax
movzwl 0x2(%rax), %r8d
movzwl (%rax), %ecx
movq (%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %r15
movq %r15, %rdx
jmp 0x405572
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
bsrl %ecx, %edx
xorl $-0x20, %edx
addl $0x21, %edx
cmpl $0x2, %ecx
movl $0x1, %r12d
cmovael %edx, %r12d
movl %eax, %edx
sarl $0x1f, %edx
addl %eax, %edx
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl %r12d, %ecx
shll %cl, %r13d
notl %r13d
andl %edx, %r13d
movzbl %r12b, %eax
movq 0x190(%rsp), %rcx
movzwl 0x2(%rcx,%rax,4), %r8d
movzwl (%rcx,%rax,4), %ecx
movq (%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %r15
movq %r15, %rdx
callq 0x405719
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movl %r13d, %ecx
movl %r12d, %r8d
callq 0x405719
movl $0x40, %eax
leal -0x1(%rax), %ecx
cmpl $0x0, 0x50(%rsp,%rcx,4)
jne 0x405592
movl %ecx, %eax
cmpl $0x1, %ecx
ja 0x40557c
jmp 0x4056eb
testl %ecx, %ecx
jle 0x4056eb
movq %rax, 0x38(%rsp)
movl %r14d, 0x18(%rsp)
movl %ecx, %edx
movl $0x1, %eax
movq %rcx, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movq %rax, 0x30(%rsp)
cltq
xorl %ecx, %ecx
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl %ecx, %r12d
movl 0x50(%rsp,%rax,4), %r13d
incl %r15d
cmpq %rdx, %rax
jg 0x4055e1
incq %rax
leal 0x1(%r12), %ecx
testl %r13d, %r13d
je 0x4055c4
movl %r15d, %eax
cmpl $0x10, %r15d
jl 0x405626
shrl $0x4, %r12d
cmpl $0x1, %r12d
adcl $0x0, %r12d
movq (%rsp), %rbx
movl 0x28(%rsp), %r14d
movl 0x24(%rsp), %ebp
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movl %r14d, %ecx
movl %ebp, %r8d
callq 0x405719
decl %r12d
jne 0x405603
movl %r15d, %eax
andl $0xf, %eax
movl %r13d, %ecx
negl %ecx
cmovsl %r13d, %ecx
bsrl %ecx, %r12d
xorl $-0x20, %r12d
addl $0x21, %r12d
cmpl $0x2, %ecx
movl $0x1, %ecx
cmovbl %ecx, %r12d
movl %r13d, %edx
sarl $0x1f, %edx
addl %r13d, %edx
movzbl %r12b, %ecx
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
shll %cl, %r13d
movq 0x30(%rsp), %rsi
addl %r15d, %esi
movl %esi, 0x2c(%rsp)
notl %r13d
andl %edx, %r13d
shll $0x4, %eax
addl %ecx, %eax
cltq
movq 0x198(%rsp), %rcx
movzwl 0x2(%rcx,%rax,4), %r8d
movzwl (%rcx,%rax,4), %ecx
movq (%rsp), %rbx
movq %rbx, %rdi
movq 0x8(%rsp), %rbp
movq %rbp, %rsi
movq 0x10(%rsp), %r14
movq %r14, %rdx
callq 0x405719
movq %rbx, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movl %r13d, %ecx
movl %r12d, %r8d
callq 0x405719
movq 0x30(%rsp), %rax
addl %r15d, %eax
incl %eax
movq 0x48(%rsp), %rcx
cmpl 0x2c(%rsp), %ecx
movq 0x40(%rsp), %rdx
jg 0x4055b5
cmpl $0x40, 0x38(%rsp)
movq 0x10(%rsp), %r15
movq (%rsp), %rbx
movl 0x18(%rsp), %r14d
je 0x405704
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movq %r15, %rdx
movl 0x20(%rsp), %ecx
movl 0x1c(%rsp), %r8d
callq 0x405719
movl %r14d, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq