// File: jmbjorndalen[P]Malbolge-gen/mbolge-gen.cpp
// Function: main
int main(int argc, char *argv[]) 
{
    MBSearch *mb = new MBSearch();
    mb->initExec();
    mb->doExec(); // get the initial execution started.. TODO: this is wrong. I'm ignoring the result of the first path, even if it could be correct (however unlikely)

    MBSearch *bestProg = NULL; 

    std::cout << "Parameters for the generator\n"; 
    std::cout << "    TARGET             '" << TARGET << "'\n";
    std::cout << "    MAX_OSEARCH        " << MAX_OSEARCH << std::endl; 
    std::cout << "    MAX_SEARCH_ITERS   " << MAX_SEARCH_ITERS << std::endl; 
    std::cout << "    maxAllowedProgLen  " << maxAllowedProgLen << std::endl;
    std::cout << "    sizeof(MBSearch)   " << sizeof(MBSearch) << std::endl; 
    std::cout << "    sizeof(xlat2)      " << sizeof(xlat2) << std::endl; 
    
    long long start = get_tod_usecs();    // start of search
    long long prevOut = start;            // previous output time
    for (int i = 0; i < MAX_SEARCH_ITERS && !globalStack.empty(); i++)
    { 
	MBSearch *cur = globalStack.top();
	globalStack.pop();

	if (bestProg != NULL && cur->maxMemLoc() >= bestProg->maxMemLoc())
	{
	    // No point in continuing along a longer code path. 
	    delete cur; 
	    continue; 
	}
	
	++cur->curNewOP; 
	if (cur->curNewOP >= NUM_OPCODES)
	{ 
	    // exchausted our options with this branch
	    delete cur; 
	    continue; 
	}

	// First, modify the current instruction
	int addr = cur->curNewMem;
	cur->orig[addr] = cur->mem[addr] = encode_op(MalbolgeOps[cur->curNewOP], addr);

	// prepare for next round, push a copy back on the stack before we execute with the current search path
	globalStack.push(new MBSearch(*cur));
	int ret = cur->doExec();
	if (i % 50000 == 0)
	{ 
	    // every once in a while, provide some output for the user
	    std::cout << "\n   done with search # " << i << " DT " << timeSince(prevOut) << std::endl;
	    printCurOutput(cur); 
	    prevOut = get_tod_usecs(); 
	}
	if (ret == MBSearch::RES_OK && cur->output.compare(TARGET) == 0)
	{ 
	    // Found a correct program that terminates after printing the target string.
	    // Check if it's better than the currently best program, and store the result
	    if ((bestProg == NULL) || (cur->maxMemLoc() < bestProg->maxMemLoc()))
	    { 
		if (bestProg)
		    delete bestProg; 
		bestProg = new MBSearch(*cur); 

		std::cout << "\nSTORING new best program with score " << bestProg->maxMemLoc()
			  << " after " << timeSince(start) << " seconds" << std::endl; 
		printCurOutput(cur); 
		storeProgram("/tmp/t-", bestProg); 
		maxAllowedProgLen = bestProg->maxMemLoc();
	    }
	}
	delete cur; 
    };
    return 0; 
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0xa8, %edi
callq 0x40a800
movq %rax, %rdi
movq %rdi, -0x98(%rbp)
xorl %esi, %esi
movl $0xa8, %edx
callq 0x401080
movq -0x98(%rbp), %rdi
callq 0x405f30
movq -0x98(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x4050a0
movq -0x18(%rbp), %rdi
callq 0x404b10
movq $0x0, -0x20(%rbp)
movq $0x5f9540, %rdi         # imm = 0x5F9540
leaq 0x180e3a(%rip), %rsi    # 0x586342
callq 0x4737e0
movq $0x5f9540, %rdi         # imm = 0x5F9540
leaq 0x180e45(%rip), %rsi    # 0x586360
callq 0x4737e0
movq %rax, %rdi
leaq 0x1f302e(%rip), %rsi    # 0x5f8558
callq 0x486110
movq %rax, %rdi
leaq 0x180e40(%rip), %rsi    # 0x586379
callq 0x4737e0
movq $0x5f9540, %rdi         # imm = 0x5F9540
leaq 0x180e30(%rip), %rsi    # 0x58637c
callq 0x4737e0
movq %rax, %rdi
movl $0xa, %esi
callq 0x473b80
movq %rax, %rdi
movq $0x4730c0, %rsi         # imm = 0x4730C0
callq 0x4722d0
movq $0x5f9540, %rdi         # imm = 0x5F9540
leaq 0x180e19(%rip), %rsi    # 0x586394
callq 0x4737e0
movq %rax, %rdi
movl $0x989680, %esi         # imm = 0x989680
callq 0x473b80
movq %rax, %rdi
movq $0x4730c0, %rsi         # imm = 0x4730C0
callq 0x4722d0
movq $0x5f9540, %rdi         # imm = 0x5F9540
leaq 0x180e02(%rip), %rsi    # 0x5863ac
callq 0x4737e0
movq %rax, %rdi
movl 0x1f0b78(%rip), %esi    # 0x5f6130
callq 0x473b80
movq %rax, %rdi
movq $0x4730c0, %rsi         # imm = 0x4730C0
callq 0x4722d0
movq $0x5f9540, %rdi         # imm = 0x5F9540
leaq 0x180dea(%rip), %rsi    # 0x5863c4
callq 0x4737e0
movq %rax, %rdi
movl $0xa8, %esi
callq 0x473df0
movq %rax, %rdi
movq $0x4730c0, %rsi         # imm = 0x4730C0
callq 0x4722d0
movq $0x5f9540, %rdi         # imm = 0x5F9540
leaq 0x180dd3(%rip), %rsi    # 0x5863dc
callq 0x4737e0
movq %rax, %rdi
movl $0x5f, %esi
callq 0x473df0
movq %rax, %rdi
movq $0x4730c0, %rsi         # imm = 0x4730C0
callq 0x4722d0
callq 0x405b20
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
xorl %eax, %eax
cmpl $0x989680, -0x34(%rbp)  # imm = 0x989680
movb %al, -0x99(%rbp)
jge 0x405667
leaq 0x1f2f1e(%rip), %rdi    # 0x5f8578
callq 0x405f70
xorb $-0x1, %al
movb %al, -0x99(%rbp)
movb -0x99(%rbp), %al
testb $0x1, %al
jne 0x405676
jmp 0x405adc
leaq 0x1f2efb(%rip), %rdi    # 0x5f8578
callq 0x405f90
movq (%rax), %rax
movq %rax, -0x40(%rbp)
leaq 0x1f2ee8(%rip), %rdi    # 0x5f8578
callq 0x405fb0
cmpq $0x0, -0x20(%rbp)
je 0x4056f3
movq -0x40(%rbp), %rdi
callq 0x405f10
movl %eax, -0xa0(%rbp)
movq -0x20(%rbp), %rdi
callq 0x405f10
movl %eax, %ecx
movl -0xa0(%rbp), %eax
cmpl %ecx, %eax
jl 0x4056f3
movq -0x40(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, %rax
je 0x4056ee
movq -0xa8(%rbp), %rdi
callq 0x405fd0
movq -0xa8(%rbp), %rdi
movl $0xa8, %esi
callq 0x4093d0
jmp 0x405ace
movq -0x40(%rbp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x10(%rax)
movq -0x40(%rbp), %rax
cmpl $0x8, 0x10(%rax)
jl 0x40573d
movq -0x40(%rbp), %rax
movq %rax, -0xb0(%rbp)
cmpq $0x0, %rax
je 0x405738
movq -0xb0(%rbp), %rdi
callq 0x405fd0
movq -0xb0(%rbp), %rdi
movl $0xa8, %esi
callq 0x4093d0
jmp 0x405ace
movq -0x40(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x40(%rbp), %rax
movslq 0x10(%rax), %rcx
leaq 0x1f09ea(%rip), %rax    # 0x5f6140
movzwl (%rax,%rcx,2), %edi
movzwl -0x44(%rbp), %esi
callq 0x4049f0
movzwl %ax, %eax
movl %eax, -0xbc(%rbp)
movq -0x40(%rbp), %rdi
addq $0x18, %rdi
movl -0x44(%rbp), %eax
movl %eax, -0x48(%rbp)
leaq -0x48(%rbp), %rsi
callq 0x405d00
movl -0xbc(%rbp), %ecx
movl %ecx, (%rax)
movq -0x40(%rbp), %rdi
addq $0x48, %rdi
movl -0x44(%rbp), %eax
movl %eax, -0x4c(%rbp)
leaq -0x4c(%rbp), %rsi
callq 0x405d00
movl -0xbc(%rbp), %ecx
movl %ecx, (%rax)
movl $0xa8, %edi
callq 0x40a800
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xb8(%rbp)
movq -0x40(%rbp), %rsi
callq 0x405e10
jmp 0x4057cc
movq -0xb8(%rbp), %rax
movq %rax, -0x58(%rbp)
leaq 0x1f2d9a(%rip), %rdi    # 0x5f8578
leaq -0x58(%rbp), %rsi
callq 0x405de0
movq -0x40(%rbp), %rdi
callq 0x404b10
movl %eax, -0x68(%rbp)
movl -0x34(%rbp), %eax
movl $0xc350, %ecx           # imm = 0xC350
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x40589a
movq $0x5f9540, %rdi         # imm = 0x5F9540
leaq 0x180bdf(%rip), %rsi    # 0x5863f4
callq 0x4737e0
movq %rax, %rdi
movl -0x34(%rbp), %esi
callq 0x473b80
movq %rax, %rdi
leaq 0x180bdd(%rip), %rsi    # 0x58640c
callq 0x4737e0
movq %rax, -0xc8(%rbp)
movq -0x30(%rbp), %rdi
callq 0x404790
movq -0xc8(%rbp), %rdi
callq 0x474750
movq %rax, %rdi
movq $0x4730c0, %rsi         # imm = 0x4730C0
callq 0x4722d0
movq -0x40(%rbp), %rdi
movl $0x1, %esi
callq 0x4052a0
callq 0x405b20
movq %rax, -0x30(%rbp)
jmp 0x40589a
movq -0xb8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
movl $0xa8, %esi
callq 0x4093d0
jmp 0x405ae7
cmpl $0x0, -0x68(%rbp)
jne 0x405a9e
movq -0x40(%rbp), %rdi
addq $0x88, %rdi
leaq 0x1f2ca2(%rip), %rsi    # 0x5f8558
callq 0x485740
cmpl $0x0, %eax
jne 0x405a9e
cmpq $0x0, -0x20(%rbp)
je 0x4058f3
movq -0x40(%rbp), %rdi
callq 0x405f10
movl %eax, -0xcc(%rbp)
movq -0x20(%rbp), %rdi
callq 0x405f10
movl %eax, %ecx
movl -0xcc(%rbp), %eax
cmpl %ecx, %eax
jge 0x405a9c
cmpq $0x0, -0x20(%rbp)
je 0x40592a
movq -0x20(%rbp), %rax
movq %rax, -0xd8(%rbp)
cmpq $0x0, %rax
je 0x405928
movq -0xd8(%rbp), %rdi
callq 0x405fd0
movq -0xd8(%rbp), %rdi
movl $0xa8, %esi
callq 0x4093d0
jmp 0x40592a
movl $0xa8, %edi
callq 0x40a800
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xe0(%rbp)
movq -0x40(%rbp), %rsi
callq 0x405e10
jmp 0x40594c
movq -0xe0(%rbp), %rax
movq %rax, -0x20(%rbp)
movq $0x5f9540, %rdi         # imm = 0x5F9540
leaq 0x180aac(%rip), %rsi    # 0x586411
callq 0x4737e0
movq %rax, -0xf8(%rbp)
movq -0x20(%rbp), %rdi
callq 0x405f10
movq -0xf8(%rbp), %rdi
movl %eax, %esi
callq 0x473b80
movq %rax, %rdi
leaq 0x180aa5(%rip), %rsi    # 0x586437
callq 0x4737e0
movq %rax, -0xf0(%rbp)
movq -0x28(%rbp), %rdi
callq 0x404790
movq -0xf0(%rbp), %rdi
callq 0x474750
movq %rax, %rdi
leaq 0x180a82(%rip), %rsi    # 0x58643f
callq 0x4737e0
movq %rax, %rdi
movq $0x4730c0, %rsi         # imm = 0x4730C0
callq 0x4722d0
movq -0x40(%rbp), %rdi
movl $0x1, %esi
callq 0x4052a0
leaq -0x89(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
callq 0x40ac70
movq -0xe8(%rbp), %rdx
leaq 0x180a48(%rip), %rsi    # 0x586448
leaq -0x88(%rbp), %rdi
callq 0x405b50
jmp 0x405a0e
movq -0x20(%rbp), %rsi
leaq -0x88(%rbp), %rdi
callq 0x405360
jmp 0x405a20
leaq -0x88(%rbp), %rdi
callq 0x4836e0
leaq -0x89(%rbp), %rdi
callq 0x40ac90
movq -0x20(%rbp), %rdi
callq 0x405f10
movl %eax, 0x1f06e9(%rip)    # 0x5f6130
jmp 0x405a9c
movq -0xe0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
movl $0xa8, %esi
callq 0x4093d0
jmp 0x405ae7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x405a8e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x4836e0
leaq -0x89(%rbp), %rdi
callq 0x40ac90
jmp 0x405ae7
jmp 0x405a9e
movq -0x40(%rbp), %rax
movq %rax, -0x100(%rbp)
cmpq $0x0, %rax
je 0x405acc
movq -0x100(%rbp), %rdi
callq 0x405fd0
movq -0x100(%rbp), %rdi
movl $0xa8, %esi
callq 0x4093d0
jmp 0x405ace
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x405642
xorl %eax, %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x4b4f90