// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x123a
movq 0x2de2(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0xe01(%rip), %rsi       # 0x2028
movb $0x0, %al
callq 0x1090
movl $0x1, -0x4(%rbp)
jmp 0x1797
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x10d0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0x1284
movq 0x2d95(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0xdcc(%rip), %rsi       # 0x203d
movb $0x0, %al
callq 0x1090
movl $0x2, -0x4(%rbp)
jmp 0x1797
movl -0x1c(%rbp), %edi
leaq -0xb0(%rbp), %rsi
callq 0x10e0
movl %eax, -0xb4(%rbp)
cmpl $0x0, -0xb4(%rbp)
je 0x12d2
movq 0x2d4f(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0xd9a(%rip), %rsi       # 0x2051
movb $0x0, %al
callq 0x1090
movl -0x1c(%rbp), %edi
callq 0x1070
movl $0x3, -0x4(%rbp)
jmp 0x1797
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 0x1040
movq %rax, -0xc8(%rbp)
movq $-0x1, %rax
cmpq %rax, -0xc8(%rbp)
jne 0x134c
movq 0x2cd5(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0xd34(%rip), %rsi       # 0x2065
movb $0x0, %al
callq 0x1090
movl -0x1c(%rbp), %edi
callq 0x1070
movl $0x4, -0x4(%rbp)
jmp 0x1797
movq $0x0, -0xd0(%rbp)
movq $0x0, -0xd8(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x17b0
movq -0xd0(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jae 0x1769
movq -0xc8(%rbp), %rax
movq -0xd0(%rbp), %rcx
movw (%rax,%rcx,2), %ax
movw %ax, -0xea(%rbp)
leaq -0xea(%rbp), %rdi
callq 0x17e0
movl %eax, -0xf0(%rbp)
movl -0xf0(%rbp), %eax
movq %rax, -0x120(%rbp)
subq $0x8, %rax
ja 0x1752
movq -0x120(%rbp), %rax
leaq 0xc32(%rip), %rcx       # 0x2004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0xd8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xd8(%rbp)
jmp 0x1752
movq -0xd8(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xd8(%rbp)
jmp 0x1752
movq -0xd8(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
callq 0x1900
movl %eax, -0xb4(%rbp)
cmpl $0x0, -0xb4(%rbp)
je 0x1455
movq 0x2bc6(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0xc39(%rip), %rsi       # 0x2079
movb $0x0, %al
callq 0x1090
movq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0x1752
movq -0xd8(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
callq 0x1960
movl %eax, -0xb4(%rbp)
cmpl $0x0, -0xb4(%rbp)
je 0x14a6
movq 0x2b75(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0xbe8(%rip), %rsi       # 0x2079
movb $0x0, %al
callq 0x1090
movq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0x1752
movq -0xd8(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
callq 0x19c0
movb %al, -0xf1(%rbp)
movzbl -0xf1(%rbp), %edi
movq 0x2afe(%rip), %rax      # 0x3fd0
movq (%rax), %rsi
callq 0x1080
movq 0x2aef(%rip), %rax      # 0x3fd0
movq (%rax), %rdi
callq 0x10a0
jmp 0x1752
movq 0x2ae3(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x1060
movl %eax, -0xf8(%rbp)
cmpl $-0x1, -0xf8(%rbp)
jne 0x151f
movq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0x1752
movq -0xd8(%rbp), %rsi
movl -0xf8(%rbp), %eax
leaq -0xe8(%rbp), %rdi
movzbl %al, %edx
callq 0x1a00
movl %eax, -0xb4(%rbp)
cmpl $0x0, -0xb4(%rbp)
je 0x1574
movq 0x2aa7(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0xb1a(%rip), %rsi       # 0x2079
movb $0x0, %al
callq 0x1090
movq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0x1752
movq -0xd8(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
callq 0x19c0
movb %al, -0xf1(%rbp)
movzbl -0xf1(%rbp), %eax
cmpl $0x0, %eax
jne 0x165d
movq $0x1, -0x100(%rbp)
cmpq $0x0, -0x100(%rbp)
je 0x165b
movq -0xd0(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jne 0x15d0
jmp 0x165b
movq -0xd0(%rbp), %rax
incq %rax
movq %rax, -0xd0(%rbp)
movq -0xc8(%rbp), %rax
movq -0xd0(%rbp), %rcx
movw (%rax,%rcx,2), %ax
movw %ax, -0x102(%rbp)
leaq -0x102(%rbp), %rdi
callq 0x17e0
movl %eax, -0x108(%rbp)
movl -0x108(%rbp), %eax
movl %eax, -0x124(%rbp)
subl $0x6, %eax
je 0x162c
jmp 0x161f
movl -0x124(%rbp), %eax
subl $0x7, %eax
je 0x1640
jmp 0x1654
movq -0x100(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x100(%rbp)
jmp 0x1656
movq -0x100(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x100(%rbp)
jmp 0x1656
jmp 0x1656
jmp 0x15ad
jmp 0x165d
jmp 0x1752
movq -0xd8(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
callq 0x19c0
movb %al, -0xf1(%rbp)
movzbl -0xf1(%rbp), %eax
cmpl $0x0, %eax
je 0x174e
movq $0x1, -0x110(%rbp)
cmpq $0x0, -0x110(%rbp)
je 0x174c
cmpq $0x0, -0xd0(%rbp)
jne 0x16c1
movq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0x174c
movq -0xd0(%rbp), %rax
decq %rax
movq %rax, -0xd0(%rbp)
movq -0xc8(%rbp), %rax
movq -0xd0(%rbp), %rcx
movw (%rax,%rcx,2), %ax
movw %ax, -0x112(%rbp)
leaq -0x112(%rbp), %rdi
callq 0x17e0
movl %eax, -0x118(%rbp)
movl -0x118(%rbp), %eax
movl %eax, -0x128(%rbp)
subl $0x6, %eax
je 0x171d
jmp 0x1710
movl -0x128(%rbp), %eax
subl $0x7, %eax
je 0x1731
jmp 0x1745
movq -0x110(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x110(%rbp)
jmp 0x1747
movq -0x110(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x110(%rbp)
jmp 0x1747
jmp 0x1747
jmp 0x1696
jmp 0x174e
jmp 0x1752
jmp 0x1752
movq -0xd0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xd0(%rbp)
jmp 0x136e
leaq -0xe8(%rbp), %rdi
callq 0x1a60
movq -0xc8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
callq 0x10c0
movl -0x1c(%rbp), %edi
callq 0x1070
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nopw %cs:(%rax,%rax)