// File: /workspace/github_c_makefile_star2_O2/martin-decky[P]ichiglyph/interpreter/brainfuck/brainfuck.c
// Function: main
int main(int argc, char *argv[])
{
	/*
	 * The first command-line argument is the Brainfuck
	 * 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;
	
	/*
	 * We mmap the entire source file.
	 */
	uint8_t *program = (uint8_t *) mmap(NULL, program_size, 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.
		 */
		uint8_t opcode = program[ip];
		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) {
						/*
						 * The language specification is not clear
						 * about the situation when no matching
						 * instruction can be found. We simply
						 * terminate the execution.
						 */
						break;
					}
					
					ip++;
					
					uint8_t opcode2 = program[ip];
					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) {
						/*
						 * The language specification is not clear
						 * about the situation when no matching
						 * instruction can be found. We simply
						 * terminate the execution.
						 */
						ip = program_size;
						break;
					}
					
					ip--;
					
					uint8_t opcode2 = program[ip];
					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 $0xb8, %rsp
cmpl $0x1, %edi
jle 0x1442
movq 0x8(%rsi), %rdi
movq %rdi, 0x18(%rsp)
xorl %esi, %esi
xorl %eax, %eax
callq 0x10d0
testl %eax, %eax
js 0x145b
movl %eax, %ebx
leaq 0x28(%rsp), %rsi
movl %eax, %edi
callq 0x10e0
testl %eax, %eax
jne 0x147d
movq 0x58(%rsp), %r13
pushq $0x1
popq %rdx
pushq $0x2
popq %rcx
xorl %edi, %edi
movq %r13, %rsi
movl %ebx, %r8d
xorl %r9d, %r9d
callq 0x1040
cmpq $-0x1, %rax
je 0x14a5
movq %rax, %r12
movl %ebx, 0x24(%rsp)
xorps %xmm0, %xmm0
movq %rsp, %rax
movaps %xmm0, (%rax)
leaq 0xd92(%rip), %rbx       # 0x2004
xorl %ebp, %ebp
xorl %r15d, %r15d
cmpq %r13, %r15
jae 0x140f
movzbl (%r12,%r15), %edi
callq 0x14d1
cmpl $0x7, %eax
ja 0x1407
movl %eax, %eax
movslq (%rbx,%rax,4), %rax
addq %rbx, %rax
jmpq *%rax
incq %rbp
jmp 0x1407
xorl %edi, %edi
cmpq %rbp, 0x8(%rsp)
jbe 0x12b7
movq (%rsp), %rax
movzbl (%rax,%rbp), %edi
movq 0x2d12(%rip), %r14      # 0x3fd0
movq (%r14), %rsi
callq 0x1080
movq (%r14), %rdi
callq 0x10a0
jmp 0x1407
movq %rsp, %rdi
movq %rbp, %rsi
callq 0x1520
testl %eax, %eax
jne 0x13e7
movq (%rsp), %rax
incb (%rax,%rbp)
jmp 0x1407
movq %rsp, %rdi
movq %rbp, %rsi
callq 0x1520
testl %eax, %eax
jne 0x13e7
movq (%rsp), %rax
decb (%rax,%rbp)
jmp 0x1407
cmpq %rbp, 0x8(%rsp)
jbe 0x1407
movq (%rsp), %rax
cmpb $0x0, (%rax,%rbp)
je 0x1407
pushq $0x1
popq %r14
movq %r15, %rcx
testq %r14, %r14
je 0x13e2
movq %rcx, %r15
subq $0x1, %r15
jb 0x1404
movzbl -0x1(%r12,%rcx), %edi
callq 0x14d1
cmpl $0x7, %eax
je 0x1364
movq %r15, %rcx
cmpl $0x6, %eax
jne 0x1331
decq %r14
jmp 0x132e
incq %r14
jmp 0x132e
decq %rbp
jmp 0x1407
movq 0x2c60(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x1060
cmpl $-0x1, %eax
je 0x1404
movl %eax, %r14d
movq %rsp, %rdi
movq %rbp, %rsi
callq 0x1520
testl %eax, %eax
jne 0x13e7
movq (%rsp), %rax
movb %r14b, (%rax,%rbp)
jmp 0x1407
cmpq %rbp, 0x8(%rsp)
jbe 0x13b2
movq (%rsp), %rax
cmpb $0x0, (%rax,%rbp)
jne 0x1407
pushq $0x1
popq %r14
testq %r14, %r14
je 0x1407
cmpq %r15, %r13
je 0x1407
movzbl 0x1(%r12,%r15), %edi
incq %r15
callq 0x14d1
cmpl $0x7, %eax
je 0x13dd
cmpl $0x6, %eax
jne 0x13b6
incq %r14
jmp 0x13b6
decq %r14
jmp 0x13b6
movq %rcx, %r15
jmp 0x1407
movq 0x2c0a(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc8d(%rip), %rsi       # 0x2085
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x1090
movq %r13, %r15
incq %r15
jmp 0x1277
movq (%rsp), %rdi
callq 0x1030
movq %r12, %rdi
movq %r13, %rsi
callq 0x10c0
movl 0x24(%rsp), %edi
callq 0x1070
xorl %ebx, %ebx
movl %ebx, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rbx
movq 0x2bac(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq (%rsi), %rdx
leaq 0xbdb(%rip), %rsi       # 0x2034
jmp 0x1474
pushq $0x2
popq %rbx
movq 0x2b93(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xbda(%rip), %rsi       # 0x2049
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x1090
jmp 0x142e
movq 0x2b74(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xbcf(%rip), %rsi       # 0x205d
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x1090
movl %ebx, %edi
callq 0x1070
pushq $0x3
jmp 0x14cb
movq 0x2b4c(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xbbb(%rip), %rsi       # 0x2071
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x1090
movl %ebx, %edi
callq 0x1070
pushq $0x4
popq %rbx
jmp 0x142e