// File: /workspace/github_c_makefile_star2_O1/martin-decky[P]ichiglyph/transpiler/bf2ig/bf2ig.c
// Function: main
int main(int argc, char *argv[])
{
	/*
	 * The first command-line argument is the Brainfuck
	 * source file.
	 */
	if (argc < 2) {
		fprintf(stderr, "Syntax: %s <source>\n", argv[0]);
		return 1;
	}
	
	char *source_name = argv[1];
	int source = open(source_name, O_RDONLY);
	if (source < 0) {
		fprintf(stderr, "%s: Unable to open\n", source_name);
		return 2;
	}
	
	struct stat stat;
	int ret = fstat(source, &stat);
	if (ret != 0) {
		fprintf(stderr, "%s: Unable to stat\n", source_name);
		close(source);
		return 3;
	}
	
	size_t program_size = stat.st_size;
	
	/*
	 * We mmap the entire source file.
	 */
	uint8_t *program = (uint8_t *) mmap(NULL, program_size, PROT_READ,
	    MAP_PRIVATE, source, 0);
	if (program == MAP_FAILED) {
		fprintf(stderr, "%s: Unable to mmap\n", source_name);
		close(source);
		return 4;
	}
	
	size_t ip = 0;
	
	while (ip < program_size) {
		/*
		 * Brainfuck instruction fetch and decode.
		 */
		uint8_t brainfuck_opcode = program[ip];
		instruction_t instruction =
		    brainfuck_opcode_decode(brainfuck_opcode);
		
		/*
		 * Ichiglyph instruction encode.
		 */
		switch (instruction) {
		case INST_DP_INC:
			fputs("ll", stdout);
			break;
		case INST_DP_DEC:
			fputs("lI", stdout);
			break;
		case INST_VAL_INC:
			fputs("Il", stdout);
			break;
		case INST_VAL_DEC:
			fputs("II", stdout);
			break;
		case INST_VAL_OUTPUT:
			fputs("1l", stdout);
			break;
		case INST_VAL_ACCEPT:
			fputs("1I", stdout);
			break;
		case INST_JMP_FORWARD:
			fputs("l1", stdout);
			break;
		case INST_JMP_BACK:
			fputs("I1", stdout);
			break;
		case INST_NOP:
			break;
		}
		
		ip++;
	}
	
	munmap(program, program_size);
	close(source);
	
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
cmpl $0x1, %edi
jle 0x1314
movq 0x8(%rsi), %r12
movq %r12, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x1080
testl %eax, %eax
js 0x1323
movl %eax, %ebx
leaq 0x8(%rsp), %rsi
movl %eax, %edi
callq 0x1090
testl %eax, %eax
jne 0x1345
movq 0x38(%rsp), %r14
xorl %edi, %edi
movq %r14, %rsi
movl $0x1, %edx
movl $0x2, %ecx
movl %ebx, %r8d
xorl %r9d, %r9d
callq 0x1030
cmpq $-0x1, %rax
je 0x1367
movq %rax, %r15
movl %ebx, 0x4(%rsp)
testq %r14, %r14
je 0x12e8
xorl %r12d, %r12d
leaq 0xdbd(%rip), %r13       # 0x2004
leaq 0xdc6(%rip), %rbp       # 0x2014
movq 0x2d83(%rip), %rbx      # 0x3fd8
movzbl (%r15,%r12), %ecx
movb $0x1, %al
cmpl $0x3b, %ecx
jg 0x127a
addl $-0x2b, %ecx
cmpl $0x3, %ecx
ja 0x12c1
movslq (%r13,%rcx,4), %rcx
addq %r13, %rcx
jmpq *%rcx
movl $0x2, %ecx
jmp 0x12c8
cmpl $0x5a, %ecx
jg 0x128d
cmpl $0x3c, %ecx
je 0x129e
cmpl $0x3e, %ecx
jne 0x12c1
xorl %ecx, %ecx
jmp 0x12c8
cmpl $0x5b, %ecx
je 0x12a5
cmpl $0x5d, %ecx
jne 0x12c1
movl $0x7, %ecx
jmp 0x12c8
movl $0x1, %ecx
jmp 0x12c8
movl $0x6, %ecx
jmp 0x12c8
movl $0x3, %ecx
jmp 0x12c8
movl $0x4, %ecx
jmp 0x12c8
movl $0x5, %ecx
jmp 0x12c8
movl $0x8, %ecx
xorl %eax, %eax
testb %al, %al
je 0x12dc
movslq (%rbp,%rcx,4), %rdi
addq %rbp, %rdi
movq (%rbx), %rsi
callq 0x1040
incq %r12
cmpq %r12, %r14
jne 0x1255
movq %r15, %rdi
movq %r14, %rsi
callq 0x1070
xorl %ebp, %ebp
movl 0x4(%rsp), %ebx
movl %ebx, %edi
callq 0x1050
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %rdi
callq 0x10b0
movl $0x1, %ebp
jmp 0x1300
movq 0x2cce(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xd15(%rip), %rsi       # 0x2049
movq %r12, %rdx
xorl %eax, %eax
callq 0x1060
movl $0x2, %ebp
jmp 0x1300
movq 0x2cac(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xd07(%rip), %rsi       # 0x205d
movq %r12, %rdx
xorl %eax, %eax
callq 0x1060
movl $0x3, %ebp
jmp 0x12f9
movq 0x2c8a(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcf9(%rip), %rsi       # 0x2071
movq %r12, %rdx
xorl %eax, %eax
callq 0x1060
movl $0x4, %ebp
jmp 0x12f9