// File: /workspace/github_c_makefile_star2_O2/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 0x149e
movq 0x8(%rsi), %rdi
movq %rdi, 0x28(%rsp)
xorl %esi, %esi
xorl %eax, %eax
callq 0x10d0
testl %eax, %eax
js 0x14b7
movl %eax, %ebx
leaq 0x38(%rsp), %rsi
movl %eax, %edi
callq 0x10e0
testl %eax, %eax
jne 0x14d9
movq 0x68(%rsp), %rbp
movq %rbp, %rsi
andq $-0x2, %rsi
pushq $0x1
popq %rdx
pushq $0x2
popq %rcx
xorl %edi, %edi
movl %ebx, %r8d
xorl %r9d, %r9d
callq 0x1040
cmpq $-0x1, %rax
je 0x1501
movq %rax, %r12
movl %ebx, 0x34(%rsp)
shrq %rbp
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rax
movaps %xmm0, (%rax)
leaq 0xc(%rsp), %r13
leaq 0xe(%rsp), %r14
xorl %ebx, %ebx
xorl %r15d, %r15d
cmpq %rbp, %r15
jae 0x146a
movzwl (%r12,%r15,2), %eax
movw %ax, 0xc(%rsp)
movq %r13, %rdi
callq 0x152d
cmpl $0x7, %eax
ja 0x1462
movl %eax, %eax
leaq 0xd54(%rip), %rcx       # 0x2004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
incq %rbx
jmp 0x1462
xorl %edi, %edi
cmpq %rbx, 0x18(%rsp)
jbe 0x12d3
movq 0x10(%rsp), %rax
movzbl (%rax,%rbx), %edi
movq 0x2cf6(%rip), %r14      # 0x3fd0
movq (%r14), %rsi
callq 0x1080
movq (%r14), %rdi
callq 0x10a0
leaq 0xe(%rsp), %r14
jmp 0x1462
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x1598
testl %eax, %eax
jne 0x141b
movq 0x10(%rsp), %rax
incb (%rax,%rbx)
jmp 0x1462
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x1598
testl %eax, %eax
jne 0x141b
movq 0x10(%rsp), %rax
decb (%rax,%rbx)
jmp 0x1462
cmpq %rbx, 0x18(%rsp)
jbe 0x1462
movq 0x10(%rsp), %rax
cmpb $0x0, (%rax,%rbx)
je 0x1462
pushq $0x1
popq %r13
testq %r13, %r13
je 0x145d
subq $0x1, %r15
jb 0x145a
movzwl (%r12,%r15,2), %eax
movw %ax, 0xe(%rsp)
movq %r14, %rdi
callq 0x152d
cmpl $0x7, %eax
je 0x138a
cmpl $0x6, %eax
jne 0x1356
decq %r13
jmp 0x1356
incq %r13
jmp 0x1356
decq %rbx
jmp 0x1462
movq 0x2c3a(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x1060
cmpl $-0x1, %eax
je 0x145a
movl %eax, %r13d
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x1598
testl %eax, %eax
jne 0x143d
movq 0x10(%rsp), %rax
movb %r13b, (%rax,%rbx)
jmp 0x145d
cmpq %rbx, 0x18(%rsp)
jbe 0x13e3
movq 0x10(%rsp), %rax
cmpb $0x0, (%rax,%rbx)
jne 0x1462
pushq $0x1
popq %r13
cmpq %r15, %rbp
je 0x145d
testq %r13, %r13
je 0x145d
movzwl 0x2(%r12,%r15,2), %eax
movw %ax, 0xe(%rsp)
movq %r14, %rdi
callq 0x152d
cmpl $0x7, %eax
je 0x1413
cmpl $0x6, %eax
jne 0x1416
incq %r13
jmp 0x1416
decq %r13
incq %r15
jmp 0x13e7
movq 0x2bd6(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc49(%rip), %rsi       # 0x2075
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x1090
movq %rbp, %r15
jmp 0x1462
movq 0x2bb4(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc27(%rip), %rsi       # 0x2075
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x1090
movq %rbp, %r15
leaq 0xc(%rsp), %r13
incq %r15
jmp 0x1283
movq 0x10(%rsp), %rdi
callq 0x1030
movq %r12, %rdi
movq %rbp, %rsi
callq 0x10c0
movl 0x34(%rsp), %edi
callq 0x1070
xorl %ebx, %ebx
movl %ebx, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rbx
movq 0x2b50(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq (%rsi), %rdx
leaq 0xb6f(%rip), %rsi       # 0x2024
jmp 0x14d0
pushq $0x2
popq %rbx
movq 0x2b37(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xb6e(%rip), %rsi       # 0x2039
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x1090
jmp 0x148a
movq 0x2b18(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xb63(%rip), %rsi       # 0x204d
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x1090
movl %ebx, %edi
callq 0x1070
pushq $0x3
jmp 0x1527
movq 0x2af0(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xb4f(%rip), %rsi       # 0x2061
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x1090
movl %ebx, %edi
callq 0x1070
pushq $0x4
popq %rbx
jmp 0x148a