// File: /workspace/github_cpp_makefile_star2_O2/rapirent[P]JPEG/encoder.c
// Function: calculate_mcu_block(_IO_FILE*, char (*) [8], int)
void calculate_mcu_block(FILE *fp,char block[][8],int yuv_id)
{
    double tmp[8][8], s[8][8];
    short quantized_block[64];
    memset(tmp,0,sizeof(tmp));
    memset(s,0,sizeof(s));
    for (int ii = 0; ii < 8; ii++) {
        for (int x = 0; x < 8; x++) {
            for (int y = 0; y < 8; y++) {
                s[ii][x] += (double)block[ii][y] * cos_cache[(y*2 + 1) * x];
            }
            s[ii][x] = c(x) *s[ii][x] / 2.0;
        }
    }
    for (int ii = 0; ii < 8; ii++) {
        for (int jj = 0; jj < 8; jj++) {
            for (int x = 0; x < 8; x++) {
                tmp[ii][jj] += s[x][jj] * cos_cache[(x*2 + 1) * ii];
            }
            tmp[ii][jj] = c(ii) * tmp[ii][jj] / 2.0;
        }
    }
    for (int x = 0; x < 8; x++) {
        for (int y = 0; y < 8; y++) {
            quantized_block[zigzag_index[x][y]] = (short)round(tmp[x][y]/(double)quantize_table[category[yuv_id]][x*8+y]);
        }
    }
    static short dc_block[5] = {0,0,0,0,0};
    int diff = (int)(quantized_block[0] - dc_block[yuv_id]);
    int need_write_length;
    int index;
    dc_block[yuv_id] = quantized_block[0];
    if (!diff) {
        for (int i = 0; i<strlen((char*)dc_diff[category[yuv_id]][0]); i++) {
            write_one_bit(fp, dc_diff[category[yuv_id]][0][i] - '0',false);
        }
    } else {
        need_write_length = find_length(&diff);
        for (int i = 0; i<strlen((char*)dc_diff[category[yuv_id]][need_write_length]); i++) {
            write_one_bit(fp, dc_diff[category[yuv_id]][need_write_length][i] - '0',false);
        }
        codeword_encode(fp,diff,need_write_length);
    }
    int count_zero = 0;
    int value;
    byte cof_index;
    int padding_start_index=63;
    while (padding_start_index>1&&!quantized_block[padding_start_index]) {
        padding_start_index--;
    }
    for (int i = 1; i <= padding_start_index; i++) {
        if (!quantized_block[i]) {
            count_zero++;
            continue;
        }
        if (count_zero>=16) {
            for (int j = 0; j<count_zero/16; j++) {
                for (int k = 0; k<strlen((char*)ac_cof[category[yuv_id]][0xf0]); k++) {
                    write_one_bit(fp,ac_cof[category[yuv_id]][0xf0][k] - '0',false);
                }
            }
            count_zero = count_zero % 16;
        }
        value = quantized_block[i];
        need_write_length = find_length(&value);
        for (int j = 0; j<strlen((char*)ac_cof[category[yuv_id]][((count_zero << 4)&0xf0)|need_write_length]); j++) {
            write_one_bit(fp,ac_cof[category[yuv_id]][((count_zero << 4)&0xf0)|need_write_length][j] - '0',false);
        }
        codeword_encode(fp,value,need_write_length);
        count_zero = 0;
    }
    if (padding_start_index!=63) {
        for (int i = 0; i<strlen((char*)ac_cof[category[yuv_id]][0x00]); i++) {
            write_one_bit(fp,ac_cof[category[yuv_id]][0x00][i] - '0',false);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4b8, %rsp            # imm = 0x4B8
movl %edx, (%rsp)
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x2b0(%rsp), %r14
xorl %r13d, %r13d
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
xorl %esi, %esi
callq 0x50f0
leaq 0xb0(%rsp), %r15
movl $0x200, %edx            # imm = 0x200
movq %r15, %rdi
xorl %esi, %esi
callq 0x50f0
leaq 0x5d0e(%rip), %rcx      # 0xb320
leaq 0x1a07(%rip), %rax      # 0x7020
movsd 0x1a1f(%rip), %xmm0     # 0x7040
cmpq $0x8, %r13
je 0x56ab
movq %r13, %rdx
shlq $0x6, %rdx
addq %rsp, %rdx
addq $0xb0, %rdx
xorl %esi, %esi
movq %rcx, %rdi
xorl %r8d, %r8d
cmpq $0x8, %r8
je 0x569f
leaq (%rdx,%r8,8), %r9
movsd (%r9), %xmm1
movq %rdi, %r10
xorl %r11d, %r11d
cmpq $0x8, %r11
je 0x5679
movsbl (%r12,%r11), %ebp
cvtsi2sd %ebp, %xmm2
mulsd (%r10), %xmm2
addsd %xmm2, %xmm1
incq %r11
addq %rsi, %r10
jmp 0x5659
xorl %r10d, %r10d
testq %r8, %r8
sete %r10b
mulsd (%rax,%r10,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%r9)
incq %r8
addq $0x8, %rdi
addq $0x10, %rsi
jmp 0x5644
incq %r13
addq $0x8, %r12
jmp 0x5621
leaq 0x5c6e(%rip), %rcx      # 0xb320
xorl %edx, %edx
xorl %esi, %esi
cmpq $0x8, %rsi
je 0x5741
xorl %edi, %edi
testq %rsi, %rsi
sete %dil
movsd (%rax,%rdi,8), %xmm1
movq %rsi, %rdi
shlq $0x6, %rdi
addq %rsp, %rdi
addq $0x2b0, %rdi            # imm = 0x2B0
movq %r15, %r8
xorl %r9d, %r9d
cmpq $0x8, %r9
je 0x5731
leaq (%rdi,%r9,8), %r10
movsd (%r10), %xmm2
movq %rcx, %r11
xorl %r12d, %r12d
cmpq $0x200, %r12            # imm = 0x200
je 0x571b
movsd (%r8,%r12), %xmm3
mulsd (%r11), %xmm3
addsd %xmm3, %xmm2
addq $0x40, %r12
addq %rdx, %r11
jmp 0x56fa
mulsd %xmm1, %xmm2
mulsd %xmm0, %xmm2
movsd %xmm2, (%r10)
incq %r9
addq $0x8, %r8
jmp 0x56e5
incq %rsi
addq $0x8, %rcx
addq $0x10, %rdx
jmp 0x56b6
movslq (%rsp), %r12
leaq 0x5ad4(%rip), %rax      # 0xb220
movzbl (%r12,%rax), %eax
movl %eax, 0x8(%rsp)
shll $0x6, %eax
leaq 0x2411(%rip), %r15      # 0x7b70
addq %rax, %r15
leaq 0x2487(%rip), %rbp      # 0x7bf0
xorl %eax, %eax
cmpq $0x8, %rax
je 0x57be
movq %rax, (%rsp)
xorl %r13d, %r13d
cmpq $0x8, %r13
je 0x57a9
movsd (%r14,%r13,8), %xmm0
movzbl (%r15,%r13), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
callq 0x5090
cvttsd2si %xmm0, %eax
movslq (%rbp,%r13,4), %rcx
movw %ax, 0x30(%rsp,%rcx,2)
incq %r13
jmp 0x5778
movq (%rsp), %rax
incq %rax
addq $0x8, %r15
addq $0x40, %r14
addq $0x20, %rbp
jmp 0x576b
movswl 0x30(%rsp), %eax
leaq 0x6198(%rip), %rcx      # 0xb962
movswl (%rcx,%r12,2), %edx
movl %eax, %esi
subl %edx, %esi
movl %esi, 0x18(%rsp)
movw %ax, (%rcx,%r12,2)
cmpw %dx, %ax
jne 0x582d
xorl %r15d, %r15d
leaq 0x4975(%rip), %rbp      # 0xa160
movl 0x8(%rsp), %eax
movzbl %al, %eax
imulq $0x60, %rax, %rax
movq (%rax,%rbp), %r14
movq %r14, %rdi
callq 0x50a0
cmpq %r15, %rax
jbe 0x5894
movsbl (%r14,%r15), %esi
addl $-0x30, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x54c8
incq %r15
leaq 0x59f9(%rip), %rax      # 0xb220
movb (%r12,%rax), %al
jmp 0x57ef
leaq 0x18(%rsp), %rdi
callq 0x5598
movzbl %al, %r14d
xorl %ebp, %ebp
leaq 0x491c(%rip), %r13      # 0xa160
movl 0x8(%rsp), %eax
movzbl %al, %eax
imulq $0x60, %rax, %rax
addq %r13, %rax
movq (%rax,%r14,8), %r15
movq %r15, %rdi
callq 0x50a0
cmpq %rbp, %rax
jbe 0x5885
movsbl (%r15,%rbp), %esi
addl $-0x30, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x54c8
incq %rbp
leaq 0x59a1(%rip), %rax      # 0xb220
movb (%r12,%rax), %al
jmp 0x5848
movl 0x18(%rsp), %esi
movq %rbx, %rdi
movl %r14d, %edx
callq 0x556d
pushq $0x40
popq %rdx
pushq $0x1
popq %rsi
leaq -0x1(%rdx), %rax
cmpl $0x2, %eax
jb 0x58b2
movl %eax, %ecx
cmpw $0x0, 0x30(%rsp,%rcx,2)
jne 0x58b8
movq %rax, %rdx
jmp 0x589a
movl %esi, 0x10(%rsp)
jmp 0x58bf
leal -0x1(%rdx), %eax
movl %eax, 0x10(%rsp)
xorl %ecx, %ecx
pushq $0x1
popq %rax
leaq 0x4955(%rip), %rbp      # 0xa220
movq %rdx, 0x20(%rsp)
cmpq %rax, %rdx
je 0x59db
movswl 0x30(%rsp,%rax,2), %esi
testl %esi, %esi
je 0x59d1
movl %esi, 0x1c(%rsp)
movq %rax, 0x28(%rsp)
movq %rcx, 0x8(%rsp)
cmpl $0x10, %ecx
jl 0x5950
movq 0x8(%rsp), %rax
shrl $0x4, %eax
movl %eax, (%rsp)
xorl %r14d, %r14d
cmpl (%rsp), %r14d
je 0x5950
xorl %r13d, %r13d
leaq 0x5909(%rip), %rax      # 0xb220
movzbl (%r12,%rax), %eax
shll $0xb, %eax
movq 0x780(%rax,%rbp), %r15
movq %r15, %rdi
callq 0x50a0
cmpq %r13, %rax
jbe 0x594b
movsbl (%r15,%r13), %esi
addl $-0x30, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x54c8
incq %r13
jmp 0x5910
incl %r14d
jmp 0x5907
movl 0x1c(%rsp), %eax
movl %eax, 0x14(%rsp)
leaq 0x14(%rsp), %rdi
callq 0x5598
movzbl %al, %ecx
movq 0x8(%rsp), %rax
shll $0x4, %eax
movzbl %al, %r13d
movl %ecx, (%rsp)
orl %ecx, %r13d
xorl %r14d, %r14d
leaq 0x589f(%rip), %rax      # 0xb220
movzbl (%r12,%rax), %eax
shll $0xb, %eax
addq %rbp, %rax
movq (%rax,%r13,8), %r15
movq %r15, %rdi
callq 0x50a0
cmpq %r14, %rax
jbe 0x59b4
movsbl (%r15,%r14), %esi
addl $-0x30, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x54c8
incq %r14
jmp 0x597a
movl 0x14(%rsp), %esi
movq %rbx, %rdi
movl (%rsp), %edx
callq 0x556d
xorl %ecx, %ecx
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rax
jmp 0x59d3
incl %ecx
incq %rax
jmp 0x58d0
cmpl $0x3f, 0x10(%rsp)
jne 0x59f4
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
leaq 0x5822(%rip), %rax      # 0xb220
movzbl (%r12,%rax), %eax
shll $0xb, %eax
movq (%rax,%rbp), %r14
movq %r14, %rdi
callq 0x50a0
cmpq %r15, %rax
jbe 0x59e2
movsbl (%r14,%r15), %esi
addl $-0x30, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x54c8
incq %r15
jmp 0x59f7