// File: /workspace/github_c_makefile_star2_O1/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 0x163a
movq 0x8(%rsi), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x10d0
testl %eax, %eax
js 0x164a
movl %eax, %ebx
leaq 0x38(%rsp), %rsi
movl %eax, %edi
callq 0x10e0
testl %eax, %eax
jne 0x166d
movq 0x68(%rsp), %r15
xorl %edi, %edi
movq %r15, %rsi
movl $0x1, %edx
movl $0x2, %ecx
movl %ebx, %r8d
xorl %r9d, %r9d
callq 0x1040
cmpq $-0x1, %rax
je 0x1690
movq %rax, %r13
movl %ebx, 0x2c(%rsp)
testq %r15, %r15
je 0x1602
movq %r14, 0x20(%rsp)
leaq -0x1(%r15), %rbp
leaq 0xd67(%rip), %r14       # 0x2004
xorl %r12d, %r12d
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
xorl %eax, %eax
movq %r15, 0x18(%rsp)
movq %rbp, 0x30(%rsp)
movq %rax, %rbx
movzbl (%r13,%r12), %edi
callq 0x16b6
cmpl $0x7, %eax
ja 0x1599
movl %eax, %eax
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
incq 0x8(%rsp)
jmp 0x1599
xorl %edi, %edi
movq 0x10(%rsp), %rax
cmpq 0x8(%rsp), %rax
jbe 0x1301
movq 0x8(%rsp), %rax
movzbl (%rbx,%rax), %edi
movq 0x2cc8(%rip), %rbp      # 0x3fd0
movq (%rbp), %rsi
callq 0x1080
movq (%rbp), %rdi
callq 0x10a0
movq 0x30(%rsp), %rbp
jmp 0x1599
movb $0x1, %r15b
movq 0x10(%rsp), %rax
cmpq 0x8(%rsp), %rax
ja 0x1532
movq 0x8(%rsp), %rax
leaq 0x8001(%rax), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x10b0
testq %rax, %rax
je 0x1525
movq %rax, %rbx
movq %rax, %rdi
movq 0x10(%rsp), %rax
addq %rax, %rdi
movq %r14, %rdx
subq %rax, %rdx
xorl %esi, %esi
callq 0x1050
movq %r14, 0x10(%rsp)
jmp 0x152b
movb $0x1, %r15b
movq 0x10(%rsp), %rax
cmpq 0x8(%rsp), %rax
ja 0x1551
movq 0x8(%rsp), %rax
leaq 0x8001(%rax), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x10b0
testq %rax, %rax
je 0x1544
movq %rax, %rbx
movq %rax, %rdi
movq 0x10(%rsp), %rax
addq %rax, %rdi
movq %r14, %rdx
subq %rax, %rdx
xorl %esi, %esi
callq 0x1050
movq %r14, 0x10(%rsp)
jmp 0x154a
movq 0x10(%rsp), %rax
cmpq 0x8(%rsp), %rax
jbe 0x1599
movq 0x8(%rsp), %rcx
cmpb $0x0, (%rbx,%rcx)
je 0x1599
movl $0x1, %r14d
movq %r12, %rax
subq $0x1, %r12
jb 0x1568
movzbl -0x1(%r13,%rax), %edi
callq 0x16b6
cmpl $0x7, %eax
je 0x1420
cmpl $0x6, %eax
jne 0x1423
decq %r14
jmp 0x1423
incq %r14
movq %rbx, %rax
testq %r14, %r14
jne 0x13f9
jmp 0x15cc
decq 0x8(%rsp)
jmp 0x1599
movq 0x2b97(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x1060
cmpl $-0x1, %eax
je 0x1591
movl %eax, %r15d
xorl %r14d, %r14d
movb $0x1, %cl
movq 0x10(%rsp), %rax
cmpq 0x8(%rsp), %rax
movq %rbx, %rax
ja 0x15b2
movq 0x8(%rsp), %rcx
leaq 0x8001(%rcx), %rbx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x10b0
xorl %r14d, %r14d
testq %rax, %rax
je 0x15aa
movq %rax, %rdi
movq 0x10(%rsp), %rcx
addq %rcx, %rdi
movq %rbx, %rdx
subq %rcx, %rdx
xorl %esi, %esi
movq %rbx, 0x10(%rsp)
movq %rax, %rbx
callq 0x1050
movq %rbx, %rax
movb $0x1, %cl
jmp 0x15b2
movq 0x10(%rsp), %rax
cmpq 0x8(%rsp), %rax
jbe 0x14e2
movq %rbx, %rax
movq 0x8(%rsp), %rcx
cmpb $0x0, (%rbx,%rcx)
jne 0x159c
cmpq %r15, %r12
jne 0x14eb
jmp 0x159c
cmpq %r15, %r12
je 0x1599
movl $0x1, %r14d
movzbl 0x1(%r13,%r12), %edi
callq 0x16b6
cmpl $0x7, %eax
je 0x150b
cmpl $0x6, %eax
jne 0x150e
incq %r14
jmp 0x150e
decq %r14
leaq 0x1(%r12), %rax
testq %r14, %r14
je 0x1520
cmpq %r12, %rbp
movq %rax, %r12
jne 0x14f1
movq %rax, %r12
jmp 0x156b
movq %rax, %rbx
xorl %r15d, %r15d
leaq 0xad2(%rip), %r14       # 0x2004
testb %r15b, %r15b
je 0x1574
movq %rbx, %rax
movq 0x8(%rsp), %rcx
incb (%rbx,%rcx)
jmp 0x1561
movq %rax, %rbx
xorl %r15d, %r15d
leaq 0xab3(%rip), %r14       # 0x2004
testb %r15b, %r15b
je 0x1574
movq %rbx, %rax
movq 0x8(%rsp), %rcx
decb (%rbx,%rcx)
movq 0x18(%rsp), %r15
jmp 0x159c
movq %r15, %r12
leaq 0xa92(%rip), %r14       # 0x2004
jmp 0x1599
movq 0x2a7d(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xb00(%rip), %rsi       # 0x2085
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x1090
movq 0x18(%rsp), %r15
movq %r15, %r12
movq %rbx, %rax
incq %r12
cmpq %r15, %r12
jb 0x12be
jmp 0x1604
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
xorl %ecx, %ecx
testb %cl, %cl
je 0x15c2
movq 0x8(%rsp), %rcx
movb %r15b, (%rax,%rcx)
xorl %r14d, %r14d
testl %r14d, %r14d
jne 0x15d5
movq 0x18(%rsp), %r15
leaq 0xa31(%rip), %r14       # 0x2004
jmp 0x159c
movq %rax, %rbx
movq 0x2a19(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xa9c(%rip), %rsi       # 0x2085
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x1090
movq %rbx, %rax
movq 0x18(%rsp), %r15
movq %r15, %r12
jmp 0x15cc
xorl %eax, %eax
movq %rax, %rdi
callq 0x1030
movq %r13, %rdi
movq %r15, %rsi
callq 0x10c0
movl 0x2c(%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 0x1625
movq 0x29a7(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x9ee(%rip), %rsi       # 0x2049
movq %r14, %rdx
xorl %eax, %eax
callq 0x1090
movl $0x2, %r14d
jmp 0x1625
movq 0x2984(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x9df(%rip), %rsi       # 0x205d
movq %r14, %rdx
xorl %eax, %eax
callq 0x1090
movl $0x3, %r14d
jmp 0x161e
movq 0x2961(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x9d0(%rip), %rsi       # 0x2071
movq %r14, %rdx
xorl %eax, %eax
callq 0x1090
movl $0x4, %r14d
jmp 0x161e