// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xe0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x11ea
movq 0x2e32(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0xf1d(%rip), %rsi       # 0x20f4
movb $0x0, %al
callq 0x1060
movl $0x1, -0x4(%rbp)
jmp 0x146d
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x1080
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0x1234
movq 0x2de5(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0xee8(%rip), %rsi       # 0x2109
movb $0x0, %al
callq 0x1060
movl $0x2, -0x4(%rbp)
jmp 0x146d
movl -0x1c(%rbp), %edi
leaq -0xb0(%rbp), %rsi
callq 0x1090
movl %eax, -0xb4(%rbp)
cmpl $0x0, -0xb4(%rbp)
je 0x1282
movq 0x2d9f(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0xeb6(%rip), %rsi       # 0x211d
movb $0x0, %al
callq 0x1060
movl -0x1c(%rbp), %edi
callq 0x1050
movl $0x3, -0x4(%rbp)
jmp 0x146d
movq -0x80(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rsi
movl -0x1c(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x1, %edx
movl $0x2, %ecx
movq %r9, %rdi
callq 0x1030
movq %rax, -0xc8(%rbp)
movq $-0x1, %rax
cmpq %rax, -0xc8(%rbp)
jne 0x12f6
movq 0x2d2b(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0xe56(%rip), %rsi       # 0x2131
movb $0x0, %al
callq 0x1060
movl -0x1c(%rbp), %edi
callq 0x1050
movl $0x4, -0x4(%rbp)
jmp 0x146d
movq $0x0, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jae 0x144b
movq -0xc8(%rbp), %rax
movq -0xd0(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0xd1(%rbp)
movzbl -0xd1(%rbp), %edi
callq 0x1480
movl %eax, -0xd8(%rbp)
movl -0xd8(%rbp), %eax
movq %rax, -0xe0(%rbp)
subq $0x8, %rax
ja 0x1434
movq -0xe0(%rbp), %rax
leaq 0xca1(%rip), %rcx       # 0x2004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x2c65(%rip), %rax      # 0x3fd8
movq (%rax), %rsi
leaq 0xdc8(%rip), %rdi       # 0x2145
callq 0x1040
jmp 0x1434
movq 0x2c4a(%rip), %rax      # 0x3fd8
movq (%rax), %rsi
leaq 0xdb0(%rip), %rdi       # 0x2148
callq 0x1040
jmp 0x1434
movq 0x2c2f(%rip), %rax      # 0x3fd8
movq (%rax), %rsi
leaq 0xd98(%rip), %rdi       # 0x214b
callq 0x1040
jmp 0x1434
movq 0x2c17(%rip), %rax      # 0x3fd8
movq (%rax), %rsi
leaq 0xd83(%rip), %rdi       # 0x214e
callq 0x1040
jmp 0x1434
movq 0x2bff(%rip), %rax      # 0x3fd8
movq (%rax), %rsi
leaq 0xd6e(%rip), %rdi       # 0x2151
callq 0x1040
jmp 0x1434
movq 0x2be7(%rip), %rax      # 0x3fd8
movq (%rax), %rsi
leaq 0xd59(%rip), %rdi       # 0x2154
callq 0x1040
jmp 0x1434
movq 0x2bcf(%rip), %rax      # 0x3fd8
movq (%rax), %rsi
leaq 0xd44(%rip), %rdi       # 0x2157
callq 0x1040
jmp 0x1434
movq 0x2bb7(%rip), %rax      # 0x3fd8
movq (%rax), %rsi
leaq 0xd2f(%rip), %rdi       # 0x215a
callq 0x1040
jmp 0x1434
jmp 0x1434
movq -0xd0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xd0(%rbp)
jmp 0x1301
movq -0xc8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
callq 0x1070
movl -0x1c(%rbp), %edi
callq 0x1050
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax)