// File: /workspace/github_c_makefile_star2_O3/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 0x1302
movq 0x8(%rsi), %r12
movq %r12, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x1080
testl %eax, %eax
js 0x1311
movl %eax, %ebx
leaq 0x8(%rsp), %rsi
movl %eax, %edi
callq 0x1090
testl %eax, %eax
jne 0x1333
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 0x1355
movq %rax, %r15
testq %r14, %r14
je 0x12da
xorl %r12d, %r12d
leaq 0xdc1(%rip), %rbp       # 0x2004
movq 0x2d8e(%rip), %r13      # 0x3fd8
movzbl (%r15,%r12), %eax
cmpl $0x3b, %eax
jg 0x126f
addl $-0x2b, %eax
cmpl $0x3, %eax
ja 0x12ce
movslq (%rbp,%rax,4), %rax
addq %rbp, %rax
jmpq *%rax
leaq 0xdfe(%rip), %rdi       # 0x206b
jmp 0x12c5
cmpl $0x5a, %eax
jg 0x1287
cmpl $0x3c, %eax
je 0x129a
leaq 0xde5(%rip), %rdi       # 0x2065
cmpl $0x3e, %eax
je 0x12c5
jmp 0x12ce
cmpl $0x5b, %eax
je 0x12a3
cmpl $0x5d, %eax
jne 0x12ce
leaq 0xde2(%rip), %rdi       # 0x207a
jmp 0x12c5
leaq 0xdc7(%rip), %rdi       # 0x2068
jmp 0x12c5
leaq 0xdcd(%rip), %rdi       # 0x2077
jmp 0x12c5
leaq 0xdbb(%rip), %rdi       # 0x206e
jmp 0x12c5
leaq 0xdb5(%rip), %rdi       # 0x2071
jmp 0x12c5
leaq 0xdaf(%rip), %rdi       # 0x2074
movq (%r13), %rsi
callq 0x1040
incq %r12
cmpq %r12, %r14
jne 0x124a
movq %r15, %rdi
movq %r14, %rsi
callq 0x1070
xorl %ebp, %ebp
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 0x12ee
movq 0x2ce0(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xd07(%rip), %rsi       # 0x2029
movq %r12, %rdx
xorl %eax, %eax
callq 0x1060
movl $0x2, %ebp
jmp 0x12ee
movq 0x2cbe(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcf9(%rip), %rsi       # 0x203d
movq %r12, %rdx
xorl %eax, %eax
callq 0x1060
movl $0x3, %ebp
jmp 0x12e7
movq 0x2c9c(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xceb(%rip), %rsi       # 0x2051
movq %r12, %rdx
xorl %eax, %eax
callq 0x1060
movl $0x4, %ebp
jmp 0x12e7