// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x58, %rsp
movl $0x0, 0x54(%rsp)
movl %edi, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movl $0x10, 0x44(%rsp)
movq $0x0, 0x38(%rsp)
movl $0x0, 0x34(%rsp)
cmpl $0x3, 0x50(%rsp)
jge 0x1242
movq 0x2ddc(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x48(%rsp), %rax
movq (%rax), %rdx
leaq 0xdd6(%rip), %rsi       # 0x2004
movb $0x0, %al
callq 0x1090
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0x15ae
cmpl $0x3, 0x50(%rsp)
jle 0x1292
movq 0x48(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0x10d0
movl %eax, 0x44(%rsp)
cmpl $0x1, 0x44(%rsp)
jge 0x1290
movq 0x2d8f(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x48(%rsp), %rax
movq 0x18(%rax), %rdx
leaq 0xdc0(%rip), %rsi       # 0x203c
movl $0x10, %ecx
movb $0x0, %al
callq 0x1090
movl $0x10, 0x44(%rsp)
jmp 0x1292
movq 0x48(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x48(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0xdf0(%rip), %rsi       # 0x20a0
callq 0x10c0
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jne 0x12f0
movq 0x2d2f(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x48(%rsp), %rax
movq 0x8(%rax), %rdx
leaq 0xdc7(%rip), %rsi       # 0x20a3
movb $0x0, %al
callq 0x1090
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0x15ae
movq 0x48(%rsp), %rax
movq 0x10(%rax), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x1070
movq %rax, %rdi
shlq $0x0, %rdi
callq 0x10b0
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x28(%rsp)
jne 0x134b
movq 0x2cd5(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xd95(%rip), %rsi       # 0x20c2
movb $0x0, %al
callq 0x1090
movq 0x18(%rsp), %rdi
callq 0x1040
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0x15ae
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0xd88(%rip), %rdx       # 0x20e9
movb $0x0, %al
callq 0x1060
movq 0x28(%rsp), %rdi
leaq 0xd7a(%rip), %rsi       # 0x20ee
callq 0x10c0
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
jne 0x13c4
movq 0x2c6b(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x28(%rsp), %rdx
leaq 0xdb4(%rip), %rsi       # 0x2150
movb $0x0, %al
callq 0x1090
movq 0x18(%rsp), %rdi
callq 0x1040
movq 0x28(%rsp), %rdi
callq 0x1030
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0x15ae
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rdx
leaq 0xd1b(%rip), %rsi       # 0x20f0
movb $0x0, %al
callq 0x1090
movq 0x18(%rsp), %rdi
callq 0x10a0
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x13f7
jmp 0x1487
cmpl $0x0, 0x34(%rsp)
jne 0x1419
movq 0x10(%rsp), %rdi
leaq 0xd1f(%rip), %rsi       # 0x2129
movb $0x0, %al
callq 0x1090
movl 0x44(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x34(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x34(%rsp)
movq 0x18(%rsp), %rdi
callq 0x1080
movl %eax, 0xc(%rsp)
cmpl $-0x1, 0xc(%rsp)
jne 0x143b
jmp 0x1487
movl 0x34(%rsp), %eax
movl 0x44(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jl 0x145d
movq 0x10(%rsp), %rdi
leaq 0xcc5(%rip), %rsi       # 0x211b
movb $0x0, %al
callq 0x1090
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %edx
leaq 0xcb0(%rip), %rsi       # 0x211d
movb $0x0, %al
callq 0x1090
movq 0x38(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x38(%rsp)
jmp 0x13dc
movq 0x10(%rsp), %rdi
leaq 0xc92(%rip), %rsi       # 0x2125
movb $0x0, %al
callq 0x1090
movq 0x10(%rsp), %rdi
callq 0x1040
movq 0x18(%rsp), %rdi
callq 0x1040
movq 0x38(%rsp), %rsi
movq 0x28(%rsp), %rdx
leaq 0xc6c(%rip), %rdi       # 0x212b
movb $0x0, %al
callq 0x1050
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0xc68(%rip), %rdx       # 0x2144
movb $0x0, %al
callq 0x1060
movq 0x28(%rsp), %rdi
leaq 0xbff(%rip), %rsi       # 0x20ee
callq 0x10c0
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
jne 0x1532
movq 0x2af0(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x28(%rsp), %rdx
leaq 0xc32(%rip), %rsi       # 0x2149
movb $0x0, %al
callq 0x1090
movq 0x28(%rsp), %rdi
callq 0x1030
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0x15ae
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rdx
movq 0x20(%rsp), %rcx
leaq 0xc27(%rip), %rsi       # 0x216f
movb $0x0, %al
callq 0x1090
movq 0x10(%rsp), %rdi
leaq 0xc39(%rip), %rsi       # 0x2194
movb $0x0, %al
callq 0x1090
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rdx
leaq 0xc36(%rip), %rsi       # 0x21a9
movb $0x0, %al
callq 0x1090
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rdx
leaq 0xc3a(%rip), %rsi       # 0x21c5
movb $0x0, %al
callq 0x1090
movq 0x10(%rsp), %rdi
callq 0x1040
movq 0x28(%rsp), %rdi
callq 0x1030
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq