// File: /workspace/github_cpp_makefile_star2_O2/sus304[P]ForRocket/src/environment/satmo1976.cpp
// Function: standardatmosphere1976::UpperAtmosphere(double)
std::vector<double> standardatmosphere1976::UpperAtmosphere(double geometric_altitude) {
    using Layer = std::array<double, 25>;
    Layer height_array = {86.0, 93.0, 100.0, 107.0, 114.0, 121.0, 128.0, 135.0, 142.0, 150.0, 
                            160.0, 170.0, 180.0, 190.0, 200.0, 220.0, 260.0, 300.0, 400.0, 
                            500.0, 600.0, 700.0, 800.0, 900.0, 1000.0};
    Layer pressure_ratio_array = {3.6850e-6, 1.0660e-6, 3.1593e-7, 1.0611e-7, 4.3892e-8, 
                                        2.3095e-8, 1.3997e-8, 9.2345e-9, 6.4440e-9, 4.4828e-9, 
                                        2.9997e-9, 2.0933e-9, 1.5072e-9, 1.1118e-9, 8.3628e-10, 
                                        4.9494e-10, 1.9634e-10, 8.6557e-11, 1.4328e-11, 2.9840e-12,  
                                        8.1056e-13, 3.1491e-13, 1.6813e-13, 1.0731e-13, 7.4155e-14};
    Layer density_ratio_array = {5.680E-6, 1.632E-6, 4.575E-7, 1.341E-7, 4.061E-8,
                                    1.614e-8, 7.932e-9, 4.461e-9, 2.741e-9, 1.694e-9,
                                    1.007e-9, 6.380e-10, 4.240e-10, 2.923e-10, 2.074e-10,
                                    1.116e-10, 3.871e-11, 1.564e-11, 2.288e-12, 4.257e-13,
                                    9.279e-14, 2.506e-14, 9.272e-15, 4.701e-15, 2.907e-15};

    Layer log_pressure_ratio_array;
    Layer log_density_ratio_array;
    for (int i=0; i < 25; ++i) {
        log_pressure_ratio_array[i] = std::log(pressure_ratio_array[i]);
        log_density_ratio_array[i] = std::log(density_ratio_array[i]);
    }

    Layer delta_log_pressure_ratio_array = {-0.174061, -0.177924, -0.167029, -0.142755, -0.107859,
                                            -0.079322, -0.064664, -0.054879, -0.048260, -0.042767,
                                            -0.037854, -0.034270, -0.031543, -0.029384, -0.027632,
                                            -0.024980, -0.021559, -0.019557, -0.016735, -0.014530,
                                            -0.011314, -0.007677, -0.005169, -0.003944, -0.003612};

    Layer delta_log_density_ratio_array = {-0.172421, -0.182258, -0.178090, -0.176372, -0.154322,
                                            -0.113750, -0.090582, -0.075033, -0.064679, -0.056067,
                                            -0.048461, -0.043042, -0.038869, -0.035648, -0.033063,
                                            -0.029164, -0.024220, -0.021336, -0.017686, -0.016035,
                                            -0.014327, -0.011631, -0.008248, -0.005580, -0.004227};

    if (geometric_altitude > height_array.back()) {
        std::vector<double> res = {density_ratio_array.back() * density_sealevel, pressure_ratio_array.back() * pressure_sealevel, 1000.0};
        return res;
    }
    
    int i = 0;
    int j = 25;
    for (;i < 25;) {
        int k = (i + j) / 2;
        if (geometric_altitude < height_array[k]) {
            j = k;
        } else {
            i = k;
        }
        if (j <= i + 1) {
            break;
        }
    }

    double delta = std::exp(EvaluateCubic(height_array[i], log_pressure_ratio_array[i], delta_log_pressure_ratio_array[i],
                                            height_array[i+1], log_pressure_ratio_array[i+1], delta_log_pressure_ratio_array[i+1], geometric_altitude));
    double sigma = std::exp(EvaluateCubic(height_array[i], log_density_ratio_array[i], delta_log_density_ratio_array[i],
                                            height_array[i+1], log_density_ratio_array[i+1], delta_log_density_ratio_array[i+1], geometric_altitude));
    double temperatrue = KineticTemperature(geometric_altitude);
    std::vector<double> res = {sigma * density_sealevel, delta * pressure_sealevel, temperatrue};
    return res;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x1e8, %rsp            # imm = 0x1E8
movsd %xmm0, 0x8(%rsp)
movq %rdi, %rbx
xorl %r14d, %r14d
leaq 0x1ef74(%rip), %r15     # 0x32528
leaq 0x1f035(%rip), %r12     # 0x325f0
cmpq $0xc8, %r14
je 0x135f1
movsd (%r14,%r15), %xmm0
callq 0x52b0
movsd %xmm0, 0x120(%rsp,%r14)
movsd (%r14,%r12), %xmm0
callq 0x52b0
movsd %xmm0, 0x58(%rsp,%r14)
addq $0x8, %r14
jmp 0x135bb
movsd 0x8(%rsp), %xmm0
ucomisd 0x1ec21(%rip), %xmm0    # 0x32220
jbe 0x13635
movsd 0x35d97(%rip), %xmm0    # 0x493a0
movhpd 0x35d87(%rip), %xmm0    # xmm0 = xmm0[0],mem[0]
mulpd 0x1ed57(%rip), %xmm0    # 0x32370
leaq 0x40(%rsp), %rsi
movapd %xmm0, (%rsi)
movabsq $0x408f400000000000, %rax # imm = 0x408F400000000000
movq %rax, 0x10(%rsi)
jmp 0x13749
xorl %esi, %esi
pushq $0x19
popq %rdi
pushq $0x2
popq %r8
leaq 0x1ee1b(%rip), %rcx     # 0x32460
cmpl $0x19, %esi
jge 0x1366f
leal (%rsi,%rdi), %eax
cltd
idivl %r8d
cltq
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x8(%rsp), %xmm0
cmoval %eax, %edi
cmoval %esi, %eax
leal 0x1(%rax), %edx
movl %eax, %esi
cmpl %edx, %edi
jg 0x13645
jmp 0x13674
leal 0x1(%rsi), %edx
movl %esi, %eax
movslq %eax, %r14
movsd (%rcx,%r14,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x120(%rsp,%r14,8), %xmm1
leaq 0x1f024(%rip), %rax     # 0x326b8
movsd (%rax,%r14,8), %xmm2
movslq %edx, %r15
movsd (%rcx,%r15,8), %xmm3
movsd %xmm3, 0x28(%rsp)
movsd 0x120(%rsp,%r15,8), %xmm4
movsd (%rax,%r15,8), %xmm5
movsd 0x8(%rsp), %xmm6
callq 0x138ee
callq 0x55c0
movaps %xmm0, 0x30(%rsp)
movsd 0x58(%rsp,%r14,8), %xmm1
leaq 0x1f0a4(%rip), %rax     # 0x32780
movsd (%rax,%r14,8), %xmm2
movsd 0x58(%rsp,%r15,8), %xmm4
movsd (%rax,%r15,8), %xmm5
movsd 0x10(%rsp), %xmm0
movsd 0x28(%rsp), %xmm3
movsd 0x8(%rsp), %xmm6
callq 0x138ee
callq 0x55c0
movaps %xmm0, 0x10(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x1394b
movapd 0x10(%rsp), %xmm2
unpcklpd 0x30(%rsp), %xmm2       # xmm2 = xmm2[0],mem[0]
movsd 0x35c71(%rip), %xmm1    # 0x493a0
movhpd 0x35c61(%rip), %xmm1    # xmm1 = xmm1[0],mem[0]
mulpd %xmm2, %xmm1
leaq 0x40(%rsp), %rsi
movapd %xmm1, (%rsi)
movsd %xmm0, 0x10(%rsi)
pushq $0x3
popq %rdx
leaq 0x7(%rsp), %rcx
movq %rbx, %rdi
callq 0x13a30
movq %rbx, %rax
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r14
popq %r15
retq