// File: sus304[P]ForRocket/src/interpolate.cpp
// Function: forrocket::interpolate::Linear1D::polate(double const&, std::vector<double, std::allocator<double>> const&, std::vector<double, std::allocator<double>> const&, int const&)
double forrocket::interpolate::Linear1D::polate(const double& x, const std::vector<double>& x_src, const std::vector<double>& y_src, const int& fill_value) {
    if (x >= x_src[0] && x <= x_src.back()) {  // 内挿
        // TODO: binary search
        for (int i=0; i < x_src.size(); ++i) {
            if (x == x_src[i]) {
                return y_src[i];
            }
            if (x < x_src[i]) {
                double slope = (y_src[i] - y_src[i-1]) / (x_src[i] - x_src[i-1]);
                return y_src[i-1] + slope * (x - x_src[i-1]);
            }
        }
    } else if (x < x_src[0]) {  // 左外挿
        switch (fill_value) {
        case 0:  // zero
            return 0;
            break;
        
        case 1:  // same
            return y_src[0];
            break;

        case 2:  // extrapolate
            double slope = (y_src[1] - y_src[0]) / (x_src[1] - x_src[0]);
            return y_src[0] + slope * (x - x_src[0]);
            break;
        }
    } else if (x > x_src[x_src.size()-1]) {  // 右外挿
        switch (fill_value) {
        case 0:  // zero
            return 0;
            break;
        
        case 1:  // same
            return y_src[y_src.size()-1];
            break;

        case 2:  // extrapolate
            int index_end = y_src.size()-1;
            double slope = (y_src[index_end] - y_src[index_end-1]) / (x_src[index_end] - x_src[index_end-1]);
            return y_src[index_end] + slope * (x - x_src[index_end]);
            break;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x18(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x60(%rbp)
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x27ce0
movsd -0x60(%rbp), %xmm0
ucomisd (%rax), %xmm0
jb 0x26b4f
movq -0x18(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x20(%rbp), %rdi
callq 0x28390
movsd -0x68(%rbp), %xmm1
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jb 0x26b4f
movl $0x0, -0x34(%rbp)
movslq -0x34(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rdi
callq 0x86e0
movq %rax, %rcx
movq -0x70(%rbp), %rax
cmpq %rcx, %rax
jae 0x26b4a
movq -0x18(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x34(%rbp), %rsi
callq 0x27ce0
movsd -0x78(%rbp), %xmm0
ucomisd (%rax), %xmm0
jne 0x26a0b
jp 0x26a0b
movq -0x28(%rbp), %rdi
movslq -0x34(%rbp), %rsi
callq 0x27ce0
movsd (%rax), %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x26e95
movq -0x18(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x80(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x34(%rbp), %rsi
callq 0x27ce0
movsd -0x80(%rbp), %xmm1
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x26b3a
movq -0x28(%rbp), %rdi
movslq -0x34(%rbp), %rsi
callq 0x27ce0
movsd (%rax), %xmm0
movsd %xmm0, -0xb0(%rbp)
movq -0x28(%rbp), %rdi
movl -0x34(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x27ce0
movsd -0xb0(%rbp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, -0xa0(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x34(%rbp), %rsi
callq 0x27ce0
movsd (%rax), %xmm0
movsd %xmm0, -0xa8(%rbp)
movq -0x20(%rbp), %rdi
movl -0x34(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x27ce0
movsd -0xa8(%rbp), %xmm1
movsd -0xa0(%rbp), %xmm0
subsd (%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
movq -0x28(%rbp), %rdi
movl -0x34(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x27ce0
movsd (%rax), %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
movq -0x18(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x98(%rbp)
movq -0x20(%rbp), %rdi
movl -0x34(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x27ce0
movsd -0x98(%rbp), %xmm2
movsd -0x90(%rbp), %xmm0
movsd -0x88(%rbp), %xmm1
subsd (%rax), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x26e95
jmp 0x26b3c
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x269a8
jmp 0x26e93
movq -0x18(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0xb8(%rbp)
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x27ce0
movsd -0xb8(%rbp), %xmm1
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x26cce
movq -0x30(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xbc(%rbp)
testl %eax, %eax
je 0x26bb1
jmp 0x26b94
movl -0xbc(%rbp), %eax
subl $0x1, %eax
je 0x26bbe
jmp 0x26ba1
movl -0xbc(%rbp), %eax
subl $0x2, %eax
je 0x26bd9
jmp 0x26cc9
xorps %xmm0, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x26e95
movq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x27ce0
movsd (%rax), %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x26e95
movq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0x27ce0
movsd (%rax), %xmm0
movsd %xmm0, -0xf0(%rbp)
movq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x27ce0
movsd -0xf0(%rbp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, -0xe0(%rbp)
movq -0x20(%rbp), %rdi
movl $0x1, %esi
callq 0x27ce0
movsd (%rax), %xmm0
movsd %xmm0, -0xe8(%rbp)
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x27ce0
movsd -0xe8(%rbp), %xmm1
movsd -0xe0(%rbp), %xmm0
subsd (%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x27ce0
movsd (%rax), %xmm0
movsd %xmm0, -0xc8(%rbp)
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0xd0(%rbp)
movq -0x18(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0xd8(%rbp)
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x27ce0
movsd -0xd8(%rbp), %xmm2
movsd -0xd0(%rbp), %xmm0
movsd -0xc8(%rbp), %xmm1
subsd (%rax), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x26e95
jmp 0x26e91
movq -0x18(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0xf8(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x20(%rbp), %rdi
callq 0x86e0
movq -0x100(%rbp), %rdi
movq %rax, %rsi
subq $0x1, %rsi
callq 0x27ce0
movsd -0xf8(%rbp), %xmm0
ucomisd (%rax), %xmm0
jbe 0x26e8f
movq -0x30(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x104(%rbp)
testl %eax, %eax
je 0x26d46
jmp 0x26d29
movl -0x104(%rbp), %eax
subl $0x1, %eax
je 0x26d53
jmp 0x26d36
movl -0x104(%rbp), %eax
subl $0x2, %eax
je 0x26d88
jmp 0x26e8d
xorps %xmm0, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x26e95
movq -0x28(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x28(%rbp), %rdi
callq 0x86e0
movq -0x110(%rbp), %rdi
movq %rax, %rsi
subq $0x1, %rsi
callq 0x27ce0
movsd (%rax), %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x26e95
movq -0x28(%rbp), %rdi
callq 0x86e0
subq $0x1, %rax
movl %eax, -0x4c(%rbp)
movq -0x28(%rbp), %rdi
movslq -0x4c(%rbp), %rsi
callq 0x27ce0
movsd (%rax), %xmm0
movsd %xmm0, -0x140(%rbp)
movq -0x28(%rbp), %rdi
movl -0x4c(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x27ce0
movsd -0x140(%rbp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, -0x130(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x4c(%rbp), %rsi
callq 0x27ce0
movsd (%rax), %xmm0
movsd %xmm0, -0x138(%rbp)
movq -0x20(%rbp), %rdi
movl -0x4c(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x27ce0
movsd -0x138(%rbp), %xmm1
movsd -0x130(%rbp), %xmm0
subsd (%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movq -0x28(%rbp), %rdi
movslq -0x4c(%rbp), %rsi
callq 0x27ce0
movsd (%rax), %xmm0
movsd %xmm0, -0x118(%rbp)
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x120(%rbp)
movq -0x18(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x128(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x4c(%rbp), %rsi
callq 0x27ce0
movsd -0x128(%rbp), %xmm2
movsd -0x120(%rbp), %xmm0
movsd -0x118(%rbp), %xmm1
subsd (%rax), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x26e95
jmp 0x26e8f
jmp 0x26e91
jmp 0x26e93
ud2
movsd -0x8(%rbp), %xmm0
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
nopw %cs:(%rax,%rax)