// File: /workspace/github_c_makefile_star2_O0/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
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 0xe51(%rip), %rsi       # 0x2028
movb $0x0, %al
callq 0x1060
movl $0x1, -0x4(%rbp)
jmp 0x1475
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 0xe1c(%rip), %rsi       # 0x203d
movb $0x0, %al
callq 0x1060
movl $0x2, -0x4(%rbp)
jmp 0x1475
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 0xdea(%rip), %rsi       # 0x2051
movb $0x0, %al
callq 0x1060
movl -0x1c(%rbp), %edi
callq 0x1050
movl $0x3, -0x4(%rbp)
jmp 0x1475
movq -0x80(%rbp), %rax
shrq %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rsi
shlq %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 0x12fc
movq 0x2d25(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0xd84(%rip), %rsi       # 0x2065
movb $0x0, %al
callq 0x1060
movl -0x1c(%rbp), %edi
callq 0x1050
movl $0x4, -0x4(%rbp)
jmp 0x1475
movq $0x0, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jae 0x1453
movq -0xc8(%rbp), %rax
movq -0xd0(%rbp), %rcx
movw (%rax,%rcx,2), %ax
movw %ax, -0xd2(%rbp)
leaq -0xd2(%rbp), %rdi
callq 0x1490
movl %eax, -0xd8(%rbp)
movl -0xd8(%rbp), %eax
movq %rax, -0xe0(%rbp)
subq $0x8, %rax
ja 0x143c
movq -0xe0(%rbp), %rax
leaq 0xc99(%rip), %rcx       # 0x2004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x2c5d(%rip), %rax      # 0x3fd8
movq (%rax), %rsi
leaq 0xcf4(%rip), %rdi       # 0x2079
callq 0x1040
jmp 0x143c
movq 0x2c42(%rip), %rax      # 0x3fd8
movq (%rax), %rsi
leaq 0xcdb(%rip), %rdi       # 0x207b
callq 0x1040
jmp 0x143c
movq 0x2c27(%rip), %rax      # 0x3fd8
movq (%rax), %rsi
leaq 0xcc2(%rip), %rdi       # 0x207d
callq 0x1040
jmp 0x143c
movq 0x2c0f(%rip), %rax      # 0x3fd8
movq (%rax), %rsi
leaq 0xcac(%rip), %rdi       # 0x207f
callq 0x1040
jmp 0x143c
movq 0x2bf7(%rip), %rax      # 0x3fd8
movq (%rax), %rsi
leaq 0xc96(%rip), %rdi       # 0x2081
callq 0x1040
jmp 0x143c
movq 0x2bdf(%rip), %rax      # 0x3fd8
movq (%rax), %rsi
leaq 0xc80(%rip), %rdi       # 0x2083
callq 0x1040
jmp 0x143c
movq 0x2bc7(%rip), %rax      # 0x3fd8
movq (%rax), %rsi
leaq 0xc6a(%rip), %rdi       # 0x2085
callq 0x1040
jmp 0x143c
movq 0x2baf(%rip), %rax      # 0x3fd8
movq (%rax), %rsi
leaq 0xc54(%rip), %rdi       # 0x2087
callq 0x1040
jmp 0x143c
jmp 0x143c
movq -0xd0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xd0(%rbp)
jmp 0x1307
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
nopw %cs:(%rax,%rax)