// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl $0xa8, %edi
callq 0x407310
movq %rax, %r14
xorl %ebx, %ebx
movl $0xa8, %edx
movq %rax, %rdi
xorl %esi, %esi
callq 0x401080
movq %r14, %rdi
callq 0x405424
movq %r14, %rdi
callq 0x404c20
movq %r14, %rdi
callq 0x404916
movq $0x5f5540, %r14         # imm = 0x5F5540
leaq 0x17e2ee(%rip), %rsi    # 0x5831a2
movq %r14, %rdi
callq 0x470290
leaq 0x17e2fd(%rip), %rsi    # 0x5831c0
movq %r14, %rdi
callq 0x470290
leaq 0x1ef686(%rip), %rsi    # 0x5f4558
movq %rax, %rdi
callq 0x482bc0
leaq 0x17e2f8(%rip), %rsi    # 0x5831d9
movq %rax, %rdi
callq 0x470290
leaq 0x17e2ec(%rip), %rsi    # 0x5831dc
movq %r14, %rdi
callq 0x470290
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x470630
movq %rax, %rdi
callq 0x46fb70
leaq 0x17e2e2(%rip), %rsi    # 0x5831f4
movq %r14, %rdi
callq 0x470290
movq %rax, %rdi
movl $0x989680, %esi         # imm = 0x989680
callq 0x470630
movq %rax, %rdi
callq 0x46fb70
leaq 0x17e2d6(%rip), %rsi    # 0x58320c
movq %r14, %rdi
callq 0x470290
movl 0x1ed1ec(%rip), %esi    # 0x5f2130
movq %rax, %rdi
callq 0x470630
movq %rax, %rdi
callq 0x46fb70
leaq 0x17e2c9(%rip), %rsi    # 0x583224
movq %r14, %rdi
callq 0x470290
movl $0xa8, %esi
movq %rax, %rdi
callq 0x470660
movq %rax, %rdi
callq 0x46fb70
leaq 0x17e2bd(%rip), %rsi    # 0x58323c
movq %r14, %rdi
callq 0x470290
pushq $0x5f
popq %rsi
movq %rax, %rdi
callq 0x470660
movq %rax, %rdi
callq 0x46fb70
callq 0x4052be
movl $0xc350, %r13d          # imm = 0xC350
leaq 0x1ef5cc(%rip), %r15    # 0x5f4578
movq %rax, 0x20(%rsp)
movq %rax, 0x10(%rsp)
xorl %r12d, %r12d
movl %ebx, %eax
xorl %edx, %edx
divl %r13d
cmpl $0x989680, %ebx         # imm = 0x989680
je 0x405274
movl %eax, %ebp
movq 0x1ef5d3(%rip), %rax    # 0x5f45a8
cmpq 0x1ef5ac(%rip), %rax    # 0x5f4588
je 0x405274
cmpq 0x1ef5c7(%rip), %rax    # 0x5f45b0
jne 0x404ffe
movq 0x1ef5ce(%rip), %rax    # 0x5f45c0
movq -0x8(%rax), %rax
movl $0x200, %ecx            # imm = 0x200
addq %rcx, %rax
movq -0x8(%rax), %r14
movq %r15, %rdi
callq 0x405e80
testq %r12, %r12
je 0x40501e
movl 0x78(%r14), %eax
cmpl 0x78(%r12), %eax
jge 0x405258
movslq 0x10(%r14), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x10(%r14)
cmpq $0x6, %rax
jg 0x405258
movq %r12, 0x18(%rsp)
movl 0xc(%r14), %r15d
leaq 0x1ed0fd(%rip), %rcx    # 0x5f2140
movzwl 0x2(%rcx,%rax,2), %eax
movl %r15d, %ecx
movl $0xffff, %esi           # imm = 0xFFFF
andl %esi, %ecx
subl %ecx, %eax
cltd
pushq $0x5e
popq %rcx
idivl %ecx
leal 0x5e(%rdx), %r13d
testl %edx, %edx
cmovnsl %edx, %r13d
addl $0x21, %r13d
andl %esi, %r13d
leaq 0x18(%r14), %rdi
movl %r15d, (%rsp)
movq %rsp, %r12
movq %r12, %rsi
callq 0x405340
movl %r13d, (%rax)
leaq 0x48(%r14), %rdi
movl %r15d, 0xc(%rsp)
leaq 0xc(%rsp), %rsi
callq 0x405340
movl %r13d, (%rax)
movl $0xa8, %edi
callq 0x407310
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x40538e
imull $0xc350, %ebp, %r13d    # imm = 0xC350
movq %r15, (%rsp)
leaq 0x1ef4b7(%rip), %r15    # 0x5f4578
movq %r15, %rdi
movq %r12, %rsi
callq 0x405c20
movq %r14, %rdi
callq 0x404916
movl %eax, %ebp
movq %rbx, %r12
cmpl %r12d, %r13d
jne 0x405144
movq $0x5f5540, %rdi         # imm = 0x5F5540
leaq 0x17e168(%rip), %rsi    # 0x583254
callq 0x470290
movq %rax, %rdi
movl %r12d, %esi
callq 0x470630
movq %rax, %rdi
leaq 0x17e166(%rip), %rsi    # 0x58326c
callq 0x470290
movq %rax, %r15
movq 0x10(%rsp), %rdi
callq 0x404718
movq %r15, %rdi
leaq 0x1ef456(%rip), %r15    # 0x5f4578
callq 0x470fc0
movq %rax, %rdi
callq 0x46fb70
movq %r14, %rdi
pushq $0x1
popq %rsi
callq 0x404d1f
callq 0x4052be
movq %rax, 0x10(%rsp)
movl $0xc350, %r13d          # imm = 0xC350
movq 0x18(%rsp), %r12
testl %ebp, %ebp
jne 0x405258
leaq 0x88(%r14), %rdi
leaq 0x1ef3f3(%rip), %rsi    # 0x5f4558
callq 0x4821f0
testl %eax, %eax
jne 0x405258
testq %r12, %r12
movq %rsp, %rbp
je 0x40519e
movl 0x78(%r14), %eax
cmpl 0x78(%r12), %eax
jge 0x405258
movq %r12, %rdi
callq 0x405476
movl $0xa8, %esi
movq %r12, %rdi
callq 0x405ee0
movl $0xa8, %edi
callq 0x407310
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x40538e
movq $0x5f5540, %rdi         # imm = 0x5F5540
leaq 0x17e0ad(%rip), %rsi    # 0x583271
callq 0x470290
movl 0x78(%r12), %esi
movq %rax, %rdi
callq 0x470630
movq %rax, %rdi
leaq 0x17e0b7(%rip), %rsi    # 0x583297
callq 0x470290
movq %rax, %r15
movq 0x20(%rsp), %rdi
callq 0x404718
movq %r15, %rdi
callq 0x470fc0
movq %rax, %rdi
leaq 0x17e09b(%rip), %rsi    # 0x58329f
callq 0x470290
movq %rax, %rdi
callq 0x46fb70
movq %r14, %rdi
pushq $0x1
popq %rsi
callq 0x404d1f
leaq 0x28(%rsp), %r15
movq %r15, %rdi
leaq 0x17e07d(%rip), %rsi    # 0x5832a8
movq %rbp, %rdx
callq 0x4052e2
movq %r15, %rdi
movq %r12, %rsi
callq 0x404dbd
movq %r15, %rdi
leaq 0x1ef330(%rip), %r15    # 0x5f4578
callq 0x480190
movl 0x78(%r12), %eax
movl %eax, 0x1eced8(%rip)    # 0x5f2130
movq %r14, %rdi
callq 0x405476
movl $0xa8, %esi
movq %r14, %rdi
callq 0x405ee0
incl %ebx
jmp 0x404fb9
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x480190
jmp 0x4052b6
movq %rax, %rbx
jmp 0x4052b6
movq %rax, %rbx
movl $0xa8, %esi
movq %r12, %rdi
jmp 0x4052b1
movq %rax, %rbx
movl $0xa8, %esi
movq %r15, %rdi
callq 0x405ee0
movq %rbx, %rdi
callq 0x4b1a40