// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rdi, 0x78(%rsp)
vxorpd %xmm0, %xmm0, %xmm0
vmovapd %xmm0, 0x50(%rsp)
movq $0x0, 0x60(%rsp)
movq 0x8(%rsi), %rax
movq %rsi, (%rsp)
cmpq (%rsi), %rax
movq %rdx, 0x10(%rsp)
je 0x3d20
vcvtsi2sd %rdx, %xmm1, %xmm0
vmovsd %xmm0, 0x18(%rsp)
xorl %r13d, %r13d
movl $0x1, %r14d
leaq 0x50(%rsp), %r12
leaq 0x20(%rsp), %rbx
xorl %ebp, %ebp
cmpq %rbp, %rdx
je 0x3d03
vcvtsi2sd %rbp, %xmm2, %xmm0
vmovsd 0x18(%rsp), %xmm1
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
movq %rax, %r15
negq %r15
cmovsq %rax, %r15
vmovsd 0x8(%rsp), %xmm0
callq 0x2370
movq 0x10(%rsp), %rsi
vroundsd $0xa, %xmm0, %xmm0, %xmm0
vcvttsd2si %xmm0, %rax
movl $0x1, %edx
subq %rax, %rdx
decq %rax
xorl %ecx, %ecx
cmpq %rsi, %rbp
cmovgq %rdx, %rax
setl %cl
addq %rax, %rcx
jle 0x3c8c
decb %cl
shlxq %rcx, %r14, %rdx
addq %rbp, %rdx
jmp 0x3ca4
movq %rbp, %rdx
jns 0x3ca4
negb %cl
movq $-0x1, %rdx
shlxq %rcx, %rdx, %rcx
leaq 0x1(%rbp,%rcx), %rdx
movq (%rsp), %rcx
movq (%rcx), %rcx
vmovsd (%rcx,%rsi,8), %xmm0
subq %rdx, %rsi
vcvtsi2sd %rsi, %xmm2, %xmm1
vsubsd (%rcx,%rbp,8), %xmm0, %xmm0
vdivsd %xmm0, %xmm1, %xmm0
vcmplesd 0x633c(%rip), %xmm0, %k1 # 0xa008
vmovsd 0x633a(%rip), %xmm0 {%k1} # 0xa010
vxorpd %xmm1, %xmm1, %xmm1
vmovapd %xmm1, 0x20(%rsp)
vmovsd %xmm0, 0x20(%rsp)
movq %rbp, 0x28(%rsp)
movq %rax, 0x30(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x4d78
addq %r15, %r13
movq 0x10(%rsp), %rdx
incq %rbp
movq (%rsp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
sarq $0x3, %rax
cmpq %rax, %rbp
jb 0x3bfa
jmp 0x3d23
xorl %r13d, %r13d
vcvtsi2sd %rdx, %xmm2, %xmm0
vxorpd %xmm1, %xmm1, %xmm1
vmovsd %xmm1, 0x48(%rsp)
xorl %eax, %eax
vmovsd %xmm0, 0x80(%rsp)
vmovsd %xmm0, 0x40(%rsp)
movq %r13, %r14
movq %r13, %rbp
testq %r13, %r13
je 0x3d63
movq %rbp, %rcx
subq %r14, %rcx
cmpq %rcx, %r13
setl %cl
testb %al, %cl
je 0x3d6b
jmp 0x3ee3
testb $0x1, %al
jne 0x3ee3
movq %r13, %rbx
movq 0x50(%rsp), %rax
vmovsd (%rax), %xmm0
vmovsd %xmm0, 0x8(%rsp)
movq 0x8(%rax), %r12
movq 0x10(%rax), %r13
leaq 0x50(%rsp), %rdi
callq 0x4e44
leaq 0x1(%r13), %rcx
leaq -0x1(%r13), %rax
movq 0x10(%rsp), %r8
cmpq %r8, %r12
movq %rax, %rdx
cmovlq %rcx, %rdx
xorl %esi, %esi
testq %rdx, %rdx
setg %sil
negq %rsi
movq %rax, %r15
sarq $0x3f, %r15
cmpq %r8, %r12
movq 0x50(%rsp), %rdi
vmovsd (%rdi), %xmm0
cmovlq %rsi, %r15
movq %rdx, %rsi
negq %rsi
cmovnsq %rdx, %rsi
movq %r13, %rdx
negq %rdx
cmovsq %r13, %rdx
addq %rdx, %rbp
addq %rsi, %rbp
vmovsd %xmm0, 0x18(%rsp)
vmovsd 0x8(%rsp), %xmm2
vucomisd %xmm2, %xmm0
jne 0x3df7
jnp 0x3e2a
cmpq %rbx, %rbp
jge 0x3e2a
vaddsd 0x6264(%rip), %xmm2, %xmm0 # 0xa068
movq (%rsp), %rdx
movq (%rdx), %rdx
vmovsd (%rdx,%r8,8), %xmm1
vmovsd %xmm0, 0x48(%rsp)
vfnmadd213sd 0x80(%rsp), %xmm0, %xmm1 # xmm1 = -(xmm0 * xmm1) + mem
vmovsd %xmm1, 0x40(%rsp)
movq %rbp, %rbx
cmpq %r8, %r12
cmovgeq %rcx, %rax
cmovgeq %rcx, %r13
testq %r13, %r13
jle 0x3e4c
decb %r13b
movl $0x1, %ecx
shlxq %r13, %rcx, %rcx
addq %r12, %rcx
jmp 0x3e65
movq %r12, %rcx
jns 0x3e65
negb %r13b
movq $-0x1, %rcx
shlxq %r13, %rcx, %rcx
leaq 0x1(%r12,%rcx), %rcx
movq (%rsp), %rdx
movq (%rdx), %rdx
vmovsd (%rdx,%r8,8), %xmm0
movq %r8, %rsi
subq %rcx, %rsi
vcvtsi2sd %rsi, %xmm3, %xmm1
vsubsd (%rdx,%r12,8), %xmm0, %xmm0
vdivsd %xmm0, %xmm1, %xmm0
vcmplesd 0x6176(%rip), %xmm0, %k1 # 0xa008
vmovsd 0x6174(%rip), %xmm0 {%k1} # 0xa010
vxorpd %xmm1, %xmm1, %xmm1
vmovapd %xmm1, 0x20(%rsp)
vmovsd %xmm0, 0x20(%rsp)
movq %r12, 0x28(%rsp)
movq %rax, 0x30(%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x4d78
vmovsd 0x18(%rsp), %xmm0
vcmpneqsd 0x8(%rsp), %xmm0, %k0
kmovd %k0, %eax
addq %r15, %r14
movq %rbx, %r13
jmp 0x3d49
movq 0x78(%rsp), %rbx
vmovsd 0x48(%rsp), %xmm0
vmovsd %xmm0, (%rbx)
vmovsd 0x40(%rsp), %xmm0
vmovsd %xmm0, 0x8(%rbx)
movq %r13, 0x10(%rbx)
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x3f18
movq 0x60(%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 0x3f31
jmp 0x3f31
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x3f4b
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movq %rbx, %rdi
callq 0x2360