// File: /workspace/github_c_makefile_star2_O2/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 0x140a
cmpl $0x3, %edi
jne 0x1221
pushq $0x10
popq %r14
jmp 0x1238
movq 0x18(%rbx), %r15
movq %r15, %rdi
callq 0x10e0
movl %eax, %r14d
testl %eax, %eax
jle 0x14c3
movq 0x8(%rbx), %rdi
movq 0x10(%rbx), %r13
leaq 0xe59(%rip), %rsi       # 0x20a0
callq 0x10d0
testq %rax, %rax
je 0x1420
movq %rax, %r15
movq 0x10(%rbx), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x1070
movq %rax, %rdi
callq 0x10c0
testq %rax, %rax
je 0x143e
movq %rax, %rbx
leaq 0xe68(%rip), %rdx       # 0x20e9
movl $0x1000, %esi           # imm = 0x1000
movq %rax, %rdi
movq %r13, %rcx
xorl %eax, %eax
callq 0x1060
leaq 0xe54(%rip), %rsi       # 0x20ee
movq %rbx, %rdi
callq 0x10d0
testq %rax, %rax
je 0x1464
movq %rax, %r12
movq %rbx, 0x10(%rsp)
leaq 0xe36(%rip), %rsi       # 0x20f0
movq %rax, %rdi
movq %r13, 0x8(%rsp)
movq %r13, %rdx
xorl %eax, %eax
callq 0x10a0
xorl %ebp, %ebp
xorl %r13d, %r13d
movq %r15, %rdi
callq 0x10b0
testl %eax, %eax
jne 0x1328
testl %ebp, %ebp
jne 0x12ef
pushq $0xa
popq %rdi
movq %r12, %rsi
callq 0x1090
movl %r14d, %ebp
movq %r15, %rdi
callq 0x1080
cmpl $-0x1, %eax
je 0x1328
movl %eax, %ebx
cmpl %r14d, %ebp
jl 0x130e
pushq $0x9
popq %rdi
movq %r12, %rsi
callq 0x1090
decl %ebp
movq %r12, %rdi
leaq 0xe01(%rip), %rsi       # 0x211b
movl %ebx, %edx
xorl %eax, %eax
callq 0x10a0
incq %r13
jmp 0x12d1
leaq 0xdf4(%rip), %rdi       # 0x2123
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
movq %r12, %rcx
callq 0x10f0
movq %r12, %rdi
callq 0x1040
movq %r15, %rdi
callq 0x1040
leaq 0xdd5(%rip), %rdi       # 0x2129
xorl %ebp, %ebp
movq %r13, %rsi
movq 0x10(%rsp), %r14
movq %r14, %rdx
xorl %eax, %eax
callq 0x1050
leaq 0xdd3(%rip), %rdx       # 0x2142
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdi
movq 0x8(%rsp), %r15
movq %r15, %rcx
xorl %eax, %eax
callq 0x1060
leaq 0xd61(%rip), %rsi       # 0x20ee
movq %r14, %rdi
callq 0x10d0
testq %rax, %rax
je 0x148c
movq %rax, %rbx
leaq 0xdc5(%rip), %rsi       # 0x216d
movq %rax, %rdi
movq %r15, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x10a0
leaq 0xdd3(%rip), %rdi       # 0x2192
pushq $0x14
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x10f0
leaq 0xdd3(%rip), %rsi       # 0x21a7
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x10a0
leaq 0xddb(%rip), %rsi       # 0x21c3
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x10a0
movq %rbx, %rdi
callq 0x1040
movq %r14, %rdi
callq 0x1030
jmp 0x14b2
movq 0x2be7(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq (%rbx), %rdx
leaq 0xbe6(%rip), %rsi       # 0x2004
jmp 0x1435
movq 0x2bd1(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x8(%rbx), %rdx
leaq 0xc6e(%rip), %rsi       # 0x20a3
xorl %eax, %eax
callq 0x10a0
jmp 0x14af
movq 0x2bb3(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc73(%rip), %rdi       # 0x20c2
pushq $0x26
popq %rsi
pushq $0x1
popq %rdx
callq 0x10f0
movq %r15, %rdi
callq 0x1040
jmp 0x14af
movq 0x2b8d(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcd9(%rip), %rsi       # 0x214e
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10a0
movq %r15, %rdi
callq 0x1040
movq %rbx, %rdi
jmp 0x14aa
movq 0x2b65(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcaa(%rip), %rsi       # 0x2147
movq %r14, %rdx
xorl %eax, %eax
callq 0x10a0
movq %r14, %rdi
callq 0x1030
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2b2e(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xb68(%rip), %rsi       # 0x203c
pushq $0x10
popq %r14
movq %r15, %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x10a0
jmp 0x1238