// File: /workspace/github_c_makefile_star2_O3/martin-decky[P]ichiglyph/interpreter/ichiglyph/ichiglyph.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;
	size_t dp = 0;
	
	data_t data;
	data_init(&data);
	
	while (ip < program_size) {
		/*
		 * Instruction fetch and decode.
		 */
		ichiglyph_opcode_t opcode;
		memcpy(&opcode, program + ip, sizeof(opcode));
		
		instruction_t instruction = opcode_decode(opcode);
		uint8_t val;
		int input_val;
		
		/*
		 * Instruction execute.
		 */
		switch (instruction) {
		case INST_DP_INC:
			dp++;
			break;
		case INST_DP_DEC:
			dp--;
			break;
		case INST_VAL_INC:
			ret = data_inc(&data, dp);
			if (ret != 0) {
				fprintf(stderr, "%s: Out of memory\n", source_name);
				ip = program_size;
			}
			
			break;
		case INST_VAL_DEC:
			ret = data_dec(&data, dp);
			if (ret != 0) {
				fprintf(stderr, "%s: Out of memory\n", source_name);
				ip = program_size;
			}
			
			break;
		case INST_VAL_OUTPUT:
			val = data_get(&data, dp);
			fputc(val, stdout);
			fflush(stdout);
			break;
		case INST_VAL_ACCEPT:
			input_val = fgetc(stdin);
			if (input_val == EOF) {
				ip = program_size;
				break;
			}
			
			ret = data_set(&data, dp, input_val);
			if (ret != 0) {
				fprintf(stderr, "%s: Out of memory\n", source_name);
				ip = program_size;
			}
			
			break;
		case INST_JMP_FORWARD:
			val = data_get(&data, dp);
			
			if (val == 0) {
				size_t balance = 1;
				
				while (balance != 0) {
					if (ip == program_size) {
						/*
						 * No matching instruction can be found.
						 * We simply terminate the execution.
						 */
						break;
					}
					
					ip++;
					
					ichiglyph_opcode_t opcode2;
					memcpy(&opcode2, program + ip, sizeof(opcode2));
					
					instruction_t instruction2 = opcode_decode(opcode2);
					
					switch (instruction2) {
					case INST_JMP_FORWARD:
						balance++;
						break;
					case INST_JMP_BACK:
						balance--;
						break;
					default:
						break;
					}
				}
			}
			
			break;
		case INST_JMP_BACK:
			val = data_get(&data, dp);
			
			if (val != 0) {
				size_t balance = 1;
				
				while (balance != 0) {
					if (ip == 0) {
						/*
						 * No matching instruction can be found.
						 * We simply terminate the execution.
						 */
						ip = program_size;
						break;
					}
					
					ip--;
					
					ichiglyph_opcode_t opcode2;
					memcpy(&opcode2, program + ip, sizeof(opcode2));
					
					instruction_t instruction2 = opcode_decode(opcode2);
					
					switch (instruction2) {
					case INST_JMP_FORWARD:
						balance--;
						break;
					case INST_JMP_BACK:
						balance++;
						break;
					default:
						break;
					}
				}
			}
			
			break;
		case INST_NOP:
			break;
		}
		
		ip++;
	}
	
	data_done(&data);
	munmap(program, program_size);
	close(source);
	
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
cmpl $0x1, %edi
jle 0x15bf
movq 0x8(%rsi), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x10d0
testl %eax, %eax
js 0x15d1
movl %eax, %ebp
leaq 0x38(%rsp), %rsi
movl %eax, %edi
callq 0x10e0
testl %eax, %eax
jne 0x15f6
movq 0x68(%rsp), %rbx
movq %rbx, %rsi
andq $-0x2, %rsi
xorl %edi, %edi
movl $0x1, %edx
movl $0x2, %ecx
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x1040
cmpq $-0x1, %rax
je 0x161b
movq %rax, %r12
movl %ebp, 0x1c(%rsp)
movq %rbx, %r15
shrq %r15
cmpq $0x2, %rbx
jae 0x12c7
xorl %edi, %edi
callq 0x1030
movq %r12, %rdi
movq %r15, %rsi
callq 0x10c0
movl 0x1c(%rsp), %ebp
xorl %ebx, %ebx
movl %ebp, %edi
callq 0x1070
movl %ebx, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, 0x10(%rsp)
leaq -0x1(%r15), %rbp
leaq 0xd2d(%rip), %r14       # 0x2004
xorl %r13d, %r13d
movq $0x0, 0x8(%rsp)
xorl %edi, %edi
movq $0x0, (%rsp)
movq %rbp, 0x30(%rsp)
movq %r15, 0x28(%rsp)
movq %rdi, %rbx
movzwl (%r12,%r13,2), %edi
movl %edi, %esi
shrl $0x8, %esi
callq 0x1640
cmpl $0x7, %eax
ja 0x1580
movl %eax, %eax
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
incq 0x8(%rsp)
jmp 0x1580
movq 0x8(%rsp), %rax
xorl %edi, %edi
cmpq %rax, (%rsp)
jbe 0x1338
movzbl (%rbx,%rax), %edi
movq 0x2c91(%rip), %r15      # 0x3fd0
movq (%r15), %rsi
callq 0x1080
movq (%r15), %rdi
callq 0x10a0
movq 0x28(%rsp), %r15
movq 0x30(%rsp), %rbp
jmp 0x1580
movq 0x8(%rsp), %rax
cmpq %rax, (%rsp)
jbe 0x148e
movq %rbx, %rdi
jmp 0x14d6
movq 0x8(%rsp), %rax
cmpq %rax, (%rsp)
jbe 0x14de
movq %rbx, %rdi
jmp 0x1526
movq 0x8(%rsp), %rax
cmpq %rax, (%rsp)
jbe 0x1580
movq %rbx, %rdi
cmpb $0x0, (%rbx,%rax)
je 0x1583
movl $0x1, %r14d
subq $0x1, %r13
jb 0x15b3
movzwl (%r12,%r13,2), %edi
movl %edi, %esi
shrl $0x8, %esi
callq 0x1640
cmpl $0x7, %eax
je 0x13d6
cmpl $0x6, %eax
jne 0x13d9
decq %r14
jmp 0x13d9
incq %r14
movq %rbx, %rdi
testq %r14, %r14
jne 0x13ae
jmp 0x15b6
decq 0x8(%rsp)
jmp 0x1580
movq 0x2be1(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x1060
cmpl $-0x1, %eax
je 0x1576
movl %eax, %r14d
movq 0x8(%rsp), %rax
cmpq %rax, (%rsp)
movq %rbx, %rdi
ja 0x145b
leaq 0x8001(%rax), %rbx
movq %rbx, %rsi
callq 0x10b0
testq %rax, %rax
je 0x1594
movq %rax, %rdi
movq %rax, 0x20(%rsp)
movq (%rsp), %rax
addq %rax, %rdi
movq %rbx, %rdx
subq %rax, %rdx
xorl %esi, %esi
callq 0x1050
movq %rbx, (%rsp)
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rax
movb %r14b, (%rdi,%rax)
jmp 0x15b6
movq 0x8(%rsp), %rax
cmpq %rax, (%rsp)
jbe 0x152b
movq %rbx, %rdi
cmpb $0x0, (%rbx,%rax)
jne 0x1583
cmpq %r15, %r13
jne 0x1538
jmp 0x1583
leaq 0x8001(%rax), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x10b0
testq %rax, %rax
je 0x1594
movq %rax, %rdi
movq %rax, %rbx
movq (%rsp), %rax
addq %rax, %rdi
movq %r14, %rdx
subq %rax, %rdx
xorl %esi, %esi
callq 0x1050
movq %rbx, %rdi
movq %r14, (%rsp)
leaq 0xb33(%rip), %r14       # 0x2004
movq 0x8(%rsp), %rax
incb (%rdi,%rax)
jmp 0x1583
leaq 0x8001(%rax), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x10b0
testq %rax, %rax
je 0x1594
movq %rax, %rdi
movq %rax, %rbx
movq (%rsp), %rax
addq %rax, %rdi
movq %r14, %rdx
subq %rax, %rdx
xorl %esi, %esi
callq 0x1050
movq %rbx, %rdi
movq %r14, (%rsp)
leaq 0xae3(%rip), %r14       # 0x2004
movq 0x8(%rsp), %rax
decb (%rdi,%rax)
jmp 0x1583
cmpq %r15, %r13
jne 0x1538
movq %rbx, %rdi
movq %r15, %r13
jmp 0x1583
movl $0x1, %r14d
movzwl 0x2(%r12,%r13,2), %edi
movl %edi, %esi
shrl $0x8, %esi
callq 0x1640
cmpl $0x7, %eax
je 0x155d
cmpl $0x6, %eax
jne 0x1560
incq %r14
jmp 0x1560
decq %r14
leaq 0x1(%r13), %rax
cmpq %r13, %rbp
je 0x1571
movq %rax, %r13
testq %r14, %r14
jne 0x153e
movq %rax, %r13
jmp 0x1579
movq %r15, %r13
leaq 0xa84(%rip), %r14       # 0x2004
movq %rbx, %rdi
incq %r13
cmpq %r15, %r13
jb 0x12f7
jmp 0x1296
movq 0x2a5d(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xad0(%rip), %rsi       # 0x2075
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x1090
xorl %edi, %edi
movq %r15, %r13
leaq 0xa47(%rip), %r14       # 0x2004
jmp 0x1583
movq %rsi, %rdi
callq 0x1100
movl $0x1, %ebx
jmp 0x12b3
movq 0x2a20(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xa57(%rip), %rsi       # 0x2039
movq %r14, %rdx
xorl %eax, %eax
callq 0x1090
movl $0x2, %ebx
jmp 0x12b3
movq 0x29fb(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xa46(%rip), %rsi       # 0x204d
movq %r14, %rdx
xorl %eax, %eax
callq 0x1090
movl $0x3, %ebx
jmp 0x12ac
movq 0x29d6(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xa35(%rip), %rsi       # 0x2061
movq %r14, %rdx
xorl %eax, %eax
callq 0x1090
movl $0x4, %ebx
jmp 0x12ac