// File: /workspace/github_cpp_makefile_star2_O2/umatin[P]LogarithmicErrorRegression/./src/fast_brute_force.h
// Function: build_regression_i(std::vector<double, std::allocator<double>>&, long)
std::pair<linear_regression, long> build_regression_i(std::vector<double>& data, long index){
    std::priority_queue<q_item, std::vector<q_item>, std::greater<q_item>> pq;
    double epsilon = std::numeric_limits<double>::epsilon();//0.00000000000000001;

    long cost = 0;
    
    //Initalize cost and priority queue
    //with a = 0, b = index
    for(long i = 0; i < data.size(); i++){
        if (i != index){
            long dist = std::abs(index - i);

            //Initialize cost
            cost += std::abs(error_fn(i, index));

            //Insert queue item
            long error = index < i ? -(error_fn(i, index)-1) : error_fn(i, index)-1;
            long target = comp_target(i, error, index);
            linear_regression lr = fit_line(target, index, data[i], data[index]);
            pq.push(q_item(lr.a, i, error));
        }
    }

    linear_regression lr_opt(0, index);
    
    //Potential function measures the maximal possible reduction in cost 
    long potential = cost;
    long min_cost = cost;

    //We need to have all changes applied in order for the cost to be accurate
    bool valid = false;
    while((min_cost != 0 && min_cost >= cost - potential) || !valid){
        q_item element = pq.top();
        pq.pop();
        long i = element.index;

        valid = pq.top().priority != element.priority;

        long last_val =  i < index ? element.error+1 : element.error-1;
        cost -= std::abs(last_val);
        cost += std::abs(element.error);
        
        //Update potential
        if (i < index){
            if (last_val >= 1){
                potential -= 1;
            }
        }
        else{
            if (last_val <= -1){
                potential -= 1; 
            }
        }

        if (valid && cost < min_cost){
            //Update minimum
            double temp_a = element.priority + epsilon;
            double temp_b = index - data[index] * temp_a;
            min_cost = cost;
            lr_opt = linear_regression(temp_a, temp_b);
        }

        long new_error = i < index ? element.error-1 : element.error+1;
        long target = comp_target(i, new_error, index);
        linear_regression lr = fit_line(target, index, data[i], data[index]);
        pq.push(q_item(lr.a, i, new_error));
    }

    return std::pair<linear_regression, long>(lr_opt, min_cost);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
leaq 0x60(%rsp), %rbp
vxorpd %xmm0, %xmm0, %xmm0
movq %rdx, %r14
movq %rsi, 0x8(%rsp)
movq %rdi, 0x48(%rsp)
xorl %r13d, %r13d
xorl %r15d, %r15d
andq $0x0, 0x10(%rbp)
vmovapd %xmm0, (%rbp)
vcvtsi2sd %rdx, %xmm1, %xmm0
vmovsd %xmm0, (%rsp)
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
sarq $0x3, %rax
cmpq %rax, %r13
jae 0x3729
cmpq %r13, %r14
je 0x3721
vmovsd (%rsp), %xmm0
movq %r13, %rdi
callq 0x4293
movq %rax, %r12
negq %r12
cmovsq %rax, %r12
cmpq %r14, %r13
jle 0x36c1
vmovsd (%rsp), %xmm0
movq %r13, %rdi
callq 0x4293
pushq $0x1
popq %rbx
subq %rax, %rbx
jmp 0x36d4
vmovsd (%rsp), %xmm0
movq %r13, %rdi
callq 0x4293
movq %rax, %rbx
decq %rbx
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x42c7
movq 0x8(%rsp), %rcx
movq %rax, %rdi
movq %r14, %rsi
movq (%rcx), %rcx
vmovsd (%rcx,%r13,8), %xmm0
vmovsd (%rcx,%r14,8), %xmm1
callq 0x25b5
vmovsd %xmm0, 0x30(%rsp)
movq %r13, 0x38(%rsp)
movq %rbx, 0x40(%rsp)
leaq 0x30(%rsp), %rsi
movq %rbp, %rdi
callq 0x42f8
addq %r12, %r15
incq %r13
jmp 0x366e
vmovsd (%rsp), %xmm1
vxorpd %xmm0, %xmm0, %xmm0
leaq 0x60(%rsp), %rbx
xorl %eax, %eax
movq %r15, %rdx
movq %r15, 0x10(%rsp)
vmovsd %xmm0, 0x20(%rsp)
vmovsd %xmm1, 0x18(%rsp)
testq %r15, %r15
je 0x3769
movq 0x10(%rsp), %rcx
subq %rdx, %rcx
cmpq %rcx, %r15
setl %cl
testb %al, %cl
je 0x3771
jmp 0x38a9
testb $0x1, %al
jne 0x38a9
movq 0x60(%rsp), %rax
movq %rdx, 0x58(%rsp)
vmovsd (%rax), %xmm0
movq 0x8(%rax), %rbp
movq 0x10(%rax), %r12
vmovsd %xmm0, 0x28(%rsp)
movq %rbx, %rdi
callq 0x430e
movq 0x60(%rsp), %rax
vmovsd 0x28(%rsp), %xmm2
xorl %ecx, %ecx
xorl %r13d, %r13d
testq %r12, %r12
leaq 0x1(%r12), %rbx
movq 0x10(%rsp), %rsi
setns %cl
setle %r13b
cmpq %r14, %rbp
cmovll %ecx, %r13d
vmovsd (%rax), %xmm0
leaq -0x1(%r12), %rax
movq %rax, %rdx
cmovlq %rbx, %rdx
movq %rdx, %rcx
negq %rcx
cmovnsq %rdx, %rcx
movq %r12, %rdx
negq %rdx
cmovsq %r12, %rdx
addq %rdx, %rsi
movq %rsi, %rdx
addq %rcx, %rdx
vucomisd %xmm2, %xmm0
vmovsd %xmm0, 0x50(%rsp)
jne 0x37fb
jnp 0x382b
cmpq %r15, %rdx
jge 0x382b
movq 0x8(%rsp), %rcx
vaddsd 0x583b(%rip), %xmm2, %xmm0 # 0x9048
movq %rdx, %r15
movq (%rcx), %rcx
vmovsd (%rcx,%r14,8), %xmm1
vmovsd %xmm0, 0x20(%rsp)
vfnmadd213sd (%rsp), %xmm0, %xmm1 # xmm1 = -(xmm0 * xmm1) + mem
vmovsd %xmm1, 0x18(%rsp)
cmpq %r14, %rbp
movq %rdx, 0x10(%rsp)
cmovlq %rax, %rbx
movq %rbp, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x42c7
movq 0x8(%rsp), %rcx
movq %rax, %rdi
movq %r14, %rsi
movq (%rcx), %rcx
vmovsd (%rcx,%rbp,8), %xmm0
vmovsd (%rcx,%r14,8), %xmm1
callq 0x25b5
vmovsd %xmm0, 0x30(%rsp)
movq %rbp, 0x38(%rsp)
movq %rbx, 0x40(%rsp)
leaq 0x60(%rsp), %rbx
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x42f8
vmovsd 0x50(%rsp), %xmm0
movq 0x58(%rsp), %rdx
movzbl %r13b, %ecx
vcmpneqsd 0x28(%rsp), %xmm0, %k0
subq %rcx, %rdx
kmovd %k0, %eax
jmp 0x374d
vmovsd 0x20(%rsp), %xmm0
vmovsd 0x18(%rsp), %xmm1
movq 0x48(%rsp), %rbx
leaq 0x60(%rsp), %rdi
vmovsd %xmm0, (%rbx)
vmovsd %xmm1, 0x8(%rbx)
movq %r15, 0x10(%rbx)
callq 0x5f62
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x38f2
jmp 0x38f2
jmp 0x38f2
jmp 0x38f2
jmp 0x38f2
jmp 0x38f2
leaq 0x60(%rsp), %rdi
movq %rax, %rbx
callq 0x5f62
movq %rbx, %rdi
callq 0x2340