// File: 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
movq %rsp, %rbp
subq $0x2b0, %rsp            # imm = 0x2B0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
xorl %eax, %eax
cmpl $0x1, -0x8(%rbp)
movb %al, -0x26d(%rbp)
jle 0x25b9
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0xaa5e(%rip), %rdi      # 0xd004
callq 0x2280
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, -0x26d(%rbp)
movb -0x26d(%rbp), %al
andb $0x1, %al
movb %al, -0x11(%rbp)
testb $0x1, -0x11(%rbp)
je 0x25df
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x2210
cmpl $0x2, -0x8(%rbp)
jne 0x26b9
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x220(%rbp), %rdi
movl $0x8, %edx
callq 0x2110
jmp 0x2610
movq -0x220(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x220(%rbp,%rax), %rdi
callq 0x20a0
movb %al, -0x26e(%rbp)
jmp 0x2630
movb -0x26e(%rbp), %al
testb $0x1, %al
jne 0x263c
jmp 0x26b7
movq 0x119ad(%rip), %rdi     # 0x13ff0
leaq 0xa9c0(%rip), %rsi      # 0xd00a
callq 0x21a0
movq %rax, -0x278(%rbp)
jmp 0x2658
movq -0x278(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x21a0
movq %rax, -0x280(%rbp)
jmp 0x2675
movq -0x280(%rbp), %rdi
movq 0x11925(%rip), %rsi     # 0x13fa8
callq 0x21f0
jmp 0x268a
movl $0x1, -0x4(%rbp)
movl $0x1, -0x230(%rbp)
jmp 0x2912
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
jmp 0x292a
jmp 0x26b9
cmpl $0x2, -0x8(%rbp)
jne 0x26cf
leaq -0x220(%rbp), %rax
movq %rax, -0x288(%rbp)
jmp 0x26df
movq 0x11902(%rip), %rax     # 0x13fd8
movq %rax, -0x288(%rbp)
jmp 0x26df
movq -0x288(%rbp), %rax
movq %rax, -0x238(%rbp)
movq -0x238(%rbp), %rsi
leaq -0x250(%rbp), %rdi
callq 0x2950
jmp 0x2702
testb $0x1, -0x11(%rbp)
je 0x28e7
movl $0x0, -0x254(%rbp)
movl $0x0, -0x258(%rbp)
movl -0x258(%rbp), %eax
movq %rax, -0x290(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x3d40
movq %rax, %rcx
movq -0x290(%rbp), %rax
cmpq %rcx, %rax
jae 0x28d4
movl -0x258(%rbp), %eax
movl %eax, %esi
leaq -0x250(%rbp), %rdi
callq 0x3d60
movq (%rax), %rcx
movq %rcx, -0x268(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x260(%rbp)
cmpl $0x10, -0x268(%rbp)
jne 0x278d
movl -0x254(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x254(%rbp)
movq 0x1182c(%rip), %rdi     # 0x13fc0
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
movl $0x6, %esi
callq 0x3d80
jmp 0x27aa
movl -0x258(%rbp), %esi
movq 0x11809(%rip), %rdi     # 0x13fc0
callq 0x2250
movq %rax, -0x298(%rbp)
jmp 0x27c5
movq -0x298(%rbp), %rdi
leaq 0xa845(%rip), %rsi      # 0xd018
callq 0x21a0
jmp 0x27da
movl $0x0, -0x26c(%rbp)
movl -0x26c(%rbp), %eax
cmpl -0x254(%rbp), %eax
jge 0x283d
movq 0x117c7(%rip), %rdi     # 0x13fc0
leaq 0xa81a(%rip), %rsi      # 0xd01a
callq 0x21a0
jmp 0x2807
jmp 0x2809
movl -0x26c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x26c(%rbp)
jmp 0x27e4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x3db0
jmp 0x292a
movq 0x1177c(%rip), %rdi     # 0x13fc0
leaq -0x268(%rbp), %rsi
callq 0x7bf0
movq %rax, -0x2a0(%rbp)
jmp 0x2859
movq -0x2a0(%rbp), %rdi
leaq 0xa7b1(%rip), %rsi      # 0xd018
callq 0x21a0
movq %rax, -0x2a8(%rbp)
jmp 0x2875
movq -0x2a8(%rbp), %rdi
movswl -0x264(%rbp), %esi
callq 0x2190
movq %rax, -0x2b0(%rbp)
jmp 0x2891
movq -0x2b0(%rbp), %rdi
leaq 0xa77d(%rip), %rsi      # 0xd01c
callq 0x21a0
jmp 0x28a6
cmpl $0x2, -0x268(%rbp)
jne 0x28be
movl -0x254(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x254(%rbp)
jmp 0x28c0
movl -0x258(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x258(%rbp)
jmp 0x2720
movl $0x0, -0x4(%rbp)
movl $0x1, -0x230(%rbp)
jmp 0x2906
leaq -0x250(%rbp), %rdi
callq 0x2a20
jmp 0x28f5
movl $0x0, -0x4(%rbp)
movl $0x1, -0x230(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x3db0
leaq -0x220(%rbp), %rdi
callq 0x2060
movl -0x4(%rbp), %eax
addq $0x2b0, %rsp            # imm = 0x2B0
popq %rbp
retq
leaq -0x220(%rbp), %rdi
callq 0x2060
movq -0x228(%rbp), %rdi
callq 0x2330
nopw %cs:(%rax,%rax)