// File: /workspace/github_c_makefile_star2_O3/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 0x1314
movq 0x8(%rsi), %r15
movq %r15, %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), %r12
movq %r12, %rsi
andq $-0x2, %rsi
xorl %edi, %edi
movl $0x1, %edx
movl $0x2, %ecx
movl %ebx, %r8d
xorl %r9d, %r9d
callq 0x1030
cmpq $-0x1, %rax
je 0x1367
movq %rax, %r14
movq %r12, %r15
shrq %r15
cmpq $0x2, %r12
jb 0x12ec
xorl %r12d, %r12d
leaq 0xe0f(%rip), %r13       # 0x205d
movq 0x2d83(%rip), %rbp      # 0x3fd8
movzwl (%r14,%r12,2), %ecx
movl %ecx, %eax
shrl $0x8, %eax
cmpb $0x31, %cl
je 0x129b
movzbl %cl, %ecx
cmpl $0x49, %ecx
je 0x1286
cmpl $0x6c, %ecx
jne 0x12e0
cmpb $0x31, %al
je 0x12d0
movzbl %al, %eax
cmpl $0x49, %eax
je 0x12c7
leaq 0xdd1(%rip), %rdi       # 0x2055
jmp 0x12a5
cmpb $0x31, %al
je 0x12be
movzbl %al, %eax
cmpl $0x49, %eax
je 0x12b5
leaq 0xdc0(%rip), %rdi       # 0x2059
jmp 0x12a5
cmpb $0x49, %al
je 0x12ac
movzbl %al, %eax
movq %r13, %rdi
cmpl $0x6c, %eax
je 0x12d7
jmp 0x12e0
leaq 0xdac(%rip), %rdi       # 0x205f
jmp 0x12d7
leaq 0xd9f(%rip), %rdi       # 0x205b
jmp 0x12d7
leaq 0xd9e(%rip), %rdi       # 0x2063
jmp 0x12d7
leaq 0xd89(%rip), %rdi       # 0x2057
jmp 0x12d7
leaq 0xd8a(%rip), %rdi       # 0x2061
movq (%rbp), %rsi
callq 0x1040
incq %r12
cmpq %r12, %r15
jne 0x1255
movq %r14, %rdi
movq %r15, %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 0x1300
movq 0x2cce(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xce5(%rip), %rsi       # 0x2019
movq %r15, %rdx
xorl %eax, %eax
callq 0x1060
movl $0x2, %ebp
jmp 0x1300
movq 0x2cac(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcd7(%rip), %rsi       # 0x202d
movq %r15, %rdx
xorl %eax, %eax
callq 0x1060
movl $0x3, %ebp
jmp 0x12f9
movq 0x2c8a(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcc9(%rip), %rsi       # 0x2041
movq %r15, %rdx
xorl %eax, %eax
callq 0x1060
movl $0x4, %ebp
jmp 0x12f9