// File: /workspace/github_cpp_makefile_star2_O2/OpenFirework[P]MyJPEG_Decoder/./bmp/bmp_code.cpp
// Function: MyBMP::BMP::resize(unsigned char*&, int, int, unsigned char*&, int&, int&, float)
int MyBMP::BMP::resize(byte *&inputdata, int inwidth, int inheight, byte *&outdata, int &outwidth, int &outheight, float scale) {

    float xscale = 0.0;
    float yscale = 0.0;
    if((outheight <= 0) || (outheight <= 0)) {
        std::cout<<"resize use scale and scale is "<<scale<<"\n";
        if(scale > 0) {
            outwidth = inwidth*scale;
            outheight = inheight*scale;
            xscale = inwidth*1.0/outwidth;
            yscale = inheight*1.0/outheight;
        } else {
            std::cout<<"scale value is wrong(less than 0)\n";
        }
       
    } else {
        std::cout<<"resize use outwidth and outheight, resized width is "<<outwidth<<" heigt is "<<outheight<<"\n";
        xscale = inwidth*1.0/outwidth;
        yscale = inheight*1.0/outheight;

    }
    std::cout<<"resized width is "<<outwidth<<" height is "<<outheight<<"\n";

    outdata = new byte[3*outwidth*outheight];

    for(int h=0; h<outheight; h++) {
        for(int w=0; w<outwidth; w++) {
            float srcx = w*xscale;
            float srcy = h*yscale;
            int topx = srcx;
            int topy = srcy;
            int reflect = topy*inwidth+topx;
            int reflect2 = 0;
            int topy2 = 0;
            int topx2 = 0;
            if(topy+1 > (inheight-1)){
                reflect2 = topy*inwidth+topx;
                topy2 = topy;
            } else {
                reflect2 = (topy+1)*inwidth+topx;
                topy2 = topy+1;
            }
            if(topx +1 > (inwidth-1)) {
                topx = topx - 1;
            }
            float fr = (topy2-srcy)*(topx+1-srcx)*inputdata[reflect*3] +
                       (topy2-srcy)*(srcx - topx)*inputdata[reflect*3+3] +
                        (srcy-topy)*(topx+1-srcx)*inputdata[reflect2*3] +
                        (srcy-topy)*(srcx-topx)*inputdata[reflect2*3+3];
            
            float fg = (topy2-srcy)*(topx+1-srcx)*inputdata[reflect*3+1] +
                       (topy2-srcy)*(srcx - topx)*inputdata[reflect*3+3+1] +
                        (srcy-topy)*(topx+1-srcx)*inputdata[reflect2*3+1] +
                        (srcy-topy)*(srcx-topx)*inputdata[reflect2*3+3+1];


            float fb = (topy2-srcy)*(topx+1-srcx)*inputdata[reflect*3+2] +
                       (topy2-srcy)*(srcx - topx)*inputdata[reflect*3+3+2] +
                        (srcy-topy)*(topx+1-srcx)*inputdata[reflect2*3+2] +
                        (srcy-topy)*(srcx-topx)*inputdata[reflect2*3+3+2];

            int offset = h*outwidth + w;
            outdata[offset*3] = fr;
            outdata[offset*3+1] = fg;
            outdata[offset*3+2] = fb;
        }
    }

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, %rbx
movq %r8, %r14
movl %ecx, %r13d
movl %edx, %r15d
movq %rsi, -0x48(%rbp)
movq 0x10(%rbp), %r12
cmpl $0x0, (%r12)
jle 0x39a5
movq 0x3699(%rip), %rdi      # 0x6fd0
leaq 0xbc8(%rip), %rsi       # 0x4506
callq 0x2110
movl (%rbx), %esi
movq %rax, %rdi
callq 0x2210
leaq 0xbe7(%rip), %rsi       # 0x453b
movq %rax, %rdi
callq 0x2110
movl (%r12), %esi
movq %rax, %rdi
callq 0x2210
leaq 0xc9f(%rip), %rsi       # 0x460e
movq %rax, %rdi
callq 0x2110
cvtsi2sd %r15d, %xmm0
cvtsi2sdl (%rbx), %xmm1
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x34(%rbp)
cvtsi2sd %r13d, %xmm0
cvtsi2sdl (%r12), %xmm1
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
jmp 0x3a42
movq 0x3624(%rip), %rdi      # 0x6fd0
leaq 0xb11(%rip), %rsi       # 0x44c4
movss %xmm0, -0x2c(%rbp)
callq 0x2110
movq %rax, %rdi
movss -0x2c(%rbp), %xmm0
callq 0x20e0
leaq 0xc3d(%rip), %rsi       # 0x460e
movq %rax, %rdi
callq 0x2110
xorps %xmm1, %xmm1
xorps %xmm0, %xmm0
movss %xmm0, -0x30(%rbp)
movss -0x2c(%rbp), %xmm0
ucomiss %xmm1, %xmm0
movss -0x2c(%rbp), %xmm1
jbe 0x3cb4
cvtsi2ss %r15d, %xmm0
mulss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movl %eax, (%rbx)
cvtsi2ss %r13d, %xmm0
mulss %xmm1, %xmm0
cvttss2si %xmm0, %eax
cvtsi2sd %r15d, %xmm1
movl %eax, (%r12)
cvtsi2sdl (%rbx), %xmm2
divsd %xmm2, %xmm1
cvtsd2ss %xmm1, %xmm1
movss %xmm1, -0x34(%rbp)
cvtsi2sd %r13d, %xmm1
cvttps2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm0
movss %xmm0, -0x30(%rbp)
movq 0x3582(%rip), %rdi      # 0x6fd0
leaq 0xad4(%rip), %rsi       # 0x4529
callq 0x2110
movl (%rbx), %esi
movq %rax, %rdi
callq 0x2210
leaq 0xadb(%rip), %rsi       # 0x4546
movq %rax, %rdi
callq 0x2110
movl (%r12), %esi
movq %rax, %rdi
callq 0x2210
leaq 0xb88(%rip), %rsi       # 0x460e
movq %rax, %rdi
callq 0x2110
movq %rbx, -0x50(%rbp)
movq %r12, %rax
movslq (%rbx), %r12
movslq (%rax), %rax
imulq %r12, %rax
leaq (%rax,%rax,2), %rdi
callq 0x2030
movq %rax, (%r14)
decl %r13d
leal -0x1(%r15), %eax
xorl %ecx, %ecx
movq %r15, -0x40(%rbp)
cvtsi2ss %ecx, %xmm1
mulss -0x30(%rbp), %xmm1
cvttss2si %xmm1, %edx
movl %edx, %esi
imull %r15d, %esi
leal 0x1(%rdx), %r8d
movl %r8d, %edi
imull %r15d, %edi
movl %r13d, %r15d
cmpl %edx, %r13d
cmovlel %edx, %r8d
cvtsi2ss %r8d, %xmm0
subss %xmm1, %xmm0
movss %xmm0, -0x2c(%rbp)
cvttps2dq %xmm1, %xmm2
cvtdq2ps %xmm2, %xmm2
subss %xmm2, %xmm1
movaps %xmm1, -0x60(%rbp)
xorl %r8d, %r8d
cmpl %r12d, %r8d
jge 0x3ca6
cvtsi2ss %r8d, %xmm2
mulss -0x34(%rbp), %xmm2
cvttss2si %xmm2, %r9d
leal (%rsi,%r9), %r11d
leal (%rdi,%r9), %r10d
cmpl %edx, %r15d
cmovlel %r11d, %r10d
xorl %ebx, %ebx
cmpl %r9d, %eax
setle %bl
movl %r9d, %r13d
subl %ebx, %r13d
negl %ebx
addl %ebx, %r9d
incl %r9d
cvtsi2ss %r9d, %xmm3
movq -0x48(%rbp), %r9
movq (%r9), %r9
movslq %r11d, %r11
leaq (%r11,%r11,2), %r11
movzbl (%r9,%r11), %ebx
cvtsi2ss %ebx, %xmm5
cvtsi2ss %r13d, %xmm0
movzbl 0x3(%r9,%r11), %ebx
cvtsi2ss %ebx, %xmm7
movslq %r10d, %r10
leaq (%r10,%r10,2), %r10
movzbl (%r9,%r10), %ebx
cvtsi2ss %ebx, %xmm9
movzbl 0x3(%r9,%r10), %ebx
cvtsi2ss %ebx, %xmm4
movzbl 0x1(%r9,%r11), %ebx
cvtsi2ss %ebx, %xmm10
movzbl 0x4(%r9,%r11), %ebx
cvtsi2ss %ebx, %xmm11
movzbl 0x1(%r9,%r10), %ebx
cvtsi2ss %ebx, %xmm12
movzbl 0x4(%r9,%r10), %ebx
cvtsi2ss %ebx, %xmm8
movzbl 0x2(%r9,%r11), %ebx
cvtsi2ss %ebx, %xmm13
movzbl 0x5(%r9,%r11), %r11d
cvtsi2ss %r11d, %xmm15
movzbl 0x2(%r9,%r10), %r11d
cvtsi2ss %r11d, %xmm14
subss %xmm2, %xmm3
movss -0x2c(%rbp), %xmm6
movaps %xmm6, %xmm1
mulss %xmm3, %xmm1
subss %xmm0, %xmm2
mulss %xmm2, %xmm6
mulss %xmm6, %xmm7
mulss %xmm1, %xmm5
addss %xmm7, %xmm5
movaps -0x60(%rbp), %xmm0
mulss %xmm0, %xmm3
mulss %xmm3, %xmm9
addss %xmm5, %xmm9
mulss %xmm0, %xmm2
mulss %xmm2, %xmm4
addss %xmm9, %xmm4
mulss %xmm6, %xmm11
mulss %xmm1, %xmm10
addss %xmm11, %xmm10
mulss %xmm3, %xmm12
addss %xmm10, %xmm12
mulss %xmm2, %xmm8
addss %xmm12, %xmm8
mulss %xmm6, %xmm15
mulss %xmm1, %xmm13
addss %xmm15, %xmm13
mulss %xmm3, %xmm14
addss %xmm13, %xmm14
movzbl 0x5(%r9,%r10), %r9d
cvtsi2ss %r9d, %xmm1
mulss %xmm2, %xmm1
addss %xmm14, %xmm1
imull %ecx, %r12d
addl %r8d, %r12d
cvttss2si %xmm4, %r9d
movq (%r14), %r10
movslq %r12d, %r11
leaq (%r11,%r11,2), %r11
movb %r9b, (%r10,%r11)
cvttss2si %xmm8, %r9d
movq (%r14), %r10
movb %r9b, 0x1(%r10,%r11)
cvttss2si %xmm1, %r9d
movq (%r14), %r10
movb %r9b, 0x2(%r10,%r11)
incl %r8d
movq -0x50(%rbp), %r9
movl (%r9), %r12d
jmp 0x3b00
incl %ecx
movl %r15d, %r13d
movq -0x40(%rbp), %r15
jmp 0x3ab8
movq 0x3315(%rip), %rdi      # 0x6fd0
leaq 0x821(%rip), %rsi       # 0x44e3
callq 0x2110
xorps %xmm0, %xmm0
movss %xmm0, -0x34(%rbp)
jmp 0x3a47