// File: umatin[P]LogarithmicErrorRegression/./src/theil_sen.h
// Function: theil_sen(std::vector<double, std::allocator<double>>&, long)
linear_regression theil_sen(std::vector<double>& data, long max_trails){
    std::vector<double> slopes;
    double a,b;
    long num = (data.size() * (data.size()-1)) / 2;

    for (int i = 0; i < max_trails; i++){
        double a_tmp, b_tmp;
        fit_line(data, rand()%data.size(), rand()%data.size(), a_tmp, b_tmp);
        slopes.push_back(a_tmp);
    }
    a = quickselect(slopes,slopes.size()/2,0,slopes.size()-1);

    std::vector<double> intercepts;
    for (int i = 0; i < max_trails; i++){
        int element = rand()%data.size();
        double intercept = (double)element - (a * data[element]);
        intercepts.push_back(intercept);
    }
    b = quickselect(intercepts,intercepts.size()/2,0,intercepts.size()-1);
    return linear_regression(a,b);
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x110(%rsp)
movq %rsi, 0x108(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x69c0
movq 0x110(%rsp), %rdi
callq 0x6400
movq %rax, 0x80(%rsp)
movq 0x110(%rsp), %rdi
callq 0x6400
movq %rax, %rcx
movq 0x80(%rsp), %rax
subq $0x1, %rcx
imulq %rcx, %rax
shrq %rax
movq %rax, 0xd8(%rsp)
movl $0x0, 0xd4(%rsp)
movslq 0xd4(%rsp), %rax
cmpq 0x108(%rsp), %rax
jge 0x4c94
movq 0x110(%rsp), %rax
movq %rax, 0x60(%rsp)
callq 0x2080
cltq
movq %rax, 0x50(%rsp)
movq 0x110(%rsp), %rdi
callq 0x6400
movq %rax, %rcx
movq 0x50(%rsp), %rax
xorl %edx, %edx
movq %rdx, 0x58(%rsp)
divq %rcx
movq %rdx, 0x68(%rsp)
callq 0x2080
cltq
movq %rax, 0x70(%rsp)
movq 0x110(%rsp), %rdi
callq 0x6400
movq 0x58(%rsp), %rdx
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq %rax, %rcx
movq 0x70(%rsp), %rax
divq %rcx
leaq 0xc8(%rsp), %rcx
movq %rcx, 0x78(%rsp)
leaq 0xc0(%rsp), %r8
callq 0x2640
movq 0x78(%rsp), %rsi
leaq 0xf0(%rsp), %rdi
callq 0x6f20
jmp 0x4c63
jmp 0x4c65
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x4baf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
jmp 0x4e85
leaq 0xf0(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x6400
movq 0x38(%rsp), %rdi
shrq %rax
movl %eax, 0x44(%rsp)
callq 0x6400
movq 0x38(%rsp), %rdi
movl 0x44(%rsp), %esi
movl %eax, %ecx
decl %ecx
xorl %edx, %edx
callq 0x6f90
vmovsd %xmm0, 0x48(%rsp)
jmp 0x4cd3
vmovsd 0x48(%rsp), %xmm0
vmovsd %xmm0, 0xe8(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x69c0
movl $0x0, 0x94(%rsp)
movslq 0x94(%rsp), %rax
cmpq 0x108(%rsp), %rax
jge 0x4de2
callq 0x2080
cltq
movq %rax, 0x20(%rsp)
movq 0x110(%rsp), %rdi
callq 0x6400
movq %rax, %rcx
movq 0x20(%rsp), %rax
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, 0x90(%rsp)
movslq 0x90(%rsp), %rsi
movl %esi, %eax
vcvtsi2sd %eax, %xmm0, %xmm0
vmovsd %xmm0, 0x28(%rsp)
vmovsd 0xe8(%rsp), %xmm0
vmovsd %xmm0, 0x30(%rsp)
movq 0x110(%rsp), %rdi
callq 0x6390
vmovsd 0x28(%rsp), %xmm2
vmovsd 0x30(%rsp), %xmm1
vmovsd (%rax), %xmm0
vfnmadd213sd %xmm2, %xmm1, %xmm0 # xmm0 = -(xmm1 * xmm0) + xmm2
vmovsd %xmm0, 0x88(%rsp)
leaq 0x98(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x6f20
jmp 0x4da4
jmp 0x4da6
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x4cfa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x6560
jmp 0x4e85
leaq 0x98(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x6400
movq 0x8(%rsp), %rdi
shrq %rax
movl %eax, 0x14(%rsp)
callq 0x6400
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %esi
movl %eax, %ecx
decl %ecx
xorl %edx, %edx
callq 0x6f90
vmovsd %xmm0, 0x18(%rsp)
jmp 0x4e21
vmovsd 0x18(%rsp), %xmm0
vmovsd %xmm0, 0xe0(%rsp)
vmovsd 0xe8(%rsp), %xmm0
vmovsd 0xe0(%rsp), %xmm1
leaq 0x118(%rsp), %rdi
callq 0x63b0
jmp 0x4e51
leaq 0x98(%rsp), %rdi
callq 0x6560
leaq 0xf0(%rsp), %rdi
callq 0x6560
vmovsd 0x118(%rsp), %xmm0
vmovsd 0x120(%rsp), %xmm1
addq $0x128, %rsp            # imm = 0x128
retq
leaq 0xf0(%rsp), %rdi
callq 0x6560
movq 0xb8(%rsp), %rdi
callq 0x23b0
nop