// File: /workspace/github_cpp_makefile_star2_O2/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
cmpl $0x2, %edi
jl 0x24d2
movl %edi, %ebp
movq 0x8(%rbx), %rsi
leaq 0x3b52(%rip), %rdi      # 0x6004
callq 0x2210
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leaq (%rbx,%rcx,8), %rbx
movq %rcx, 0x38(%rsp)
subl %ecx, %ebp
cmpl $0x2, %ebp
sete %bpl
jmp 0x24dd
movq $0x0, 0x38(%rsp)
xorl %ebp, %ebp
leaq 0x40(%rsp), %rdi
callq 0x21b0
testb %bpl, %bpl
je 0x253f
movq 0x8(%rbx), %rsi
leaq 0x40(%rsp), %r14
pushq $0x8
popq %rdx
movq %r14, %rdi
callq 0x20e0
movq 0x40(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x60(%rsp,%rax)
je 0x2546
movq 0x6ad9(%rip), %rdi      # 0x8ff0
leaq 0x3aec(%rip), %rsi      # 0x600a
callq 0x2170
movq 0x8(%rbx), %rsi
movq %rax, %rdi
callq 0x2170
movq %rax, %rdi
callq 0x20a0
pushq $0x1
popq %rbx
jmp 0x2743
movq 0x6a92(%rip), %r14      # 0x8fd8
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x38bc
movq %rsp, %rdi
leaq 0x18(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x41e8
leaq 0x18(%rsp), %rbx
movq %rsp, %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2e72
movq %r14, %rdi
callq 0x37c8
xorps %xmm0, %xmm0
andq $0x0, 0x10(%r14)
movaps %xmm0, (%r14)
xorl %ebp, %ebp
leaq 0x34(%rsp), %r15
xorl %r12d, %r12d
movq 0x20(%rsp), %rax
subq 0x18(%rsp), %rax
sarq $0x4, %rax
cmpq %rax, %r12
jae 0x2618
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2ece
cmpl $0x2, (%rax)
jne 0x25c7
movl %r12d, 0x34(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x2f10
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2ece
cmpl $0x10, (%rax)
jne 0x260e
movq 0x8(%rsp), %rax
movl -0x4(%rax), %r13d
movslq %r13d, %rsi
movq %rbx, %rdi
callq 0x2ece
movl %r12d, %ecx
subl %r13d, %ecx
movw %cx, 0x4(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2ece
addl %ebp, %r13d
movw %r13w, 0x4(%rax)
addq $-0x4, 0x8(%rsp)
incq %r12
decl %ebp
jmp 0x2594
movq %rsp, %rbx
movq %rbx, %rdi
callq 0x309c
movl $0xf, (%rbx)
andw $0x0, 0x4(%rbx)
andq $0x0, 0x8(%rbx)
leaq 0x18(%rsp), %rdi
movq %rsp, %rsi
callq 0x30ba
cmpb $0x0, 0x38(%rsp)
je 0x272d
xorl %r15d, %r15d
movq 0x696b(%rip), %rbx      # 0x8fc0
leaq 0x39be(%rip), %r13      # 0x601a
xorl %ebp, %ebp
movl %ebp, %ebp
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
cmpq %rbp, %rcx
jbe 0x2737
movq %rbp, %rcx
shlq $0x4, %rcx
movups (%rax,%rcx), %xmm0
movaps %xmm0, (%rsp)
xorl %r12d, %r12d
cmpl $0x10, (%rsp)
sete %r14b
movq (%rbx), %rax
movq -0x18(%rax), %rax
movq $0x6, 0x10(%rbx,%rax)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2110
movq %rax, %rdi
leaq 0x395f(%rip), %rsi      # 0x6018
callq 0x2170
movb %r14b, %r12b
subl %r12d, %r15d
testl %r15d, %r15d
movl $0x0, %r14d
cmovgl %r15d, %r14d
subl $0x1, %r14d
jb 0x26e4
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2170
jmp 0x26d1
movq %rbx, %rdi
movq %rsp, %rsi
callq 0x37e8
movq %rax, %rdi
leaq 0x391f(%rip), %rsi      # 0x6018
callq 0x2170
movswl 0x4(%rsp), %esi
movq %rax, %rdi
callq 0x2160
movq %rax, %rdi
leaq 0x3907(%rip), %rsi      # 0x601c
callq 0x2170
xorl %eax, %eax
cmpl $0x2, (%rsp)
sete %al
addl %eax, %r15d
incl %ebp
jmp 0x265e
leaq 0x18(%rsp), %rdi
callq 0x27a0
leaq 0x18(%rsp), %rdi
callq 0x37c8
xorl %ebx, %ebx
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
jmp 0x2781
jmp 0x2781
jmp 0x2781
jmp 0x2769
movq %rax, %rbx
jmp 0x278e
jmp 0x2772
jmp 0x2772
movq %rax, %rbx
movq %rsp, %rdi
callq 0x309c
jmp 0x2784
jmp 0x2781
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x37c8
leaq 0x40(%rsp), %rdi
callq 0x2050
movq %rbx, %rdi
callq 0x22c0