// File: /workspace/github_cpp_makefile_star2_O3/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 $0x4d8, %rsp            # imm = 0x4D8
movl %edx, %ebp
movq %rsi, %r12
movq %rdi, 0x18(%rsp)
leaq 0x2d0(%rsp), %r14
xorl %r13d, %r13d
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
xorl %esi, %esi
callq 0x50c0
leaq 0xd0(%rsp), %r15
movl $0x200, %edx            # imm = 0x200
movq %r15, %rdi
xorl %esi, %esi
callq 0x50c0
leaq 0x6cb1(%rip), %rcx      # 0xc300
leaq 0x2a0a(%rip), %rax      # 0x8060
movsd 0x29ba(%rip), %xmm0     # 0x8018
movq %r13, %rdx
shlq $0x6, %rdx
addq %rsp, %rdx
addq $0xd0, %rdx
xorl %esi, %esi
movq %rcx, %rdi
xorl %r8d, %r8d
leaq (%rdx,%r8,8), %r9
movsd (%r9), %xmm1
movq %rdi, %r10
xorl %r11d, %r11d
movsbl (%r12,%r11), %ebx
xorps %xmm2, %xmm2
cvtsi2sd %ebx, %xmm2
mulsd (%r10), %xmm2
addsd %xmm2, %xmm1
incq %r11
addq %rsi, %r10
cmpq $0x8, %r11
jne 0x5686
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
cmpq $0x8, %r8
jne 0x5677
incq %r13
addq $0x8, %r12
cmpq $0x8, %r13
jne 0x565e
leaq 0x6c1b(%rip), %rcx      # 0xc300
xorl %edx, %edx
xorl %esi, %esi
xorl %edi, %edi
testq %rsi, %rsi
sete %dil
movsd (%rax,%rdi,8), %xmm1
movq %rsi, %rdi
shlq $0x6, %rdi
addq %rsp, %rdi
addq $0x2d0, %rdi            # imm = 0x2D0
movq %r15, %r8
xorl %r9d, %r9d
leaq (%rdi,%r9,8), %r10
movsd (%r10), %xmm2
movq %rcx, %r11
xorl %ebx, %ebx
movsd (%r8,%rbx), %xmm3
mulsd (%r11), %xmm3
addsd %xmm3, %xmm2
addq $0x40, %rbx
addq %rdx, %r11
cmpq $0x200, %rbx            # imm = 0x200
jne 0x571c
mulsd %xmm1, %xmm2
mulsd %xmm0, %xmm2
movsd %xmm2, (%r10)
incq %r9
addq $0x8, %r8
cmpq $0x8, %r9
jne 0x570e
incq %rsi
addq $0x8, %rcx
addq $0x10, %rdx
cmpq $0x8, %rsi
jne 0x56e9
movslq %ebp, %rax
leaq 0x6a90(%rip), %rcx      # 0xc200
movq %rax, 0x8(%rsp)
movzbl (%rax,%rcx), %r15d
movl %r15d, %eax
shll $0x6, %eax
leaq 0x33e9(%rip), %r12      # 0x8b70
addq %rax, %r12
leaq 0x345f(%rip), %r13      # 0x8bf0
xorl %ebp, %ebp
xorl %ebx, %ebx
movsd (%r14,%rbx,8), %xmm0
movzbl (%r12,%rbx), %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
callq 0x5080
cvttsd2si %xmm0, %eax
movslq (%r13,%rbx,4), %rcx
movw %ax, 0x50(%rsp,%rcx,2)
incq %rbx
cmpq $0x8, %rbx
jne 0x5795
incq %rbp
addq $0x40, %r14
addq $0x20, %r13
addq $0x8, %r12
cmpq $0x8, %rbp
jne 0x5793
movswl 0x50(%rsp), %eax
leaq 0x715a(%rip), %rdx      # 0xc942
movq 0x8(%rsp), %rsi
movswl (%rdx,%rsi,2), %ecx
movw %ax, (%rdx,%rsi,2)
cmpw %cx, %ax
jne 0x585f
leaq (%r15,%r15,2), %rax
shll $0x5, %eax
leaq 0x5938(%rip), %r12      # 0xb140
movq (%rax,%r12), %r14
cmpb $0x0, (%r14)
movq 0x18(%rsp), %r13
je 0x5913
xorl %ebx, %ebx
movsbl (%r14,%rbx), %esi
addl $-0x30, %esi
movq %r13, %rdi
xorl %edx, %edx
callq 0x54df
incq %rbx
movq 0x8(%rsp), %rax
leaq 0x69c2(%rip), %rcx      # 0xc200
movzbl (%rax,%rcx), %eax
leaq (%rax,%rax,2), %rax
shll $0x5, %eax
movq (%rax,%r12), %r14
movq %r14, %rdi
callq 0x5090
cmpq %rbx, %rax
ja 0x581d
jmp 0x5913
movq %r15, %rsi
subl %ecx, %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
movl $0x1, %ebp
leaq 0x58c9(%rip), %r12      # 0xb140
movl %ecx, %edx
shrl %edx
incl %ebp
addq $0x8, %r12
cmpl $0x1, %ecx
movl %edx, %ecx
ja 0x5879
leal -0x1(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
testl %eax, %eax
leal -0x1(%rax,%rdx), %r15d
cmovgl %eax, %r15d
leaq (%rsi,%rsi,2), %rax
shll $0x5, %eax
movq (%r12,%rax), %r14
cmpb $0x0, (%r14)
movq 0x18(%rsp), %r13
je 0x58f2
xorl %ebx, %ebx
movsbl (%r14,%rbx), %esi
addl $-0x30, %esi
movq %r13, %rdi
xorl %edx, %edx
callq 0x54df
incq %rbx
movq 0x8(%rsp), %rax
leaq 0x692a(%rip), %rcx      # 0xc200
movzbl (%rax,%rcx), %eax
leaq (%rax,%rax,2), %rax
shll $0x5, %eax
movq (%r12,%rax), %r14
movq %r14, %rdi
callq 0x5090
cmpq %rbx, %rax
ja 0x58b5
leal -0x2(%rbp), %eax
movzbl %al, %eax
xorl %esi, %esi
btl %eax, %r15d
setb %sil
movq %r13, %rdi
xorl %edx, %edx
callq 0x54df
decl %ebp
cmpl $0x1, %ebp
ja 0x58f2
movl $0x3f, %ecx
movq 0x8(%rsp), %r15
leaq 0x68dc(%rip), %r13      # 0xc200
movl %ecx, %eax
cmpw $0x0, 0x50(%rsp,%rax,2)
jne 0x5944
leal -0x1(%rcx), %eax
cmpl $0x2, %ecx
movl %eax, %ecx
ja 0x5924
movl $0x1, %ecx
movq 0x18(%rsp), %rbp
jmp 0x5951
testl %ecx, %ecx
movq 0x18(%rsp), %rbp
jle 0x5b24
movq %rcx, 0x30(%rsp)
leal 0x1(%rcx), %eax
movq %rax, 0x38(%rsp)
xorl %r14d, %r14d
movl $0x1, %ebx
movswl 0x50(%rsp,%rbx,2), %eax
movq %rax, 0x48(%rsp)
testl %eax, %eax
je 0x5b07
movq %rbx, 0x40(%rsp)
movq %r14, 0x28(%rsp)
cmpl $0x10, %r14d
leaq 0x5873(%rip), %r15      # 0xb200
jl 0x5a23
movq 0x28(%rsp), %rax
shrl $0x4, %eax
movl %eax, 0x14(%rsp)
movq 0x8(%rsp), %rax
movb (%rax,%r13), %bl
xorl %ecx, %ecx
movzbl %bl, %eax
shll $0xb, %eax
movq 0x780(%rax,%r15), %r14
cmpb $0x0, (%r14)
je 0x5a1b
movl %ecx, 0x20(%rsp)
xorl %ebp, %ebp
movq 0x18(%rsp), %r15
movq %r13, %r12
movq 0x8(%rsp), %r13
movsbl (%r14,%rbp), %esi
addl $-0x30, %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x54df
incq %rbp
movzbl (%r13,%r12), %ebx
movl %ebx, %eax
shll $0xb, %eax
leaq 0x5808(%rip), %rcx      # 0xb200
movq 0x780(%rax,%rcx), %r14
movq %r14, %rdi
callq 0x5090
cmpq %rbp, %rax
ja 0x59d1
movq %r12, %r13
leaq 0x57e9(%rip), %r15      # 0xb200
movl 0x20(%rsp), %ecx
incl %ecx
cmpl 0x14(%rsp), %ecx
jne 0x59aa
movq 0x48(%rsp), %r8
movl %r8d, %eax
negl %eax
cmovsl %r8d, %eax
xorl %ecx, %ecx
movl $0x2, %esi
movl %eax, %edx
movl %esi, %r12d
shrl %edx
incl %ecx
leal 0x1(%r12), %esi
cmpl $0x1, %eax
movl %edx, %eax
ja 0x5a3a
movl $0x1, %eax
shll %cl, %eax
testw %r8w, %r8w
leal -0x1(%r8,%rax), %eax
cmovgl %r8d, %eax
movl %eax, 0x14(%rsp)
movq 0x28(%rsp), %rax
shll $0x4, %eax
movzbl %al, %edx
orl %ecx, %edx
movq 0x8(%rsp), %rax
movzbl (%rax,%r13), %eax
shll $0xb, %eax
addq %r15, %rax
movq %rdx, 0x20(%rsp)
movq (%rax,%rdx,8), %r14
cmpb $0x0, (%r14)
movq 0x18(%rsp), %rbp
je 0x5ad3
xorl %ebx, %ebx
movsbl (%r14,%rbx), %esi
addl $-0x30, %esi
movq %rbp, %rdi
xorl %edx, %edx
callq 0x54df
incq %rbx
movq 0x8(%rsp), %rax
movzbl (%rax,%r13), %eax
shll $0xb, %eax
addq %r15, %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %r14
movq %r14, %rdi
callq 0x5090
cmpq %rbx, %rax
ja 0x5a98
movq 0x40(%rsp), %rbx
movl 0x14(%rsp), %r15d
leal -0x2(%r12), %eax
movzbl %al, %eax
xorl %esi, %esi
btl %eax, %r15d
setb %sil
xorl %r14d, %r14d
movq %rbp, %rdi
xorl %edx, %edx
callq 0x54df
decl %r12d
cmpl $0x1, %r12d
ja 0x5add
jmp 0x5b0a
incl %r14d
incq %rbx
cmpq 0x38(%rsp), %rbx
jne 0x5966
cmpl $0x3f, 0x30(%rsp)
movq 0x8(%rsp), %r15
je 0x5b6e
movzbl (%r15,%r13), %eax
shll $0xb, %eax
leaq 0x56cd(%rip), %rbx      # 0xb200
movq (%rax,%rbx), %r14
cmpb $0x0, (%r14)
je 0x5b6e
xorl %r12d, %r12d
movsbl (%r14,%r12), %esi
addl $-0x30, %esi
movq %rbp, %rdi
xorl %edx, %edx
callq 0x54df
incq %r12
movzbl (%r15,%r13), %eax
shll $0xb, %eax
movq (%rax,%rbx), %r14
movq %r14, %rdi
callq 0x5090
cmpq %r12, %rax
ja 0x5b40
addq $0x4d8, %rsp            # imm = 0x4D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq