// File: /workspace/github_c_makefile_star2_O2/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 $0x148, %rsp            # imm = 0x148
movl %r9d, 0x20(%rsp)
movq %r8, %rbp
movq %rcx, %r12
movq %rdx, %r15
movq %rsi, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
movq 0x188(%rsp), %rcx
movl (%rcx), %eax
movl %eax, 0x34(%rsp)
movl 0x3c0(%rcx), %eax
movl %eax, 0x30(%rsp)
leaq 0x1c(%r12), %rbx
xorl %r14d, %r14d
cmpq $0x40, %r14
jae 0x404ff6
leaq -0x1c(%rbx), %rdi
leaq -0x18(%rbx), %rsi
leaq -0x14(%rbx), %rdx
leaq -0x10(%rbx), %rcx
leaq -0xc(%rbx), %r8
leaq -0x8(%rbx), %r9
leaq -0x4(%rbx), %rax
pushq %rbx
pushq %rax
callq 0x40529e
popq %rax
popq %rcx
addq $0x8, %r14
addq $0x20, %rbx
jmp 0x404fc1
xorl %ebx, %ebx
cmpq $0x20, %rbx
je 0x405046
leaq (%r12,%rbx), %rdi
leaq (%r12,%rbx), %rsi
addq $0x20, %rsi
leaq 0x40(%r12,%rbx), %rdx
leaq 0x60(%r12,%rbx), %rcx
leaq 0x80(%r12,%rbx), %r8
leaq 0xa0(%r12,%rbx), %r9
leaq 0xc0(%r12,%rbx), %rax
leaq 0xe0(%r12,%rbx), %r10
pushq %r10
pushq %rax
callq 0x40529e
addq $0x10, %rsp
addq $0x4, %rbx
jmp 0x404ff8
xorl %ebx, %ebx
leaq 0xce831(%rip), %r13     # 0x4d3880
cmpq $0x40, %rbx
je 0x405097
movss (%r12,%rbx,4), %xmm0
mulss (%rbp,%rbx,4), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jbe 0x405078
addss 0xcd417(%rip), %xmm0    # 0x4d2488
callq 0x401128
jmp 0x405085
addss 0xcd3fc(%rip), %xmm0    # 0x4d247c
callq 0x401150
cvttss2si %xmm0, %eax
movzbl (%rbx,%r13), %ecx
movl %eax, 0x40(%rsp,%rcx,4)
incq %rbx
jmp 0x40504f
movl 0x40(%rsp), %ebx
movl %ebx, %edi
subl 0x20(%rsp), %edi
jne 0x4050ba
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r15, %rdx
movq 0x180(%rsp), %rcx
jmp 0x405101
movl %ebx, %ebp
leaq 0xc(%rsp), %r12
movq %r12, %rsi
callq 0x4053b7
movzwl 0x2(%r12), %eax
movq 0x180(%rsp), %rcx
leaq (%rcx,%rax,4), %rcx
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq 0x18(%rsp), %rbx
movq %rbx, %rsi
movq %r15, %rdx
callq 0x40523b
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
movl %ebp, %ebx
callq 0x40523b
xorl %ecx, %ecx
pushq $0x3f
popq %r13
testl %r13d, %r13d
je 0x405210
movl %r13d, %eax
cmpl $0x0, 0x40(%rsp,%rax,4)
jne 0x405126
incl %ecx
decq %r13
jmp 0x40510c
movl %ecx, 0x28(%rsp)
movl %ebx, 0x2c(%rsp)
pushq $0x1
popq %rax
leaq 0x30(%rsp), %r12
movq %r15, 0x38(%rsp)
cmpl %r13d, %eax
jg 0x405205
movq %rax, 0x20(%rsp)
cltq
xorl %ecx, %ecx
pushq $-0x1
popq %rdx
movl %edx, %ebx
movl %ecx, %r14d
movl 0x40(%rsp,%rax,4), %ebp
incl %ebx
cmpq %r13, %rax
jg 0x40516b
incq %rax
leal 0x1(%r14), %ecx
testl %ebp, %ebp
je 0x405152
movl %ebx, %r15d
cmpl $0x10, %ebx
jl 0x4051a0
shrl $0x4, %r14d
movq 0x38(%rsp), %r15
subl $0x1, %r14d
jb 0x405199
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x40523b
jmp 0x40517c
movl %ebx, %r15d
andl $0xf, %r15d
movl %ebp, %edi
leaq 0xc(%rsp), %rsi
callq 0x4053b7
shll $0x4, %r15d
movzwl 0xe(%rsp), %eax
movslq %r15d, %rcx
addq %rax, %rcx
movq 0x188(%rsp), %rax
leaq (%rax,%rcx,4), %rcx
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
movq 0x18(%rsp), %r14
movq %r14, %rsi
movq 0x38(%rsp), %r15
movq %r15, %rdx
callq 0x40523b
movq %rbp, %rdi
movq %r14, %rsi
movq %r15, %rdx
leaq 0xc(%rsp), %rcx
callq 0x40523b
movq 0x20(%rsp), %rax
addl %ebx, %eax
incl %eax
jmp 0x40513b
cmpl $0x0, 0x28(%rsp)
movl 0x2c(%rsp), %ebx
je 0x405227
leaq 0x34(%rsp), %rcx
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r15, %rdx
callq 0x40523b
movl %ebx, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq