// File: /workspace/github_c_makefile_star2_O1/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, %r12
cmpl $0x2, %edi
jle 0x14e9
movl $0x10, %ebx
cmpl $0x3, %edi
je 0x1307
movq 0x18(%r12), %r14
movq %r14, %rdi
callq 0x10e0
movl %eax, %ebx
testl %eax, %eax
jle 0x1536
movq 0x8(%r12), %rdi
movq 0x10(%r12), %r14
leaq 0xd88(%rip), %rsi       # 0x20a0
callq 0x10d0
testq %rax, %rax
je 0x14f3
movq %rax, %r15
movq 0x10(%r12), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x1070
movq %rax, %rdi
callq 0x10c0
testq %rax, %rax
je 0x1501
movq %rax, %r13
leaq 0xd96(%rip), %rdx       # 0x20e9
movl $0x1000, %esi           # imm = 0x1000
movq %rax, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x1060
leaq 0xd82(%rip), %rsi       # 0x20ee
movq %r13, %rdi
callq 0x10d0
testq %rax, %rax
je 0x150b
movq %rax, %r12
movq %r13, 0x8(%rsp)
leaq 0xd64(%rip), %rsi       # 0x20f0
movq %rax, %rdi
movq %r14, 0x10(%rsp)
movq %r14, %rdx
xorl %eax, %eax
callq 0x10a0
xorl %ebp, %ebp
xorl %r13d, %r13d
movq %r15, %rdi
callq 0x10b0
testl %eax, %eax
jne 0x1402
testl %ebp, %ebp
jne 0x13c2
movl $0xa, %edi
movq %r12, %rsi
callq 0x1090
movl %ebx, %ebp
movq %r15, %rdi
callq 0x1080
movl %eax, %r14d
cmpl $-0x1, %eax
je 0x13fa
cmpl %ebx, %ebp
jl 0x13e3
movl $0x9, %edi
movq %r12, %rsi
callq 0x1090
movq %r12, %rdi
leaq 0xd2e(%rip), %rsi       # 0x211b
movl %r14d, %edx
xorl %eax, %eax
callq 0x10a0
incq %r13
decl %ebp
cmpl $-0x1, %r14d
jne 0x13a3
leaq 0xd1a(%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 0xcf7(%rip), %rdi       # 0x2129
xorl %ebp, %ebp
movq %r13, %rsi
movq 0x8(%rsp), %r15
movq %r15, %rdx
xorl %eax, %eax
callq 0x1050
leaq 0xcf5(%rip), %rdx       # 0x2142
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rdi
movq 0x10(%rsp), %r14
movq %r14, %rcx
xorl %eax, %eax
callq 0x1060
leaq 0xc83(%rip), %rsi       # 0x20ee
movq %r15, %rdi
callq 0x10d0
testq %rax, %rax
je 0x1518
movq %rax, %rbx
leaq 0xce7(%rip), %rsi       # 0x216d
movq %rax, %rdi
movq %r14, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x10a0
leaq 0xcf5(%rip), %rdi       # 0x2192
movl $0x14, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x10f0
leaq 0xcf1(%rip), %rsi       # 0x21a7
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x10a0
leaq 0xcf9(%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 0x1525
movq %r12, %rdi
callq 0x11bb
jmp 0x1520
addq $0x8, %r12
movq %r12, %rdi
callq 0x119d
jmp 0x1520
movq %r15, %rdi
callq 0x1174
jmp 0x1520
movq %r13, %rdi
movq %r15, %rsi
callq 0x1138
jmp 0x1520
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 0x2abb(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xaf5(%rip), %rsi       # 0x203c
movl $0x10, %ebx
movq %r14, %rdx
movl $0x10, %ecx
xorl %eax, %eax
callq 0x10a0
jmp 0x1307