// File: /workspace/github_cpp_makefile_star2_O3/CMU-SAFARI[P]optimal-seed-solver/optimalSolverLN.cc
// Function: OptimalSolverLN::solveFirstOptimal(int, int, int)
int OptimalSolverLN::solveFirstOptimal(int opt_div, int pos, int l) {
	int lfreq = level[l-1][opt_div].frequency;
	int rfreq = level0[pos - opt_div][opt_div + l * minLength].frequency;
	int minFreq = lfreq + rfreq;
	int prev_lfreq = lfreq;
	int prev_rfreq = rfreq;

#ifdef TEST
	cout << "div: " << opt_div << " lfreq: " << lfreq << " rfreq: " << rfreq
		<< " prev_lfreq: " << prev_lfreq << " prev_rfreq: " << prev_rfreq
		<< " total: " << lfreq + rfreq << endl;
#endif
	int div;
	int totalTravel = 0;
	for (div = opt_div - 1; div >= 0; div--) {
		lfreq = level[l-1][div].frequency;
		
		//divider sprinting left
		if (lfreq == prev_lfreq && div > 0 && lfreq == level[l-1][div-1].frequency) {
			continue;
		}

		rfreq = level0[pos - div][div + l * minLength].frequency;

#ifdef TEST
		cout << "div: " << div << " lfreq: " << lfreq << " rfreq: " << rfreq
			<< " prev_lfreq: " << prev_lfreq << " prev_rfreq: " << prev_rfreq
			<< " total: " << lfreq + rfreq << endl;
#endif

		if (lfreq + rfreq <= minFreq) {
			minFreq = lfreq + rfreq;
			opt_div = div;
		}

		//early divider termination
		if (lfreq - prev_lfreq > prev_rfreq) {
			div--;
			break;
		}

#ifdef DEBUG
		cout << " div_after: " << div << endl;
#endif

		prev_lfreq = lfreq;
		prev_rfreq = rfreq;
		totalTravel++;
	}
	
	divTravel[totalTravel]++;

#ifdef TEST
	cout << " opt_div: " << opt_div << " minFreq: " << minFreq << endl;
#endif

	return opt_div;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbp
movslq %ecx, %rdi
movq 0x380(%rbp), %rax
movq 0x398(%rbp), %rcx
movq -0x8(%rax,%rdi,8), %rax
movl %esi, (%rsp)
movslq %esi, %r15
leaq (%r15,%r15,4), %rsi
movq %rsi, 0x10(%rsp)
movl 0xc(%rax,%rsi,8), %r12d
movl %edx, 0x8(%rsp)
movl %edx, %eax
subl %r15d, %eax
cltq
movq (%rcx,%rax,8), %rax
movl 0x368(%rbp), %ecx
movq %rdi, 0x30(%rsp)
imull %edi, %ecx
addl %r15d, %ecx
movslq %ecx, %rcx
leaq (%rcx,%rcx,4), %rcx
movl 0xc(%rax,%rcx,8), %r14d
leal (%r14,%r12), %ebx
movq 0xb74a(%rip), %r13      # 0x10fc8
leaq 0x6a75(%rip), %rsi      # 0xc2fa
movl $0x5, %edx
movq %r13, %rdi
callq 0x32e0
movq %r13, %rdi
movl %r15d, %esi
callq 0x3450
movq %rax, %r13
leaq 0x6a17(%rip), %rsi      # 0xc2be
movl $0x8, %edx
movq %rax, %rdi
callq 0x32e0
movq %r13, %rdi
movl %r12d, %esi
callq 0x3450
movq %rax, %r13
leaq 0x69fe(%rip), %rsi      # 0xc2c7
movl $0x8, %edx
movq %rax, %rdi
callq 0x32e0
movq %r13, %rdi
movl %r14d, %esi
callq 0x3450
movq %rax, %r13
leaq 0x69e5(%rip), %rsi      # 0xc2d0
movl $0xd, %edx
movq %rax, %rdi
callq 0x32e0
movq %r13, %rdi
movq %r12, 0x18(%rsp)
movl %r12d, %esi
movl %ebx, %r12d
callq 0x3450
movq %rax, %r13
leaq 0x69c9(%rip), %rsi      # 0xc2de
movl $0xd, %edx
movq %rax, %rdi
callq 0x32e0
movq %r13, %rdi
movq %r14, 0x20(%rsp)
movl %r14d, %esi
callq 0x3450
movq %rax, %r13
leaq 0x69b0(%rip), %rsi      # 0xc2ec
movl $0x8, %edx
movq %rax, %rdi
callq 0x32e0
movq %r13, %rdi
movl %ebx, %esi
callq 0x3450
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq %r13, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31c0
xorl %edi, %edi
testl %r15d, %r15d
jle 0x5b88
movl %r12d, 0x4(%rsp)
movq 0x30(%rsp), %rdx
leaq -0x1(%rdx), %rax
movq %rax, 0x40(%rsp)
movslq 0x8(%rsp), %r14
subq %r15, %r14
decq %r15
incq %r14
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rbx
movq %rbp, 0x28(%rsp)
movq 0x380(%rbp), %rax
movq 0x40(%rsp), %r8
movq (%rax,%r8,8), %rax
movl -0x1c(%rax,%rcx,8), %r8d
testq %r15, %r15
je 0x59e9
cmpl %ebx, %r8d
jne 0x59e9
cmpl -0x44(%rax,%rcx,8), %ebx
jne 0x59e9
movl %ebx, %esi
movq 0x20(%rsp), %r13
jmp 0x5b64
movq %rdi, 0x38(%rsp)
movq %rcx, 0x10(%rsp)
movq 0x398(%rbp), %rax
movq (%rax,%r14,8), %rax
movslq 0x368(%rbp), %rcx
imulq %rdx, %rcx
addq %r15, %rcx
leaq (%rcx,%rcx,4), %rcx
movl 0xc(%rax,%rcx,8), %r13d
movl $0x5, %edx
movq 0xb5a7(%rip), %r12      # 0x10fc8
movq %r12, %rdi
movq %rbx, 0x18(%rsp)
leaq 0x68ca(%rip), %rsi      # 0xc2fa
movq %r8, 0x8(%rsp)
callq 0x32e0
movq %r12, %rdi
movl %r15d, %esi
callq 0x3450
movq %rax, %rbp
movl $0x8, %edx
movq %rax, %rdi
leaq 0x6867(%rip), %rsi      # 0xc2be
callq 0x32e0
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
callq 0x3450
movq %rax, %rbp
movl $0x8, %edx
movq %rax, %rdi
leaq 0x684c(%rip), %rsi      # 0xc2c7
callq 0x32e0
movq %rbp, %rdi
movl %r13d, %esi
callq 0x3450
movq %rax, %rbp
movl $0xd, %edx
movq %rax, %rdi
leaq 0x6833(%rip), %rsi      # 0xc2d0
callq 0x32e0
movq %rbp, %rdi
movl %ebx, %esi
callq 0x3450
movq %rax, %rbp
movl $0xd, %edx
movq %rax, %rdi
leaq 0x6820(%rip), %rsi      # 0xc2de
callq 0x32e0
movq %rbp, %rdi
movq 0x20(%rsp), %r12
movl %r12d, %esi
callq 0x3450
movq %rax, %rbx
movl $0x8, %edx
movq %rax, %rdi
leaq 0x6807(%rip), %rsi      # 0xc2ec
callq 0x32e0
movq 0x8(%rsp), %rax
leal (%rax,%r13), %ebp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x3450
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31c0
movq 0x8(%rsp), %rsi
movl 0x4(%rsp), %ecx
cmpl %ecx, %ebp
movl (%rsp), %eax
cmovlel %r15d, %eax
movl %eax, (%rsp)
cmovll %ebp, %ecx
movl %ecx, 0x4(%rsp)
movl %esi, %eax
subl 0x18(%rsp), %eax
cmpl %r12d, %eax
jg 0x5b8e
movq 0x38(%rsp), %rdi
incl %edi
movq 0x28(%rsp), %rbp
movq 0x30(%rsp), %rdx
movq 0x10(%rsp), %rcx
leaq -0x1(%r15), %rax
incq %r15
addq $-0x5, %rcx
incq %r14
movq %r13, 0x20(%rsp)
movl %esi, %ebx
cmpq $0x1, %r15
movq %rax, %r15
jg 0x59b8
jmp 0x5b98
movl (%rsp), %r14d
jmp 0x5ba4
movq 0x28(%rsp), %rbp
movq 0x38(%rsp), %rdi
movslq %edi, %rdi
movl (%rsp), %r14d
movl 0x4(%rsp), %r12d
movq 0x3e8(%rbp), %rax
incq (%rax,%rdi,8)
movq 0xb412(%rip), %rbx      # 0x10fc8
leaq 0x6738(%rip), %rsi      # 0xc2f5
movl $0xa, %edx
movq %rbx, %rdi
callq 0x32e0
movq %rbx, %rdi
movl %r14d, %esi
callq 0x3450
movq %rax, %rbx
leaq 0x6721(%rip), %rsi      # 0xc300
movl $0xa, %edx
movq %rax, %rdi
callq 0x32e0
movq %rbx, %rdi
movl %r12d, %esi
callq 0x3450
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31c0
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop