// File: /workspace/github_c_makefile_star2_O2/martin-decky[P]ichiglyph/transpiler/ig2bf/ig2bf.c
// Function: main
int main(int argc, char *argv[])
{
	/*
	 * The first command-line argument is the Ichiglyph
	 * 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 / sizeof(ichiglyph_opcode_t);
	
	/*
	 * We mmap the entire source file.
	 */
	ichiglyph_opcode_t *program = (ichiglyph_opcode_t *) mmap(NULL,
	    program_size * sizeof(ichiglyph_opcode_t), 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) {
		/*
		 * Ichiglyph instruction fetch and decode.
		 */
		ichiglyph_opcode_t ichiglyph_opcode;
		memcpy(&ichiglyph_opcode, program + ip,
		    sizeof(ichiglyph_opcode));
		
		instruction_t instruction =
		    ichiglyph_opcode_decode(ichiglyph_opcode);
		
		/*
		 * Brainfuck instruction encode.
		 */
		switch (instruction) {
		case INST_DP_INC:
			fputs(">", stdout);
			break;
		case INST_DP_DEC:
			fputs("<", stdout);
			break;
		case INST_VAL_INC:
			fputs("+", stdout);
			break;
		case INST_VAL_DEC:
			fputs("-", stdout);
			break;
		case INST_VAL_OUTPUT:
			fputs(".", stdout);
			break;
		case INST_VAL_ACCEPT:
			fputs(",", stdout);
			break;
		case INST_JMP_FORWARD:
			fputs("[", stdout);
			break;
		case INST_JMP_BACK:
			fputs("]", 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 0x12e5
movq 0x8(%rsi), %r12
movq %r12, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x1080
testl %eax, %eax
js 0x1304
movl %eax, %ebx
leaq 0x8(%rsp), %rsi
movl %eax, %edi
callq 0x1090
testl %eax, %eax
jne 0x1324
movq 0x38(%rsp), %r14
movq %r14, %rsi
andq $-0x2, %rsi
pushq $0x1
popq %rdx
pushq $0x2
popq %rcx
xorl %edi, %edi
movl %ebx, %r8d
xorl %r9d, %r9d
callq 0x1030
cmpq $-0x1, %rax
je 0x1343
movq %rax, %r15
shrq %r14
xorl %r12d, %r12d
leaq 0xe40(%rip), %r13       # 0x205d
movq 0x2db4(%rip), %rbp      # 0x3fd8
cmpq %r12, %r14
je 0x12bd
movzwl (%r15,%r12,2), %edx
movl %edx, %ecx
shrl $0x8, %ecx
movzbl %cl, %eax
cmpb $0x31, %dl
je 0x1272
movzbl %dl, %edx
cmpl $0x49, %edx
je 0x125f
cmpl $0x6c, %edx
jne 0x12b5
cmpb $0x31, %cl
je 0x12a5
cmpl $0x49, %eax
je 0x129c
leaq 0xdf8(%rip), %rdi       # 0x2055
jmp 0x127a
cmpb $0x31, %cl
je 0x1293
cmpl $0x49, %eax
je 0x128a
leaq 0xde9(%rip), %rdi       # 0x2059
jmp 0x127a
cmpb $0x49, %cl
je 0x1281
movq %r13, %rdi
cmpl $0x6c, %eax
je 0x12ac
jmp 0x12b5
leaq 0xdd7(%rip), %rdi       # 0x205f
jmp 0x12ac
leaq 0xdca(%rip), %rdi       # 0x205b
jmp 0x12ac
leaq 0xdc9(%rip), %rdi       # 0x2063
jmp 0x12ac
leaq 0xdb4(%rip), %rdi       # 0x2057
jmp 0x12ac
leaq 0xdb5(%rip), %rdi       # 0x2061
movq (%rbp), %rsi
callq 0x1040
incq %r12
jmp 0x1224
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 0x2d0c(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq (%rsi), %rdx
leaq 0xd0b(%rip), %rsi       # 0x2004
xorl %eax, %eax
callq 0x1060
pushq $0x1
jmp 0x1321
movq 0x2ced(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xd04(%rip), %rsi       # 0x2019
movq %r12, %rdx
xorl %eax, %eax
callq 0x1060
pushq $0x2
popq %rbp
jmp 0x12d1
movq 0x2ccd(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcf8(%rip), %rsi       # 0x202d
movq %r12, %rdx
xorl %eax, %eax
callq 0x1060
pushq $0x3
jmp 0x1360
movq 0x2cae(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xced(%rip), %rsi       # 0x2041
movq %r12, %rdx
xorl %eax, %eax
callq 0x1060
pushq $0x4
popq %rbp
jmp 0x12ca