// File: /workspace/github_cpp_makefile_star2_O2/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 $0x188, %rsp            # imm = 0x188
movq %r8, -0xe0(%rbp)
movq %rcx, -0xb8(%rbp)
movq %rdx, %r14
movq %rsi, -0x78(%rbp)
movq %rdi, -0x58(%rbp)
cmpb $0x1, 0x5b48(%rip)      # 0x91da
jne 0x36c9
movq 0x5935(%rip), %rbx      # 0x8fd0
leaq 0x2b9b(%rip), %rsi      # 0x623d
movq %rbx, %rdi
callq 0x2190
movq %rax, %rdi
callq 0x20e0
leaq 0x2b93(%rip), %rsi      # 0x624c
movq %rbx, %rdi
callq 0x2190
movq %rax, %rdi
callq 0x20e0
movq %r14, -0x70(%rbp)
leaq -0xf8(%rbp), %rdi
leaq -0x1b0(%rbp), %rdx
xorl %esi, %esi
callq 0x47d4
leaq -0x128(%rbp), %rdi
leaq -0x1b0(%rbp), %rdx
xorl %esi, %esi
callq 0x4826
pushq $0x1
popq %r14
leaq -0xf8(%rbp), %r12
xorl %r13d, %r13d
xorl %edi, %edi
movq -0x70(%rbp), %rsi
movl 0xc(%rsi), %eax
cmpq %rax, %r14
jae 0x37d7
movq 0x10(%rsi), %rax
imulq $0xc, %r14, %rbx
movl 0x4(%rax,%rbx), %r15d
imull (%rax,%rbx), %r15d
addl %edi, %r15d
movq %r12, %rdi
movq %r15, %rsi
callq 0x4878
leaq -0x128(%rbp), %rdi
movq %r15, %rsi
callq 0x48b0
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rax
leaq (%rax,%rbx), %rsi
movq -0x128(%rbp), %rcx
leaq (%rax,%rbx), %rdi
addq $0x4, %rdi
movl %r13d, %r8d
leaq (,%r8,4), %r9
addq -0xf8(%rbp), %r9
movq %r8, %rax
shlq $0x4, %rax
leaq (%rcx,%rax), %r10
addq $0xc, %r10
xorl %ecx, %ecx
leaq (%r8,%rcx), %r13
cmpq %r15, %r13
jae 0x37cc
movl %r14d, (%r9,%rcx,4)
movl (%rsi), %r11d
movl %ecx, %eax
xorl %edx, %edx
divl %r11d
shll $0x3, %edx
movl %edx, -0xc(%r10)
shll $0x3, %eax
movl %eax, -0x8(%r10)
movq -0x70(%rbp), %rbx
movl 0x18(%rbx), %eax
xorl %edx, %edx
divl %r11d
movl %eax, -0x4(%r10)
movl 0x1c(%rbx), %eax
xorl %edx, %edx
divl (%rdi)
movl %eax, (%r10)
incq %rcx
addq $0x10, %r10
jmp 0x3784
incq %r14
movl %r15d, %edi
jmp 0x3707
movl 0x18(%rsi), %r8d
shll $0x3, %r8d
movl 0x1c(%rsi), %r9d
shll $0x3, %r9d
movl 0x4(%rsi), %ecx
movl 0x8(%rsi), %eax
decl %eax
xorl %edx, %edx
movl %r8d, -0x8c(%rbp)
divl %r8d
movl %eax, %r8d
incl %r8d
decl %ecx
movl %ecx, %eax
xorl %edx, %edx
movl %r9d, -0x88(%rbp)
divl %r9d
incl %eax
movl %eax, -0x44(%rbp)
movl %r8d, -0x5c(%rbp)
imull %r8d, %eax
cmpb $0x0, 0x59b5(%rip)      # 0x91da
movq %rdi, -0xd8(%rbp)
movl %edi, %ecx
movq %rcx, -0xb0(%rbp)
movl %eax, -0x84(%rbp)
movl %eax, %eax
movq %rax, -0xa8(%rbp)
movq %rsi, %r14
je 0x3908
movq 0x577c(%rip), %rdi      # 0x8fd0
leaq 0x29fd(%rip), %rsi      # 0x6258
callq 0x2190
movq %rax, %rdi
movq -0xb0(%rbp), %rsi
callq 0x2110
movq %rax, %rdi
callq 0x20e0
movq 0x5752(%rip), %rdi      # 0x8fd0
leaq 0x29e6(%rip), %rsi      # 0x626b
callq 0x2190
movl -0x5c(%rbp), %esi
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
callq 0x20e0
movq 0x572c(%rip), %rdi      # 0x8fd0
leaq 0x29cc(%rip), %rsi      # 0x6277
callq 0x2190
movl -0x44(%rbp), %esi
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
callq 0x20e0
movq 0x5706(%rip), %rdi      # 0x8fd0
leaq 0x29b2(%rip), %rsi      # 0x6283
callq 0x2190
movq %rax, %rdi
movq -0xa8(%rbp), %rsi
callq 0x2110
movq %rax, %rdi
callq 0x20e0
movq 0x56dc(%rip), %rdi      # 0x8fd0
leaq 0x2998(%rip), %rsi      # 0x6293
callq 0x2190
movq %rax, %rdi
callq 0x20e0
pushq $0x40
popq %rdx
movl -0x84(%rbp), %edi
movq -0xd8(%rbp), %rsi
callq 0x33e8
movq %rax, -0x38(%rbp)
movb $0x0, -0xcc(%rbp)
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
movq %rax, -0xc8(%rbp)
movl 0xc(%r14), %esi
leaq -0x1b0(%rbp), %rdx
andw $0x0, (%rdx)
leaq -0x110(%rbp), %rdi
leaq -0x29(%rbp), %rcx
callq 0x48ea
xorl %r15d, %r15d
pushq $0x1
popq %r12
leaq 0x582a(%rip), %r13      # 0x9190
cmpq -0xa8(%rbp), %r15
jae 0x3c4d
xorl %eax, %eax
movq %r15, -0xa0(%rbp)
cmpq -0xb0(%rbp), %rax
je 0x3c45
movq %rax, %rdx
movq 0x10(%r14), %rax
movq -0xf8(%rbp), %rcx
movq %rdx, -0x40(%rbp)
movl (%rcx,%rdx,4), %ecx
imulq $0xc, %rcx, %rdx
movl 0x8(%rax,%rdx), %eax
movq %rax, -0x98(%rbp)
movq -0xe0(%rbp), %rax
movq 0x8(%rax), %rax
movl (%rax,%rcx,8), %edx
movl %edx, -0xc0(%rbp)
movl 0x4(%rax,%rcx,8), %eax
addl $0x2, %eax
movl %eax, -0x60(%rbp)
xorl %edx, %edx
xorl %eax, %eax
cmpl $0x3f, %edx
ja 0x3b7f
movl %edx, %ecx
movq %rcx, -0x80(%rbp)
testb $0x1, %al
jne 0x3c19
movl %edx, -0x50(%rbp)
testl %edx, %edx
movl -0x60(%rbp), %eax
cmovel -0xc0(%rbp), %eax
imulq $0x30, %rax, %rax
movq -0xb8(%rbp), %rcx
leaq (%rcx,%rax), %rbx
leaq (%rcx,%rax), %r15
addq $0x8, %r15
movb $0x1, %r14b
xorl %r13d, %r13d
movq -0x58(%rbp), %rdi
leaq -0xcc(%rbp), %rsi
leaq -0x110(%rbp), %rdx
movl %r12d, %ecx
movl %r12d, %r8d
callq 0x24c2
addl %r13d, %r13d
orl %r13d, %eax
movl %eax, %r13d
movb %r14b, -0x1b0(%rbp)
movw %ax, -0x1ae(%rbp)
movq %rbx, %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x53c4
incb %r14b
cmpq %r15, %rax
je 0x3a12
movzbl 0x24(%rax), %ebx
cmpl $0x0, -0x50(%rbp)
je 0x3ae1
testl %ebx, %ebx
movq -0x70(%rbp), %r14
movq -0xa0(%rbp), %r15
leaq 0x5715(%rip), %r13      # 0x9190
je 0x3b5a
movl %ebx, %ecx
andl $0xf, %ecx
movq -0x58(%rbp), %rdi
leaq -0xcc(%rbp), %rsi
leaq -0x110(%rbp), %rdx
xorl %r8d, %r8d
callq 0x24c2
shrl $0x4, %ebx
movq -0x80(%rbp), %rdi
addq %rbx, %rdi
movq -0x38(%rbp), %rcx
movq (%rcx,%r15,8), %rcx
movq -0x40(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movw %ax, (%rcx,%rdi,2)
movq -0x78(%rbp), %rdx
movq -0x98(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movzbl (%rdx,%rdi), %edx
imull %eax, %edx
movw %dx, (%rcx,%rdi,2)
xorl %eax, %eax
movl %edi, %edx
jmp 0x3b78
movq -0x58(%rbp), %rdi
leaq -0xcc(%rbp), %rsi
leaq -0x110(%rbp), %rdx
movl %ebx, %ecx
xorl %r8d, %r8d
callq 0x24c2
movq -0x70(%rbp), %r14
movq -0xa0(%rbp), %r15
leaq 0x5681(%rip), %r13      # 0x9190
movq -0xf8(%rbp), %rcx
movq -0x40(%rbp), %rdi
movl (%rcx,%rdi,4), %ecx
movq -0x110(%rbp), %rdx
addw (%rdx,%rcx,2), %ax
movq -0x38(%rbp), %rsi
movq (%rsi,%r15,8), %rsi
movq (%rsi,%rdi,8), %rsi
movw %ax, (%rsi)
movw %ax, (%rdx,%rcx,2)
movq -0x78(%rbp), %rax
movq -0x98(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movzbl (%rax), %eax
imulw (%rsi), %ax
movw %ax, (%rsi)
xorl %edx, %edx
xorl %eax, %eax
jmp 0x3b78
movq -0x38(%rbp), %rax
movq (%rax,%r15,8), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x80(%rbp), %rcx
andw $0x0, (%rax,%rcx,2)
movb $0x1, %al
movl -0x50(%rbp), %edx
incl %edx
jmp 0x39cf
movq -0x38(%rbp), %rax
movq (%rax,%r15,8), %rcx
movq -0x40(%rbp), %rax
movq %rcx, -0x50(%rbp)
movq (%rcx,%rax,8), %rax
xorl %ecx, %ecx
cmpq $0x40, %rcx
je 0x3bb1
movzbl (%rcx,%r13), %edx
movzwl (%rax,%rdx,2), %edx
movw %dx, -0x1b0(%rbp,%rcx,2)
incq %rcx
jmp 0x3b95
xorl %ebx, %ebx
cmpl $0x8, %ebx
je 0x3bcd
leaq -0x1b0(%rbp), %rdi
movl %ebx, %esi
movl %r12d, %edx
callq 0x25be
incl %ebx
jmp 0x3bb3
xorl %ebx, %ebx
cmpl $0x8, %ebx
je 0x3be8
leaq -0x1b0(%rbp), %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0x25be
incl %ebx
jmp 0x3bcf
movq -0x40(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rcx,%rax,8), %rax
xorl %ecx, %ecx
cmpq $0x40, %rcx
je 0x3c0d
movzwl -0x1b0(%rbp,%rcx,2), %edx
movw %dx, (%rax,%rcx,2)
incq %rcx
jmp 0x3bf6
movq -0x40(%rbp), %rax
incq %rax
jmp 0x397c
movq -0x38(%rbp), %rax
movq (%rax,%r15,8), %rcx
movq -0x40(%rbp), %rax
movq %rcx, -0x50(%rbp)
movq (%rcx,%rax,8), %rax
movq -0x80(%rbp), %rcx
cmpq $0x40, %rcx
je 0x3b93
andw $0x0, (%rax,%rcx,2)
incq %rcx
jmp 0x3c31
incq %r15
jmp 0x3966
cmpb $0x0, 0x5586(%rip)      # 0x91da
je 0x3c71
movq 0x5373(%rip), %rdi      # 0x8fd0
leaq 0x263f(%rip), %rsi      # 0x62a3
callq 0x2190
movq %rax, %rdi
callq 0x20e0
movq -0xe0(%rbp), %rax
movl (%rax), %edi
movl -0x44(%rbp), %esi
imull -0x88(%rbp), %esi
movl -0x5c(%rbp), %edx
imull -0x8c(%rbp), %edx
movl %edi, -0x78(%rbp)
movl %esi, -0x44(%rbp)
callq 0x33e8
movq %rax, -0x58(%rbp)
xorl %r9d, %r9d
cmpq -0xa8(%rbp), %r9
jae 0x3de3
movl %r9d, %eax
xorl %edx, %edx
divl -0x5c(%rbp)
imull -0x8c(%rbp), %edx
movl %edx, -0xa0(%rbp)
imull -0x88(%rbp), %eax
movl %eax, -0x60(%rbp)
movq -0x128(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0x98(%rbp)
xorl %ebx, %ebx
cmpq -0xb0(%rbp), %rbx
je 0x3ddb
movq %rbx, %rax
shlq $0x4, %rax
movq -0xb8(%rbp), %rcx
leaq (%rcx,%rax), %rdx
movq %rdx, -0x40(%rbp)
movq -0x98(%rbp), %rdx
movl (%rdx,%rbx,4), %edx
decl %edx
movq %rdx, -0x50(%rbp)
movl 0xc(%rcx,%rax), %r11d
movl (%rcx,%rax), %r14d
addl -0xa0(%rbp), %r14d
movl 0x4(%rcx,%rax), %eax
addl -0x60(%rbp), %eax
movq %rax, -0xc0(%rbp)
xorl %ecx, %ecx
cmpl %r11d, %ecx
je 0x3dcf
movq -0x40(%rbp), %rax
movl 0x8(%rax), %edi
movq -0xc0(%rbp), %rax
movq %rcx, -0x80(%rbp)
addl %ecx, %eax
xorl %edx, %edx
cmpl %edi, %edx
je 0x3dc4
leal (%r14,%rdx), %esi
movq -0x38(%rbp), %rcx
movq %r9, %r8
movq (%rcx,%r9,8), %r9
movq %rbx, %r10
movq (%r9,%rbx,8), %r9
movq -0x58(%rbp), %rcx
movq -0x50(%rbp), %rbx
movq (%rcx,%rbx,8), %r13
xorl %ebx, %ebx
cmpq $0x40, %rbx
je 0x3dba
movl %ebx, %r15d
andl $0x7, %r15d
imull %edi, %r15d
addl %esi, %r15d
movl %ebx, %r12d
shrl $0x3, %r12d
imull %r11d, %r12d
addl %eax, %r12d
movzwl (%r9,%rbx,2), %ecx
movq (%r13,%r12,8), %r12
movw %cx, (%r12,%r15,2)
incq %rbx
jmp 0x3d84
incl %edx
movq %r8, %r9
movq %r10, %rbx
jmp 0x3d5c
movq -0x80(%rbp), %rcx
incl %ecx
jmp 0x3d3d
incq %rbx
movq -0x70(%rbp), %r14
jmp 0x3cea
incq %r9
jmp 0x3ca0
movq -0x38(%rbp), %rdi
movl -0x84(%rbp), %esi
movq -0xd8(%rbp), %rdx
callq 0x3490
cmpb $0x0, 0x53da(%rip)      # 0x91da
je 0x3e1d
movq 0x51c7(%rip), %rdi      # 0x8fd0
leaq 0x24a3(%rip), %rsi      # 0x62b3
callq 0x2190
movq %rax, %rdi
callq 0x20e0
movl 0x4(%r14), %esi
movl 0x8(%r14), %edx
movl -0x78(%rbp), %edi
callq 0x33e8
movq %rax, %rbx
movl 0x4(%r14), %eax
movl 0x8(%r14), %ecx
xorl %edx, %edx
movsd 0x223e(%rip), %xmm0     # 0x6080
movsd 0x223e(%rip), %xmm1     # 0x6088
movsd 0x223e(%rip), %xmm2     # 0x6090
movsd 0x223e(%rip), %xmm3     # 0x6098
movw $0xff, %si
cmpq %rax, %rdx
je 0x3f6a
xorl %edi, %edi
cmpq %rdi, %rcx
je 0x3f62
movq -0x58(%rbp), %r11
movq (%r11), %r8
movq 0x8(%r11), %r9
movq (%r8,%rdx,8), %r8
movzwl (%r8,%rdi,2), %r8d
movq (%r9,%rdx,8), %r9
movswl (%r9,%rdi,2), %r10d
movq 0x10(%r11), %r9
movq (%r9,%rdx,8), %r9
movswl (%r9,%rdi,2), %r9d
cvtsi2sd %r9d, %xmm5
movapd %xmm5, %xmm4
mulsd %xmm0, %xmm4
cvttsd2si %xmm4, %r9d
leal 0x80(%r8,%r9), %r9d
cvtsi2sd %r10d, %xmm4
movapd %xmm4, %xmm6
mulsd %xmm1, %xmm6
mulsd %xmm2, %xmm5
mulsd %xmm3, %xmm4
cmpw %si, %r9w
jl 0x3ed7
movl $0xff, %r9d
testw %r9w, %r9w
jg 0x3ee0
xorl %r9d, %r9d
cvttsd2si %xmm6, %r10d
cvttsd2si %xmm5, %r11d
addl %r10d, %r11d
negl %r11d
leal (%r8,%r11), %r10d
addl $0x80, %r10d
movq (%rbx), %r11
movq (%r11,%rdx,8), %r11
movw %r9w, (%r11,%rdi,2)
cmpw %si, %r10w
jl 0x3f13
movl $0xff, %r10d
testw %r10w, %r10w
jg 0x3f1c
xorl %r10d, %r10d
cvttsd2si %xmm4, %r9d
addl %r9d, %r8d
addl $0x80, %r8d
movq 0x8(%rbx), %r9
movq (%r9,%rdx,8), %r9
movw %r10w, (%r9,%rdi,2)
cmpw %si, %r8w
jl 0x3f44
movl $0xff, %r8d
testw %r8w, %r8w
jg 0x3f4d
xorl %r8d, %r8d
movq 0x10(%rbx), %r9
movq (%r9,%rdx,8), %r9
movw %r8w, (%r9,%rdi,2)
incq %rdi
jmp 0x3e69
incq %rdx
jmp 0x3e5e
movq -0x58(%rbp), %rdi
movl -0x78(%rbp), %esi
movl -0x44(%rbp), %edx
callq 0x3490
cmpb $0x0, 0x525a(%rip)      # 0x91da
je 0x40a7
leaq 0x2335(%rip), %rsi      # 0x62c2
leaq -0x1b0(%rbp), %r15
leaq -0x29(%rbp), %rdx
movq %r15, %rdi
callq 0x20b0
leaq -0x190(%rbp), %r12
leaq 0x2318(%rip), %rsi      # 0x62c6
leaq -0x62(%rbp), %rdx
movq %r12, %rdi
callq 0x20b0
movq %rbx, -0x38(%rbp)
leaq -0x170(%rbp), %r12
leaq 0x2300(%rip), %rsi      # 0x62cc
leaq -0x61(%rbp), %rdx
movq %r12, %rdi
callq 0x20b0
movq 0x4ff1(%rip), %rbx      # 0x8fd0
leaq 0x22eb(%rip), %r13      # 0x62d1
xorl %r14d, %r14d
cmpq $0x3, %r14
je 0x4086
movq %rbx, %rdi
leaq 0x2116(%rip), %rsi      # 0x6113
callq 0x2190
movq %r14, %rcx
shlq $0x5, %rcx
leaq (%rcx,%rbp), %rsi
addq $-0x1b0, %rsi           # imm = 0xFE50
movq %rax, %rdi
callq 0x2160
movq %rax, %rdi
callq 0x20e0
xorl %r15d, %r15d
cmpq $0x10, %r15
je 0x4076
movq %rbx, %rdi
leaq 0x20da(%rip), %rsi      # 0x6111
callq 0x2190
movq -0x38(%rbp), %rax
movq (%rax), %r12
xorl %ebx, %ebx
cmpq $0x10, %rbx
je 0x4062
movq (%r12,%r15,8), %rax
movswl (%rax,%rbx,2), %esi
movq %r13, %rdi
xorl %eax, %eax
callq 0x2040
incq %rbx
jmp 0x4045
movq 0x4f67(%rip), %rbx      # 0x8fd0
movq %rbx, %rdi
callq 0x20e0
incq %r15
jmp 0x4027
movq %rbx, %rdi
callq 0x20e0
incq %r14
jmp 0x3fe9
pushq $0x40
popq %rbx
leaq (%rbx,%rbp), %rdi
addq $-0x1b0, %rdi           # imm = 0xFE50
callq 0x20d0
addq $-0x20, %rbx
cmpq $-0x20, %rbx
jne 0x4089
movq -0x38(%rbp), %rbx
leaq -0x110(%rbp), %rdi
callq 0x5316
leaq -0x128(%rbp), %rdi
callq 0x4f88
leaq -0xf8(%rbp), %rdi
callq 0x4e7e
movq %rbx, %rax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x40e2
movq %rax, %rbx
addq $-0x20, %r12
movq %r12, %rdi
callq 0x20d0
cmpq %r15, %r12
jne 0x40e5
jmp 0x413e
jmp 0x413b
jmp 0x413b
jmp 0x413b
jmp 0x410d
jmp 0x410d
movq %rax, %rbx
jmp 0x4156
jmp 0x413b
jmp 0x410d
jmp 0x4112
movq %rax, %rbx
jmp 0x414a
movq %rax, %rbx
pushq $0x40
popq %r14
leaq (%r14,%rbp), %rdi
addq $-0x1b0, %rdi           # imm = 0xFE50
callq 0x20d0
addq $-0x20, %r14
cmpq $-0x20, %r14
jne 0x4119
jmp 0x413e
jmp 0x413b
jmp 0x413b
jmp 0x413b
movq %rax, %rbx
leaq -0x110(%rbp), %rdi
callq 0x5316
leaq -0x128(%rbp), %rdi
callq 0x4f88
leaq -0xf8(%rbp), %rdi
callq 0x4e7e
movq %rbx, %rdi
callq 0x22d0