// File: /workspace/github_c_makefile_star2_O2/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 0x12d1
movq 0x8(%rsi), %r12
movq %r12, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x1080
testl %eax, %eax
js 0x12f0
movl %eax, %ebx
leaq 0x8(%rsp), %rsi
movl %eax, %edi
callq 0x1090
testl %eax, %eax
jne 0x1310
movq 0x38(%rsp), %r14
pushq $0x1
popq %rdx
pushq $0x2
popq %rcx
xorl %edi, %edi
movq %r14, %rsi
movl %ebx, %r8d
xorl %r9d, %r9d
callq 0x1030
cmpq $-0x1, %rax
je 0x132f
movq %rax, %r15
movl %ebx, 0x4(%rsp)
xorl %r12d, %r12d
leaq 0xe4b(%rip), %r13       # 0x2065
movq 0x2db7(%rip), %rbp      # 0x3fd8
leaq 0xddc(%rip), %rbx       # 0x2004
cmpq %r12, %r14
je 0x12a5
movzbl (%r15,%r12), %eax
leal -0x2b(%rax), %ecx
cmpl $0x3, %ecx
jbe 0x125a
cmpl $0x5d, %eax
je 0x126c
movq %r13, %rdi
cmpl $0x3e, %eax
je 0x1297
cmpl $0x5b, %eax
je 0x127e
cmpl $0x3c, %eax
jne 0x12a0
leaq 0xe10(%rip), %rdi       # 0x2068
jmp 0x1297
movslq (%rbx,%rcx,4), %rax
addq %rbx, %rax
jmpq *%rax
leaq 0xe01(%rip), %rdi       # 0x206b
jmp 0x1297
leaq 0xe07(%rip), %rdi       # 0x207a
jmp 0x1297
leaq 0xdf2(%rip), %rdi       # 0x206e
jmp 0x1297
leaq 0xdf2(%rip), %rdi       # 0x2077
jmp 0x1297
leaq 0xde3(%rip), %rdi       # 0x2071
jmp 0x1297
leaq 0xddd(%rip), %rdi       # 0x2074
movq (%rbp), %rsi
callq 0x1040
incq %r12
jmp 0x1228
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 0x2d20(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq (%rsi), %rdx
leaq 0xd2f(%rip), %rsi       # 0x2014
xorl %eax, %eax
callq 0x1060
pushq $0x1
jmp 0x130d
movq 0x2d01(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xd28(%rip), %rsi       # 0x2029
movq %r12, %rdx
xorl %eax, %eax
callq 0x1060
pushq $0x2
popq %rbp
jmp 0x12bd
movq 0x2ce1(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xd1c(%rip), %rsi       # 0x203d
movq %r12, %rdx
xorl %eax, %eax
callq 0x1060
pushq $0x3
jmp 0x134c
movq 0x2cc2(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xd11(%rip), %rsi       # 0x2051
movq %r12, %rdx
xorl %eax, %eax
callq 0x1060
pushq $0x4
popq %rbp
jmp 0x12b6