// File: 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:
subq $0x528, %rsp            # imm = 0x528
movq %rdi, 0x520(%rsp)
movq %rsi, 0x518(%rsp)
movl %edx, 0x514(%rsp)
leaq 0x310(%rsp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x5120
leaq 0x110(%rsp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x5120
movl $0x0, 0x8c(%rsp)
cmpl $0x8, 0x8c(%rsp)
jge 0x5a4d
movl $0x0, 0x88(%rsp)
cmpl $0x8, 0x88(%rsp)
jge 0x5a35
movl $0x0, 0x84(%rsp)
cmpl $0x8, 0x84(%rsp)
jge 0x59bf
movq 0x518(%rsp), %rax
movslq 0x8c(%rsp), %rcx
leaq (%rax,%rcx,8), %rcx
movslq 0x84(%rsp), %rax
movsbl (%rax,%rcx), %eax
cvtsi2sd %eax, %xmm0
movl 0x84(%rsp), %eax
shll %eax
addl $0x1, %eax
imull 0x88(%rsp), %eax
movslq %eax, %rcx
leaq 0x9a2d(%rip), %rax      # 0xf3a0
movsd (%rax,%rcx,8), %xmm2
movslq 0x8c(%rsp), %rcx
leaq 0x110(%rsp), %rax
shlq $0x6, %rcx
addq %rcx, %rax
movslq 0x88(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x5923
movl 0x88(%rsp), %edi
callq 0x5640
movslq 0x8c(%rsp), %rcx
leaq 0x110(%rsp), %rax
shlq $0x6, %rcx
addq %rcx, %rax
movslq 0x88(%rsp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd 0x4631(%rip), %xmm1     # 0xa028
divsd %xmm1, %xmm0
movslq 0x8c(%rsp), %rcx
leaq 0x110(%rsp), %rax
shlq $0x6, %rcx
addq %rcx, %rax
movslq 0x88(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x88(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x88(%rsp)
jmp 0x590a
jmp 0x5a37
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x58f1
movl $0x0, 0x80(%rsp)
cmpl $0x8, 0x80(%rsp)
jge 0x5b83
movl $0x0, 0x7c(%rsp)
cmpl $0x8, 0x7c(%rsp)
jge 0x5b6b
movl $0x0, 0x78(%rsp)
cmpl $0x8, 0x78(%rsp)
jge 0x5b01
movslq 0x78(%rsp), %rcx
leaq 0x110(%rsp), %rax
shlq $0x6, %rcx
addq %rcx, %rax
movslq 0x7c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movl 0x78(%rsp), %eax
shll %eax
addl $0x1, %eax
imull 0x80(%rsp), %eax
movslq %eax, %rcx
leaq 0x98df(%rip), %rax      # 0xf3a0
movsd (%rax,%rcx,8), %xmm2
movslq 0x80(%rsp), %rcx
leaq 0x310(%rsp), %rax
shlq $0x6, %rcx
addq %rcx, %rax
movslq 0x7c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x5a81
movl 0x80(%rsp), %edi
callq 0x5640
movslq 0x80(%rsp), %rcx
leaq 0x310(%rsp), %rax
shlq $0x6, %rcx
addq %rcx, %rax
movslq 0x7c(%rsp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd 0x44f2(%rip), %xmm1     # 0xa028
divsd %xmm1, %xmm0
movslq 0x80(%rsp), %rcx
leaq 0x310(%rsp), %rax
shlq $0x6, %rcx
addq %rcx, %rax
movslq 0x7c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x5a6e
jmp 0x5b6d
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0x5a58
movl $0x0, 0x74(%rsp)
cmpl $0x8, 0x74(%rsp)
jge 0x5c51
movl $0x0, 0x70(%rsp)
cmpl $0x8, 0x70(%rsp)
jge 0x5c3f
movslq 0x74(%rsp), %rcx
movl %ecx, %eax
shlq $0x6, %rcx
leaq 0x310(%rsp,%rcx), %rcx
movslq 0x70(%rsp), %rdx
movl %edx, %esi
movsd (%rcx,%rdx,8), %xmm0
movslq 0x514(%rsp), %rcx
leaq 0x96c9(%rip), %rdx      # 0xf2a0
movzbl (%rcx,%rdx), %ecx
shll $0x6, %ecx
movl %ecx, %edx
leaq 0x4f79(%rip), %rcx      # 0xab60
addq %rdx, %rcx
movl %eax, %edx
movl %esi, %eax
leal (%rax,%rdx,8), %eax
cltq
movzbl (%rax,%rcx), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
callq 0x50d0
cvttsd2si %xmm0, %eax
movw %ax, %cx
movslq 0x74(%rsp), %rdx
leaq 0x4fc9(%rip), %rax      # 0xabe0
shlq $0x5, %rdx
addq %rdx, %rax
movslq 0x70(%rsp), %rdx
movslq (%rax,%rdx,4), %rax
movw %cx, 0x90(%rsp,%rax,2)
movl 0x70(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x70(%rsp)
jmp 0x5b9e
jmp 0x5c41
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x5b8b
movswl 0x90(%rsp), %eax
movslq 0x514(%rsp), %rdx
leaq 0x9d7a(%rip), %rcx      # 0xf9e2
movswl (%rcx,%rdx,2), %ecx
subl %ecx, %eax
movl %eax, 0x6c(%rsp)
movw 0x90(%rsp), %dx
movslq 0x514(%rsp), %rcx
leaq 0x9d59(%rip), %rax      # 0xf9e2
movw %dx, (%rax,%rcx,2)
cmpl $0x0, 0x6c(%rsp)
jne 0x5d38
movl $0x0, 0x60(%rsp)
movslq 0x60(%rsp), %rax
movq %rax, 0x30(%rsp)
movslq 0x514(%rsp), %rcx
leaq 0x95e7(%rip), %rax      # 0xf2a0
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x851a(%rip), %rax      # 0xe1e0
imulq $0x60, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x50f0
movq %rax, %rcx
movq 0x30(%rsp), %rax
cmpq %rcx, %rax
jae 0x5d33
movq 0x520(%rsp), %rdi
movslq 0x514(%rsp), %rcx
leaq 0x95a7(%rip), %rax      # 0xf2a0
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x84da(%rip), %rax      # 0xe1e0
imulq $0x60, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movslq 0x60(%rsp), %rcx
movsbl (%rax,%rcx), %esi
subl $0x30, %esi
xorl %edx, %edx
callq 0x5680
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x5ca0
jmp 0x5e08
leaq 0x6c(%rsp), %rdi
callq 0x5810
movzbl %al, %eax
movl %eax, 0x68(%rsp)
movl $0x0, 0x5c(%rsp)
movslq 0x5c(%rsp), %rax
movq %rax, 0x28(%rsp)
movslq 0x514(%rsp), %rcx
leaq 0x9536(%rip), %rax      # 0xf2a0
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x8469(%rip), %rax      # 0xe1e0
imulq $0x60, %rcx, %rcx
addq %rcx, %rax
movslq 0x68(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x50f0
movq %rax, %rcx
movq 0x28(%rsp), %rax
cmpq %rcx, %rax
jae 0x5df0
movq 0x520(%rsp), %rdi
movslq 0x514(%rsp), %rcx
leaq 0x94f0(%rip), %rax      # 0xf2a0
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x8423(%rip), %rax      # 0xe1e0
imulq $0x60, %rcx, %rcx
addq %rcx, %rax
movslq 0x68(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x5c(%rsp), %rcx
movsbl (%rax,%rcx), %esi
subl $0x30, %esi
xorl %edx, %edx
callq 0x5680
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x5d51
movq 0x520(%rsp), %rdi
movl 0x6c(%rsp), %esi
movl 0x68(%rsp), %eax
movzbl %al, %edx
callq 0x57b0
movl $0x0, 0x58(%rsp)
movl $0x3f, 0x4c(%rsp)
xorl %eax, %eax
cmpl $0x1, 0x4c(%rsp)
movb %al, 0x27(%rsp)
jle 0x5e3c
movslq 0x4c(%rsp), %rax
cmpw $0x0, 0x90(%rsp,%rax,2)
setne %al
xorb $-0x1, %al
movb %al, 0x27(%rsp)
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0x5e46
jmp 0x5e53
movl 0x4c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x5e18
movl $0x1, 0x48(%rsp)
movl 0x48(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jg 0x609a
movslq 0x48(%rsp), %rax
cmpw $0x0, 0x90(%rsp,%rax,2)
jne 0x5e89
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x608a
cmpl $0x10, 0x58(%rsp)
jl 0x5f83
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0x58(%rsp), %eax
movl $0x10, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl 0x20(%rsp), %eax
cmpl %ecx, %eax
jge 0x5f73
movl $0x0, 0x40(%rsp)
movslq 0x40(%rsp), %rax
movq %rax, 0x18(%rsp)
movslq 0x514(%rsp), %rcx
leaq 0x93c1(%rip), %rax      # 0xf2a0
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x83b4(%rip), %rax      # 0xe2a0
shlq $0xb, %rcx
addq %rcx, %rax
movq 0x780(%rax), %rdi
callq 0x50f0
movq %rax, %rcx
movq 0x18(%rsp), %rax
cmpq %rcx, %rax
jae 0x5f61
movq 0x520(%rsp), %rdi
movslq 0x514(%rsp), %rcx
leaq 0x937d(%rip), %rax      # 0xf2a0
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x8370(%rip), %rax      # 0xe2a0
shlq $0xb, %rcx
addq %rcx, %rax
movq 0x780(%rax), %rax
movslq 0x40(%rsp), %rcx
movsbl (%rax,%rcx), %esi
subl $0x30, %esi
xorl %edx, %edx
callq 0x5680
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x5ec6
jmp 0x5f63
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x5e9c
movl 0x58(%rsp), %eax
movl $0x10, %ecx
cltd
idivl %ecx
movl %edx, 0x58(%rsp)
movslq 0x48(%rsp), %rax
movswl 0x90(%rsp,%rax,2), %eax
movl %eax, 0x54(%rsp)
leaq 0x54(%rsp), %rdi
callq 0x5810
movzbl %al, %eax
movl %eax, 0x68(%rsp)
movl $0x0, 0x3c(%rsp)
movslq 0x3c(%rsp), %rax
movq %rax, 0x10(%rsp)
movslq 0x514(%rsp), %rcx
leaq 0x92da(%rip), %rax      # 0xf2a0
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x82cd(%rip), %rax      # 0xe2a0
shlq $0xb, %rcx
addq %rcx, %rax
movl 0x58(%rsp), %ecx
shll $0x4, %ecx
andl $0xf0, %ecx
orl 0x68(%rsp), %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x50f0
movq %rax, %rcx
movq 0x10(%rsp), %rax
cmpq %rcx, %rax
jae 0x606a
movq 0x520(%rsp), %rdi
movslq 0x514(%rsp), %rcx
leaq 0x9285(%rip), %rax      # 0xf2a0
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x8278(%rip), %rax      # 0xe2a0
shlq $0xb, %rcx
addq %rcx, %rax
movl 0x58(%rsp), %ecx
shll $0x4, %ecx
andl $0xf0, %ecx
orl 0x68(%rsp), %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq 0x3c(%rsp), %rcx
movsbl (%rax,%rcx), %esi
subl $0x30, %esi
xorl %edx, %edx
callq 0x5680
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x5fad
movq 0x520(%rsp), %rdi
movl 0x54(%rsp), %esi
movl 0x68(%rsp), %eax
movzbl %al, %edx
callq 0x57b0
movl $0x0, 0x58(%rsp)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x5e5b
cmpl $0x3f, 0x4c(%rsp)
je 0x6142
movl $0x0, 0x38(%rsp)
movslq 0x38(%rsp), %rax
movq %rax, 0x8(%rsp)
movslq 0x514(%rsp), %rcx
leaq 0x91da(%rip), %rax      # 0xf2a0
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x81cd(%rip), %rax      # 0xe2a0
shlq $0xb, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x50f0
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
jae 0x6140
movq 0x520(%rsp), %rdi
movslq 0x514(%rsp), %rcx
leaq 0x919a(%rip), %rax      # 0xf2a0
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x818d(%rip), %rax      # 0xe2a0
shlq $0xb, %rcx
addq %rcx, %rax
movq (%rax), %rax
movslq 0x38(%rsp), %rcx
movsbl (%rax,%rcx), %esi
subl $0x30, %esi
xorl %edx, %edx
callq 0x5680
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x60ad
jmp 0x6142
addq $0x528, %rsp            # imm = 0x528
retq
nopw (%rax,%rax)