// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movl %edx, 0xc(%rsp)
movq %rdi, %r14
movslq %ecx, %rdi
leaq -0x1(%rdi), %rbp
movq 0x380(%r14), %rax
movq 0x398(%r14), %rcx
movq -0x8(%rax,%rdi,8), %rax
movl %esi, 0x14(%rsp)
movslq %esi, %r15
imulq $0x28, %r15, %rsi
movl 0xc(%rax,%rsi), %r13d
movl %edx, %eax
subl %r15d, %eax
cltq
movq (%rcx,%rax,8), %rax
movl 0x368(%r14), %ecx
movq %rdi, 0x28(%rsp)
imull %edi, %ecx
addl %r15d, %ecx
movslq %ecx, %rcx
imulq $0x28, %rcx, %rcx
movl 0xc(%rax,%rcx), %ebx
leal (%rbx,%r13), %r12d
movq 0xbe3c(%rip), %rdi      # 0x10fc8
leaq 0x6147(%rip), %rsi      # 0xb2da
callq 0x3240
movq %rax, %rdi
movl %r15d, %esi
callq 0x3440
leaq 0x60f4(%rip), %rsi      # 0xb29e
movq %rax, %rdi
callq 0x3240
movq %rax, %rdi
movl %r13d, %esi
callq 0x3440
leaq 0x60e3(%rip), %rsi      # 0xb2a7
movq %rax, %rdi
callq 0x3240
movq %rax, %rdi
movl %ebx, %esi
callq 0x3440
leaq 0x60d3(%rip), %rsi      # 0xb2b0
movq %rax, %rdi
callq 0x3240
movq %rax, %rdi
movl %r13d, %esi
callq 0x3440
leaq 0x60c7(%rip), %rsi      # 0xb2be
movq %rax, %rdi
callq 0x3240
movq %rax, %rdi
movl %ebx, %esi
callq 0x3440
leaq 0x60bc(%rip), %rsi      # 0xb2cc
movq %rax, %rdi
callq 0x3240
movq %rax, %rdi
movl %r12d, 0x10(%rsp)
movl %r12d, %esi
callq 0x3440
movq %rax, %rdi
callq 0x3120
movslq 0xc(%rsp), %r11
incq %r11
xorl %r10d, %r10d
movq %r14, 0x18(%rsp)
movq %r11, 0x20(%rsp)
movq 0x380(%r14), %rax
leaq (%r15,%r15,4), %rcx
movq %r11, %rsi
subq %r15, %rsi
movq %r15, %r8
testq %r8, %r8
jle 0x53b9
movq %r8, %r15
movq %rsi, %rdx
movq (%rax,%rbp,8), %rdi
movl -0x1c(%rdi,%rcx,8), %r12d
cmpq $0x1, %r8
je 0x5292
cmpl %r13d, %r12d
jne 0x5292
leaq -0x5(%rcx), %r9
leaq 0x1(%rdx), %rsi
leaq -0x1(%r15), %r8
cmpl -0x44(%rdi,%rcx,8), %r13d
movq %r9, %rcx
je 0x5259
movl %r10d, 0xc(%rsp)
movq 0x398(%r14), %rax
movq (%rax,%rdx,8), %rax
movslq 0x368(%r14), %rcx
imulq 0x28(%rsp), %rcx
addq %r15, %rcx
imulq $0x28, %rcx, %rcx
movl -0x1c(%rcx,%rax), %r14d
movq 0xbd06(%rip), %rdi      # 0x10fc8
leaq 0x6011(%rip), %rsi      # 0xb2da
callq 0x3240
decq %r15
movq %rax, %rdi
movl %r15d, %esi
callq 0x3440
movq %rax, %rdi
leaq 0x5fb8(%rip), %rsi      # 0xb29e
callq 0x3240
movq %rax, %rdi
movl %r12d, %esi
callq 0x3440
movq %rax, %rdi
leaq 0x5fa7(%rip), %rsi      # 0xb2a7
callq 0x3240
movq %rax, %rdi
movl %r14d, %esi
callq 0x3440
movq %rax, %rdi
leaq 0x5f96(%rip), %rsi      # 0xb2b0
callq 0x3240
movq %rax, %rdi
movl %r13d, %esi
callq 0x3440
movq %rax, %rdi
leaq 0x5f8a(%rip), %rsi      # 0xb2be
callq 0x3240
movq %rax, %rdi
movl %ebx, %esi
callq 0x3440
movq %rax, %rdi
leaq 0x5f7f(%rip), %rsi      # 0xb2cc
callq 0x3240
movq %rbx, 0x30(%rsp)
leal (%r14,%r12), %ebx
movq %rax, %rdi
movl %ebx, %esi
callq 0x3440
movq %rax, %rdi
callq 0x3120
movl 0x10(%rsp), %ecx
cmpl %ecx, %ebx
movl 0x14(%rsp), %eax
cmovlel %r15d, %eax
movl %eax, 0x14(%rsp)
cmovll %ebx, %ecx
movl %ecx, 0x10(%rsp)
movl %r12d, %eax
subl %r13d, %eax
cmpl 0x30(%rsp), %eax
jg 0x53af
movl 0xc(%rsp), %r10d
incl %r10d
movl %r12d, %r13d
movl %r14d, %ebx
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %r11
jmp 0x5245
movq 0x18(%rsp), %r14
movl 0xc(%rsp), %r10d
movl %r10d, %eax
movq 0x3e8(%r14), %rcx
incq (%rcx,%rax,8)
movq 0xbbfa(%rip), %rdi      # 0x10fc8
leaq 0x5f00(%rip), %rsi      # 0xb2d5
callq 0x3240
movq %rax, %rdi
movl 0x14(%rsp), %ebx
movl %ebx, %esi
callq 0x3440
leaq 0x5ef1(%rip), %rsi      # 0xb2e0
movq %rax, %rdi
callq 0x3240
movq %rax, %rdi
movl 0x10(%rsp), %esi
callq 0x3440
movq %rax, %rdi
callq 0x3120
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq