// File: /workspace/github_c_makefile_star2_O1/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 $0xd8, %rsp
cmpl $0x1, %edi
jle 0x1624
movq 0x8(%rsi), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x10d0
testl %eax, %eax
js 0x1633
movl %eax, %ebp
leaq 0x48(%rsp), %rsi
movl %eax, %edi
callq 0x10e0
testl %eax, %eax
jne 0x1655
movq 0x78(%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 0x1677
movq %rax, %r12
movl %ebp, 0x34(%rsp)
movq %rbx, %rbp
shrq %rbp
cmpq $0x2, %rbx
jae 0x129b
xorl %edi, %edi
jmp 0x15f3
movq %r14, 0x28(%rsp)
leaq -0x1(%rbp), %rax
movq %rax, 0x40(%rsp)
leaq 0xc(%rsp), %rbx
leaq 0xe(%rsp), %r15
xorl %r13d, %r13d
movq $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
xorl %edi, %edi
movq %rbp, 0x20(%rsp)
movq %rdi, %r14
movzwl (%r12,%r13,2), %eax
movw %ax, 0xc(%rsp)
movq %rbx, %rdi
callq 0x169c
cmpl $0x7, %eax
ja 0x1528
movl %eax, %eax
leaq 0xd0e(%rip), %rcx       # 0x2004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
incq 0x18(%rsp)
jmp 0x1528
xorl %edi, %edi
movq 0x18(%rsp), %rax
cmpq %rax, 0x10(%rsp)
jbe 0x131c
movzbl (%r14,%rax), %edi
movq 0x2cad(%rip), %r15      # 0x3fd0
movq (%r15), %rsi
callq 0x1080
movq (%r15), %rdi
callq 0x10a0
leaq 0xe(%rsp), %r15
jmp 0x1528
movb $0x1, %bpl
movq 0x18(%rsp), %rax
cmpq %rax, 0x10(%rsp)
ja 0x153a
leaq 0x8001(%rax), %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10b0
testq %rax, %rax
je 0x1530
movq %rax, %r14
movq %rax, %rdi
movq 0x10(%rsp), %rax
addq %rax, %rdi
movq %rbx, %rdx
subq %rax, %rdx
xorl %esi, %esi
callq 0x1050
movq %rbx, 0x10(%rsp)
jmp 0x1535
movb $0x1, %bpl
movq 0x18(%rsp), %rax
cmpq %rax, 0x10(%rsp)
ja 0x1554
leaq 0x8001(%rax), %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10b0
testq %rax, %rax
je 0x154a
movq %rax, %r14
movq %rax, %rdi
movq 0x10(%rsp), %rax
addq %rax, %rdi
movq %rbx, %rdx
subq %rax, %rdx
xorl %esi, %esi
callq 0x1050
movq %rbx, 0x10(%rsp)
jmp 0x154f
movq 0x18(%rsp), %rax
cmpq %rax, 0x10(%rsp)
jbe 0x1528
movq %r14, %rdi
cmpb $0x0, (%r14,%rax)
je 0x15e7
movl $0x1, %ebx
subq $0x1, %r13
jb 0x15df
movzwl (%r12,%r13,2), %eax
movw %ax, 0xe(%rsp)
movq %r15, %rdi
callq 0x169c
cmpl $0x7, %eax
je 0x1431
cmpl $0x6, %eax
jne 0x1434
decq %rbx
jmp 0x1434
incq %rbx
movq %r14, %rdi
testq %rbx, %rbx
jne 0x1406
jmp 0x15e2
decq 0x18(%rsp)
jmp 0x1528
movq 0x2b86(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x1060
cmpl $-0x1, %eax
je 0x1520
movl %eax, %ebx
xorl %ebp, %ebp
movb $0x1, %cl
movq 0x18(%rsp), %rax
cmpq %rax, 0x10(%rsp)
ja 0x159d
leaq 0x8001(%rax), %rsi
movq %r14, %rdi
movq %rsi, 0x38(%rsp)
callq 0x10b0
xorl %ebp, %ebp
testq %rax, %rax
je 0x1593
movq %rax, %r14
movq %rax, %rdi
movq 0x10(%rsp), %rax
addq %rax, %rdi
movq 0x38(%rsp), %rdx
subq %rax, %rdx
xorl %esi, %esi
callq 0x1050
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movb $0x1, %cl
jmp 0x159d
movq 0x18(%rsp), %rax
cmpq %rax, 0x10(%rsp)
jbe 0x14d9
cmpb $0x0, (%r14,%rax)
jne 0x1528
cmpq %rbp, %r13
je 0x1528
movl $0x1, %ebx
movzwl 0x2(%r12,%r13,2), %eax
movw %ax, 0xe(%rsp)
movq %r15, %rdi
callq 0x169c
cmpl $0x7, %eax
je 0x1505
cmpl $0x6, %eax
jne 0x1508
incq %rbx
jmp 0x1508
decq %rbx
leaq 0x1(%r13), %rax
cmpq %r13, 0x40(%rsp)
je 0x151b
movq %rax, %r13
testq %rbx, %rbx
jne 0x14e3
movq %rax, %r13
jmp 0x1523
movq %rbp, %r13
leaq 0xc(%rsp), %rbx
movq %r14, %rdi
jmp 0x15e7
movq %rax, %r14
xorl %ebp, %ebp
leaq 0xc(%rsp), %rbx
testb %bpl, %bpl
je 0x156c
movq 0x18(%rsp), %rax
incb (%r14,%rax)
jmp 0x1562
movq %rax, %r14
xorl %ebp, %ebp
leaq 0xc(%rsp), %rbx
testb %bpl, %bpl
je 0x156c
movq 0x18(%rsp), %rax
decb (%r14,%rax)
movq %r14, %rdi
movq 0x20(%rsp), %rbp
jmp 0x15e7
movq 0x2a85(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xaf8(%rip), %rsi       # 0x2075
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x1090
movq 0x20(%rsp), %rbp
movq %rbp, %r13
jmp 0x1528
movq %rax, %r14
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
testb %cl, %cl
je 0x15ac
movq 0x18(%rsp), %rax
movb %bl, (%r14,%rax)
xorl %ebp, %ebp
testl %ebp, %ebp
jne 0x15ba
movq %r14, %rdi
movq 0x20(%rsp), %rbp
jmp 0x15e2
movq 0x2a37(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xaaa(%rip), %rsi       # 0x2075
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x1090
movq %r14, %rdi
movq 0x20(%rsp), %rbp
movq %rbp, %r13
leaq 0xc(%rsp), %rbx
incq %r13
cmpq %rbp, %r13
jb 0x12cf
callq 0x1030
movq %r12, %rdi
movq %rbp, %rsi
callq 0x10c0
movl 0x34(%rsp), %ebp
xorl %ebx, %ebx
movl %ebp, %edi
callq 0x1070
movl %ebx, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %rdi
callq 0x1100
movl $0x1, %ebx
jmp 0x1610
movq 0x29be(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x9f5(%rip), %rsi       # 0x2039
movq %r14, %rdx
xorl %eax, %eax
callq 0x1090
movl $0x2, %ebx
jmp 0x1610
movq 0x299c(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x9e7(%rip), %rsi       # 0x204d
movq %r14, %rdx
xorl %eax, %eax
callq 0x1090
movl $0x3, %ebx
jmp 0x1609
movq 0x297a(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x9d9(%rip), %rsi       # 0x2061
movq %r14, %rdx
xorl %eax, %eax
callq 0x1090
movl $0x4, %ebx
jmp 0x1609