// File: /workspace/github_cpp_makefile_star2_O3/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, %rsi
addl $0x36, %eax
movl %eax, 0x1c(%rsp)
movl $0x1, %edi
callq 0x51e0
movq %rax, 0x28(%rsp)
movq %rbx, 0x20(%rsp)
testl %ebx, %ebx
jle 0x2f070
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 0x2f064
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
movslq %ebx, %rbx
movq %rax, %r14
imulq %rbx, %r14
addq %rdi, %r14
leaq (%r14,%r14,2), %r13
addq %rdx, %r13
movb (%r8,%r11,4), %bpl
movb (%r9,%r11,4), %r12b
movb (%r10,%r11,4), %r15b
addl $0x4, %ebx
movl $0x4, %r14d
movb %bpl, (%r13)
movb %r12b, -0x1(%r13)
movb %r15b, -0x2(%r13)
addq %rsi, %r13
decl %r14d
jne 0x2f048
incq %r11
cmpq %rcx, %r11
jne 0x2f01f
incq %rdi
cmpq %rax, %rdi
jne 0x2efe3
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 0x80c0(%rip), %rsi      # 0x371e1
callq 0x53a0
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 0x2f1b7
movslq %ebx, %rax
movq %rax, 0x10(%rsp)
negl %r13d
andl $0x3, %r13d
movq %rbx, %rax
movl %r14d, %ebx
decl %r14d
leal (%rax,%rax,2), %ebp
imull %ebp, %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 %ebp, %r14d
decq %rbx
jne 0x2f181
movq %r15, %rdi
callq 0x53d0
movq %r12, %rdi
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x52e0