// File: 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:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movl %esi, 0x3c(%rsp)
movl %edx, 0x38(%rsp)
movl %ecx, 0x34(%rsp)
movq 0x40(%rsp), %rdi
movq %rdi, 0x10(%rsp)
addq $0x380, %rdi            # imm = 0x380
movl 0x34(%rsp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0xa600
movq 0x10(%rsp), %rdi
movq (%rax), %rax
movslq 0x3c(%rsp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, 0x30(%rsp)
addq $0x398, %rdi            # imm = 0x398
movl 0x38(%rsp), %eax
subl 0x3c(%rsp), %eax
movslq %eax, %rsi
callq 0xa600
movq 0x10(%rsp), %rsi
movq (%rax), %rax
movl 0x3c(%rsp), %ecx
movl 0x34(%rsp), %edx
imull 0x368(%rsi), %edx
addl %edx, %ecx
movslq %ecx, %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, 0x2c(%rsp)
movl 0x30(%rsp), %eax
addl 0x2c(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x30(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x20(%rsp)
movq 0x1a9d4(%rip), %rdi     # 0x23fc8
leaq 0x11cdf(%rip), %rsi     # 0x1b2da
callq 0x32e0
movq %rax, %rdi
movl 0x3c(%rsp), %esi
callq 0x35b0
movq %rax, %rdi
leaq 0x11c88(%rip), %rsi     # 0x1b29e
callq 0x32e0
movq %rax, %rdi
movl 0x30(%rsp), %esi
callq 0x35b0
movq %rax, %rdi
leaq 0x11c76(%rip), %rsi     # 0x1b2a7
callq 0x32e0
movq %rax, %rdi
movl 0x2c(%rsp), %esi
callq 0x35b0
movq %rax, %rdi
leaq 0x11c64(%rip), %rsi     # 0x1b2b0
callq 0x32e0
movq %rax, %rdi
movl 0x24(%rsp), %esi
callq 0x35b0
movq %rax, %rdi
leaq 0x11c57(%rip), %rsi     # 0x1b2be
callq 0x32e0
movq %rax, %rdi
movl 0x20(%rsp), %esi
callq 0x35b0
movq %rax, %rdi
leaq 0x11c4a(%rip), %rsi     # 0x1b2cc
callq 0x32e0
movq %rax, %rdi
movl 0x30(%rsp), %esi
addl 0x2c(%rsp), %esi
callq 0x35b0
movq %rax, %rdi
movq 0x1a917(%rip), %rsi     # 0x23fb8
callq 0x3330
movl $0x0, 0x18(%rsp)
movl 0x3c(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x1c(%rsp)
cmpl $0x0, 0x1c(%rsp)
jl 0x98b5
movq 0x10(%rsp), %rdi
addq $0x380, %rdi            # imm = 0x380
movl 0x34(%rsp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0xa600
movq (%rax), %rax
movslq 0x1c(%rsp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, 0x30(%rsp)
movl 0x30(%rsp), %eax
cmpl 0x24(%rsp), %eax
jne 0x974e
cmpl $0x0, 0x1c(%rsp)
jle 0x974e
movq 0x10(%rsp), %rdi
movl 0x30(%rsp), %eax
movl %eax, 0xc(%rsp)
addq $0x380, %rdi            # imm = 0x380
movl 0x34(%rsp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0xa600
movq %rax, %rcx
movl 0xc(%rsp), %eax
movq (%rcx), %rcx
movl 0x1c(%rsp), %edx
subl $0x1, %edx
movslq %edx, %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl 0xc(%rcx), %eax
jne 0x974e
jmp 0x98a5
movq 0x10(%rsp), %rdi
addq $0x398, %rdi            # imm = 0x398
movl 0x38(%rsp), %eax
subl 0x1c(%rsp), %eax
movslq %eax, %rsi
callq 0xa600
movq 0x10(%rsp), %rsi
movq (%rax), %rax
movl 0x1c(%rsp), %ecx
movl 0x34(%rsp), %edx
imull 0x368(%rsi), %edx
addl %edx, %ecx
movslq %ecx, %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, 0x2c(%rsp)
movq 0x1a82d(%rip), %rdi     # 0x23fc8
leaq 0x11b38(%rip), %rsi     # 0x1b2da
callq 0x32e0
movq %rax, %rdi
movl 0x1c(%rsp), %esi
callq 0x35b0
movq %rax, %rdi
leaq 0x11ae1(%rip), %rsi     # 0x1b29e
callq 0x32e0
movq %rax, %rdi
movl 0x30(%rsp), %esi
callq 0x35b0
movq %rax, %rdi
leaq 0x11acf(%rip), %rsi     # 0x1b2a7
callq 0x32e0
movq %rax, %rdi
movl 0x2c(%rsp), %esi
callq 0x35b0
movq %rax, %rdi
leaq 0x11abd(%rip), %rsi     # 0x1b2b0
callq 0x32e0
movq %rax, %rdi
movl 0x24(%rsp), %esi
callq 0x35b0
movq %rax, %rdi
leaq 0x11ab0(%rip), %rsi     # 0x1b2be
callq 0x32e0
movq %rax, %rdi
movl 0x20(%rsp), %esi
callq 0x35b0
movq %rax, %rdi
leaq 0x11aa3(%rip), %rsi     # 0x1b2cc
callq 0x32e0
movq %rax, %rdi
movl 0x30(%rsp), %esi
addl 0x2c(%rsp), %esi
callq 0x35b0
movq %rax, %rdi
movq 0x1a770(%rip), %rsi     # 0x23fb8
callq 0x3330
movl 0x30(%rsp), %eax
addl 0x2c(%rsp), %eax
cmpl 0x28(%rsp), %eax
jg 0x986f
movl 0x30(%rsp), %eax
addl 0x2c(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, 0x3c(%rsp)
movl 0x30(%rsp), %eax
subl 0x24(%rsp), %eax
cmpl 0x20(%rsp), %eax
jle 0x988a
movl 0x1c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x98b5
movl 0x30(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
movl 0x1c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x96b9
movq 0x10(%rsp), %rdi
addq $0x3e8, %rdi            # imm = 0x3E8
movslq 0x18(%rsp), %rsi
callq 0xa7a0
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
movq 0x1a6ec(%rip), %rdi     # 0x23fc8
leaq 0x119f2(%rip), %rsi     # 0x1b2d5
callq 0x32e0
movq %rax, %rdi
movl 0x3c(%rsp), %esi
callq 0x35b0
movq %rax, %rdi
leaq 0x119e2(%rip), %rsi     # 0x1b2e0
callq 0x32e0
movq %rax, %rdi
movl 0x28(%rsp), %esi
callq 0x35b0
movq %rax, %rdi
movq 0x1a69f(%rip), %rsi     # 0x23fb8
callq 0x3330
movl 0x3c(%rsp), %eax
addq $0x48, %rsp
retq
nopw (%rax,%rax)