// File: /workspace/github_cpp_makefile_star2_O2/gvaumour[P]cache-simulator/src/common.cc
// Function: writeBMPimage(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, int, std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>>, std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>>, std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>>)
void writeBMPimage(std::string image_name , int width , int height , vector< vector<int> > red, vector< vector<int> > blue, vector< vector<int> > green )
{
	FILE *f;
	int factor = 4;
	unsigned char *img = NULL;
	int w = width;
	int h = height*factor;
	int filesize = 54 + 3*w*h;

	img = (unsigned char *)malloc(3*w*h);
	memset(img,0,3*w*h);
	unsigned char r,g,b;
	int a = 0;
	
	for(int i=0; i<width; i++)
	{
	    a = 0;
	    for(int j=0; j<height; j++)
	    {
		r = red[i][j];
		g = green[i][j];
		b = blue[i][j];
		
		for(int k = 0 ; k < factor;k++)
		{
			img[(i+a*w)*3+2] = (unsigned char)(r);
			img[(i+a*w)*3+1] = (unsigned char)(g);
			img[(i+a*w)*3+0] = (unsigned char)(b);
			a++;
		}
	    }
	}

	unsigned char bmpfileheader[14] = {'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0};
	unsigned char bmpinfoheader[40] = {40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0};
	unsigned char bmppad[3] = {0,0,0};

	bmpfileheader[ 2] = (unsigned char)(filesize    );
	bmpfileheader[ 3] = (unsigned char)(filesize>> 8);
	bmpfileheader[ 4] = (unsigned char)(filesize>>16);
	bmpfileheader[ 5] = (unsigned char)(filesize>>24);

	bmpinfoheader[ 4] = (unsigned char)(       w    );
	bmpinfoheader[ 5] = (unsigned char)(       w>> 8);
	bmpinfoheader[ 6] = (unsigned char)(       w>>16);
	bmpinfoheader[ 7] = (unsigned char)(       w>>24);
	bmpinfoheader[ 8] = (unsigned char)(       h    );
	bmpinfoheader[ 9] = (unsigned char)(       h>> 8);
	bmpinfoheader[10] = (unsigned char)(       h>>16);
	bmpinfoheader[11] = (unsigned char)(       h>>24);

	f = fopen(image_name.c_str(),"wb");
	fwrite(bmpfileheader,1,14,f);
	fwrite(bmpinfoheader,1,40,f);
	for(int i=0; i<h; i++)
	{
	    fwrite(img+(w*(h-i-1)*3),3,w,f);
	    fwrite(bmppad,1,(4-(w*3)%4)%4,f);
	}

	free(img);
	fclose(f);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, 0x48(%rsp)
movq %r8, %r13
movq %rcx, %r12
movl %edx, %r14d
movl %esi, %ebx
movq %rdi, 0x18(%rsp)
leal (,%r14,4), %eax
leal (%rbx,%rbx,2), %ecx
movq %rax, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
imull %ecx, %eax
movl %eax, 0x4(%rsp)
movslq %eax, %rsi
pushq $0x1
popq %rdi
callq 0x51f0
movslq %ebx, %rsi
xorl %ecx, %ecx
testl %r14d, %r14d
movl $0x0, %edx
movq %r14, 0x20(%rsp)
cmovgl %r14d, %edx
testl %esi, %esi
movl $0x0, %edi
movq %rbx, 0x28(%rsp)
cmovgl %ebx, %edi
movq %rdi, 0x40(%rsp)
movq %rax, 0x8(%rsp)
addq $0x2, %rax
movq %rsi, 0x10(%rsp)
leaq (%rsi,%rsi,2), %rdi
cmpq 0x40(%rsp), %rcx
je 0x25893
xorl %r9d, %r9d
imulq $0x18, %rcx, %r10
pushq $0x4
popq %r11
xorl %ebx, %ebx
cmpq %rdx, %r9
je 0x2588b
movslq %r11d, %r8
movslq %ebx, %rbx
subq %rbx, %r8
movq 0x10(%rsp), %rsi
imulq %rbx, %rsi
addq %rcx, %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rax, %rsi
movq (%r12), %r14
movq (%r14,%r10), %r14
movb (%r14,%r9,4), %r15b
movq 0x48(%rsp), %r14
movq (%r14), %r14
movq (%r14,%r10), %r14
movb (%r14,%r9,4), %r14b
movq (%r13), %rbp
movq (%rbp,%r10), %rbp
movb (%rbp,%r9,4), %bpl
addl $0x4, %ebx
subq $0x1, %r8
jb 0x25882
movb %r15b, (%rsi)
movb %r14b, -0x1(%rsi)
movb %bpl, -0x2(%rsi)
addq %rdi, %rsi
jmp 0x2586c
incq %r9
addl $0x4, %r11d
jmp 0x2581e
incq %rcx
jmp 0x25806
movl 0x4(%rsp), %ecx
addl $0x36, %ecx
movabsq $0x3600000000, %rax     # imm = 0x3600000000
leaq 0x50(%rsp), %rbp
movq %rax, 0x6(%rbp)
movq $0x4d42, (%rbp)         # imm = 0x4D42
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %r15
movaps %xmm0, (%r15)
andq $0x0, 0x20(%r15)
movaps %xmm0, 0x10(%r15)
movb $0x28, (%r15)
movb $0x1, 0xc(%r15)
movb $0x18, 0xe(%r15)
movq %rsp, %rax
andw $0x0, (%rax)
movb $0x0, 0x2(%rax)
movl %ecx, 0x2(%rbp)
movq 0x28(%rsp), %rcx
movb %cl, 0x4(%r15)
movb %ch, 0x65(%rsp)
movl %ecx, %eax
shrl $0x10, %eax
movb %al, 0x6(%r15)
movl %ecx, %eax
movq %rcx, %r12
shrl $0x18, %eax
movb %al, 0x7(%r15)
movq 0x38(%rsp), %r14
movb %r14b, 0x8(%r15)
movq 0x20(%rsp), %rcx
movl %ecx, %eax
shrl $0x6, %eax
movb %al, 0x9(%r15)
movl %ecx, %eax
shrl $0xe, %eax
movb %al, 0xa(%r15)
shrl $0x16, %ecx
movb %cl, 0xb(%r15)
movq 0x18(%rsp), %rax
movq (%rax), %rdi
leaq 0x6852(%rip), %rsi      # 0x2c191
callq 0x5380
movq %rax, %r13
pushq $0x1
popq %rbx
pushq $0xe
popq %rdx
movq %rbp, %rdi
movq %rbx, %rsi
movq %rax, %rcx
callq 0x55e0
pushq $0x28
popq %rdx
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rcx
callq 0x55e0
movq 0x30(%rsp), %r15
negl %r15d
andl $0x3, %r15d
xorl %ebx, %ebx
testl %r14d, %r14d
cmovgl %r14d, %ebx
decl %r14d
leal (%r12,%r12,2), %ebp
imull %ebp, %r14d
pushq $0x3
popq %r12
subq $0x1, %rbx
jb 0x259c8
movslq %r14d, %r14
movq 0x8(%rsp), %rax
leaq (%rax,%r14), %rdi
movq %r12, %rsi
movq 0x10(%rsp), %rdx
movq %r13, %rcx
callq 0x55e0
movq %rsp, %rdi
pushq $0x1
popq %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x55e0
subl %ebp, %r14d
jmp 0x25990
movq 0x8(%rsp), %rdi
callq 0x53a0
movq %r13, %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x52d0