// File: /workspace/github_cpp_makefile_star2_O1/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 0x4076b0
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)
movq %r15, 0x40(%rbx)
leaq 0x50(%rbx), %rax
movl %r15d, 0x50(%rbx)
movq %r15, 0x58(%rbx)
movq %rax, 0x60(%rbx)
movq %rax, 0x68(%rbx)
movq %r15, 0x70(%rbx)
leaq 0x88(%rbx), %rdi
leaq 0x98(%rbx), %rax
movq %rax, 0x88(%rbx)
movq %r15, 0x90(%rbx)
movb $0x0, 0x98(%rbx)
movl %r15d, (%rbx)
movw $0x0, 0x4(%rbx)
leaq 0x19b46e(%rip), %rcx    # 0x5a0532
xorl %esi, %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x481180
movl %r15d, 0x14(%rbx)
movl %r15d, 0x7c(%rbx)
movq %rbx, %rdi
callq 0x404904
movq $0x5f3540, %r14         # imm = 0x5F3540
leaq 0x17d0d2(%rip), %rsi    # 0x5821c0
movl $0x1d, %edx
movq %r14, %rdi
callq 0x470100
leaq 0x17d0dc(%rip), %rsi    # 0x5821de
movl $0x18, %edx
movq %r14, %rdi
callq 0x470100
movq 0x1ed442(%rip), %rsi    # 0x5f2558
movq 0x1ed443(%rip), %rdx    # 0x5f2560
movq %r14, %rdi
callq 0x470100
leaq 0x17d0cb(%rip), %rsi    # 0x5821f7
movl $0x2, %edx
movq %rax, %rdi
callq 0x470100
leaq 0x17d0ba(%rip), %rsi    # 0x5821fa
movl $0x17, %edx
movq %r14, %rdi
callq 0x470100
movq %r14, %rdi
movl $0xa, %esi
callq 0x4709d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x451250
movsbl %al, %esi
movq %rbx, %rdi
callq 0x46fc00
movq %rax, %rdi
callq 0x46f1d0
leaq 0x17d087(%rip), %rsi    # 0x582212
movl $0x17, %edx
movq %r14, %rdi
callq 0x470100
movq %r14, %rdi
movl $0x989680, %esi         # imm = 0x989680
callq 0x4709d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x451250
movsbl %al, %esi
movq %rbx, %rdi
callq 0x46fc00
movq %rax, %rdi
callq 0x46f1d0
leaq 0x17d054(%rip), %rsi    # 0x58222a
movl $0x17, %edx
movq %r14, %rdi
callq 0x470100
movl 0x1eaf47(%rip), %esi    # 0x5f0130
movq %r14, %rdi
callq 0x4709d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x451250
movsbl %al, %esi
movq %rbx, %rdi
callq 0x46fc00
movq %rax, %rdi
callq 0x46f1d0
leaq 0x17d020(%rip), %rsi    # 0x582242
movl $0x17, %edx
movq %r14, %rdi
callq 0x470100
movl $0xa8, %esi
movq %r14, %rdi
callq 0x470a00
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x451250
movsbl %al, %esi
movq %rbx, %rdi
callq 0x46fc00
movq %rax, %rdi
callq 0x46f1d0
leaq 0x17cfed(%rip), %rsi    # 0x58225a
movl $0x17, %edx
movq %r14, %rdi
callq 0x470100
movl $0x5f, %esi
movq %r14, %rdi
callq 0x470a00
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x451250
movsbl %al, %esi
movq %rbx, %rdi
callq 0x46fc00
movq %rax, %rdi
callq 0x46f1d0
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x530c20
imulq $0xf4240, (%rbx), %rax  # imm = 0xF4240
addq 0x8(%rbx), %rax
movq %rax, 0x38(%rsp)
movq %rax, 0x30(%rsp)
xorl %ebp, %ebp
movq 0x1ed2ca(%rip), %rax    # 0x5f25a8
cmpq 0x1ed2a3(%rip), %rax    # 0x5f2588
je 0x405807
cmpq 0x1ed2be(%rip), %rax    # 0x5f25b0
jne 0x405307
movq 0x1ed2c5(%rip), %rax    # 0x5f25c0
movq -0x8(%rax), %rax
movl $0x200, %ecx            # imm = 0x200
addq %rcx, %rax
movq -0x8(%rax), %r13
leaq 0x1ed266(%rip), %rdi    # 0x5f2578
callq 0x406232
testq %r15, %r15
je 0x405326
movl 0x78(%r13), %eax
cmpl 0x78(%r15), %eax
jge 0x405340
movslq 0x10(%r13), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x10(%r13)
cmpq $0x7, %rax
jl 0x405370
testq %r13, %r13
je 0x4057f9
movq 0x88(%r13), %rdi
leaq 0x98(%r13), %rax
cmpq %rax, %rdi
je 0x40535e
movq (%rax), %rsi
incq %rsi
callq 0x406280
leaq 0x48(%r13), %rdi
callq 0x405bb6
leaq 0x18(%r13), %r14
jmp 0x4057e4
movl %ebp, 0x28(%rsp)
movq %r15, 0x18(%rsp)
movl 0xc(%r13), %ebx
leaq 0x1eadbc(%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 0x405892
movl %r15d, (%rax)
leaq 0x48(%r13), %r14
movl %ebx, 0x2c(%rsp)
movq %r14, %rdi
leaq 0x2c(%rsp), %rsi
callq 0x405892
movl %r15d, (%rax)
movl $0xa8, %edi
callq 0x4076b0
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 0x405952
leaq 0x48(%r12), %r15
movq %r15, %rdi
movq %r14, 0x20(%rsp)
movq %r14, %rsi
callq 0x405952
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 0x405b26
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 0x1ed0c8(%rip), %rdi    # 0x5f2578
leaq 0x8(%rsp), %r15
movq %r15, %rsi
callq 0x405fd6
movq %r13, %rdi
callq 0x404904
movl %eax, %r12d
cmpl %ebp, %ebx
jne 0x405595
movl $0x17, %edx
movq $0x5f3540, %rbx         # imm = 0x5F3540
movq %rbx, %rdi
leaq 0x17cd8c(%rip), %rsi    # 0x582272
callq 0x470100
movq %rbx, %rdi
movl %ebp, %esi
callq 0x4709d0
movq %rax, %rbx
movl $0x4, %edx
movq %rax, %rdi
leaq 0x17cd83(%rip), %rsi    # 0x58228a
callq 0x470100
movq %r15, %rdi
xorl %esi, %esi
callq 0x530c20
imulq $0xf4240, 0x8(%rsp), %rax # imm = 0xF4240
movq 0x10(%rsp), %rcx
subq 0x30(%rsp), %rcx
addq %rax, %rcx
cvtsi2sd %rcx, %xmm0
divsd 0x17cacf(%rip), %xmm0   # 0x582008
movq %rbx, %rdi
callq 0x471360
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x451250
movsbl %al, %esi
movq %rbx, %rdi
callq 0x46fc00
movq %rax, %rdi
callq 0x46f1d0
movq %r13, %rdi
movl $0x1, %esi
callq 0x404e59
movq %r15, %rdi
xorl %esi, %esi
callq 0x530c20
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 0x4057be
movl $0x88, %eax
leaq (%rax,%r13), %rdi
leaq 0x1ecfa0(%rip), %rsi    # 0x5f2558
callq 0x482590
testl %eax, %eax
jne 0x4057be
leaq 0x78(%r13), %r12
testq %r15, %r15
je 0x40561e
movl (%r12), %eax
cmpl 0x78(%r15), %eax
jge 0x4057be
testq %r15, %r15
je 0x40561e
movq 0x88(%r15), %rdi
leaq 0x98(%r15), %rax
cmpq %rax, %rdi
je 0x4055ff
movq (%rax), %rsi
incq %rsi
callq 0x406280
leaq 0x48(%r15), %rdi
callq 0x405bb6
leaq 0x18(%r15), %rdi
callq 0x405bb6
movl $0xa8, %esi
movq %r15, %rdi
callq 0x406280
movl $0xa8, %edi
callq 0x4076b0
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 0x405952
leaq 0x48(%r15), %rbx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0x405952
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 0x405b26
movl $0x25, %edx
movq $0x5f3540, %rbx         # imm = 0x5F3540
movq %rbx, %rdi
leaq 0x17cbd9(%rip), %rsi    # 0x58228f
callq 0x470100
movl 0x78(%r15), %esi
movq %rbx, %rdi
callq 0x4709d0
movq %rax, %rbx
movl $0x7, %edx
movq %rax, %rdi
leaq 0x17cbdc(%rip), %rsi    # 0x5822b5
callq 0x470100
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x530c20
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 0x17c8f8(%rip), %xmm0   # 0x582008
movq %rbx, %rdi
callq 0x471360
movq %rax, %rbx
movl $0x8, %edx
movq %rax, %rdi
leaq 0x17cb93(%rip), %rsi    # 0x5822bd
callq 0x470100
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x451250
movsbl %al, %esi
movq %rbx, %rdi
callq 0x46fc00
movq %rax, %rdi
callq 0x46f1d0
movq %r13, %rdi
movl $0x1, %esi
callq 0x404e59
leaq 0x58(%rsp), %rbx
movq %rbx, 0x48(%rsp)
leaq 0x48(%rsp), %r12
movq %r12, %rdi
leaq 0x17cb4a(%rip), %rsi    # 0x5822c6
leaq 0x17cb4a(%rip), %rdx    # 0x5822cd
callq 0x405bca
movq %r12, %rdi
movq %r15, %rsi
callq 0x404f44
movq 0x48(%rsp), %rdi
cmpq %rbx, %rdi
je 0x4057aa
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x406280
movq %r15, %rax
addq $0x78, %rax
movl (%rax), %eax
movl %eax, 0x1ea977(%rip)    # 0x5f0130
movq 0x20(%rsp), %rbx
movq 0x88(%r13), %rdi
leaq 0x98(%r13), %rax
cmpq %rax, %rdi
je 0x4057dc
movq (%rax), %rsi
incq %rsi
callq 0x406280
movq %rbx, %rdi
callq 0x405bb6
movq %r14, %rdi
callq 0x405bb6
movl $0xa8, %esi
movq %r13, %rdi
callq 0x406280
incl %ebp
cmpl $0x989680, %ebp         # imm = 0x989680
jne 0x4052d7
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 0x405884
movq 0x58(%rsp), %rsi
incq %rsi
jmp 0x40587f
movq %rax, %r14
movq %rbx, %rdi
callq 0x405bb6
jmp 0x40583f
movq %rax, %r14
movq 0x18(%rsp), %rdi
callq 0x405bb6
jmp 0x40584e
movq %rax, %r14
movl $0xa8, %esi
movq %r15, %rdi
jmp 0x40587f
movq %rax, %r14
movq %r15, %rdi
callq 0x405bb6
jmp 0x405868
movq %rax, %r14
movq 0x40(%rsp), %rdi
callq 0x405bb6
jmp 0x405877
movq %rax, %r14
movl $0xa8, %esi
movq %r12, %rdi
callq 0x406280
movq %r14, %rdi
callq 0x4b1280