// File: /workspace/github_cpp_makefile_star2_O3/istellartech[P]OpenTsiolkovsky/src/fileio.cpp
// Function: interp_matrix_2d(double, double, Eigen::Matrix<double, -1, -1, 0, -1, -1>)
double interp_matrix_2d(double mach, double alpha, MatrixXd matrix){
    // 2変数関数として線形補間をする。範囲外のデータが入力された場合強制終了。
    // 先頭行先頭列に変数を並べる
    // @params mach  取得したいデータ縦方向
    // @params alpha 取得したいデータ横方向
    // @params matrix 2x? データ配列
    // @return y(double) 入力mach, alphaのデータ
    const int col_number = 15;
    double y;

    int index_mach, index_alpha;
    double Dmach = -1;
    double Dalpha = -1;

    // search index mach and calculate Dmach
    if(mach < matrix(1, 0) || mach > matrix(matrix.rows()-1, 0)){
        std::cout << "ERROR : interp_matrix_2d. First argument is out of the boundary of matrix.";
        exit(1);
    }
    for (int i = 2; i < matrix.rows(); i++) {
        if(mach < matrix(i, 0)){
            index_mach = i - 1;

            double mach_lower, mach_higher;
            mach_lower  = matrix(i - 1, 0);
            mach_higher = matrix(    i, 0);
            Dmach = (mach - mach_lower)/(mach_higher - mach_lower);

            break;
        }
    }
    

    // search index alpha and calculate Dalpha
    if(alpha < matrix(0, 1) || alpha > matrix(0, col_number -1)){
        std::cout << "ERROR : interp_matrix_2d. Second argument is out of the boundary of matrix.";
        exit(1);
    }
    for (int i = 2; i < col_number; i++) {
        if(alpha < matrix(0, i)){
            index_alpha = i - 1;

            double alpha_lower, alpha_higher;
            alpha_lower  = matrix(0, i - 1);
            alpha_higher = matrix(0,     i);
            Dalpha = (alpha - alpha_lower)/(alpha_higher - alpha_lower);

            break;
        }
    }

    // calculate y
    if(Dmach < 0.5){
        if(Dalpha < 0.5){
            y = matrix(index_mach, index_alpha)
              + ( matrix(index_mach + 1, index_alpha) - matrix(index_mach, index_alpha)) * Dmach
              + ( matrix(index_mach, index_alpha + 1) - matrix(index_mach, index_alpha)) * Dalpha;
        }else{
            y = matrix(index_mach, index_alpha + 1)
              + ( matrix(index_mach + 1, index_alpha + 1) - matrix(index_mach, index_alpha + 1)) * Dmach
              + ( matrix(index_mach, index_alpha + 1) - matrix(index_mach, index_alpha)) * (Dalpha - 1);
        }
    }else{
        if(Dalpha < 0.5){
            y = matrix(index_mach + 1, index_alpha)
              + ( matrix(index_mach + 1, index_alpha) - matrix(index_mach, index_alpha)) * (Dmach - 1)
              + ( matrix(index_mach + 1, index_alpha + 1) - matrix(index_mach + 1, index_alpha)) * Dalpha;
        }else{
            y = matrix(index_mach + 1, index_alpha + 1)
              + ( matrix(index_mach + 1, index_alpha + 1) - matrix(index_mach, index_alpha + 1)) * (Dmach - 1)
              + ( matrix(index_mach + 1, index_alpha + 1) - matrix(index_mach + 1, index_alpha)) * (Dalpha - 1);
        }
    }

    return y;
}

// Assembly:
pushq %rax
movq 0x8(%rdi), %rax
cmpq $0x2, %rax
jl 0x1adab
movq 0x10(%rdi), %rcx
testq %rcx, %rcx
jle 0x1adab
movq (%rdi), %rdx
movsd 0x8(%rdx), %xmm2
ucomisd %xmm0, %xmm2
ja 0x1adca
ucomisd -0x8(%rdx,%rax,8), %xmm0
ja 0x1adca
movsd 0xb330(%rip), %xmm2     # 0x25e20
cmpq $0x2, %rax
je 0x1ab0e
movl $0x2, %esi
movsd (%rdx,%rsi,8), %xmm3
ucomisd %xmm0, %xmm3
ja 0x1ab10
incq %rsi
cmpq %rsi, %rax
jne 0x1aafb
jmp 0x1ab2a
decl %esi
movslq %esi, %rdi
movsd (%rdx,%rdi,8), %xmm2
subsd %xmm2, %xmm0
subsd %xmm2, %xmm3
divsd %xmm3, %xmm0
movapd %xmm0, %xmm2
cmpq $0x1, %rcx
je 0x1adab
movsd (%rdx,%rax,8), %xmm0
ucomisd %xmm1, %xmm0
ja 0x1adda
cmpq $0xe, %rcx
jbe 0x1adab
imulq $0x70, %rax, %rdi
ucomisd (%rdx,%rdi), %xmm1
ja 0x1adda
leaq (,%rax,8), %r8
movq %rax, %r10
shlq $0x4, %r10
movl $0x1, %edi
movq %rdx, %r9
movsd (%r9,%r10), %xmm0
ucomisd %xmm1, %xmm0
ja 0x1aba6
addq %r8, %r9
incq %rdi
cmpq $0xe, %rdi
jne 0x1ab73
movsd 0xb28d(%rip), %xmm1     # 0x25e20
movsd 0xb185(%rip), %xmm0     # 0x25d20
ucomisd %xmm2, %xmm0
ja 0x1abd4
jmp 0x1ac5a
movsd (%r9,%r8), %xmm3
subsd %xmm3, %xmm1
subsd %xmm3, %xmm0
divsd %xmm0, %xmm1
movsd 0xb160(%rip), %xmm0     # 0x25d20
ucomisd %xmm2, %xmm0
jbe 0x1ac50
ucomisd %xmm1, %xmm0
jbe 0x1ace3
testl %esi, %esi
js 0x1adab
movslq %esi, %r8
cmpq %r8, %rax
jle 0x1adab
testl %edi, %edi
js 0x1adab
movslq %edi, %r9
cmpq %r9, %rcx
jbe 0x1adab
incl %esi
cmpq %rsi, %rax
jbe 0x1adab
incl %edi
cmpq %rdi, %rcx
jbe 0x1adab
imulq %rax, %r9
leaq (%rdx,%r9,8), %rcx
movsd (%rcx,%r8,8), %xmm3
movsd (%rcx,%rsi,8), %xmm4
subsd %xmm3, %xmm4
mulsd %xmm2, %xmm4
addsd %xmm3, %xmm4
imulq %rax, %rdi
leaq (%rdx,%rdi,8), %rax
movsd (%rax,%r8,8), %xmm0
subsd %xmm3, %xmm0
mulsd %xmm1, %xmm0
addsd %xmm4, %xmm0
jmp 0x1ada9
ucomisd %xmm1, %xmm0
jbe 0x1ad47
leal 0x1(%rsi), %r8d
movslq %r8d, %r8
cmpq %r8, %rax
jle 0x1adab
testl %edi, %edi
js 0x1adab
movslq %edi, %r9
cmpq %r9, %rcx
jbe 0x1adab
testl %esi, %esi
js 0x1adab
movslq %esi, %rsi
cmpq %rsi, %rax
jbe 0x1adab
incl %edi
cmpq %rdi, %rcx
jbe 0x1adab
imulq %rax, %r9
leaq (%rdx,%r9,8), %rcx
movsd (%rcx,%r8,8), %xmm3
movapd %xmm3, %xmm0
subsd (%rcx,%rsi,8), %xmm0
addsd 0xb164(%rip), %xmm2     # 0x25e20
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
imulq %rax, %rdi
leaq (%rdx,%rdi,8), %rax
movsd (%rax,%r8,8), %xmm0
subsd %xmm3, %xmm0
mulsd %xmm1, %xmm0
addsd %xmm2, %xmm0
jmp 0x1ada9
testl %esi, %esi
js 0x1adab
movslq %esi, %rdx
cmpq %rdx, %rax
jle 0x1adab
incl %esi
cmpq %rsi, %rax
jbe 0x1adab
cmpq %rdi, %rcx
jbe 0x1adab
leaq (%r10,%rdx,8), %rax
movsd (%r9,%rax), %xmm0
leaq (%r10,%rsi,8), %rax
movsd (%r9,%rax), %xmm3
subsd %xmm0, %xmm3
mulsd %xmm2, %xmm3
addsd %xmm0, %xmm3
leaq (%r8,%rdx,8), %rax
subsd (%r9,%rax), %xmm0
addsd 0xb0e3(%rip), %xmm1     # 0x25e20
mulsd %xmm0, %xmm1
addsd %xmm3, %xmm1
jmp 0x1ada5
leal 0x1(%rsi), %edx
movslq %edx, %rdx
cmpq %rdx, %rax
jle 0x1adab
testl %esi, %esi
js 0x1adab
movslq %esi, %rsi
cmpq %rsi, %rax
jbe 0x1adab
cmpq %rdi, %rcx
jbe 0x1adab
leaq (%r10,%rdx,8), %rax
movsd (%r9,%rax), %xmm0
leaq (%r10,%rsi,8), %rax
movapd %xmm0, %xmm3
subsd (%r9,%rax), %xmm3
movsd 0xb09d(%rip), %xmm4     # 0x25e20
addsd %xmm4, %xmm2
mulsd %xmm3, %xmm2
addsd %xmm0, %xmm2
leaq (%r8,%rdx,8), %rax
subsd (%r9,%rax), %xmm0
addsd %xmm4, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
movapd %xmm1, %xmm0
popq %rax
retq
leaq 0xa95b(%rip), %rdi      # 0x2570d
leaq 0xa989(%rip), %rsi      # 0x25742
leaq 0xa9ae(%rip), %rcx      # 0x2576e
movl $0x151, %edx            # imm = 0x151
callq 0x5250
movq 0x171df(%rip), %rdi     # 0x31fb0
leaq 0xcb28(%rip), %rsi      # 0x27900
jmp 0x1ade8
movq 0x171cf(%rip), %rdi     # 0x31fb0
leaq 0xcb63(%rip), %rsi      # 0x2794b
callq 0x5380
movl $0x1, %edi
callq 0x5470