// File: /workspace/github_c_makefile_star2_O0/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
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 0xecd(%rip), %rsi       # 0x20f4
movb $0x0, %al
callq 0x1090
movl $0x1, -0x4(%rbp)
jmp 0x178b
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 0xe98(%rip), %rsi       # 0x2109
movb $0x0, %al
callq 0x1090
movl $0x2, -0x4(%rbp)
jmp 0x178b
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 0xe66(%rip), %rsi       # 0x211d
movb $0x0, %al
callq 0x1090
movl -0x1c(%rbp), %edi
callq 0x1070
movl $0x3, -0x4(%rbp)
jmp 0x178b
movq -0x80(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %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 0x1346
movq 0x2cdb(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0xe06(%rip), %rsi       # 0x2131
movb $0x0, %al
callq 0x1090
movl -0x1c(%rbp), %edi
callq 0x1070
movl $0x4, -0x4(%rbp)
jmp 0x178b
movq $0x0, -0xd0(%rbp)
movq $0x0, -0xd8(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x17a0
movq -0xd0(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jae 0x175d
movq -0xc8(%rbp), %rax
movq -0xd0(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0xe9(%rbp)
movzbl -0xe9(%rbp), %edi
callq 0x17d0
movl %eax, -0xf0(%rbp)
movl -0xf0(%rbp), %eax
movq %rax, -0x120(%rbp)
subq $0x8, %rax
ja 0x1746
movq -0x120(%rbp), %rax
leaq 0xc3a(%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 0x1746
movq -0xd8(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xd8(%rbp)
jmp 0x1746
movq -0xd8(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
callq 0x1860
movl %eax, -0xb4(%rbp)
cmpl $0x0, -0xb4(%rbp)
je 0x144d
movq 0x2bce(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0xd0d(%rip), %rsi       # 0x2145
movb $0x0, %al
callq 0x1090
movq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0x1746
movq -0xd8(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
callq 0x18c0
movl %eax, -0xb4(%rbp)
cmpl $0x0, -0xb4(%rbp)
je 0x149e
movq 0x2b7d(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0xcbc(%rip), %rsi       # 0x2145
movb $0x0, %al
callq 0x1090
movq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0x1746
movq -0xd8(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
callq 0x1920
movb %al, -0xf1(%rbp)
movzbl -0xf1(%rbp), %edi
movq 0x2b06(%rip), %rax      # 0x3fd0
movq (%rax), %rsi
callq 0x1080
movq 0x2af7(%rip), %rax      # 0x3fd0
movq (%rax), %rdi
callq 0x10a0
jmp 0x1746
movq 0x2aeb(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x1060
movl %eax, -0xf8(%rbp)
cmpl $-0x1, -0xf8(%rbp)
jne 0x1517
movq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0x1746
movq -0xd8(%rbp), %rsi
movl -0xf8(%rbp), %eax
leaq -0xe8(%rbp), %rdi
movzbl %al, %edx
callq 0x1960
movl %eax, -0xb4(%rbp)
cmpl $0x0, -0xb4(%rbp)
je 0x156c
movq 0x2aaf(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0xbee(%rip), %rsi       # 0x2145
movb $0x0, %al
callq 0x1090
movq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0x1746
movq -0xd8(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
callq 0x1920
movb %al, -0xf1(%rbp)
movzbl -0xf1(%rbp), %eax
cmpl $0x0, %eax
jne 0x1653
movq $0x1, -0x100(%rbp)
cmpq $0x0, -0x100(%rbp)
je 0x1651
movq -0xd0(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jne 0x15c8
jmp 0x1651
movq -0xd0(%rbp), %rax
incq %rax
movq %rax, -0xd0(%rbp)
movq -0xc8(%rbp), %rax
movq -0xd0(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x101(%rbp)
movzbl -0x101(%rbp), %edi
callq 0x17d0
movl %eax, -0x108(%rbp)
movl -0x108(%rbp), %eax
movl %eax, -0x124(%rbp)
subl $0x6, %eax
je 0x1622
jmp 0x1615
movl -0x124(%rbp), %eax
subl $0x7, %eax
je 0x1636
jmp 0x164a
movq -0x100(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x100(%rbp)
jmp 0x164c
movq -0x100(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x100(%rbp)
jmp 0x164c
jmp 0x164c
jmp 0x15a5
jmp 0x1653
jmp 0x1746
movq -0xd8(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
callq 0x1920
movb %al, -0xf1(%rbp)
movzbl -0xf1(%rbp), %eax
cmpl $0x0, %eax
je 0x1742
movq $0x1, -0x110(%rbp)
cmpq $0x0, -0x110(%rbp)
je 0x1740
cmpq $0x0, -0xd0(%rbp)
jne 0x16b7
movq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0x1740
movq -0xd0(%rbp), %rax
decq %rax
movq %rax, -0xd0(%rbp)
movq -0xc8(%rbp), %rax
movq -0xd0(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x111(%rbp)
movzbl -0x111(%rbp), %edi
callq 0x17d0
movl %eax, -0x118(%rbp)
movl -0x118(%rbp), %eax
movl %eax, -0x128(%rbp)
subl $0x6, %eax
je 0x1711
jmp 0x1704
movl -0x128(%rbp), %eax
subl $0x7, %eax
je 0x1725
jmp 0x1739
movq -0x110(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x110(%rbp)
jmp 0x173b
movq -0x110(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x110(%rbp)
jmp 0x173b
jmp 0x173b
jmp 0x168c
jmp 0x1742
jmp 0x1746
jmp 0x1746
movq -0xd0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xd0(%rbp)
jmp 0x1368
leaq -0xe8(%rbp), %rdi
callq 0x19c0
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 (%rax,%rax)