// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movslq %ecx, %r13
movq 0x380(%rdi), %rax
movq 0x398(%rdi), %rcx
movq -0x8(%rax,%r13,8), %rax
movslq %esi, %r12
leaq (%r12,%r12,4), %r8
movl 0xc(%rax,%r8,8), %r15d
movl %edx, 0x10(%rsp)
movl %edx, %eax
subl %r12d, %eax
cltq
movq (%rcx,%rax,8), %rax
movq %rdi, 0x18(%rsp)
movl 0x368(%rdi), %ecx
imull %r13d, %ecx
movl %esi, 0xc(%rsp)
addl %esi, %ecx
movslq %ecx, %rcx
leaq (%rcx,%rcx,4), %rcx
movl 0xc(%rax,%rcx,8), %ebp
leal (%r15,%rbp), %r14d
movq 0xb816(%rip), %rbx      # 0x10fc8
leaq 0x6b41(%rip), %rsi      # 0xc2fa
movl $0x5, %edx
movq %rbx, %rdi
callq 0x32e0
movq %rbx, %rdi
movl %r12d, %esi
callq 0x3450
movq %rax, %rbx
leaq 0x6ae3(%rip), %rsi      # 0xc2be
movl $0x8, %edx
movq %rax, %rdi
callq 0x32e0
movq %rbx, %rdi
movl %r15d, %esi
callq 0x3450
movq %rax, %rbx
leaq 0x6aca(%rip), %rsi      # 0xc2c7
movl $0x8, %edx
movq %rax, %rdi
callq 0x32e0
movq %rbx, %rdi
movl %ebp, %esi
callq 0x3450
movq %rax, %rbx
leaq 0x6ab2(%rip), %rsi      # 0xc2d0
movl $0xd, %edx
movq %rax, %rdi
callq 0x32e0
movq %rbx, %rdi
movq %r15, 0x28(%rsp)
movl %r15d, %esi
movl %r14d, %r15d
callq 0x3450
movq %rax, %rbx
leaq 0x6a96(%rip), %rsi      # 0xc2de
movl $0xd, %edx
movq %rax, %rdi
callq 0x32e0
movq %rbx, %rdi
movq %rbp, 0x20(%rsp)
movl %ebp, %esi
callq 0x3450
movq %rax, %rbx
leaq 0x6a7e(%rip), %rsi      # 0xc2ec
movl $0x8, %edx
movq %rax, %rdi
callq 0x32e0
movq %rbx, %rdi
movl %r14d, %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
xorl %edx, %edx
testl %r12d, %r12d
jle 0x5ac9
movl %r15d, 0x14(%rsp)
movq %r13, 0x38(%rsp)
leaq -0x1(%r13), %rax
movq %rax, 0x40(%rsp)
movl 0xc(%rsp), %eax
movslq 0x10(%rsp), %r8
leaq 0x1(%rax), %rbx
leaq (,%rax,8), %rcx
leaq (%rcx,%rcx,4), %r13
subq %rax, %r8
incq %r8
movq 0x18(%rsp), %rcx
movq 0x28(%rsp), %r14
movq 0x380(%rcx), %rax
movq 0x40(%rsp), %rdi
movq (%rax,%rdi,8), %rax
movl -0x1c(%rax,%r13), %r15d
cmpq $0x2, %rbx
je 0x592e
cmpl %r14d, %r15d
jne 0x592e
cmpl -0x44(%rax,%r13), %r15d
jne 0x592e
movl %r14d, %esi
movq 0x20(%rsp), %r14
movl %r14d, %r12d
jmp 0x5aa8
movq %rdx, 0x30(%rsp)
movq 0x398(%rcx), %rax
movq %r8, 0x48(%rsp)
movq (%rax,%r8,8), %rax
movslq 0x368(%rcx), %rcx
imulq 0x38(%rsp), %rcx
addq %rbx, %rcx
leaq (%rcx,%rcx,4), %rcx
movl -0x44(%rax,%rcx,8), %eax
movq %rax, 0x50(%rsp)
movl $0x5, %edx
movq 0xb65c(%rip), %r12      # 0x10fc8
movq %r12, %rdi
movq %r14, 0x28(%rsp)
leaq 0x697f(%rip), %rsi      # 0xc2fa
callq 0x32e0
leal -0x2(%rbx), %esi
movl %esi, 0x10(%rsp)
movq %r12, %rdi
callq 0x3450
movq %rax, %rbp
movl $0x8, %edx
movq %rax, %rdi
leaq 0x691d(%rip), %rsi      # 0xc2be
callq 0x32e0
movq %rbp, %rdi
movl %r15d, %esi
callq 0x3450
movq %rax, %rbp
movl $0x8, %edx
movq %rax, %rdi
leaq 0x6904(%rip), %rsi      # 0xc2c7
callq 0x32e0
movq %rbp, %rdi
movq 0x50(%rsp), %r12
movl %r12d, %esi
callq 0x3450
movq %rax, %rbp
movl $0xd, %edx
movq %rax, %rdi
leaq 0x68e6(%rip), %rsi      # 0xc2d0
callq 0x32e0
movq %rbp, %rdi
movl %r14d, %esi
callq 0x3450
movq %rax, %rbp
movl $0xd, %edx
movq %rax, %rdi
leaq 0x68d2(%rip), %rsi      # 0xc2de
callq 0x32e0
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
callq 0x3450
movq %rax, %r14
movl $0x8, %edx
movq %rax, %rdi
leaq 0x68bc(%rip), %rsi      # 0xc2ec
callq 0x32e0
leal (%r12,%r15), %ebp
movq %r14, %rdi
movl %ebp, %esi
callq 0x3450
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31c0
movl 0x14(%rsp), %eax
cmpl %eax, %ebp
movl 0xc(%rsp), %ecx
cmovlel 0x10(%rsp), %ecx
movl %ecx, 0xc(%rsp)
cmovll %ebp, %eax
movl %eax, 0x14(%rsp)
movl %r15d, %eax
subl 0x28(%rsp), %eax
cmpl 0x20(%rsp), %eax
jg 0x5ad0
movq %r15, %rsi
movq 0x30(%rsp), %rdx
incl %edx
movq 0x18(%rsp), %rcx
movq 0x48(%rsp), %r8
decq %rbx
addq $-0x28, %r13
incq %r8
movl %r12d, %r14d
movq %r14, 0x20(%rsp)
movl %esi, %r14d
cmpq $0x1, %rbx
jg 0x58f7
jmp 0x5ada
movq 0x18(%rsp), %rcx
jmp 0x5ae2
movq 0x18(%rsp), %rcx
movq 0x30(%rsp), %rdx
movslq %edx, %rdx
movl 0x14(%rsp), %r15d
movq 0x3e8(%rcx), %rax
incq (%rax,%rdx,8)
movq 0xb4d4(%rip), %rbx      # 0x10fc8
leaq 0x67fa(%rip), %rsi      # 0xc2f5
movl $0xa, %edx
movq %rbx, %rdi
callq 0x32e0
movq %rbx, %rdi
movl 0xc(%rsp), %ebp
movl %ebp, %esi
callq 0x3450
movq %rax, %rbx
leaq 0x67e0(%rip), %rsi      # 0xc300
movl $0xa, %edx
movq %rax, %rdi
callq 0x32e0
movq %rbx, %rdi
movl %r15d, %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 %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop