// File: /workspace/github_cpp_makefile_star2_O1/JohnCGriffin[P]BrainForked/bf_main.cpp
// Function: main
int main(int argc, char** argv)
{
    bool dump = argc > 1 && !strcmp("-dump",argv[1]);
    if(dump){
	argc--;
	argv++;
    }
    std::ifstream ifs;

    if(argc == 2){
	ifs.open(argv[1]);
	if(!ifs){
	    std::cerr << "could not open " << argv[1] << std::endl;
	    return 1;
	}
    }

    std::istream& is(argc == 2 ? ifs : std::cin);

    auto instrs = bf::load_instructions(is);

    if(dump){
	auto while_level = 0;
	for(unsigned i=0; i<instrs.size(); i++){
	    auto instr = instrs[i];
	    if(instr.action == bf::TRUEJUMP){
		while_level--;
	    }
	    std::cout.width(6);
	    std::cout << i << " ";
	    for(int j=0; j<while_level; j++){
		std::cout << "	";
	    }
	    std::cout << instr.action << " " << instr.val << "\n";
	    if(instr.action == bf::FALSEJUMP){
		while_level++;
	    }
	}
	return 0;
    }

#ifdef PROFILER

    std::thread profiler([&]{
			     while(!current_loop) ;
			     while(current_loop){
				 auto count = counts[current_loop];
				 if(count > 500000){
				     break;
				 }
				 counts[current_loop]++;
				 std::this_thread::yield();
				 std::cout << std::flush;
			     }
			 });

    bf::execute(instrs);
    current_loop = 0;

    profiler.join();

    for(auto entry : counts){
	std::cout << entry.second << " " << entry.first
		  << " " << instrs.at(entry.first).val + entry.first << "\n";
    }
#else

    bf::execute(instrs);

#endif

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movq %rsi, %rbx
movl %edi, %ebp
cmpl $0x2, %edi
jl 0x251f
movq 0x8(%rbx), %rsi
leaq 0x3af2(%rip), %rdi      # 0x6004
callq 0x2220
testl %eax, %eax
sete %r12b
jmp 0x2522
xorl %r12d, %r12d
movzbl %r12b, %r15d
subl %r15d, %ebp
leaq 0x40(%rsp), %rdi
callq 0x21d0
cmpl $0x2, %ebp
jne 0x25ab
shll $0x3, %r15d
movq 0x8(%rbx,%r15), %rsi
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movl $0x8, %edx
callq 0x20f0
movq 0x40(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x60(%rsp,%rax)
je 0x25b2
movq 0x6a86(%rip), %r14      # 0x8ff0
leaq 0x3a99(%rip), %rsi      # 0x600a
movl $0xf, %edx
movq %r14, %rdi
callq 0x21e0
movq 0x8(%rbx,%r15), %rbx
testq %rbx, %rbx
je 0x2862
movq %rbx, %rdi
callq 0x20a0
movq 0x6a55(%rip), %rdi      # 0x8ff0
movq %rbx, %rsi
movq %rax, %rdx
callq 0x21e0
jmp 0x287a
movq 0x6a26(%rip), %r14      # 0x8fd8
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x3904
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
movl $0x1, %edx
callq 0x4198
movq 0x20(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq (%rsp), %rax
movq %rax, 0x20(%rsp)
movups 0x8(%rsp), %xmm0
movups %xmm0, 0x28(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
testq %rdi, %rdi
je 0x260b
subq %rdi, %rsi
callq 0x21b0
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x2621
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq 0x20(%rsp), %rax
cmpq %rax, 0x28(%rsp)
je 0x26e6
xorl %r13d, %r13d
movq %rsp, %r14
leaq 0x3c(%rsp), %r15
xorl %ebp, %ebp
xorl %ebx, %ebx
cmpl $0x2, (%rax,%r13)
jne 0x267f
movl %ebx, 0x3c(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0x2674
movl %ebx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rsp)
jmp 0x267f
movq %r14, %rdi
movq %r15, %rdx
callq 0x3050
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rdx
subq %rax, %rdx
sarq $0x4, %rdx
cmpq %rbx, %rdx
jbe 0x28c8
cmpl $0x10, (%rax,%r13)
jne 0x26d4
movq 0x8(%rsp), %rcx
movslq -0x4(%rcx), %rsi
cmpq %rsi, %rdx
jbe 0x28d9
addq $-0x4, %rcx
movl %ebx, %edi
subl %esi, %edi
movq %rsi, %r8
shlq $0x4, %r8
movw %di, 0x4(%rax,%r8)
addl %ebp, %esi
movw %si, 0x4(%rax,%r13)
movq %rcx, 0x8(%rsp)
incq %rbx
decl %ebp
addq $0x10, %r13
cmpq %rdx, %rbx
jb 0x2650
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x26fc
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
movl $0xf, (%rsp)
movw $0x0, 0x4(%rsp)
movq $0x0, 0x8(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x272e
movups (%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x28(%rsp)
jmp 0x273b
leaq 0x20(%rsp), %rdi
movq %rsp, %rdx
callq 0x318c
testb %r12b, %r12b
je 0x283d
movq 0x20(%rsp), %rax
cmpq %rax, 0x28(%rsp)
je 0x2847
xorl %ebx, %ebx
movq 0x6863(%rip), %r14      # 0x8fc0
leaq 0x38b6(%rip), %r12      # 0x601a
xorl %ebp, %ebp
movq %rbx, %rcx
shlq $0x4, %rcx
movups (%rax,%rcx), %xmm0
movaps %xmm0, (%rsp)
xorl %r13d, %r13d
cmpl $0x10, (%rsp)
sete %r15b
movq (%r14), %rax
movq -0x18(%rax), %rax
movq $0x6, 0x10(%r14,%rax)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2120
movl $0x1, %edx
movq %rax, %rdi
leaq 0x386e(%rip), %rsi      # 0x6018
callq 0x21e0
movb %r15b, %r13b
subl %r13d, %ebp
movl %ebp, %r15d
testl %ebp, %ebp
jle 0x27d1
movl $0x1, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x21e0
decl %r15d
jne 0x27bc
movq %r14, %rdi
movq %rsp, %rsi
callq 0x36dc
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x382a(%rip), %rsi      # 0x6018
callq 0x21e0
movswl 0x4(%rsp), %esi
movq %r13, %rdi
callq 0x2180
movl $0x1, %edx
movq %rax, %rdi
leaq 0x380d(%rip), %rsi      # 0x601c
callq 0x21e0
xorl %eax, %eax
cmpl $0x2, (%rsp)
sete %al
addl %eax, %ebp
incl %ebx
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
cmpq %rbx, %rcx
ja 0x2766
jmp 0x2847
leaq 0x20(%rsp), %rdi
callq 0x2941
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x285e
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
xorl %ebx, %ebx
jmp 0x28aa
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x22c0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq 0x6758(%rip), %rdi      # 0x8ff0
callq 0x2030
movl $0x1, %ebx
movq %rax, %rdi
callq 0x2130
leaq 0x40(%rsp), %rdi
callq 0x2050
movl %ebx, %eax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x374f(%rip), %rdi      # 0x601e
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2270
leaq 0x373e(%rip), %rdi      # 0x601e
xorl %eax, %eax
callq 0x2270
jmp 0x2915
jmp 0x2915
jmp 0x2915
jmp 0x28ef
movq %rax, %rbx
jmp 0x292f
jmp 0x28f8
jmp 0x28f8
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x2918
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
jmp 0x2918
jmp 0x2915
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x292f
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x21b0
leaq 0x40(%rsp), %rdi
callq 0x2050
movq %rbx, %rdi
callq 0x22e0