// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1f8, %rsp            # imm = 0x1F8
movsd %xmm0, 0x8(%rsp)
movq %rdi, %rbx
xorl %r14d, %r14d
leaq 0x233a3(%rip), %r15     # 0x3ae08
leaq 0x23464(%rip), %r12     # 0x3aed0
movsd (%r14,%r15), %xmm0
callq 0x52c0
movsd %xmm0, 0x130(%rsp,%r14)
movsd (%r14,%r12), %xmm0
callq 0x52c0
movsd %xmm0, 0x68(%rsp,%r14)
addq $0x8, %r14
cmpq $0xc8, %r14
jne 0x17a6c
movsd 0x8(%rsp), %xmm4
ucomisd 0x23052(%rip), %xmm4    # 0x3ab00
jbe 0x17ae4
movsd 0x318f0(%rip), %xmm0    # 0x493a8
movhpd 0x318e0(%rip), %xmm0    # xmm0 = xmm0[0],mem[0]
mulpd 0x23188(%rip), %xmm0    # 0x3ac50
leaq 0x50(%rsp), %rsi
movapd %xmm0, (%rsi)
movabsq $0x408f400000000000, %rax # imm = 0x408F400000000000
movq %rax, 0x10(%rsi)
jmp 0x17c8a
xorl %edx, %edx
movl $0x19, %ecx
leaq 0x2324e(%rip), %rax     # 0x3ad40
cmpl $0x19, %edx
jge 0x17b20
leal (%rdx,%rcx), %edi
movl %edi, %esi
shrl $0x1f, %esi
addl %edi, %esi
sarl %esi
movslq %esi, %rdi
movsd (%rax,%rdi,8), %xmm0
ucomisd %xmm4, %xmm0
jbe 0x17b15
movl %esi, %ecx
movl %edx, %esi
leal 0x1(%rsi), %edi
movl %esi, %edx
cmpl %edi, %ecx
jg 0x17af2
jmp 0x17b25
leal 0x1(%rdx), %edi
movl %edx, %esi
movslq %esi, %r14
movsd (%rax,%r14,8), %xmm0
movsd 0x130(%rsp,%r14,8), %xmm1
leaq 0x23459(%rip), %rcx     # 0x3af98
movslq %edi, %r15
movsd (%rax,%r15,8), %xmm6
movsd 0x130(%rsp,%r15,8), %xmm2
movapd %xmm2, %xmm3
subsd %xmm1, %xmm3
subsd %xmm0, %xmm6
divsd %xmm6, %xmm3
movapd %xmm4, %xmm5
subsd %xmm0, %xmm5
divsd %xmm6, %xmm5
movapd %xmm6, %xmm7
movsd %xmm6, 0x20(%rsp)
movsd 0x22ba0(%rip), %xmm6    # 0x3a720
subsd %xmm5, %xmm6
movapd %xmm6, 0x30(%rsp)
mulsd %xmm5, %xmm2
mulsd %xmm6, %xmm1
movapd %xmm3, %xmm0
subsd (%rcx,%r14,8), %xmm0
addsd %xmm2, %xmm1
subsd (%rcx,%r15,8), %xmm3
movsd %xmm5, 0x28(%rsp)
mulsd %xmm5, %xmm3
mulsd %xmm6, %xmm0
subsd %xmm3, %xmm0
movapd 0x22b50(%rip), %xmm2    # 0x3a710
xorpd %xmm6, %xmm2
mulsd %xmm5, %xmm2
mulsd %xmm7, %xmm2
movapd %xmm2, 0x10(%rsp)
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
callq 0x55f0
movapd %xmm0, 0x40(%rsp)
movsd 0x68(%rsp,%r14,8), %xmm1
leaq 0x2346d(%rip), %rax     # 0x3b060
movsd 0x68(%rsp,%r15,8), %xmm2
movapd %xmm2, %xmm3
subsd %xmm1, %xmm3
divsd 0x20(%rsp), %xmm3
movsd 0x28(%rsp), %xmm5
mulsd %xmm5, %xmm2
movapd 0x30(%rsp), %xmm4
mulsd %xmm4, %xmm1
movapd %xmm3, %xmm0
subsd (%rax,%r14,8), %xmm0
addsd %xmm2, %xmm1
subsd (%rax,%r15,8), %xmm3
mulsd %xmm5, %xmm3
mulsd %xmm4, %xmm0
subsd %xmm3, %xmm0
mulsd 0x10(%rsp), %xmm0
addsd %xmm1, %xmm0
callq 0x55f0
movapd %xmm0, 0x10(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x17e99
movapd 0x10(%rsp), %xmm2
unpcklpd 0x40(%rsp), %xmm2       # xmm2 = xmm2[0],mem[0]
movsd 0x31738(%rip), %xmm1    # 0x493a8
movhpd 0x31728(%rip), %xmm1    # xmm1 = xmm1[0],mem[0]
mulpd %xmm2, %xmm1
leaq 0x50(%rsp), %rsi
movapd %xmm1, (%rsi)
movsd %xmm0, 0x10(%rsi)
leaq 0x7(%rsp), %rcx
movl $0x3, %edx
movq %rbx, %rdi
callq 0x17f80
movq %rbx, %rax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r14
popq %r15
retq