// File: 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:
subq $0x228, %rsp            # imm = 0x228
movq %rdi, 0xd8(%rsp)
movq %rdi, 0xe0(%rsp)
movq %rsi, 0x220(%rsp)
movq %rdx, 0x218(%rsp)
leaq 0x1f8(%rsp), %rdi
callq 0x7070
callq 0x70a0
vmovsd %xmm0, 0x1f0(%rsp)
movq $0x0, 0x1e8(%rsp)
movq $0x0, 0x1e0(%rsp)
movq 0x1e0(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq 0x220(%rsp), %rdi
callq 0x6400
movq %rax, %rcx
movq 0xd0(%rsp), %rax
cmpq %rcx, %rax
jae 0x51f4
movq 0x1e0(%rsp), %rax
cmpq 0x218(%rsp), %rax
je 0x51d9
movq 0x218(%rsp), %rdi
movq 0x1e0(%rsp), %rax
subq %rax, %rdi
callq 0x70b0
movq %rax, 0xc8(%rsp)
jmp 0x4fa3
movq 0xc8(%rsp), %rax
movq %rax, 0x1d8(%rsp)
movq 0x1e0(%rsp), %rdi
vcvtsi2sdq 0x218(%rsp), %xmm0, %xmm0
callq 0x70d0
movq %rax, 0xc0(%rsp)
jmp 0x4fd4
movq 0xc0(%rsp), %rdi
callq 0x70b0
movq %rax, 0xb8(%rsp)
jmp 0x4feb
movq 0xb8(%rsp), %rax
addq 0x1e8(%rsp), %rax
movq %rax, 0x1e8(%rsp)
movq 0x218(%rsp), %rax
cmpq 0x1e0(%rsp), %rax
jge 0x5051
movq 0x1e0(%rsp), %rdi
vcvtsi2sdq 0x218(%rsp), %xmm0, %xmm0
callq 0x70d0
movq %rax, 0xb0(%rsp)
jmp 0x5036
movq 0xb0(%rsp), %rcx
subq $0x1, %rcx
xorl %eax, %eax
subq %rcx, %rax
movq %rax, 0xa8(%rsp)
jmp 0x5086
movq 0x1e0(%rsp), %rdi
vcvtsi2sdq 0x218(%rsp), %xmm0, %xmm0
callq 0x70d0
movq %rax, 0xa0(%rsp)
jmp 0x5072
movq 0xa0(%rsp), %rax
subq $0x1, %rax
movq %rax, 0xa8(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0x1c0(%rsp)
movq 0x1e0(%rsp), %rdi
movq 0x1c0(%rsp), %rsi
movq 0x218(%rsp), %rdx
callq 0x7130
movq %rax, 0x98(%rsp)
jmp 0x50bd
movq 0x98(%rsp), %rax
movq %rax, 0x1b8(%rsp)
movq 0x1b8(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x218(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x220(%rsp), %rdi
movq 0x1e0(%rsp), %rsi
callq 0x6390
vmovsd (%rax), %xmm0
vmovsd %xmm0, 0x80(%rsp)
movq 0x220(%rsp), %rdi
movq 0x218(%rsp), %rsi
callq 0x6390
movq 0x70(%rsp), %rdi
movq 0x78(%rsp), %rsi
vmovsd 0x80(%rsp), %xmm0
vmovsd (%rax), %xmm1
callq 0x2930
vmovsd %xmm1, 0x88(%rsp)
vmovsd %xmm0, 0x90(%rsp)
jmp 0x514e
vmovsd 0x88(%rsp), %xmm0
vmovsd 0x90(%rsp), %xmm1
vmovsd %xmm1, 0x1a8(%rsp)
vmovsd %xmm0, 0x1b0(%rsp)
vmovsd 0x1a8(%rsp), %xmm0
movq 0x1e0(%rsp), %rsi
movq 0x1c0(%rsp), %rdx
leaq 0x190(%rsp), %rdi
callq 0x7220
jmp 0x519a
leaq 0x1f8(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0x71c0
jmp 0x51b1
jmp 0x51d9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1d0(%rsp)
movl %eax, 0x1cc(%rsp)
leaq 0x1f8(%rsp), %rdi
callq 0x7310
jmp 0x564f
jmp 0x51db
movq 0x1e0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x1e0(%rsp)
jmp 0x4f3a
vcvtsi2sdq 0x218(%rsp), %xmm0, %xmm1
leaq 0x180(%rsp), %rdi
vpxor %xmm0, %xmm0, %xmm0
callq 0x63b0
jmp 0x5211
movq 0x1e8(%rsp), %rax
movq %rax, 0x178(%rsp)
movq 0x1e8(%rsp), %rax
movq %rax, 0x170(%rsp)
movb $0x0, 0x16f(%rsp)
cmpq $0x0, 0x170(%rsp)
je 0x5267
movq 0x170(%rsp), %rcx
movq 0x1e8(%rsp), %rdx
subq 0x178(%rsp), %rdx
movb $0x1, %al
cmpq %rdx, %rcx
movb %al, 0x6f(%rsp)
jge 0x5274
movb 0x16f(%rsp), %al
xorb $-0x1, %al
movb %al, 0x6f(%rsp)
movb 0x6f(%rsp), %al
testb $0x1, %al
jne 0x5281
jmp 0x5613
leaq 0x1f8(%rsp), %rdi
callq 0x7270
movq %rax, 0x60(%rsp)
jmp 0x5295
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x160(%rsp)
vmovdqu (%rax), %xmm0
vmovdqa %xmm0, 0x150(%rsp)
leaq 0x1f8(%rsp), %rdi
callq 0x7280
jmp 0x52c2
movq 0x158(%rsp), %rax
movq %rax, 0x148(%rsp)
leaq 0x1f8(%rsp), %rdi
callq 0x7270
movq %rax, 0x58(%rsp)
jmp 0x52e6
movq 0x58(%rsp), %rax
vmovsd (%rax), %xmm0
vucomisd 0x150(%rsp), %xmm0
setne %al
setp %cl
orb %cl, %al
andb $0x1, %al
movb %al, 0x16f(%rsp)
movq 0x148(%rsp), %rax
cmpq 0x218(%rsp), %rax
jge 0x532e
movq 0x160(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x50(%rsp)
jmp 0x533f
movq 0x160(%rsp), %rax
subq $0x1, %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x140(%rsp)
movq 0x140(%rsp), %rdi
callq 0x70b0
movq %rax, 0x48(%rsp)
jmp 0x5360
movq 0x48(%rsp), %rcx
movq 0x1e8(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x1e8(%rsp)
movq 0x160(%rsp), %rdi
callq 0x70b0
movq %rax, 0x40(%rsp)
jmp 0x538c
movq 0x40(%rsp), %rax
addq 0x1e8(%rsp), %rax
movq %rax, 0x1e8(%rsp)
movq 0x148(%rsp), %rax
cmpq 0x218(%rsp), %rax
jge 0x53d4
cmpq $0x1, 0x140(%rsp)
jl 0x53d2
movq 0x178(%rsp), %rax
subq $0x1, %rax
movq %rax, 0x178(%rsp)
jmp 0x53f5
cmpq $-0x1, 0x140(%rsp)
jg 0x53f3
movq 0x178(%rsp), %rax
subq $0x1, %rax
movq %rax, 0x178(%rsp)
jmp 0x53f5
testb $0x1, 0x16f(%rsp)
je 0x54ca
movq 0x1e8(%rsp), %rax
cmpq 0x170(%rsp), %rax
jge 0x54ca
vmovsd 0x150(%rsp), %xmm0
vmovsd 0x1f0(%rsp), %xmm1
vaddsd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, 0x138(%rsp)
movq 0x218(%rsp), %rsi
vcvtsi2sd %rsi, %xmm0, %xmm0
vmovsd %xmm0, 0x38(%rsp)
movq 0x220(%rsp), %rdi
callq 0x6390
vmovsd 0x38(%rsp), %xmm2
vmovsd (%rax), %xmm1
vmovsd 0x138(%rsp), %xmm0
vfnmadd213sd %xmm2, %xmm1, %xmm0 # xmm0 = -(xmm1 * xmm0) + xmm2
vmovsd %xmm0, 0x130(%rsp)
movq 0x1e8(%rsp), %rax
movq %rax, 0x170(%rsp)
vmovsd 0x138(%rsp), %xmm0
vmovsd 0x130(%rsp), %xmm1
leaq 0x120(%rsp), %rdi
callq 0x63b0
jmp 0x54aa
movq 0x120(%rsp), %rax
movq %rax, 0x180(%rsp)
movq 0x128(%rsp), %rax
movq %rax, 0x188(%rsp)
movq 0x148(%rsp), %rax
cmpq 0x218(%rsp), %rax
jge 0x54ef
movq 0x160(%rsp), %rax
subq $0x1, %rax
movq %rax, 0x30(%rsp)
jmp 0x5500
movq 0x160(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x118(%rsp)
movq 0x148(%rsp), %rdi
movq 0x118(%rsp), %rsi
movq 0x218(%rsp), %rdx
callq 0x7130
movq %rax, 0x28(%rsp)
jmp 0x5531
movq 0x28(%rsp), %rax
movq %rax, 0x110(%rsp)
movq 0x110(%rsp), %rax
movq %rax, (%rsp)
movq 0x218(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x220(%rsp), %rdi
movq 0x148(%rsp), %rsi
callq 0x6390
vmovsd (%rax), %xmm0
vmovsd %xmm0, 0x10(%rsp)
movq 0x220(%rsp), %rdi
movq 0x218(%rsp), %rsi
callq 0x6390
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
vmovsd 0x10(%rsp), %xmm0
vmovsd (%rax), %xmm1
callq 0x2930
vmovsd %xmm1, 0x18(%rsp)
vmovsd %xmm0, 0x20(%rsp)
jmp 0x55b1
vmovsd 0x18(%rsp), %xmm0
vmovsd 0x20(%rsp), %xmm1
vmovsd %xmm1, 0x100(%rsp)
vmovsd %xmm0, 0x108(%rsp)
vmovsd 0x100(%rsp), %xmm0
movq 0x148(%rsp), %rsi
movq 0x118(%rsp), %rdx
leaq 0xe8(%rsp), %rdi
callq 0x7220
jmp 0x55f7
leaq 0x1f8(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x71c0
jmp 0x560e
jmp 0x5239
movq 0xd8(%rsp), %rdi
leaq 0x180(%rsp), %rsi
leaq 0x170(%rsp), %rdx
callq 0x72d0
jmp 0x5632
leaq 0x1f8(%rsp), %rdi
callq 0x7310
movq 0xe0(%rsp), %rax
addq $0x228, %rsp            # imm = 0x228
retq
movq 0x1d0(%rsp), %rdi
callq 0x23b0
nopl (%rax)