// File: /workspace/github_c_makefile_star2_O0/CerfVert94[P]lenet_cnn/main.c
// Function: main
int main(void){
	#if !defined(_CONV_WEIGHTS_)
	char Input[10][29*29] = {};

	//Load gray scale image.
	const char *bmp_path[10] = {"./Data/0.bmp",
								"./Data/1.bmp",
								"./Data/2.bmp",
								"./Data/3.bmp",
								"./Data/4.bmp",
								"./Data/5.bmp",
								"./Data/6.bmp",
								"./Data/7.bmp",
								"./Data/8.bmp",
								"./Data/9.bmp"};
	int nb_idx, input_idx, result;
	
	for (nb_idx = 0; nb_idx < 10; nb_idx++) {
		// Read the image
		read_data(bmp_path[nb_idx], Input[nb_idx]);
		// Print the handwritten digit : 
		for(input_idx = 0; input_idx < IMG_WIDTH * IMG_HEIGHT; input_idx++) {
			if((input_idx % IMG_WIDTH) == 0)
				fputc('\n',stdout);
			
			fputc(Input[nb_idx][input_idx] + '0',stdout);
		}
		fputc('\n',stdout);
		
		// Run the neural network to recognized the digit : 
		result = evaluate(Input[nb_idx]);
		printf("The recognized digit is %d\n", result);	
		if (nb_idx != result) {
			fprintf(stderr, "Recognized digit is a mismatch : %d =/= %d\n", nb_idx, result);
			exit(EXIT_FAILURE);
		}
		
	}
	write_data(Input, 10);
	fprintf(stdout, "Success\n");
	#else
	#define NB_WEIGHTS_LAYER1 ((5 * 5 + 1) * 6)
	#define NB_WEIGHTS_LAYER2 (((5 * 5 + 1) * 6) * 50)
	#define NB_WEIGHTS_LAYER3 ((5 * 5 * 50 + 1) * 100)
	#define NB_WEIGHTS_LAYER4 ((100 + 1) * 10)
	
	FILE *ptr_file1, *ptr_file2;
	// Create new header files.
	ptr_file1 = fopen("short_weight.h", "wb");
	ptr_file2 = fopen("char_weight.h", "wb");
	// Null pointer signifies an error :
	if (!ptr_file1 || !ptr_file2) {
		fprintf(stdout, "Failed creating new header files\n");
		exit(EXIT_FAILURE);
	}
	
	fprintf(ptr_file1, "#ifndef _SHORT_WEIGHT_H_\n#define _SHORT_WEIGHT_H_\n");
	fprintf(ptr_file2, "#ifndef _CHAR_WEIGHT_H_\n#define _CHAR_WEIGHT_H_\n");
	write_weights(ptr_file1, ptr_file2, "Layer1_Weights_CPU", Layer1_Weights_CPU, NB_WEIGHTS_LAYER1);
	write_weights(ptr_file1, ptr_file2, "Layer2_Weights_CPU", Layer2_Weights_CPU, NB_WEIGHTS_LAYER2);
	write_weights(ptr_file1, ptr_file2, "Layer3_Weights_CPU", Layer3_Weights_CPU, NB_WEIGHTS_LAYER3);
	write_weights(ptr_file1, ptr_file2, "Layer4_Weights_CPU", Layer4_Weights_CPU, NB_WEIGHTS_LAYER4);
	fprintf(ptr_file1, "#endif\n");
	fprintf(ptr_file2, "#endif\n");
	fclose(ptr_file1);
	fclose(ptr_file2);
	#endif
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2140, %rsp           # imm = 0x2140
movl $0x0, -0x4(%rbp)
leaq -0x20e0(%rbp), %rdi
xorl %esi, %esi
movl $0x20da, %edx           # imm = 0x20DA
callq 0x1060
leaq -0x2130(%rbp), %rdi
leaq 0x3a1d(%rip), %rsi      # 0x4d80
movl $0x50, %edx
callq 0x10b0
movl $0x0, -0x2134(%rbp)
cmpl $0xa, -0x2134(%rbp)
jge 0x14d0
movslq -0x2134(%rbp), %rax
movq -0x2130(%rbp,%rax,8), %rdi
movslq -0x2134(%rbp), %rax
leaq -0x20e0(%rbp), %rsi
imulq $0x349, %rax, %rax      # imm = 0x349
addq %rax, %rsi
callq 0x1510
movl $0x0, -0x2138(%rbp)
cmpl $0x349, -0x2138(%rbp)   # imm = 0x349
jge 0x1433
movl -0x2138(%rbp), %eax
movl $0x1d, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x13ed
movq 0x3bf0(%rip), %rax      # 0x4fd0
movq (%rax), %rsi
movl $0xa, %edi
callq 0x1080
movslq -0x2134(%rbp), %rcx
leaq -0x20e0(%rbp), %rax
imulq $0x349, %rcx, %rcx      # imm = 0x349
addq %rcx, %rax
movslq -0x2138(%rbp), %rcx
movsbl (%rax,%rcx), %edi
addl $0x30, %edi
movq 0x3bb6(%rip), %rax      # 0x4fd0
movq (%rax), %rsi
callq 0x1080
movl -0x2138(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2138(%rbp)
jmp 0x13ba
movq 0x3b96(%rip), %rax      # 0x4fd0
movq (%rax), %rsi
movl $0xa, %edi
callq 0x1080
movslq -0x2134(%rbp), %rax
leaq -0x20e0(%rbp), %rdi
imulq $0x349, %rax, %rax      # imm = 0x349
addq %rax, %rdi
callq 0x1200
movl %eax, -0x213c(%rbp)
movl -0x213c(%rbp), %esi
leaq 0x1c23(%rip), %rdi      # 0x309a
movb $0x0, %al
callq 0x1040
movl -0x2134(%rbp), %eax
cmpl -0x213c(%rbp), %eax
je 0x14ba
movq 0x3b5d(%rip), %rax      # 0x4ff0
movq (%rax), %rdi
movl -0x2134(%rbp), %edx
movl -0x213c(%rbp), %ecx
leaq 0x1c0d(%rip), %rsi      # 0x30b6
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x10f0
jmp 0x14bc
movl -0x2134(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2134(%rbp)
jmp 0x1377
leaq -0x20e0(%rbp), %rdi
movl $0xa, %esi
callq 0x1660
movq 0x3ae8(%rip), %rax      # 0x4fd0
movq (%rax), %rdi
leaq 0x1bf0(%rip), %rsi      # 0x30e2
movb $0x0, %al
callq 0x1090
xorl %eax, %eax
addq $0x2140, %rsp           # imm = 0x2140
popq %rbp
retq
nopw %cs:(%rax,%rax)