// File: /workspace/github_cpp_makefile_star2_O1/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, -0xd8(%rbp)
movq %rcx, -0x88(%rbp)
movq %rdx, %r13
movq %rsi, -0xe0(%rbp)
movq %rdi, -0x48(%rbp)
cmpb $0x1, 0x5574(%rip)      # 0x91fa
jne 0x3d05
movq 0x5341(%rip), %rbx      # 0x8fd0
leaq 0x25a7(%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 0x257b(%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, -0xd0(%rbp)
xorl %eax, %eax
movq %rax, -0xc0(%rbp)
movq $0x0, -0x58(%rbp)
movq %rax, -0xf0(%rbp)
movapd %xmm0, -0x100(%rbp)
cmpl $0x2, 0xc(%r13)
movq %r13, -0x70(%rbp)
jb 0x3e35
movq $0x0, -0x58(%rbp)
movl $0x1, %r15d
leaq -0x100(%rbp), %r14
xorl %r12d, %r12d
movq -0x58(%rbp), %rax
movq 0x10(%r13), %rcx
leaq (%r15,%r15,2), %r13
movl 0x4(%rcx,%r13,4), %ebx
imull (%rcx,%r13,4), %ebx
addl %eax, %ebx
leaq -0xd0(%rbp), %rdi
movq %rbx, %rsi
callq 0x52e2
movq %r14, %rdi
movq %rbx, %rsi
callq 0x531a
cmpl %ebx, %r12d
movq %rbx, -0x58(%rbp)
jae 0x3e21
movq -0x70(%rbp), %rcx
movq 0x10(%rcx), %rax
leaq (%rax,%r13,4), %rsi
movq %rcx, %r13
movq -0x100(%rbp), %rax
movl %r12d, %r12d
movl %ebx, %ecx
leaq (,%r12,4), %rdi
addq -0xd0(%rbp), %rdi
movq %r12, %rdx
shlq $0x4, %rdx
leaq (%rax,%rdx), %r8
addq $0xc, %r8
movq %r12, %r9
subq %rcx, %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(%r8)
shll $0x3, %eax
movl %eax, -0x8(%r8)
movl 0x18(%r13), %eax
xorl %edx, %edx
divl %r10d
movl %eax, -0x4(%r8)
movl 0x1c(%r13), %eax
xorl %edx, %edx
divl 0x4(%rsi)
movl %eax, (%r8)
incq %rcx
addq $0x10, %r8
movq %r9, %rax
addq %rcx, %rax
jne 0x3dd8
addl %ecx, %r12d
jmp 0x3e25
movq -0x70(%rbp), %r13
incq %r15
movl 0xc(%r13), %eax
cmpq %rax, %r15
jb 0x3d58
movl 0x18(%r13), %esi
shll $0x3, %esi
movl 0x1c(%r13), %edi
shll $0x3, %edi
movl 0x4(%r13), %ecx
movl 0x8(%r13), %eax
decl %eax
xorl %edx, %edx
movl %esi, -0x80(%rbp)
divl %esi
movl %eax, %esi
incl %esi
decl %ecx
movl %ecx, %eax
xorl %edx, %edx
movl %edi, -0x7c(%rbp)
divl %edi
incl %eax
movl %eax, -0x4c(%rbp)
movl %esi, -0x64(%rbp)
imull %esi, %eax
cmpb $0x0, 0x5385(%rip)      # 0x91fa
movl -0x58(%rbp), %ecx
movq %rcx, -0xa8(%rbp)
movl %eax, -0x50(%rbp)
movl %eax, %eax
movq %rax, -0xa0(%rbp)
je 0x4023
movq 0x5138(%rip), %rdi      # 0x8fd0
leaq 0x23b9(%rip), %rsi      # 0x6258
movl $0x12, %edx
callq 0x2210
movq 0x5120(%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 0x50e3(%rip), %rdi      # 0x8fd0
leaq 0x2377(%rip), %rsi      # 0x626b
movl $0xb, %edx
callq 0x2210
movl -0x64(%rbp), %esi
movq 0x50c8(%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 0x5092(%rip), %rdi      # 0x8fd0
leaq 0x2332(%rip), %rsi      # 0x6277
movl $0xb, %edx
callq 0x2210
movl -0x4c(%rbp), %esi
movq 0x5077(%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 0x5041(%rip), %rdi      # 0x8fd0
leaq 0x22ed(%rip), %rsi      # 0x6283
movl $0xf, %edx
callq 0x2210
movq 0x5029(%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 0x4fec(%rip), %rbx      # 0x8fd0
leaq 0x22a8(%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 0x4fba(%rip), %rdi      # 0x8fd0
callq 0x2040
movq %rax, %rdi
callq 0x2120
movl -0x50(%rbp), %edi
movq -0x58(%rbp), %rsi
movl $0x40, %edx
callq 0x393c
movq %rax, -0x60(%rbp)
movb $0x0, -0xb4(%rbp)
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
movq %rax, -0xb0(%rbp)
movl 0xc(%r13), %esi
leaq -0x1a0(%rbp), %rdx
movw $0x0, (%rdx)
leaq -0x118(%rbp), %rdi
leaq -0x65(%rbp), %rcx
callq 0x5354
cmpl $0x0, -0x50(%rbp)
je 0x43a4
movq $0x0, -0x30(%rbp)
leaq 0x5127(%rip), %rbx      # 0x91b0
leaq -0x1a0(%rbp), %r14
cmpl $0x0, -0x58(%rbp)
je 0x438c
xorl %r12d, %r12d
movq 0x10(%r13), %rax
movq -0xd0(%rbp), %rcx
movl (%rcx,%r12,4), %ecx
leaq (%rcx,%rcx,2), %rdx
movl 0x8(%rax,%rdx,4), %eax
movq %rax, -0xe8(%rbp)
movq -0xd8(%rbp), %rax
movq 0x8(%rax), %rax
movl (%rax,%rcx,8), %edx
movl %edx, -0x38(%rbp)
movl 0x4(%rax,%rcx,8), %eax
addl $0x2, %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x78(%rbp)
xorl %r15d, %r15d
movq %r12, -0x90(%rbp)
testb $0x1, -0x78(%rbp)
jne 0x42da
movq %r15, -0x98(%rbp)
testl %r15d, %r15d
movl -0x34(%rbp), %eax
cmovel -0x38(%rbp), %eax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movq -0x88(%rbp), %rcx
leaq (%rcx,%rax), %r15
leaq (%rcx,%rax), %r13
addq $0x8, %r13
movb $0x1, %bl
xorl %r12d, %r12d
movl $0x0, -0x40(%rbp)
xorl %r14d, %r14d
movq -0x48(%rbp), %rdi
leaq -0xb4(%rbp), %rsi
leaq -0x118(%rbp), %rdx
movl $0x1, %ecx
movl $0x1, %r8d
callq 0x253e
addl %r12d, %r12d
orl %r12d, %eax
movl %eax, %r12d
movb %bl, -0x1a0(%rbp)
movw %ax, -0x19e(%rbp)
movq %r15, %rdi
leaq -0x1a0(%rbp), %rsi
callq 0x5b20
cmpq %r13, %rax
je 0x4181
movb 0x24(%rax), %al
movl %eax, -0x40(%rbp)
movb $0x1, %r14b
incb %bl
testb $0x1, %r14b
je 0x412c
movq -0x98(%rbp), %r15
testl %r15d, %r15d
je 0x4224
movl -0x40(%rbp), %r13d
testb %r13b, %r13b
leaq 0x5009(%rip), %rbx      # 0x91b0
leaq -0x1a0(%rbp), %r14
movq -0x90(%rbp), %r12
je 0x42a9
movl %r13d, %eax
andb $0xf, %al
movzbl %al, %ecx
movq -0x48(%rbp), %rdi
leaq -0xb4(%rbp), %rsi
leaq -0x118(%rbp), %rdx
xorl %r8d, %r8d
callq 0x253e
shrb $0x4, %r13b
movl %r15d, %ecx
movzbl %r13b, %r15d
addq %rcx, %r15
movq -0x60(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq (%rcx,%r12,8), %rcx
movw %ax, (%rcx,%r15,2)
movq -0xe0(%rbp), %rdx
movq -0xe8(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movzbl (%rdx,%r15), %edx
imull %eax, %edx
movw %dx, (%rcx,%r15,2)
jmp 0x42c7
movzbl -0x40(%rbp), %ecx
movq -0x48(%rbp), %rdi
leaq -0xb4(%rbp), %rsi
leaq -0x118(%rbp), %rdx
xorl %r8d, %r8d
callq 0x253e
movq -0x70(%rbp), %r13
leaq 0x4f63(%rip), %rbx      # 0x91b0
leaq -0x1a0(%rbp), %r14
movq -0x90(%rbp), %r12
movq -0xd0(%rbp), %rcx
movl (%rcx,%r12,4), %ecx
movq -0x118(%rbp), %rdx
addw (%rdx,%rcx,2), %ax
movq -0x60(%rbp), %rsi
movq -0x30(%rbp), %rdi
movq (%rsi,%rdi,8), %rsi
movq (%rsi,%r12,8), %rsi
movw %ax, (%rsi)
movw %ax, (%rdx,%rcx,2)
movq -0xe0(%rbp), %rax
movq -0xe8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movzbl (%rax), %eax
imulw (%rsi), %ax
movw %ax, (%rsi)
xorl %r15d, %r15d
jmp 0x42cb
movq -0x60(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax,%r12,8), %rax
movl %r15d, %ecx
movw $0x0, (%rax,%rcx,2)
movb $0x1, %al
movl %eax, -0x78(%rbp)
movq -0x70(%rbp), %r13
incl %r15d
cmpl $0x40, %r15d
jb 0x40e7
jmp 0x42ff
movq -0x60(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rax
addl %r15d, %r15d
movq (%rax,%r12,8), %rdi
addq %r15, %rdi
movl $0x80, %edx
subl %r15d, %edx
xorl %esi, %esi
callq 0x2100
movq -0x60(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax,%r12,8), %rax
xorl %ecx, %ecx
movzbl (%rcx,%rbx), %edx
movzwl (%rax,%rdx,2), %edx
movw %dx, -0x1a0(%rbp,%rcx,2)
incq %rcx
cmpq $0x40, %rcx
jne 0x4311
xorl %r15d, %r15d
movq %r14, %rdi
movl %r15d, %esi
movl $0x1, %edx
callq 0x264a
incl %r15d
cmpl $0x8, %r15d
jne 0x432d
xorl %r15d, %r15d
movq %r14, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x264a
incl %r15d
cmpl $0x8, %r15d
jne 0x4349
movq -0x60(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax,%r12,8), %rdi
movl $0x80, %edx
movq %r14, %rsi
callq 0x2160
incq %r12
cmpq -0xa8(%rbp), %r12
jne 0x409d
movq -0x30(%rbp), %rcx
incq %rcx
movq %rcx, -0x30(%rbp)
cmpq -0xa0(%rbp), %rcx
jb 0x4090
cmpb $0x0, 0x4e4f(%rip)      # 0x91fa
je 0x43f3
movq 0x4c1c(%rip), %rbx      # 0x8fd0
leaq 0x1ee8(%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 0x4bea(%rip), %rdi      # 0x8fd0
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq -0xd8(%rbp), %rax
movl (%rax), %edi
movl -0x4c(%rbp), %esi
imull -0x7c(%rbp), %esi
movl -0x64(%rbp), %edx
imull -0x80(%rbp), %edx
movl %edi, -0x88(%rbp)
movl %esi, -0x4c(%rbp)
callq 0x393c
movq %rax, -0x78(%rbp)
movq -0x58(%rbp), %rcx
cmpl $0x0, -0x50(%rbp)
je 0x455b
movq -0x100(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x90(%rbp)
xorl %eax, %eax
movq %rax, -0x98(%rbp)
xorl %edx, %edx
divl -0x64(%rbp)
movl %edx, -0x38(%rbp)
movl %eax, -0x34(%rbp)
testl %ecx, %ecx
je 0x4540
movl -0x38(%rbp), %eax
imull -0x80(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x34(%rbp), %eax
imull -0x7c(%rbp), %eax
movl %eax, -0x34(%rbp)
xorl %r8d, %r8d
movq %r8, %rcx
shlq $0x4, %rcx
movq -0x30(%rbp), %rax
movl 0xc(%rax,%rcx), %r9d
testl %r9d, %r9d
je 0x4530
addq -0x30(%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 -0x38(%rbp), %ebx
movl 0x4(%rcx), %eax
addl -0x34(%rbp), %eax
movq %rax, -0x40(%rbp)
xorl %r15d, %r15d
testl %r11d, %r11d
je 0x4528
movq -0x40(%rbp), %rax
leal (%rax,%r15), %r12d
movq -0x60(%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 0x44ef
incl %r13d
cmpl %r11d, %r13d
jne 0x44e9
incl %r15d
cmpl %r9d, %r15d
jne 0x44ba
incq %r8
cmpq -0xa8(%rbp), %r8
jne 0x4476
movq -0x98(%rbp), %rax
incq %rax
cmpq -0xa0(%rbp), %rax
movq -0x58(%rbp), %rcx
jb 0x4445
movq -0x60(%rbp), %rdi
movl -0x50(%rbp), %esi
movl %ecx, %edx
callq 0x39fa
cmpb $0x0, 0x4c8a(%rip)      # 0x91fa
je 0x45b8
movq 0x4a57(%rip), %rbx      # 0x8fd0
leaq 0x1d33(%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 0x4a25(%rip), %rdi      # 0x8fd0
callq 0x2040
movq %rax, %rdi
callq 0x2120
movq -0x70(%rbp), %rbx
movl 0x4(%rbx), %esi
movl 0x8(%rbx), %edx
movl -0x88(%rbp), %edi
callq 0x393c
movq %rax, -0x48(%rbp)
movl 0x4(%rbx), %eax
movq %rax, -0x40(%rbp)
testq %rax, %rax
je 0x4728
movl 0x8(%rbx), %ecx
xorl %edx, %edx
movsd 0x1a92(%rip), %xmm0     # 0x6080
movsd 0x1a92(%rip), %xmm1     # 0x6088
movsd 0x1a92(%rip), %xmm2     # 0x6090
movsd 0x1a92(%rip), %xmm3     # 0x6098
testq %rcx, %rcx
je 0x471b
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 0x46a0
movl $0xff, %r12d
testw %r12w, %r12w
jg 0x46a9
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 0x46d6
movl $0xff, %r13d
testw %r13w, %r13w
jg 0x46df
xorl %r13d, %r13d
cvttsd2si %xmm4, %eax
addl %eax, %r14d
addl $0x80, %r14d
movw %r13w, (%r10,%rbx,2)
movswl %r14w, %eax
cmpl %r15d, %eax
jl 0x4701
movl $0xff, %r14d
testw %r14w, %r14w
jg 0x470a
xorl %r14d, %r14d
movw %r14w, (%r11,%rbx,2)
incq %rbx
cmpq %rbx, %rcx
jne 0x4647
incq %rdx
cmpq -0x40(%rbp), %rdx
jne 0x4606
movq -0x78(%rbp), %rdi
movl -0x88(%rbp), %esi
movl -0x4c(%rbp), %edx
callq 0x39fa
cmpb $0x0, 0x4ab9(%rip)      # 0x91fa
je 0x4905
leaq -0x190(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1b69(%rip), %rsi      # 0x62c2
leaq 0x1b65(%rip), %rdx      # 0x62c5
leaq -0x1a0(%rbp), %r12
movq %r12, %rdi
callq 0x55d4
leaq -0x180(%rbp), %r15
leaq -0x170(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1b3e(%rip), %rsi      # 0x62c6
leaq 0x1b3c(%rip), %rdx      # 0x62cb
movq %r15, %rdi
callq 0x55d4
leaq -0x160(%rbp), %r15
leaq -0x150(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x1b1c(%rip), %rsi      # 0x62cc
leaq 0x1b19(%rip), %rdx      # 0x62d0
movq %r15, %rdi
callq 0x55d4
movq 0x480a(%rip), %rbx      # 0x8fd0
leaq 0x1b04(%rip), %r13      # 0x62d1
xorl %r12d, %r12d
movl $0x2, %edx
movq %rbx, %rdi
leaq 0x1934(%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, -0x40(%rbp)
xorl %r15d, %r15d
movl $0x4, %edx
movq %rbx, %rdi
leaq 0x18ce(%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 0x4851
movq 0x475d(%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 0x4834
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movq -0x40(%rbp), %r12
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
incq %r12
cmpq $0x3, %r12
jne 0x47d0
leaq -0x150(%rbp), %rbx
xorl %r14d, %r14d
movq -0x160(%rbp,%r14), %rdi
leaq (%rbx,%r14), %rax
cmpq %rdi, %rax
je 0x48fb
callq 0x21a0
addq $-0x20, %r14
cmpq $-0x60, %r14
jne 0x48e5
movq -0x118(%rbp), %rdi
testq %rdi, %rdi
je 0x4916
callq 0x21a0
movq -0x100(%rbp), %rdi
testq %rdi, %rdi
je 0x4927
callq 0x21a0
movq -0xd0(%rbp), %rdi
testq %rdi, %rdi
je 0x4938
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 0x4950
movq %rax, %rbx
jmp 0x495b
movq %rax, %rbx
movq %r12, %r15
cmpq %r15, %r12
je 0x49cb
addq $-0x10, %r15
leaq -0x1a0(%rbp), %r14
movq -0x10(%r15), %rdi
cmpq %rdi, %r15
je 0x4979
callq 0x21a0
leaq -0x20(%r15), %rax
addq $-0x10, %r15
cmpq %r14, %r15
movq %rax, %r15
jne 0x496b
jmp 0x49cb
jmp 0x49c8
jmp 0x4997
jmp 0x4997
jmp 0x49c8
jmp 0x49c8
jmp 0x4997
movq %rax, %rbx
jmp 0x49dc
jmp 0x49a2
jmp 0x49c8
jmp 0x49c8
movq %rax, %rbx
movq $-0x60, %r15
movq -0x10(%r14), %rdi
cmpq %rdi, %r14
je 0x49ba
callq 0x21a0
addq $-0x20, %r14
addq $0x20, %r15
jne 0x49ac
jmp 0x49cb
jmp 0x49c8
movq %rax, %rbx
movq -0x118(%rbp), %rdi
testq %rdi, %rdi
je 0x49dc
callq 0x21a0
movq -0x100(%rbp), %rdi
testq %rdi, %rdi
je 0x49ed
callq 0x21a0
movq -0xd0(%rbp), %rdi
testq %rdi, %rdi
je 0x49fe
callq 0x21a0
movq %rbx, %rdi
callq 0x22f0