// File: /workspace/github_c_makefile_star2_O3/Mikejmoffitt[P]romtools/binclude/binclude.c
// Function: main
int main(int argc, char **argv)
{
	int max_items_per_line = MAX_ITEMS_PER_LINE;
	size_t bytes_written = 0;
	int out_count_per_line = 0;
	char *out_fpath;
	char *symbol_name;
	FILE *fin, *fout;
	if (argc < 3)
	{
		fprintf(stderr, "Usage:\n%s <binary file> <symbol name> "
		                "[items per line]\n", argv[0]);
		return -1;
	}

	if (argc > 3)
	{
		max_items_per_line = atoi(argv[3]);
		if (max_items_per_line < 1)
		{
			fprintf(stderr, "Warning: Invalid value \"%s\" provided for \
			                 max items per line. Defaulting to %d.\n",
			                 argv[3], MAX_ITEMS_PER_LINE);
			max_items_per_line = MAX_ITEMS_PER_LINE;
		}
	}

	symbol_name = argv[2];

	fin = fopen(argv[1], "rb");
	if (!fin)
	{
		fprintf(stderr, "Couldn't open %s for reading.\n", argv[1]);
		return -1;
	}

	out_fpath = malloc(sizeof(char) * strnlen(argv[2], 4096));
	if (!out_fpath)
	{
		fprintf(stderr, "Couldn't allocate file output path...\n");
		fclose(fin);
		return -1;
	}

	snprintf(out_fpath, 4096, "%s.c", symbol_name);

	fout = fopen(out_fpath, "w");
	if (!fout)
	{
		fprintf(stderr, "Couldn't open %s for writing.\n", out_fpath);
		fclose(fin);
		free(out_fpath);
		return -1;
	}

	// Write const array to C file
	fprintf(fout, "#include <stdint.h>\nconst uint8_t %s[] =\n{",
	        symbol_name);

	while (!feof(fin))
	{
		if (out_count_per_line == 0)
		{
			fprintf(fout, "\n");
			out_count_per_line = max_items_per_line;
		}

		out_count_per_line--;

		int fetch = fgetc(fin);
		if (fetch == EOF)
		{
			break;
		}
		if (out_count_per_line >= max_items_per_line - 1)
		{
			fprintf(fout, "\t");
		}
		fprintf(fout, "0x%02X,", fetch);
		bytes_written++;
	}
	fprintf(fout, "\n};\n\n");
	fclose(fout);
	fclose(fin);

	printf("Wrote %d bytes to \"%s\".\n", bytes_written, out_fpath);

	// Write header file
	snprintf(out_fpath, 4096, "%s.h", symbol_name);
	fout = fopen(out_fpath, "w");
	if (!fout)
	{
		fprintf(stderr, "Error: Couldn't open %s for writing.\n", out_fpath);
		free(out_fpath);
		return -1;
	}

	fprintf(fout, "#ifndef _BIN_%s_H\n#define _BIN_%s_H\n", symbol_name, symbol_name);
	fprintf(fout, "#include <stdint.h>\n");
	fprintf(fout, "extern const uint8_t %s[];\n", symbol_name);
	fprintf(fout, "#endif // _BIN_%s_H\n", symbol_name);
	fclose(fout);

	free(out_fpath);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
cmpl $0x2, %edi
jle 0x14fc
movl $0x10, 0x4(%rsp)
cmpl $0x3, %edi
je 0x130b
movq 0x18(%rbx), %r14
movq %r14, %rdi
callq 0x10e0
movl %eax, 0x4(%rsp)
testl %eax, %eax
jle 0x1549
movq 0x8(%rbx), %rdi
movq 0x10(%rbx), %r14
leaq 0xd86(%rip), %rsi       # 0x20a0
callq 0x10d0
testq %rax, %rax
je 0x1506
movq %rax, %r15
movq 0x10(%rbx), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x1070
movq %rax, %rdi
callq 0x10c0
testq %rax, %rax
je 0x1514
movq %rax, %rbx
leaq 0xd95(%rip), %rdx       # 0x20e9
movl $0x1000, %esi           # imm = 0x1000
movq %rax, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x1060
leaq 0xd81(%rip), %rsi       # 0x20ee
movq %rbx, %rdi
callq 0x10d0
testq %rax, %rax
je 0x151e
movq %rax, %r12
movq %rbx, 0x8(%rsp)
leaq 0xd63(%rip), %rsi       # 0x20f0
movq %rax, %rdi
movq %r14, 0x10(%rsp)
movq %r14, %rdx
xorl %eax, %eax
callq 0x10a0
movq %r15, %rdi
callq 0x10b0
testl %eax, %eax
je 0x13b0
xorl %r13d, %r13d
jmp 0x1415
leaq 0xd64(%rip), %rbx       # 0x211b
xorl %r13d, %r13d
xorl %ebp, %ebp
testl %ebp, %ebp
jne 0x13d1
movl $0xa, %edi
movq %r12, %rsi
callq 0x1090
movl 0x4(%rsp), %ebp
movq %r15, %rdi
callq 0x1080
cmpl $-0x1, %eax
je 0x1415
movl %eax, %r14d
cmpl 0x4(%rsp), %ebp
jl 0x13f4
movl $0x9, %edi
movq %r12, %rsi
callq 0x1090
decl %ebp
movq %r12, %rdi
movq %rbx, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x10a0
incq %r13
movq %r15, %rdi
callq 0x10b0
testl %eax, %eax
je 0x13bc
leaq 0xd07(%rip), %rdi       # 0x2123
movl $0x5, %esi
movl $0x1, %edx
movq %r12, %rcx
callq 0x10f0
movq %r12, %rdi
callq 0x1040
movq %r15, %rdi
callq 0x1040
leaq 0xce4(%rip), %rdi       # 0x2129
xorl %ebp, %ebp
movq %r13, %rsi
movq 0x8(%rsp), %r15
movq %r15, %rdx
xorl %eax, %eax
callq 0x1050
leaq 0xce2(%rip), %rdx       # 0x2142
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rdi
movq 0x10(%rsp), %r14
movq %r14, %rcx
xorl %eax, %eax
callq 0x1060
leaq 0xc70(%rip), %rsi       # 0x20ee
movq %r15, %rdi
callq 0x10d0
testq %rax, %rax
je 0x152b
movq %rax, %rbx
leaq 0xcd4(%rip), %rsi       # 0x216d
movq %rax, %rdi
movq %r14, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x10a0
leaq 0xce2(%rip), %rdi       # 0x2192
movl $0x14, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x10f0
leaq 0xcde(%rip), %rsi       # 0x21a7
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x10a0
leaq 0xce6(%rip), %rsi       # 0x21c3
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x10a0
movq %rbx, %rdi
callq 0x1040
movq %r15, %rdi
callq 0x1030
jmp 0x1538
movq %rbx, %rdi
callq 0x11bb
jmp 0x1533
addq $0x8, %rbx
movq %rbx, %rdi
callq 0x119d
jmp 0x1533
movq %r15, %rdi
callq 0x1174
jmp 0x1533
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1138
jmp 0x1533
movq %r15, %rdi
callq 0x1110
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2aa8(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xae2(%rip), %rsi       # 0x203c
movl $0x10, 0x4(%rsp)
movq %r14, %rdx
movl $0x10, %ecx
xorl %eax, %eax
callq 0x10a0
jmp 0x130b