// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movl $0xa8, %edi
callq 0x407640
movq %rax, %rbx
leaq 0x20(%rax), %r14
xorl %r15d, %r15d
movl $0xa8, %edx
movq %rax, %rdi
xorl %esi, %esi
callq 0x401060
movq %r14, 0x30(%rbx)
movq %r14, 0x38(%rbx)
leaq 0x50(%rbx), %rax
movl %r15d, 0x50(%rbx)
movq %rax, 0x60(%rbx)
movq %rax, 0x68(%rbx)
leaq 0x88(%rbx), %rdi
leaq 0x98(%rbx), %rax
movq %rax, 0x88(%rbx)
movb $0x0, 0x98(%rbx)
movw $0x0, 0x2(%rbx)
leaq 0x19b4f2(%rip), %rcx    # 0x5a0532
xorl %esi, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x481110
movl %r15d, 0x14(%rbx)
movl %r15d, 0x7c(%rbx)
movq %rbx, %rdi
callq 0x4048f4
movq $0x5f3540, %r14         # imm = 0x5F3540
leaq 0x17d156(%rip), %rsi    # 0x5821c0
movl $0x1d, %edx
movq %r14, %rdi
callq 0x470090
leaq 0x17d160(%rip), %rsi    # 0x5821de
movl $0x18, %edx
movq %r14, %rdi
callq 0x470090
movq 0x1ed4c6(%rip), %rsi    # 0x5f2558
movq 0x1ed4c7(%rip), %rdx    # 0x5f2560
movq %r14, %rdi
callq 0x470090
leaq 0x17d14f(%rip), %rsi    # 0x5821f7
movl $0x2, %edx
movq %rax, %rdi
callq 0x470090
leaq 0x17d13e(%rip), %rsi    # 0x5821fa
movl $0x17, %edx
movq %r14, %rdi
callq 0x470090
movq %r14, %rdi
movl $0xa, %esi
callq 0x470960
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4511e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x46fb90
movq %rax, %rdi
callq 0x46f160
leaq 0x17d10b(%rip), %rsi    # 0x582212
movl $0x17, %edx
movq %r14, %rdi
callq 0x470090
movq %r14, %rdi
movl $0x989680, %esi         # imm = 0x989680
callq 0x470960
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4511e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x46fb90
movq %rax, %rdi
callq 0x46f160
leaq 0x17d0d8(%rip), %rsi    # 0x58222a
movl $0x17, %edx
movq %r14, %rdi
callq 0x470090
movl 0x1eafcb(%rip), %esi    # 0x5f0130
movq %r14, %rdi
callq 0x470960
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4511e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x46fb90
movq %rax, %rdi
callq 0x46f160
leaq 0x17d0a4(%rip), %rsi    # 0x582242
movl $0x17, %edx
movq %r14, %rdi
callq 0x470090
movl $0xa8, %esi
movq %r14, %rdi
callq 0x470990
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4511e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x46fb90
movq %rax, %rdi
callq 0x46f160
leaq 0x17d071(%rip), %rsi    # 0x58225a
movl $0x17, %edx
movq %r14, %rdi
callq 0x470090
movl $0x5f, %esi
movq %r14, %rdi
callq 0x470990
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4511e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x46fb90
movq %rax, %rdi
callq 0x46f160
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x530be0
imulq $0xf4240, (%rbx), %rax  # imm = 0xF4240
addq 0x8(%rbx), %rax
movq %rax, 0x38(%rsp)
movq %rax, 0x30(%rsp)
xorl %ebp, %ebp
movq 0x1ed34e(%rip), %rax    # 0x5f25a8
cmpq 0x1ed327(%rip), %rax    # 0x5f2588
je 0x405775
cmpq 0x1ed342(%rip), %rax    # 0x5f25b0
jne 0x405283
movq 0x1ed349(%rip), %rax    # 0x5f25c0
movq -0x8(%rax), %rax
movl $0x200, %ecx            # imm = 0x200
addq %rcx, %rax
movq -0x8(%rax), %r13
leaq 0x1ed2ea(%rip), %rdi    # 0x5f2578
callq 0x4061b8
testq %r15, %r15
je 0x4052a2
movl 0x78(%r13), %eax
cmpl 0x78(%r15), %eax
jge 0x4052b3
movslq 0x10(%r13), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x10(%r13)
cmpq $0x7, %rax
jl 0x4052e3
movq 0x88(%r13), %rdi
leaq 0x98(%r13), %rax
cmpq %rax, %rdi
je 0x4052d1
movq (%rax), %rsi
incq %rsi
callq 0x406210
leaq 0x48(%r13), %rdi
callq 0x405b32
leaq 0x18(%r13), %r14
jmp 0x405752
movl %ebp, 0x28(%rsp)
movq %r15, 0x18(%rsp)
movl 0xc(%r13), %ebx
leaq 0x1eae49(%rip), %rcx    # 0x5f0140
movzwl 0x2(%rcx,%rax,2), %eax
movl %ebx, %ecx
movl $0xffff, %esi           # imm = 0xFFFF
andl %esi, %ecx
subl %ecx, %eax
cltq
imulq $-0x51b3bea3, %rax, %rcx # imm = 0xAE4C415D
shrq $0x20, %rcx
addl %eax, %ecx
movl %ecx, %edx
shrl $0x1f, %edx
sarl $0x6, %ecx
addl %edx, %ecx
imull $0x5e, %ecx, %ecx
subl %ecx, %eax
leal 0x5e(%rax), %r15d
testl %eax, %eax
cmovnsl %eax, %r15d
addl $0x21, %r15d
andl %esi, %r15d
leaq 0x18(%r13), %rbp
movl %ebx, 0x8(%rsp)
movq %rbp, %rdi
leaq 0x8(%rsp), %rsi
callq 0x405800
movl %r15d, (%rax)
leaq 0x48(%r13), %r14
movl %ebx, 0x2c(%rsp)
movq %r14, %rdi
leaq 0x2c(%rsp), %rsi
callq 0x405800
movl %r15d, (%rax)
movl $0xa8, %edi
callq 0x407640
movq %rax, %r12
movupd (%r13), %xmm0
movupd %xmm0, (%rax)
movq 0x10(%r13), %rax
movq %rax, 0x10(%r12)
leaq 0x18(%r12), %rdi
movq %rdi, 0x40(%rsp)
movq %rbp, %rbx
movq %rbp, %rsi
callq 0x4058c8
leaq 0x48(%r12), %r15
movq %r15, %rdi
movq %r14, 0x20(%rsp)
movq %r14, %rsi
callq 0x4058c8
movl 0x28(%rsp), %ebp
movzwl 0x80(%r13), %eax
movw %ax, 0x80(%r12)
movq 0x78(%r13), %rax
movq %rax, 0x78(%r12)
movl $0x88, %eax
leaq (%r12,%rax), %rdi
movq %r12, %rax
addq $0x98, %rax
movq %rax, 0x88(%r12)
movq 0x88(%r13), %rsi
movq 0x90(%r13), %rdx
addq %rsi, %rdx
callq 0x405a9c
movq %rbx, %r14
movl %ebp, %eax
shrl $0x4, %eax
imulq $0xa7c5ac5, %rax, %rax  # imm = 0xA7C5AC5
shrq $0x27, %rax
imull $0xc350, %eax, %ebx     # imm = 0xC350
movq %r12, 0x8(%rsp)
leaq 0x1ed155(%rip), %rdi    # 0x5f2578
leaq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0x405f56
movq %r13, %rdi
callq 0x4048f4
movl %eax, %r12d
cmpl %ebp, %ebx
jne 0x405508
movl $0x17, %edx
movq $0x5f3540, %rbx         # imm = 0x5F3540
movq %rbx, %rdi
leaq 0x17ce19(%rip), %rsi    # 0x582272
callq 0x470090
movq %rbx, %rdi
movl %ebp, %esi
callq 0x470960
movq %rax, %rbx
movl $0x4, %edx
movq %rax, %rdi
leaq 0x17ce10(%rip), %rsi    # 0x58228a
callq 0x470090
movq %r15, %rdi
xorl %esi, %esi
callq 0x530be0
imulq $0xf4240, 0x8(%rsp), %rax # imm = 0xF4240
movq 0x10(%rsp), %rcx
subq 0x30(%rsp), %rcx
addq %rax, %rcx
cvtsi2sd %rcx, %xmm0
divsd 0x17cb5c(%rip), %xmm0   # 0x582008
movq %rbx, %rdi
callq 0x4712f0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4511e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x46fb90
movq %rax, %rdi
callq 0x46f160
movq %r13, %rdi
movl $0x1, %esi
callq 0x404deb
movq %r15, %rdi
xorl %esi, %esi
callq 0x530be0
imulq $0xf4240, 0x8(%rsp), %rax # imm = 0xF4240
addq 0x10(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x18(%rsp), %r15
movq 0x20(%rsp), %rbx
testl %r12d, %r12d
jne 0x40572c
movl $0x88, %eax
leaq (%rax,%r13), %rdi
leaq 0x1ed02d(%rip), %rsi    # 0x5f2558
callq 0x482520
testl %eax, %eax
jne 0x40572c
leaq 0x78(%r13), %r12
testq %r15, %r15
je 0x40558c
movl (%r12), %eax
cmpl 0x78(%r15), %eax
jge 0x40572c
movq 0x88(%r15), %rdi
leaq 0x98(%r15), %rax
cmpq %rax, %rdi
je 0x40556d
movq (%rax), %rsi
incq %rsi
callq 0x406210
leaq 0x48(%r15), %rdi
callq 0x405b32
leaq 0x18(%r15), %rdi
callq 0x405b32
movl $0xa8, %esi
movq %r15, %rdi
callq 0x406210
movl $0xa8, %edi
callq 0x407640
movq %rax, %r15
movupd (%r13), %xmm0
movupd %xmm0, (%rax)
movq 0x10(%r13), %rax
movq %rax, 0x10(%r15)
leaq 0x18(%r15), %rdi
movq %rdi, 0x18(%rsp)
movq %r14, %rsi
callq 0x4058c8
leaq 0x48(%r15), %rbx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0x4058c8
movzwl 0x8(%r12), %eax
movw %ax, 0x80(%r15)
movq (%r12), %rax
movq %rax, 0x78(%r15)
leaq 0x88(%r15), %rdi
leaq 0x98(%r15), %rax
movq %rax, 0x88(%r15)
movq 0x88(%r13), %rsi
movq 0x90(%r13), %rdx
addq %rsi, %rdx
callq 0x405a9c
movl $0x25, %edx
movq $0x5f3540, %rbx         # imm = 0x5F3540
movq %rbx, %rdi
leaq 0x17cc6b(%rip), %rsi    # 0x58228f
callq 0x470090
movl 0x78(%r15), %esi
movq %rbx, %rdi
callq 0x470960
movq %rax, %rbx
movl $0x7, %edx
movq %rax, %rdi
leaq 0x17cc6e(%rip), %rsi    # 0x5822b5
callq 0x470090
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x530be0
imulq $0xf4240, 0x8(%rsp), %rax # imm = 0xF4240
movq 0x10(%rsp), %rcx
subq 0x38(%rsp), %rcx
addq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
divsd 0x17c98a(%rip), %xmm0   # 0x582008
movq %rbx, %rdi
callq 0x4712f0
movq %rax, %rbx
movl $0x8, %edx
movq %rax, %rdi
leaq 0x17cc25(%rip), %rsi    # 0x5822bd
callq 0x470090
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4511e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x46fb90
movq %rax, %rdi
callq 0x46f160
movq %r13, %rdi
movl $0x1, %esi
callq 0x404deb
leaq 0x58(%rsp), %rbx
movq %rbx, 0x48(%rsp)
leaq 0x48(%rsp), %r12
movq %r12, %rdi
leaq 0x17cbdc(%rip), %rsi    # 0x5822c6
leaq 0x17cbdc(%rip), %rdx    # 0x5822cd
callq 0x405b46
movq %r12, %rdi
movq %r15, %rsi
callq 0x404ed6
movq 0x48(%rsp), %rdi
cmpq %rbx, %rdi
je 0x405718
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x406210
movq %r15, %rax
addq $0x78, %rax
movl (%rax), %eax
movl %eax, 0x1eaa09(%rip)    # 0x5f0130
movq 0x20(%rsp), %rbx
movq 0x88(%r13), %rdi
leaq 0x98(%r13), %rax
cmpq %rax, %rdi
je 0x40574a
movq (%rax), %rsi
incq %rsi
callq 0x406210
movq %rbx, %rdi
callq 0x405b32
movq %r14, %rdi
callq 0x405b32
movl $0xa8, %esi
movq %r13, %rdi
callq 0x406210
incl %ebp
cmpl $0x989680, %ebp         # imm = 0x989680
jne 0x405253
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq 0x48(%rsp), %rdi
cmpq %rbx, %rdi
je 0x4057f2
movq 0x58(%rsp), %rsi
incq %rsi
jmp 0x4057ed
movq %rax, %r14
movq %rbx, %rdi
callq 0x405b32
jmp 0x4057ad
movq %rax, %r14
movq 0x18(%rsp), %rdi
callq 0x405b32
jmp 0x4057bc
movq %rax, %r14
movl $0xa8, %esi
movq %r15, %rdi
jmp 0x4057ed
movq %rax, %r14
movq %r15, %rdi
callq 0x405b32
jmp 0x4057d6
movq %rax, %r14
movq 0x40(%rsp), %rdi
callq 0x405b32
jmp 0x4057e5
movq %rax, %r14
movl $0xa8, %esi
movq %r12, %rdi
callq 0x406210
movq %r14, %rdi
callq 0x4b1210