// File: /workspace/github_cpp_makefile_star2_O1/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 0x1cbc8
movq 0x10(%rdi), %rdx
testq %rdx, %rdx
jle 0x1cbc8
movq (%rdi), %rcx
movsd 0x8(%rcx), %xmm2
ucomisd %xmm0, %xmm2
ja 0x1cbe7
ucomisd -0x8(%rcx,%rax,8), %xmm0
ja 0x1cbe7
movsd 0xb50d(%rip), %xmm2     # 0x27de8
cmpq $0x2, %rax
je 0x1c8f9
movl $0x2, %esi
movsd (%rcx,%rsi,8), %xmm3
ucomisd %xmm0, %xmm3
ja 0x1c8fb
incq %rsi
cmpq %rsi, %rax
jne 0x1c8e6
jmp 0x1c91f
decq %rsi
movslq %esi, %rdi
cmpq %rdi, %rax
jle 0x1cbc8
movsd (%rcx,%rdi,8), %xmm2
subsd %xmm2, %xmm0
subsd %xmm2, %xmm3
divsd %xmm3, %xmm0
movapd %xmm0, %xmm2
cmpq $0x1, %rdx
je 0x1cbc8
movsd (%rcx,%rax,8), %xmm0
ucomisd %xmm1, %xmm0
ja 0x1cbf7
cmpq $0xe, %rdx
jle 0x1cbc8
imulq $0x70, %rax, %rdi
ucomisd (%rcx,%rdi), %xmm1
ja 0x1cbf7
leaq (,%rax,8), %r9
movq %rax, %r10
shlq $0x4, %r10
movl $0x1, %edi
movsd 0xb47b(%rip), %xmm0     # 0x27de8
movq %rcx, %r8
movsd (%r8,%r10), %xmm3
ucomisd %xmm1, %xmm3
ja 0x1c98a
incq %rdi
addq %r9, %r8
cmpq $0xe, %rdi
jne 0x1c970
jmp 0x1c9a9
cmpq %rdi, %rdx
jle 0x1cbc8
movsd (%r8,%rax,8), %xmm0
subsd %xmm0, %xmm1
subsd %xmm0, %xmm3
divsd %xmm3, %xmm1
movapd %xmm1, %xmm0
movsd 0xb33f(%rip), %xmm1     # 0x27cf0
ucomisd %xmm2, %xmm1
jbe 0x1ca41
ucomisd %xmm0, %xmm1
movslq %esi, %r8
jbe 0x1cad8
testl %esi, %esi
js 0x1cbc8
cmpq %r8, %rax
jle 0x1cbc8
testl %edi, %edi
js 0x1cbc8
movslq %edi, %r9
cmpq %r9, %rdx
jle 0x1cbc8
incl %esi
cmpq %rsi, %rax
jle 0x1cbc8
incl %edi
cmpq %rdi, %rdx
jle 0x1cbc8
imulq %rax, %r9
leaq (%rcx,%r9,8), %rdx
movsd (%rdx,%r8,8), %xmm3
movsd (%rdx,%rsi,8), %xmm4
subsd %xmm3, %xmm4
mulsd %xmm2, %xmm4
addsd %xmm3, %xmm4
imulq %rax, %rdi
leaq (%rcx,%rdi,8), %rax
movsd (%rax,%r8,8), %xmm1
subsd %xmm3, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm4, %xmm1
jmp 0x1cacf
leal 0x1(%rsi), %r8d
ucomisd %xmm0, %xmm1
movslq %r8d, %r8
jbe 0x1cb57
cmpq %r8, %rax
jle 0x1cbc8
testl %edi, %edi
js 0x1cbc8
movslq %edi, %r9
cmpq %r9, %rdx
jle 0x1cbc8
testl %esi, %esi
js 0x1cbc8
movslq %esi, %rsi
cmpq %rsi, %rax
jle 0x1cbc8
incl %edi
cmpq %rdi, %rdx
jle 0x1cbc8
imulq %rax, %r9
leaq (%rcx,%r9,8), %rdx
movsd (%rdx,%r8,8), %xmm3
movapd %xmm3, %xmm1
subsd (%rdx,%rsi,8), %xmm1
addsd 0xb33b(%rip), %xmm2     # 0x27de8
mulsd %xmm1, %xmm2
addsd %xmm3, %xmm2
imulq %rax, %rdi
leaq (%rcx,%rdi,8), %rax
movsd (%rax,%r8,8), %xmm1
subsd %xmm3, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
movapd %xmm1, %xmm0
jmp 0x1cbc6
testl %esi, %esi
js 0x1cbc8
cmpq %r8, %rax
jle 0x1cbc8
leal 0x1(%rdi), %r9d
movslq %r9d, %r9
cmpq %r9, %rdx
jle 0x1cbc8
incl %esi
cmpq %rsi, %rax
jle 0x1cbc8
testl %edi, %edi
js 0x1cbc8
movslq %edi, %rdi
cmpq %rdi, %rdx
jle 0x1cbc8
imulq %rax, %r9
leaq (%rcx,%r9,8), %rdx
movsd (%rdx,%r8,8), %xmm1
movsd (%rdx,%rsi,8), %xmm3
subsd %xmm1, %xmm3
mulsd %xmm2, %xmm3
addsd %xmm1, %xmm3
imulq %rax, %rdi
leaq (%rcx,%rdi,8), %rax
subsd (%rax,%r8,8), %xmm1
addsd 0xb29b(%rip), %xmm0     # 0x27de8
mulsd %xmm1, %xmm0
addsd %xmm3, %xmm0
jmp 0x1cbc6
cmpq %r8, %rax
jle 0x1cbc8
leal 0x1(%rdi), %r9d
movslq %r9d, %r9
cmpq %r9, %rdx
jle 0x1cbc8
testl %esi, %esi
js 0x1cbc8
movslq %esi, %r10
cmpq %r10, %rax
jle 0x1cbc8
testl %edi, %edi
js 0x1cbc8
movslq %edi, %rsi
cmpq %rsi, %rdx
jle 0x1cbc8
imulq %rax, %r9
leaq (%rcx,%r9,8), %rdx
movsd (%rdx,%r8,8), %xmm1
movapd %xmm1, %xmm3
subsd (%rdx,%r10,8), %xmm3
movsd 0xb248(%rip), %xmm4     # 0x27de8
addsd %xmm4, %xmm2
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm2
imulq %rax, %rsi
leaq (%rcx,%rsi,8), %rax
subsd (%rax,%r8,8), %xmm1
addsd %xmm4, %xmm0
mulsd %xmm1, %xmm0
addsd %xmm2, %xmm0
popq %rax
retq
leaq 0xaafe(%rip), %rdi      # 0x276cd
leaq 0xab2c(%rip), %rsi      # 0x27702
leaq 0xab51(%rip), %rcx      # 0x2772e
movl $0x151, %edx            # imm = 0x151
callq 0x5250
movq 0x163c2(%rip), %rdi     # 0x32fb0
leaq 0xcbc4(%rip), %rsi      # 0x297b9
jmp 0x1cc05
movq 0x163b2(%rip), %rdi     # 0x32fb0
leaq 0xcbff(%rip), %rsi      # 0x29804
callq 0x5380
movl $0x1, %edi
callq 0x5470