// File: /workspace/github_cpp_makefile_star2_O1/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 $0x98, %rsp
movq %r9, 0x58(%rsp)
movq %r8, 0x50(%rsp)
movq %rcx, 0x48(%rsp)
movl %esi, %ebx
movq %rdi, 0x30(%rsp)
movq %rdx, 0x10(%rsp)
leal (,%rdx,4), %eax
leal (%rbx,%rbx,2), %ecx
movq %rax, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
imull %ecx, %eax
movslq %eax, %r14
addl $0x36, %eax
movl %eax, 0x1c(%rsp)
movq %r14, %rdi
callq 0x5420
movq %rax, 0x28(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x51b0
movq %rbx, 0x20(%rsp)
testl %ebx, %ebx
jle 0x2f401
movl 0x20(%rsp), %eax
movl 0x10(%rsp), %ecx
movq 0x28(%rsp), %rdx
addq $0x2, %rdx
leaq (%rax,%rax,2), %rsi
xorl %edi, %edi
cmpl $0x0, 0x10(%rsp)
jle 0x2f3f5
movq 0x48(%rsp), %r8
movq (%r8), %r8
leaq (,%rdi,8), %r9
leaq (%r9,%r9,2), %r10
movq (%r8,%r10), %r8
movq 0x58(%rsp), %r9
movq (%r9), %r9
movq (%r9,%r10), %r9
movq 0x50(%rsp), %r11
movq (%r11), %r11
movq (%r11,%r10), %r10
xorl %r11d, %r11d
xorl %ebx, %ebx
movb (%r8,%r11,4), %r13b
movb (%r9,%r11,4), %bpl
movb (%r10,%r11,4), %r12b
movslq %ebx, %r14
imulq %rax, %r14
addq %rdi, %r14
leaq (%r14,%r14,2), %r15
addq %rdx, %r15
xorl %r14d, %r14d
movb %r13b, (%r15)
movb %bpl, -0x1(%r15)
movb %r12b, -0x2(%r15)
addq %rsi, %r15
decl %r14d
cmpl $-0x4, %r14d
jne 0x2f3d3
subl %r14d, %ebx
incq %r11
cmpq %rcx, %r11
jne 0x2f3b3
incq %rdi
cmpq %rax, %rdi
jne 0x2f377
movabsq $0x3600000000, %rax     # imm = 0x3600000000
leaq 0x60(%rsp), %rbp
movq %rax, 0x6(%rbp)
movq $0x4d42, (%rbp)         # imm = 0x4D42
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %r13
movaps %xmm0, (%r13)
movaps %xmm0, 0x10(%r13)
movq $0x0, 0x20(%r13)
movb $0x28, (%r13)
movb $0x1, 0xc(%r13)
movb $0x18, 0xe(%r13)
movb $0x0, 0xe(%rsp)
movw $0x0, 0xc(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, 0x2(%rbp)
movq 0x20(%rsp), %rbx
movb %bl, 0x4(%r13)
movb %bh, 0x75(%rsp)
movl %ebx, %eax
shrl $0x10, %eax
movb %al, 0x6(%r13)
movl %ebx, %eax
shrl $0x18, %eax
movb %al, 0x7(%r13)
movq 0x40(%rsp), %r14
movb %r14b, 0x8(%r13)
movq 0x10(%rsp), %r15
movl %r15d, %eax
shrl $0x6, %eax
movb %al, 0x9(%r13)
movl %r15d, %eax
shrl $0xe, %eax
movb %al, 0xa(%r13)
movl %r15d, %eax
shrl $0x16, %eax
movb %al, 0xb(%r13)
movq 0x30(%rsp), %rax
movq (%rax), %rdi
leaq 0x7d2f(%rip), %rsi      # 0x371e1
callq 0x5390
movq %rax, %r12
movl $0x1, %esi
movl $0xe, %edx
movq %rbp, %rdi
movq %rax, %rcx
callq 0x5600
movl $0x1, %esi
movl $0x28, %edx
movq %r13, %rdi
movq %r12, %rcx
callq 0x5600
testl %r15d, %r15d
movq 0x38(%rsp), %r13
movq 0x28(%rsp), %r15
jle 0x2f54f
movslq %ebx, %rax
movq %rax, 0x10(%rsp)
negl %r13d
andl $0x3, %r13d
cmpl $0x2, %r14d
movl $0x1, %ebp
cmovgel %r14d, %ebp
decl %r14d
leal (%rbx,%rbx,2), %ebx
imull %ebx, %r14d
movslq %r14d, %r14
leaq (%r15,%r14), %rdi
movl $0x3, %esi
movq 0x10(%rsp), %rdx
movq %r12, %rcx
callq 0x5600
movl $0x1, %esi
leaq 0xc(%rsp), %rdi
movq %r13, %rdx
movq %r12, %rcx
callq 0x5600
subl %ebx, %r14d
decq %rbp
jne 0x2f519
movq %r15, %rdi
callq 0x53c0
movq %r12, %rdi
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x52d0