// File: rapirent[P]JPEG/encoder.c
// Function: calculate_mcu(_IO_FILE*, int, int)
void calculate_mcu(FILE *fp,int block_x, int block_y)
{

    rgb_t colour;
    char mcu[8][8];
    byte R,G,B;
    for (int yuv_id = 0; yuv_id<3; yuv_id++) {
        for (int y=0; y<8; y++) {
            for (int x=0; x<8; x++) {
                if (image_width - block_x < 7 && image_height - block_y < 7) {
                    rgb_image.get_pixel((x + block_x - 8), (y + block_y - 8), colour);
                } else if (image_width - block_x < 7) {
                    rgb_image.get_pixel((x + block_x - 8), y, colour);
                } else if(image_height - block_y < 7) {
                    rgb_image.get_pixel(x, (y + block_y - 8), colour);
                } else {
                    rgb_image.get_pixel(x+block_x, y+block_y, colour);
                }
                switch(yuv_id) {
                    case 0:
                        mcu[y][x] = (byte)(0.299 * colour.red + 0.587 * colour.green + 0.114 * colour.blue - 128);
                        break;
                    case 1:
                        mcu[y][x] = (byte)(-0.1687 * colour.red - 0.3313 * colour.green + 0.5 * colour.blue);
                        break;
                    case 2:
                        mcu[y][x] = (byte)(0.5 * colour.red - 0.4187 * colour.green - 0.0813 * colour.blue);
                        break;
                }
            }
        }
        calculate_mcu_block(fp,mcu,yuv_id);
    }

}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movl %esi, 0x6c(%rsp)
movl %edx, 0x68(%rsp)
movl $0x0, 0x18(%rsp)
cmpl $0x3, 0x18(%rsp)
jge 0x6426
movl $0x0, 0x14(%rsp)
cmpl $0x8, 0x14(%rsp)
jge 0x6403
movl $0x0, 0x10(%rsp)
cmpl $0x8, 0x10(%rsp)
jge 0x63f1
movl 0x9128(%rip), %eax      # 0xf2c8
subl 0x6c(%rsp), %eax
cmpl $0x7, %eax
jge 0x61e4
movl 0x9115(%rip), %eax      # 0xf2c4
subl 0x68(%rsp), %eax
cmpl $0x7, %eax
jge 0x61e4
movl 0x10(%rsp), %esi
addl 0x6c(%rsp), %esi
subl $0x8, %esi
movl 0x14(%rsp), %edx
addl 0x68(%rsp), %edx
subl $0x8, %edx
leaq 0x917b(%rip), %rdi      # 0xf350
leaq 0x65(%rsp), %rcx
callq 0x71c0
jmp 0x626b
movl 0x90de(%rip), %eax      # 0xf2c8
subl 0x6c(%rsp), %eax
cmpl $0x7, %eax
jge 0x6215
movl 0x10(%rsp), %esi
addl 0x6c(%rsp), %esi
subl $0x8, %esi
movl 0x14(%rsp), %edx
leaq 0x9147(%rip), %rdi      # 0xf350
leaq 0x65(%rsp), %rcx
callq 0x71c0
jmp 0x6269
movl 0x90a9(%rip), %eax      # 0xf2c4
subl 0x68(%rsp), %eax
cmpl $0x7, %eax
jge 0x6246
movl 0x10(%rsp), %esi
movl 0x14(%rsp), %edx
addl 0x68(%rsp), %edx
subl $0x8, %edx
leaq 0x9116(%rip), %rdi      # 0xf350
leaq 0x65(%rsp), %rcx
callq 0x71c0
jmp 0x6267
movl 0x10(%rsp), %esi
addl 0x6c(%rsp), %esi
movl 0x14(%rsp), %edx
addl 0x68(%rsp), %edx
leaq 0x90f3(%rip), %rdi      # 0xf350
leaq 0x65(%rsp), %rcx
callq 0x71c0
jmp 0x6269
jmp 0x626b
movl 0x18(%rsp), %eax
movl %eax, 0xc(%rsp)
testl %eax, %eax
je 0x629a
jmp 0x6279
movl 0xc(%rsp), %eax
subl $0x1, %eax
je 0x6311
jmp 0x6288
movl 0xc(%rsp), %eax
subl $0x2, %eax
je 0x6379
jmp 0x63df
movzbl 0x65(%rsp), %eax
cvtsi2sd %eax, %xmm1
movzbl 0x66(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x3d9c(%rip), %xmm2     # 0xa050
mulsd %xmm2, %xmm0
movsd 0x3d98(%rip), %xmm2     # 0xa058
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movzbl 0x67(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x3d87(%rip), %xmm2     # 0xa060
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x3d7f(%rip), %xmm1     # 0xa068
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, %dl
movslq 0x14(%rsp), %rcx
leaq 0x20(%rsp), %rax
shlq $0x3, %rcx
addq %rcx, %rax
movslq 0x10(%rsp), %rcx
movb %dl, (%rax,%rcx)
jmp 0x63df
movzbl 0x65(%rsp), %eax
cvtsi2sd %eax, %xmm1
movzbl 0x66(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x3d15(%rip), %xmm2     # 0xa040
mulsd %xmm2, %xmm0
movsd 0x3d11(%rip), %xmm2     # 0xa048
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movzbl 0x67(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x3cd0(%rip), %xmm2     # 0xa020
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, %dl
movslq 0x14(%rsp), %rcx
leaq 0x20(%rsp), %rax
shlq $0x3, %rcx
addq %rcx, %rax
movslq 0x10(%rsp), %rcx
movb %dl, (%rax,%rcx)
jmp 0x63df
movzbl 0x65(%rsp), %eax
cvtsi2sd %eax, %xmm1
movzbl 0x66(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x3c9d(%rip), %xmm2     # 0xa030
mulsd %xmm2, %xmm0
movsd 0x3c81(%rip), %xmm2     # 0xa020
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movzbl 0x67(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd 0x3c80(%rip), %xmm2     # 0xa038
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, %dl
movslq 0x14(%rsp), %rcx
leaq 0x20(%rsp), %rax
shlq $0x3, %rcx
addq %rcx, %rax
movslq 0x10(%rsp), %rcx
movb %dl, (%rax,%rcx)
jmp 0x63e1
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x618f
jmp 0x63f3
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x617c
movq 0x70(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movl 0x18(%rsp), %edx
callq 0x58a0
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x6169
addq $0x78, %rsp
retq
nopl (%rax,%rax)