// File: /workspace/github_c_makefile_star2_O3/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 $0xc8, %rsp
cmpl $0x1, %edi
jle 0x15e0
movq 0x8(%rsi), %r15
movq %r15, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x10d0
testl %eax, %eax
js 0x15f0
movl %eax, %ebx
leaq 0x38(%rsp), %rsi
movl %eax, %edi
callq 0x10e0
testl %eax, %eax
jne 0x1613
movq 0x68(%rsp), %r14
xorl %edi, %edi
movq %r14, %rsi
movl $0x1, %edx
movl $0x2, %ecx
movl %ebx, %r8d
xorl %r9d, %r9d
callq 0x1040
cmpq $-0x1, %rax
je 0x1636
movq %rax, %r13
movl %ebx, 0x1c(%rsp)
testq %r14, %r14
movq %r14, %rax
je 0x15a8
movq %r15, 0x10(%rsp)
movq %rax, %r14
leaq -0x1(%rax), %rbp
leaq 0xd61(%rip), %r15       # 0x2004
xorl %r12d, %r12d
movq $0x0, 0x8(%rsp)
movq $0x0, (%rsp)
xorl %edi, %edi
movq %rbp, 0x30(%rsp)
movq %rax, 0x28(%rsp)
movq %rdi, %rbx
movzbl (%r13,%r12), %edi
callq 0x165c
cmpl $0x7, %eax
ja 0x1567
movl %eax, %eax
movslq (%r15,%rax,4), %rax
addq %r15, %rax
jmpq *%rax
incq 0x8(%rsp)
jmp 0x1567
movq 0x8(%rsp), %rax
xorl %edi, %edi
cmpq %rax, (%rsp)
jbe 0x1300
movzbl (%rbx,%rax), %edi
movq 0x2cc9(%rip), %r14      # 0x3fd0
movq (%r14), %rsi
callq 0x1080
movq (%r14), %rdi
callq 0x10a0
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %rbp
jmp 0x1473
movq 0x8(%rsp), %rax
cmpq %rax, (%rsp)
jbe 0x147b
movq %rbx, %rdi
jmp 0x14c3
movq 0x8(%rsp), %rax
cmpq %rax, (%rsp)
jbe 0x14cb
movq %rbx, %rdi
jmp 0x1513
movq 0x8(%rsp), %rax
cmpq %rax, (%rsp)
jbe 0x1567
cmpb $0x0, (%rbx,%rax)
je 0x1567
movl $0x1, %r15d
movq %r12, %rax
subq $0x1, %r12
jb 0x1466
movzbl -0x1(%r13,%rax), %edi
callq 0x165c
cmpl $0x7, %eax
je 0x139a
cmpl $0x6, %eax
jne 0x139d
decq %r15
jmp 0x139d
incq %r15
movq %rbx, %rdi
testq %r15, %r15
jne 0x1373
leaq 0xc58(%rip), %r15       # 0x2004
jmp 0x156a
decq 0x8(%rsp)
jmp 0x1567
movq 0x2c16(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x1060
cmpl $-0x1, %eax
je 0x1466
movl %eax, %r15d
movq 0x8(%rsp), %rax
cmpq %rax, (%rsp)
movq %rbx, %rdi
ja 0x1421
leaq 0x8001(%rax), %rbx
movq %rbx, %rsi
callq 0x10b0
testq %rax, %rax
je 0x157e
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 %r14, %rax
movq 0x8(%rsp), %rcx
movb %r15b, (%rdi,%rcx)
leaq 0xbd0(%rip), %r15       # 0x2004
jmp 0x156d
movq 0x8(%rsp), %rax
cmpq %rax, (%rsp)
jbe 0x1518
movq %rbx, %rdi
cmpb $0x0, (%rbx,%rax)
movq %r14, %rax
jne 0x156d
cmpq %rax, %r12
jne 0x1528
jmp 0x156d
movq %r14, %rax
movq %r14, %r12
leaq 0xb91(%rip), %r15       # 0x2004
movq %rbx, %rdi
jmp 0x156d
leaq 0x8001(%rax), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x10b0
testq %rax, %rax
je 0x157e
movq %rax, %rdi
movq %rax, %rbx
movq (%rsp), %rax
addq %rax, %rdi
movq %r15, %rdx
subq %rax, %rdx
xorl %esi, %esi
callq 0x1050
movq %rbx, %rdi
movq %r15, (%rsp)
leaq 0xb46(%rip), %r15       # 0x2004
movq 0x8(%rsp), %rax
incb (%rdi,%rax)
jmp 0x156a
leaq 0x8001(%rax), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x10b0
testq %rax, %rax
je 0x157e
movq %rax, %rdi
movq %rax, %rbx
movq (%rsp), %rax
addq %rax, %rdi
movq %r15, %rdx
subq %rax, %rdx
xorl %esi, %esi
callq 0x1050
movq %rbx, %rdi
movq %r15, (%rsp)
leaq 0xaf6(%rip), %r15       # 0x2004
movq 0x8(%rsp), %rax
decb (%rdi,%rax)
jmp 0x156a
cmpq %r14, %r12
jne 0x1528
movq %r14, %rax
movq %rbx, %rdi
movq %r14, %r12
jmp 0x156d
movl $0x1, %r15d
movzbl 0x1(%r13,%r12), %edi
callq 0x165c
cmpl $0x7, %eax
je 0x1548
cmpl $0x6, %eax
jne 0x154b
incq %r15
jmp 0x154b
decq %r15
leaq 0x1(%r12), %rax
testq %r15, %r15
je 0x155d
cmpq %r12, %rbp
movq %rax, %r12
jne 0x152e
movq %rax, %r12
leaq 0xa9d(%rip), %r15       # 0x2004
movq %rbx, %rdi
movq %r14, %rax
incq %r12
movq %rax, %r14
cmpq %rax, %r12
jb 0x12c3
jmp 0x15ad
movq 0x2a73(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xaf6(%rip), %rsi       # 0x2085
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x1090
xorl %edi, %edi
movq %r14, %rax
movq %r14, %r12
jmp 0x142d
movq %rax, %r14
xorl %edi, %edi
callq 0x1030
movq %r13, %rdi
movq %r14, %rsi
callq 0x10c0
movl 0x1c(%rsp), %ebx
xorl %r14d, %r14d
movl %ebx, %edi
callq 0x1070
movl %r14d, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %rdi
callq 0x1100
movl $0x1, %r14d
jmp 0x15cb
movq 0x2a01(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xa48(%rip), %rsi       # 0x2049
movq %r15, %rdx
xorl %eax, %eax
callq 0x1090
movl $0x2, %r14d
jmp 0x15cb
movq 0x29de(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xa39(%rip), %rsi       # 0x205d
movq %r15, %rdx
xorl %eax, %eax
callq 0x1090
movl $0x3, %r14d
jmp 0x15c4
movq 0x29bb(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xa2a(%rip), %rsi       # 0x2071
movq %r15, %rdx
xorl %eax, %eax
callq 0x1090
movl $0x4, %r14d
jmp 0x15c4