// File: /workspace/github_c_makefile_star2_O1/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, 0x28(%rsp)
movq %r8, %rbp
movq %rcx, %r13
movq %rdx, %r15
movq %rsi, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
movq 0x198(%rsp), %rcx
movzwl (%rcx), %eax
movw %ax, 0x24(%rsp)
movzwl 0x2(%rcx), %eax
movw %ax, 0x26(%rsp)
movzwl 0x3c0(%rcx), %eax
movw %ax, 0x20(%rsp)
movzwl 0x3c2(%rcx), %eax
movw %ax, 0x22(%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 0x4056e7
addq $0x10, %rsp
addq $0x8, %rbx
addq $0x20, %r12
cmpq $0x38, %rbx
jb 0x40537f
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 0x4056e7
addq $0x10, %rsp
addq $0x4, %rbx
cmpq $0x20, %rbx
jne 0x4053c1
xorl %ebx, %ebx
leaq 0xd149a(%rip), %r14     # 0x4d68b0
movss (%r13,%rbx,4), %xmm0
mulss (%rbp,%rbx,4), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jbe 0x40543a
addss 0xd0085(%rip), %xmm0    # 0x4d54b8
callq 0x401120
jmp 0x405447
addss 0xd006a(%rip), %xmm0    # 0x4d54ac
callq 0x401148
cvttss2si %xmm0, %eax
movzbl (%rbx,%r14), %ecx
movl %eax, 0x50(%rsp,%rcx,4)
incq %rbx
cmpq $0x40, %rbx
jne 0x405416
movl 0x50(%rsp), %r14d
movl %r14d, %eax
subl 0x28(%rsp), %eax
jne 0x405482
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r15, %rdx
movq 0x190(%rsp), %rcx
jmp 0x4054f2
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
bsrl %ecx, %edx
xorl $-0x20, %edx
addl $0x21, %edx
cmpl $0x2, %ecx
movl $0x1, %ecx
cmovael %edx, %ecx
leaq 0xc(%rsp), %r12
movw %cx, 0x2(%r12)
movl %eax, %edx
sarl $0x1f, %edx
addl %eax, %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
shll %cl, %eax
notl %eax
andl %edx, %eax
movw %ax, (%r12)
movzbl %cl, %eax
movq 0x190(%rsp), %rcx
leaq (%rcx,%rax,4), %rcx
movq 0x18(%rsp), %r13
movq %r13, %rdi
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
movq %r15, %rdx
callq 0x405683
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x405683
movl $0x40, %eax
leal -0x1(%rax), %r13d
cmpl $0x0, 0x50(%rsp,%r13,4)
jne 0x405512
decl %eax
cmpl $0x1, %eax
ja 0x4054fc
xorl %r13d, %r13d
testl %r13d, %r13d
je 0x405657
movl %r14d, 0x30(%rsp)
jle 0x40564c
movl %r13d, %edx
movl $0x1, %ebp
movq %r15, 0x38(%rsp)
movq %r13, 0x40(%rsp)
movq %rdx, 0x48(%rsp)
movq %rbp, 0x28(%rsp)
movslq %ebp, %rax
xorl %ecx, %ecx
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ecx, %r15d
movl 0x50(%rsp,%rax,4), %r14d
incl %ebx
cmpq %rdx, %rax
jg 0x405567
incq %rax
leal 0x1(%r15), %ecx
testl %r14d, %r14d
je 0x40554c
movl %ebx, %eax
cmpl $0x10, %ebx
jl 0x4055ab
shrl $0x4, %r15d
cmpl $0x1, %r15d
adcl $0x0, %r15d
movq 0x10(%rsp), %rbp
movq 0x38(%rsp), %r12
leaq 0x20(%rsp), %r13
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x405683
decl %r15d
jne 0x405589
movl %ebx, %eax
andl $0xf, %eax
movq 0x40(%rsp), %r13
movl %r14d, %ecx
negl %ecx
cmovsl %r14d, %ecx
bsrl %ecx, %edx
xorl $-0x20, %edx
addl $0x21, %edx
cmpl $0x2, %ecx
movl $0x1, %ecx
cmovbl %ecx, %edx
movw %dx, 0xe(%rsp)
movl %r14d, %esi
sarl $0x1f, %esi
addl %r14d, %esi
movzbl %dl, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
shll %cl, %edx
movq 0x28(%rsp), %rbp
leal (%rbx,%rbp), %edi
movl %edi, 0x34(%rsp)
notl %edx
andl %esi, %edx
movw %dx, 0xc(%rsp)
shll $0x4, %eax
addl %ecx, %eax
cltq
movq 0x198(%rsp), %rcx
leaq (%rcx,%rax,4), %rcx
movq 0x18(%rsp), %r12
movq %r12, %rdi
movq 0x10(%rsp), %r14
movq %r14, %rsi
movq 0x38(%rsp), %r15
movq %r15, %rdx
callq 0x405683
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
leaq 0xc(%rsp), %rcx
callq 0x405683
addl %ebx, %ebp
incl %ebp
cmpl 0x34(%rsp), %r13d
movq 0x48(%rsp), %rdx
jg 0x40553d
cmpl $0x3f, %r13d
movl 0x30(%rsp), %r14d
je 0x40566e
leaq 0x24(%rsp), %rcx
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0x405683
movl %r14d, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq