// File: /workspace/github_cpp_makefile_star2_O3/qhan1028[P]JPEG-Decoder/jpeg-decoder.cpp
// Function: process_scan(std::basic_ifstream<char, std::char_traits<char>>&, unsigned char**, sof&, std::map<std::pair<unsigned char, unsigned short>, unsigned char, std::less<std::pair<unsigned char, unsigned short>>, std::allocator<std::pair<std::pair<unsigned char, unsigned short> const, unsigned char>>>*, sos&)
image_t process_scan(ifstream& jpegfile, qtable_t qtable, sof_t& sof, htable_t* htable, sos_t& sos) {
    if (debug) {
        cout << "[Process Scan]" << endl;
        cout << "  get index" << endl;
    }

    /* get number of blocks in MCU, get start px and py of each block */
    unsigned int n_blocks = 0, ci = 0;
    vector<unsigned int> cid(n_blocks); // channel ids for each blocks in 1 MCU
    vector<block_sxy_t> b_sxy(n_blocks); // start index of blocks of each channel in 1 MCU

    for (unsigned int i = 1; i < sof.N; i++) { // id starts from 1
        n_blocks += sof.hvq[i].H * sof.hvq[i].V;

        cid.resize(n_blocks); // extend cid
        b_sxy.resize(n_blocks); // extend b_sxy
        unsigned int sci = ci; // start of current ci
        while(ci < n_blocks) {
            cid[ci] = i;
            b_sxy[ci].stx = ((ci - sci) % sof.hvq[i].H) * 8;
            b_sxy[ci].sty = ((ci - sci) / sof.hvq[i].H) * 8;
            b_sxy[ci].sdx = sof.hmax / sof.hvq[i].H;
            b_sxy[ci].sdy = sof.vmax / sof.hvq[i].V;
            ci++;
        }
    }

    unsigned int x_blocks = sof.hmax;
    unsigned int y_blocks = sof.vmax;
    unsigned int block_w = 8;
    unsigned int block_h = 8;

    /* get number of MCUs in this image */
    unsigned int MCU_w = sof.hmax * 8;
    unsigned int MCU_h = sof.vmax * 8;
    unsigned int x_MCUs = (sof.X - 1) / (MCU_w) + 1;
    unsigned int y_MCUs = (sof.Y - 1) / (MCU_h) + 1;
    unsigned int n_MCUs = x_MCUs * y_MCUs;

    if (debug) {
        cout << "  blocks in MCU = " << n_blocks << endl;
        cout << "  x MCUs = " << x_MCUs << endl;
        cout << "  y MCUs = " << y_MCUs << endl;
        cout << "  total MCUs = " << n_MCUs << endl;
        cout << "  read image..." << endl;
    }

    /* init image and buffer */
    image_t image = init_image(n_MCUs, n_blocks, 64);
    buffer_t buffer;

    /* prev dc, init to 0, index start from 1 */
    vector<uint16_t> prev_dc(sof.N, 0);

    /* read image */
    for (unsigned int i = 0; i < n_MCUs; i++) {
        for (unsigned int j = 0; j < n_blocks; j++) {

            bool end_of_block = false;

            /* qtable id, dc table id, ac table id */
            unsigned int qid = sof.hvq[cid[j]].Q;
            unsigned int dc = sos.cda[cid[j]].td;
            unsigned int ac = 2 + sos.cda[cid[j]].ta;

            for (unsigned int k = 0; k < 64; k++) {

                /* if end of block, then fill 0 until end */
                if (end_of_block) {
                    while (k < 64) {
                        image[i][j][k] = 0;
                        k++;
                    }
                    break;
                }

                /* get h table id */
                unsigned int hid = (k == 0) ? dc : ac;

                /* read bits and check htable */
                bool get_hkey = false;
                uint8_t len = 0;
                uint16_t symbol = 0;
                uint8_t code = 0;
                htable_t::iterator iter;
                do {
                    symbol = symbol << 1; // left shift 1 bit
                    symbol = symbol | read_bits(jpegfile, buffer, prev_dc, 1, true);
                    len += 1;

                    /* search from h table with length and symbol */
                    iter = htable[hid].find(hkey_t(len, symbol));
                    if (iter != htable[hid].end()) {
                        get_hkey = true;
                        code = iter->second;
                        //cout << "len = " << (int)len << ", symbol = " << symbol << ", code = " << (uint)code << endl;
                    }
                } while (!get_hkey);

                if (k == 0) {
                    int16_t delta = read_bits(jpegfile, buffer, prev_dc, code, false);
                    image[i][j][k] = prev_dc[cid[j]] + delta;
                    prev_dc[cid[j]] = image[i][j][k];

                    image[i][j][k] = image[i][j][k] * qtable[qid][k];

                } else {
                    if (code == 0x00) {
                        /* end of block */
                        image[i][j][k] = 0;
                        end_of_block = true;

                    } else {
                        /* run length coding */
                        uint8_t n_zeros = code >> 4;
                        uint8_t value = code & 0x0F;

                        k += n_zeros;
                        //cout << k << ", " << (int)n_zeros << ", " << (int)value << endl;

                        image[i][j][k] = read_bits(jpegfile, buffer, prev_dc, value, false);
                        image[i][j][k] = image[i][j][k] * qtable[qid][k];
                    }
                }
            }
            

            /* reverse zig-zag */
            int16_t uzz_block[64]; // un-zig-zagged block

            for (int k = 0; k < 64; k++) {
                uzz_block[k] = image[i][j][zzi[k]];
            }

            /* fast inverse discrete cosine transform */
            for (int r = 0; r < 8; r++) {
                fast_idct(uzz_block, r, true);
            }
            for (int c = 0; c < 8; c++) {
                fast_idct(uzz_block, c, false);
            }

            /* assign back to image */
            for (int k = 0; k < 64; k++) {
                image[i][j][k] = uzz_block[k];
            }
        }
    }

    if (debug) {
        cout << "  MCUs to YCbCr" << endl;
    }

    /* convert MCUs to YCbCr pixels */
    unsigned int channels = sos.ns;
    unsigned int height = MCU_h * y_MCUs;
    unsigned int width = MCU_w * x_MCUs;
    
    image_t ycbcr = init_image(channels, height, width);

    for (unsigned int i = 0; i < n_MCUs; i++) {
        /* find top left coord of MCU */
        unsigned int mcu_sx = (i % x_MCUs) * MCU_w;
        unsigned int mcu_sy = ((int)(i / x_MCUs)) * MCU_h;

        for (unsigned int j = 0; j < n_blocks; j++) {
            /* top left coord of block */
            unsigned int b_sx = mcu_sx + b_sxy[j].stx;
            unsigned int b_sy = mcu_sy + b_sxy[j].sty;

            /* channel */
            unsigned int c = cid[j] - 1; // channel id start from 1

            /* stride y */
            for (unsigned int sdy = 0; sdy < b_sxy[j].sdy; sdy++) {
                
                /* stride x */
                for (unsigned int sdx = 0; sdx < b_sxy[j].sdx; sdx++) {
                    
                    /* block */
                    for (unsigned int k = 0; k < 64; k++) {
                        /* find coord */
                        unsigned int px = b_sx + ((k % 8) * b_sxy[j].sdx + sdx);
                        unsigned int py = b_sy + ((k / 8) * b_sxy[j].sdy + sdy);

                        ycbcr[c][py][px] = image[i][j][k];
                    }
                }
            }
        }
    }

    /* free memory of image */
    delete_image(image, n_MCUs, n_blocks, 64);

    if (debug) {
        cout << "  YCbCr to RGB" << endl;
    }

    /* convert YCbCr to RGB */
    image_t rgb = init_image(channels, sof.Y, sof.X);

    for (int py = 0; py < sof.Y; py++) {
        for (int px = 0; px < sof.X; px++) {
            int16_t y = ycbcr[0][py][px];
            int16_t cb = ycbcr[1][py][px];
            int16_t cr = ycbcr[2][py][px];

            int16_t r = y + (int16_t)(1.402 * cr) + 128;
            int16_t g = y - (int16_t)(0.34414 * cb) - (int16_t)(0.71414 * cr) + 128;
            int16_t b = y + (int16_t)(1.772 * cb) + 128;

            rgb[R][py][px] = (r < 0) ? 0 : (r > 255) ? 255 : r;
            rgb[G][py][px] = (g < 0) ? 0 : (g > 255) ? 255 : g;
            rgb[B][py][px] = (b < 0) ? 0 : (b > 255) ? 255 : b;
        }
    }

    /* free memory of YCbCr */
    delete_image(ycbcr, channels, height, width);

    if (debug) {
        string rgb_str[3] = {"Red", "Green", "Blue"};

        for (int c = 0; c < 3; c++) {
            cout << "  " << rgb_str[c] << endl;

            for (int py = 0; py < 16; py++) {
                cout << "    ";

                for (int px = 0; px < 16; px++) {
                    printf("%3d ", rgb[0][py][px]);
                }
                cout << endl;
            }
            cout << endl;
        }
    }
    
    return rgb;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %r8, -0xe8(%rbp)
movq %rcx, -0x68(%rbp)
movq %rdx, %r14
movq %rsi, -0x60(%rbp)
movq %rdi, -0x48(%rbp)
cmpb $0x1, 0x5584(%rip)      # 0x91fa
jne 0x3cf5
movq 0x5351(%rip), %rbx      # 0x8fd0
leaq 0x25b7(%rip), %rsi      # 0x623d
movl $0xe, %edx
movq %rbx, %rdi
callq 0x2210
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
leaq 0x258b(%rip), %rsi      # 0x624c
movl $0xb, %edx
movq %rbx, %rdi
callq 0x2210
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
xorpd %xmm0, %xmm0
movapd %xmm0, -0xe0(%rbp)
xorl %eax, %eax
movq %rax, -0xd0(%rbp)
movq $0x0, -0x40(%rbp)
movq %rax, -0xf0(%rbp)
movapd %xmm0, -0x100(%rbp)
cmpl $0x2, 0xc(%r14)
movq %r14, %rsi
movq %r14, -0x88(%rbp)
jb 0x3e24
movq $0x0, -0x40(%rbp)
movl $0x1, %r15d
leaq -0x100(%rbp), %r14
xorl %r12d, %r12d
movq -0x40(%rbp), %rax
movq 0x10(%rsi), %rcx
leaq (%r15,%r15,2), %r13
movl 0x4(%rcx,%r13,4), %ebx
imull (%rcx,%r13,4), %ebx
addl %eax, %ebx
leaq -0xe0(%rbp), %rdi
movq %rbx, %rsi
callq 0x5266
movq %r14, %rdi
movq %rbx, %rsi
callq 0x529e
cmpl %ebx, %r12d
movq %rbx, -0x40(%rbp)
jae 0x3e0e
movq -0x88(%rbp), %r11
movq 0x10(%r11), %rax
leaq (%rax,%r13,4), %rsi
movq -0x100(%rbp), %rax
movl %r12d, %ecx
leaq (,%rcx,4), %rdi
addq -0xe0(%rbp), %rdi
subq %rcx, %rbx
shlq $0x4, %rcx
leaq (%rax,%rcx), %r9
addq $0xc, %r9
xorl %ecx, %ecx
movl %r15d, (%rdi,%rcx,4)
movl (%rsi), %r10d
movl %ecx, %eax
xorl %edx, %edx
divl %r10d
shll $0x3, %edx
movl %edx, -0xc(%r9)
shll $0x3, %eax
movl %eax, -0x8(%r9)
movl 0x18(%r11), %eax
xorl %edx, %edx
divl %r10d
movl %eax, -0x4(%r9)
movl 0x1c(%r11), %eax
xorl %edx, %edx
divl 0x4(%rsi)
movl %eax, (%r9)
incq %rcx
addq $0x10, %r9
cmpq %rcx, %rbx
jne 0x3dc6
movq -0x40(%rbp), %rax
movl %eax, %r12d
incq %r15
movq -0x88(%rbp), %rsi
movl 0xc(%rsi), %eax
cmpq %rax, %r15
jb 0x3d4e
movl 0x18(%rsi), %edi
shll $0x3, %edi
movl 0x1c(%rsi), %r8d
shll $0x3, %r8d
movl 0x4(%rsi), %ecx
movl 0x8(%rsi), %eax
decl %eax
xorl %edx, %edx
movl %edi, -0x80(%rbp)
divl %edi
movl %eax, %edi
incl %edi
decl %ecx
movl %ecx, %eax
xorl %edx, %edx
movl %r8d, -0x7c(%rbp)
divl %r8d
movl %eax, %r15d
incl %r15d
movl %r15d, %eax
movl %edi, -0x54(%rbp)
imull %edi, %eax
cmpb $0x0, 0x5392(%rip)      # 0x91fa
movl -0x40(%rbp), %ecx
movq %rcx, -0xa8(%rbp)
movl %eax, -0x4c(%rbp)
movl %eax, %eax
movq %rax, -0xa0(%rbp)
movq %rsi, %r14
je 0x4019
movq 0x5142(%rip), %rdi      # 0x8fd0
leaq 0x23c3(%rip), %rsi      # 0x6258
movl $0x12, %edx
callq 0x2210
movq 0x512a(%rip), %rdi      # 0x8fd0
movq -0xa8(%rbp), %rsi
callq 0x2110
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq 0x50ed(%rip), %rdi      # 0x8fd0
leaq 0x2381(%rip), %rsi      # 0x626b
movl $0xb, %edx
callq 0x2210
movl -0x54(%rbp), %esi
movq 0x50d2(%rip), %rdi      # 0x8fd0
callq 0x2110
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq 0x509c(%rip), %rdi      # 0x8fd0
leaq 0x233c(%rip), %rsi      # 0x6277
movl $0xb, %edx
callq 0x2210
movl %r15d, %esi
movq 0x5081(%rip), %rdi      # 0x8fd0
callq 0x2110
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq 0x504b(%rip), %rdi      # 0x8fd0
leaq 0x22f7(%rip), %rsi      # 0x6283
movl $0xf, %edx
callq 0x2210
movq 0x5033(%rip), %rdi      # 0x8fd0
movq -0xa0(%rbp), %rsi
callq 0x2110
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq 0x4ff6(%rip), %rbx      # 0x8fd0
leaq 0x22b2(%rip), %rsi      # 0x6293
movl $0xf, %edx
movq %rbx, %rdi
callq 0x2210
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq 0x4fc4(%rip), %rdi      # 0x8fd0
callq 0x2040
movq %rax, %rdi
callq 0x2120
movl -0x4c(%rbp), %edi
movq -0x40(%rbp), %rsi
movl $0x40, %edx
callq 0x3924
movq %rax, -0x38(%rbp)
movl %r15d, -0x50(%rbp)
movb $0x0, -0xbc(%rbp)
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
movq %rax, -0xb8(%rbp)
movl 0xc(%r14), %esi
leaq -0x1a0(%rbp), %rdx
movw $0x0, (%rdx)
leaq -0x118(%rbp), %rdi
leaq -0x55(%rbp), %rcx
callq 0x52d8
cmpl $0x0, -0x4c(%rbp)
je 0x432e
movq $0x0, -0x30(%rbp)
leaq -0x1a0(%rbp), %r12
cmpl $0x0, -0x40(%rbp)
je 0x4316
xorl %ebx, %ebx
movq 0x10(%r14), %rax
movq -0xe0(%rbp), %rcx
movl (%rcx,%rbx,4), %ecx
leaq (%rcx,%rcx,2), %rdx
movl 0x8(%rax,%rdx,4), %eax
movq %rax, -0x90(%rbp)
movq -0xe8(%rbp), %rax
movq 0x8(%rax), %rax
movl (%rax,%rcx,8), %edx
movl %edx, -0x98(%rbp)
movl 0x4(%rax,%rcx,8), %eax
addl $0x2, %eax
movl %eax, -0x78(%rbp)
xorl %eax, %eax
xorl %r14d, %r14d
movq %rbx, -0x70(%rbp)
testb $0x1, %al
jne 0x426e
movq %r14, -0xb0(%rbp)
testl %r14d, %r14d
movl -0x78(%rbp), %eax
cmovel -0x98(%rbp), %eax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movq -0x68(%rbp), %rcx
leaq (%rcx,%rax), %r15
leaq (%rcx,%rax), %r14
addq $0x8, %r14
movb $0x1, %r13b
xorl %ebx, %ebx
movq -0x48(%rbp), %rdi
leaq -0xbc(%rbp), %rsi
leaq -0x118(%rbp), %rdx
movl $0x1, %ecx
movl $0x1, %r8d
callq 0x253e
addl %ebx, %ebx
orl %ebx, %eax
movl %eax, %ebx
movb %r13b, -0x1a0(%rbp)
movw %ax, -0x19e(%rbp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x5a86
incb %r13b
cmpq %r14, %rax
je 0x410c
movzbl 0x24(%rax), %r15d
movq -0xb0(%rbp), %r14
testl %r14d, %r14d
je 0x41d1
testl %r15d, %r15d
movq -0x70(%rbp), %rbx
je 0x4242
movl %r15d, %ecx
andl $0xf, %ecx
movq -0x48(%rbp), %rdi
leaq -0xbc(%rbp), %rsi
leaq -0x118(%rbp), %rdx
xorl %r8d, %r8d
callq 0x253e
shrl $0x4, %r15d
movl %r14d, %r14d
addq %r15, %r14
movq -0x38(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq (%rcx,%rdx,8), %r13
movq (%r13,%rbx,8), %r15
movw %ax, (%r15,%r14,2)
movq -0x60(%rbp), %rcx
movq -0x90(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movzbl (%rcx,%r14), %ecx
imull %eax, %ecx
movw %cx, (%r15,%r14,2)
jmp 0x423e
movq -0x48(%rbp), %rdi
leaq -0xbc(%rbp), %rsi
leaq -0x118(%rbp), %rdx
movl %r15d, %ecx
xorl %r8d, %r8d
callq 0x253e
movq -0x70(%rbp), %rbx
movq -0xe0(%rbp), %rcx
movl (%rcx,%rbx,4), %ecx
movq -0x118(%rbp), %rdx
addw (%rdx,%rcx,2), %ax
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rdi
movq (%rsi,%rdi,8), %r13
movq (%r13,%rbx,8), %r15
movw %ax, (%r15)
movw %ax, (%rdx,%rcx,2)
movq -0x60(%rbp), %rax
movq -0x90(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movzbl (%rax), %eax
imulw (%r15), %ax
movw %ax, (%r15)
xorl %r14d, %r14d
xorl %eax, %eax
jmp 0x425f
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %r13
movq (%r13,%rbx,8), %r15
movl %r14d, %eax
movw $0x0, (%r15,%rax,2)
movb $0x1, %al
incl %r14d
cmpl $0x40, %r14d
jb 0x40d3
jmp 0x4295
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %r13
movq (%r13,%rbx,8), %r15
addl %r14d, %r14d
leaq (%r14,%r15), %rdi
movl $0x80, %edx
subl %r14d, %edx
xorl %esi, %esi
callq 0x2100
xorl %eax, %eax
leaq 0x4f12(%rip), %rdx      # 0x91b0
movzbl (%rax,%rdx), %ecx
movzwl (%r15,%rcx,2), %ecx
movw %cx, -0x1a0(%rbp,%rax,2)
incq %rax
cmpq $0x40, %rax
jne 0x429e
xorl %r15d, %r15d
movq -0x88(%rbp), %r14
movq %r12, %rdi
movl %r15d, %esi
movl $0x1, %edx
callq 0x2649
incl %r15d
cmpl $0x8, %r15d
jne 0x42c2
xorl %r15d, %r15d
movq %r12, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x2649
incl %r15d
cmpl $0x8, %r15d
jne 0x42de
movq (%r13,%rbx,8), %rdi
movl $0x80, %edx
movq %r12, %rsi
callq 0x2160
incq %rbx
cmpq -0xa8(%rbp), %rbx
jne 0x408f
movq -0x30(%rbp), %rcx
incq %rcx
movq %rcx, -0x30(%rbp)
cmpq -0xa0(%rbp), %rcx
jb 0x4083
cmpb $0x0, 0x4ec5(%rip)      # 0x91fa
je 0x437d
movq 0x4c92(%rip), %rbx      # 0x8fd0
leaq 0x1f5e(%rip), %rsi      # 0x62a3
movl $0xf, %edx
movq %rbx, %rdi
callq 0x2210
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq 0x4c60(%rip), %rdi      # 0x8fd0
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq -0xe8(%rbp), %rax
movl (%rax), %edi
movl -0x50(%rbp), %esi
imull -0x7c(%rbp), %esi
movl -0x54(%rbp), %edx
imull -0x80(%rbp), %edx
movl %edi, -0x60(%rbp)
movl %esi, -0x50(%rbp)
callq 0x3924
movq %rax, -0x78(%rbp)
movq -0x40(%rbp), %rcx
cmpl $0x0, -0x4c(%rbp)
je 0x44e8
movq -0x100(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0x90(%rbp)
xorl %eax, %eax
movq %rax, -0x98(%rbp)
xorl %edx, %edx
divl -0x54(%rbp)
movl %edx, -0x30(%rbp)
movl %eax, -0x68(%rbp)
testl %ecx, %ecx
je 0x44cd
movl -0x30(%rbp), %eax
imull -0x80(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x68(%rbp), %eax
imull -0x7c(%rbp), %eax
movl %eax, -0x68(%rbp)
xorl %r8d, %r8d
movq %r8, %rcx
shlq $0x4, %rcx
movq -0x70(%rbp), %rax
movl 0xc(%rax,%rcx), %r9d
testl %r9d, %r9d
je 0x44bd
addq -0x70(%rbp), %rcx
movq -0x90(%rbp), %rax
movl (%rax,%r8,4), %eax
decl %eax
movq %rax, -0x48(%rbp)
movl 0x8(%rcx), %r11d
movl (%rcx), %ebx
addl -0x30(%rbp), %ebx
movl 0x4(%rcx), %eax
addl -0x68(%rbp), %eax
movq %rax, -0xb0(%rbp)
xorl %r15d, %r15d
testl %r11d, %r11d
je 0x44b5
movq -0xb0(%rbp), %rax
leal (%rax,%r15), %r12d
movq -0x38(%rbp), %rax
movq -0x98(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq (%rcx,%r8,8), %rdi
movq -0x78(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
xorl %r13d, %r13d
leal (%rbx,%r13), %ecx
xorl %eax, %eax
movl %eax, %r10d
andl $0x7, %r10d
imull %r11d, %r10d
addl %ecx, %r10d
movl %eax, %edx
shrl $0x3, %edx
imull %r9d, %edx
addl %r12d, %edx
movzwl (%rdi,%rax,2), %r14d
movq (%rsi,%rdx,8), %rdx
movw %r14w, (%rdx,%r10,2)
incq %rax
cmpq $0x40, %rax
jne 0x447c
incl %r13d
cmpl %r11d, %r13d
jne 0x4476
incl %r15d
cmpl %r9d, %r15d
jne 0x4444
incq %r8
cmpq -0xa8(%rbp), %r8
jne 0x43fd
movq -0x98(%rbp), %rax
incq %rax
cmpq -0xa0(%rbp), %rax
movq -0x40(%rbp), %rcx
jb 0x43cc
movq -0x38(%rbp), %rdi
movl -0x4c(%rbp), %esi
movl %ecx, %edx
callq 0x39dd
cmpb $0x0, 0x4cfd(%rip)      # 0x91fa
je 0x4545
movq 0x4aca(%rip), %rbx      # 0x8fd0
leaq 0x1da6(%rip), %rsi      # 0x62b3
movl $0xe, %edx
movq %rbx, %rdi
callq 0x2210
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq 0x4a98(%rip), %rdi      # 0x8fd0
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq -0x88(%rbp), %rbx
movl 0x4(%rbx), %esi
movl 0x8(%rbx), %edx
movl -0x60(%rbp), %edi
callq 0x3924
movq %rax, -0x48(%rbp)
movl 0x4(%rbx), %eax
movq %rax, -0x38(%rbp)
testq %rax, %rax
je 0x46b5
movl 0x8(%rbx), %ecx
xorl %edx, %edx
movsd 0x1b05(%rip), %xmm0     # 0x6080
movsd 0x1b05(%rip), %xmm1     # 0x6088
movsd 0x1b05(%rip), %xmm2     # 0x6090
movsd 0x1b05(%rip), %xmm3     # 0x6098
testq %rcx, %rcx
je 0x46a8
movq -0x78(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdi
movq (%rsi,%rdx,8), %rsi
movq (%rdi,%rdx,8), %rdi
movq 0x10(%rax), %r8
movq (%r8,%rdx,8), %r8
movq -0x48(%rbp), %rax
movq (%rax), %r9
movq 0x8(%rax), %r10
movq (%r9,%rdx,8), %r9
movq (%r10,%rdx,8), %r10
movq 0x10(%rax), %r11
movq (%r11,%rdx,8), %r11
xorl %ebx, %ebx
movzwl (%rsi,%rbx,2), %r14d
movswl (%rdi,%rbx,2), %r15d
movswl (%r8,%rbx,2), %r12d
xorps %xmm5, %xmm5
cvtsi2sd %r12d, %xmm5
movapd %xmm5, %xmm4
mulsd %xmm0, %xmm4
cvttsd2si %xmm4, %r12d
xorps %xmm4, %xmm4
cvtsi2sd %r15d, %xmm4
leal 0x80(%r14,%r12), %r12d
movapd %xmm4, %xmm6
mulsd %xmm1, %xmm6
mulsd %xmm2, %xmm5
mulsd %xmm3, %xmm4
movswl %r12w, %r13d
movl $0xff, %r15d
cmpl %r15d, %r13d
jl 0x462d
movl $0xff, %r12d
testw %r12w, %r12w
jg 0x4636
xorl %r12d, %r12d
cvttsd2si %xmm6, %r13d
cvttsd2si %xmm5, %eax
addl %r13d, %eax
negl %eax
leal (%r14,%rax), %r13d
addl $0x80, %r13d
movw %r12w, (%r9,%rbx,2)
movswl %r13w, %eax
cmpl %r15d, %eax
jl 0x4663
movl $0xff, %r13d
testw %r13w, %r13w
jg 0x466c
xorl %r13d, %r13d
cvttsd2si %xmm4, %eax
addl %eax, %r14d
addl $0x80, %r14d
movw %r13w, (%r10,%rbx,2)
movswl %r14w, %eax
cmpl %r15d, %eax
jl 0x468e
movl $0xff, %r14d
testw %r14w, %r14w
jg 0x4697
xorl %r14d, %r14d
movw %r14w, (%r11,%rbx,2)
incq %rbx
cmpq %rbx, %rcx
jne 0x45d4
incq %rdx
cmpq -0x38(%rbp), %rdx
jne 0x4593
movq -0x78(%rbp), %rdi
movl -0x60(%rbp), %esi
movl -0x50(%rbp), %edx
callq 0x39dd
cmpb $0x0, 0x4b2f(%rip)      # 0x91fa
je 0x488f
leaq -0x190(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1bdf(%rip), %rsi      # 0x62c2
leaq 0x1bdb(%rip), %rdx      # 0x62c5
leaq -0x1a0(%rbp), %r15
movq %r15, %rdi
callq 0x554c
leaq -0x180(%rbp), %r12
leaq -0x170(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1bb4(%rip), %rsi      # 0x62c6
leaq 0x1bb2(%rip), %rdx      # 0x62cb
movq %r12, %rdi
callq 0x554c
leaq -0x160(%rbp), %r12
leaq -0x150(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x1b92(%rip), %rsi      # 0x62cc
leaq 0x1b8f(%rip), %rdx      # 0x62d0
movq %r12, %rdi
callq 0x554c
movq 0x4880(%rip), %rbx      # 0x8fd0
leaq 0x1b7a(%rip), %r13      # 0x62d1
xorl %r12d, %r12d
movl $0x2, %edx
movq %rbx, %rdi
leaq 0x19aa(%rip), %rsi      # 0x6113
callq 0x2210
movq %r12, %rax
shlq $0x5, %rax
movq -0x1a0(%rbp,%rax), %rsi
movq -0x198(%rbp,%rax), %rdx
movq %rbx, %rdi
callq 0x2210
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq %r12, -0x38(%rbp)
xorl %r15d, %r15d
movl $0x4, %edx
movq %rbx, %rdi
leaq 0x1944(%rip), %rsi      # 0x6111
callq 0x2210
movq -0x48(%rbp), %rax
movq (%rax), %r12
xorl %ebx, %ebx
movq (%r12,%r15,8), %rax
movswl (%rax,%rbx,2), %esi
movq %r13, %rdi
xorl %eax, %eax
callq 0x2050
incq %rbx
cmpq $0x10, %rbx
jne 0x47db
movq 0x47d3(%rip), %rbx      # 0x8fd0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
incq %r15
cmpq $0x10, %r15
jne 0x47be
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movq -0x38(%rbp), %r12
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
incq %r12
cmpq $0x3, %r12
jne 0x475a
leaq -0x150(%rbp), %rbx
xorl %r14d, %r14d
movq -0x160(%rbp,%r14), %rdi
leaq (%rbx,%r14), %rax
cmpq %rdi, %rax
je 0x4885
callq 0x21a0
addq $-0x20, %r14
cmpq $-0x60, %r14
jne 0x486f
movq -0x118(%rbp), %rdi
testq %rdi, %rdi
je 0x48a0
callq 0x21a0
movq -0x100(%rbp), %rdi
testq %rdi, %rdi
je 0x48b1
callq 0x21a0
movq -0xe0(%rbp), %rdi
testq %rdi, %rdi
je 0x48c2
callq 0x21a0
movq -0x48(%rbp), %rax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x48da
movq %rax, %rbx
addq $-0x10, %r12
movq -0x10(%r12), %rdi
cmpq %rdi, %r12
je 0x48f0
callq 0x21a0
leaq -0x20(%r12), %rax
addq $-0x10, %r12
cmpq %r15, %r12
movq %rax, %r12
jne 0x48e1
jmp 0x4945
jmp 0x4942
jmp 0x4942
jmp 0x4911
jmp 0x4911
jmp 0x4942
jmp 0x4942
jmp 0x4911
movq %rax, %rbx
jmp 0x4956
jmp 0x491c
jmp 0x4942
jmp 0x4942
movq %rax, %rbx
movq $-0x60, %r15
movq -0x10(%r14), %rdi
cmpq %rdi, %r14
je 0x4934
callq 0x21a0
addq $-0x20, %r14
addq $0x20, %r15
jne 0x4926
jmp 0x4945
jmp 0x4942
movq %rax, %rbx
movq -0x118(%rbp), %rdi
testq %rdi, %rdi
je 0x4956
callq 0x21a0
movq -0x100(%rbp), %rdi
testq %rdi, %rdi
je 0x4967
callq 0x21a0
movq -0xe0(%rbp), %rdi
testq %rdi, %rdi
je 0x4978
callq 0x21a0
movq %rbx, %rdi
callq 0x22f0