// File: /workspace/github_c_makefile_star2_O1/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 0x132c
movq 0x8(%rsi), %r15
movq %r15, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x1080
testl %eax, %eax
js 0x133e
movl %eax, %ebx
leaq 0x8(%rsp), %rsi
movl %eax, %edi
callq 0x1090
testl %eax, %eax
jne 0x1363
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 0x1388
movq %rax, %r14
movq %r12, %r15
shrq %r15
cmpq $0x2, %r12
jae 0x1268
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
xorl %r12d, %r12d
leaq 0xdf6(%rip), %r13       # 0x2068
movq 0x2d5f(%rip), %rbp      # 0x3fd8
movzwl (%r14,%r12,2), %eax
movl %eax, %ecx
shrl $0x8, %ecx
cmpb $0x31, %al
je 0x12cf
movzbl %al, %edx
movl $0x8, %eax
cmpl $0x49, %edx
je 0x12b6
cmpl $0x6c, %edx
jne 0x12ea
cmpb $0x31, %cl
je 0x1325
movzbl %cl, %ecx
cmpl $0x6c, %ecx
je 0x1321
cmpl $0x49, %ecx
jne 0x12ea
movl $0x1, %eax
jmp 0x12ea
cmpb $0x31, %cl
je 0x131a
movzbl %cl, %ecx
cmpl $0x6c, %ecx
je 0x1313
cmpl $0x49, %ecx
jne 0x12ea
movl $0x3, %eax
jmp 0x12ea
xorl %eax, %eax
cmpw $0x49, %cx
setne %al
leal (%rax,%rax,2), %eax
addl $0x5, %eax
cmpw $0x6c, %cx
movl $0x4, %ecx
cmovel %ecx, %eax
cmpl $0x7, %eax
ja 0x1302
movl %eax, %eax
movslq (%r13,%rax,4), %rdi
addq %r13, %rdi
movq (%rbp), %rsi
callq 0x1040
incq %r12
cmpq %r12, %r15
jne 0x1279
jmp 0x1240
movl $0x2, %eax
jmp 0x12ea
movl $0x7, %eax
jmp 0x12ea
xorl %eax, %eax
jmp 0x12ea
movl $0x6, %eax
jmp 0x12ea
movq %rsi, %rdi
callq 0x10b0
movl $0x1, %ebp
jmp 0x1254
movq 0x2cb3(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcca(%rip), %rsi       # 0x2019
movq %r15, %rdx
xorl %eax, %eax
callq 0x1060
movl $0x2, %ebp
jmp 0x1254
movq 0x2c8e(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcb9(%rip), %rsi       # 0x202d
movq %r15, %rdx
xorl %eax, %eax
callq 0x1060
movl $0x3, %ebp
jmp 0x124d
movq 0x2c69(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xca8(%rip), %rsi       # 0x2041
movq %r15, %rdx
xorl %eax, %eax
callq 0x1060
movl $0x4, %ebp
jmp 0x124d