// File: /workspace/github_cpp_makefile_star2_O3/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
vxorpd %xmm0, %xmm0, %xmm0
movq $0x0, 0x40(%rsp)
movq %rsi, %r13
movq %rdi, 0x70(%rsp)
movq %rdx, 0x18(%rsp)
vmovapd %xmm0, 0x30(%rsp)
vcvtsi2sd %rdx, %xmm1, %xmm0
movq (%rsi), %rax
movq 0x8(%rsi), %rcx
vmovsd %xmm0, 0x10(%rsp)
cmpq %rax, %rcx
je 0x3cc9
leaq 0x30(%rsp), %r12
leaq 0x58(%rsp), %rbx
xorl %esi, %esi
xorl %ebp, %ebp
cmpq %rbp, %rdx
je 0x3cb1
vmovsd 0x10(%rsp), %xmm1
vcvtsi2sd %rbp, %xmm2, %xmm0
movq %rsi, %r14
vsubsd %xmm0, %xmm1, %xmm0
vcvttsd2si %xmm0, %rax
movq %rax, %rcx
negq %rcx
cmovsq %rax, %rcx
incq %rcx
vcvtsi2sd %rcx, %xmm2, %xmm0
vmovsd %xmm0, 0x8(%rsp)
callq 0x2370
vroundsd $0xa, %xmm0, %xmm0, %xmm0
vcvttsd2si %xmm0, %rax
vmovsd 0x8(%rsp), %xmm0
movq %rax, %r15
negq %r15
cmovsq %rax, %r15
callq 0x2370
movq 0x18(%rsp), %rsi
vroundsd $0xa, %xmm0, %xmm0, %xmm0
movl $0x1, %edx
xorl %ecx, %ecx
vcvttsd2si %xmm0, %rax
subq %rax, %rdx
decq %rax
cmpq %rsi, %rbp
cmovgq %rdx, %rax
setl %cl
addq %rax, %rcx
jle 0x3c3c
decb %cl
movl $0x1, %edx
shlxq %rcx, %rdx, %rdx
addq %rbp, %rdx
jmp 0x3c54
movq %rbp, %rdx
jns 0x3c54
negb %cl
movq $-0x1, %rdx
shlxq %rcx, %rdx, %rcx
leaq 0x1(%rbp,%rcx), %rdx
movq (%r13), %rcx
vmovsd (%rcx,%rsi,8), %xmm0
subq %rdx, %rsi
vcvtsi2sd %rsi, %xmm2, %xmm1
vsubsd (%rcx,%rbp,8), %xmm0, %xmm0
vdivsd %xmm0, %xmm1, %xmm0
vcmplesd 0x638f(%rip), %xmm0, %k1 # 0xa008
vmovsd 0x638d(%rip), %xmm0 {%k1} # 0xa010
vmovsd %xmm0, 0x58(%rsp)
movq %rbp, 0x60(%rsp)
movq %rax, 0x68(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x4d22
movq (%r13), %rax
movq 0x8(%r13), %rcx
movq 0x18(%rsp), %rdx
addq %r15, %r14
movq %r14, %rsi
movq %rcx, %rdi
subq %rax, %rdi
incq %rbp
sarq $0x3, %rdi
cmpq %rdi, %rbp
jb 0x3ba2
jmp 0x3ccb
xorl %esi, %esi
vmovsd 0x10(%rsp), %xmm1
vxorpd %xmm0, %xmm0, %xmm0
xorl %eax, %eax
movq %rsi, %rdx
movq %rsi, %r14
vmovsd %xmm0, 0x28(%rsp)
vmovsd %xmm1, 0x20(%rsp)
testq %rsi, %rsi
je 0x3d03
movq %r14, %rcx
subq %rdx, %rcx
cmpq %rcx, %rsi
setl %cl
testb %al, %cl
je 0x3d0b
jmp 0x3e81
testb $0x1, %al
jne 0x3e81
movq 0x30(%rsp), %rax
movq %r13, %rbp
movq %rdx, 0x80(%rsp)
movq %rsi, %rbx
vmovsd (%rax), %xmm0
movq 0x8(%rax), %r15
movq 0x10(%rax), %r13
vmovsd %xmm0, 0x8(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x4dec
movq 0x30(%rsp), %rax
movq 0x18(%rsp), %rdi
vmovsd 0x8(%rsp), %xmm2
xorl %edx, %edx
xorl %r12d, %r12d
testq %r13, %r13
leaq 0x1(%r13), %rcx
setns %dl
setle %r12b
vmovsd (%rax), %xmm0
leaq -0x1(%r13), %rax
cmpq %rdi, %r15
movq %rax, %rsi
cmovlq %rcx, %rsi
cmovll %edx, %r12d
movq %rsi, %rdx
negq %rdx
cmovnsq %rsi, %rdx
movq %r13, %rsi
negq %rsi
cmovsq %r13, %rsi
addq %rsi, %r14
addq %rdx, %r14
vucomisd %xmm2, %xmm0
vmovsd %xmm0, 0x78(%rsp)
jne 0x3d9b
jnp 0x3dc7
cmpq %rbx, %r14
jge 0x3dc7
movq (%rbp), %rdx
vaddsd 0x62bc(%rip), %xmm2, %xmm0 # 0xa068
movq %r14, %rbx
vmovsd (%rdx,%rdi,8), %xmm1
vfnmadd213sd 0x10(%rsp), %xmm0, %xmm1 # xmm1 = -(xmm0 * xmm1) + mem
vmovsd %xmm0, 0x28(%rsp)
vmovsd %xmm1, 0x20(%rsp)
cmpq %rdi, %r15
cmovgeq %rcx, %rax
cmovgeq %rcx, %r13
testq %r13, %r13
jle 0x3de9
decb %r13b
movl $0x1, %ecx
shlxq %r13, %rcx, %rcx
addq %r15, %rcx
jmp 0x3e02
movq %r15, %rcx
jns 0x3e02
negb %r13b
movq $-0x1, %rcx
shlxq %r13, %rcx, %rcx
leaq 0x1(%r15,%rcx), %rcx
movq (%rbp), %rdx
movq %rdi, %rsi
subq %rcx, %rsi
movq %rbp, %r13
vmovsd (%rdx,%rdi,8), %xmm0
vcvtsi2sd %rsi, %xmm3, %xmm1
vsubsd (%rdx,%r15,8), %xmm0, %xmm0
vdivsd %xmm0, %xmm1, %xmm0
vcmplesd 0x61da(%rip), %xmm0, %k1 # 0xa008
vmovsd 0x61d8(%rip), %xmm0 {%k1} # 0xa010
vmovsd %xmm0, 0x58(%rsp)
movq %r15, 0x60(%rsp)
movq %rax, 0x68(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x4d22
vmovsd 0x78(%rsp), %xmm0
movq 0x80(%rsp), %rdx
movzbl %r12b, %ecx
movq %rbx, %rsi
vcmpneqsd 0x8(%rsp), %xmm0, %k0
subq %rcx, %rdx
kmovd %k0, %eax
jmp 0x3ce9
vmovsd 0x28(%rsp), %xmm0
vmovsd 0x20(%rsp), %xmm1
movq 0x70(%rsp), %rbx
movq 0x30(%rsp), %rdi
vmovsd %xmm0, (%rbx)
vmovsd %xmm1, 0x8(%rbx)
movq %rsi, 0x10(%rbx)
testq %rdi, %rdi
je 0x3eb6
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3ecf
jmp 0x3ecf
movq 0x30(%rsp), %rdi
movq %rax, %rbx
testq %rdi, %rdi
je 0x3ee9
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movq %rbx, %rdi
callq 0x2360