// File: 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 %rbp
movq %rsp, %rbp
subq $0x6f0, %rsp            # imm = 0x6F0
movq %rdi, -0x640(%rbp)
movq %rdi, %rax
movq %rax, -0x638(%rbp)
movq %rdi, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
leaq -0xd8(%rbp), %rdi
leaq 0x627d5(%rip), %rsi     # 0x97228
movl $0xc8, %edx
callq 0x63a0
leaq -0x1a0(%rbp), %rdi
leaq 0x62885(%rip), %rsi     # 0x972f0
movl $0xc8, %edx
callq 0x63a0
leaq -0x268(%rbp), %rdi
leaq 0x62935(%rip), %rsi     # 0x973b8
movl $0xc8, %edx
callq 0x63a0
movl $0x0, -0x3fc(%rbp)
cmpl $0x19, -0x3fc(%rbp)
jge 0x34b3e
movslq -0x3fc(%rbp), %rsi
leaq -0x1a0(%rbp), %rdi
callq 0x35910
movsd (%rax), %xmm0
callq 0x6390
movsd %xmm0, -0x650(%rbp)
movslq -0x3fc(%rbp), %rsi
leaq -0x330(%rbp), %rdi
callq 0x35910
movsd -0x650(%rbp), %xmm0
movsd %xmm0, (%rax)
movslq -0x3fc(%rbp), %rsi
leaq -0x268(%rbp), %rdi
callq 0x35910
movsd (%rax), %xmm0
callq 0x6390
movsd %xmm0, -0x648(%rbp)
movslq -0x3fc(%rbp), %rsi
leaq -0x3f8(%rbp), %rdi
callq 0x35910
movsd -0x648(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0x3fc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3fc(%rbp)
jmp 0x34a97
leaq -0x4c8(%rbp), %rdi
leaq 0x62934(%rip), %rsi     # 0x97480
movl $0xc8, %edx
callq 0x63a0
leaq -0x590(%rbp), %rdi
leaq 0x629e4(%rip), %rsi     # 0x97548
movl $0xc8, %edx
callq 0x63a0
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x658(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x35940
movsd -0x658(%rbp), %xmm0
ucomisd (%rax), %xmm0
jbe 0x34c9a
movb $0x0, -0x591(%rbp)
leaq -0x268(%rbp), %rdi
callq 0x35940
movsd (%rax), %xmm0
movsd 0xaf8e0(%rip), %xmm1    # 0xe4498
mulsd %xmm1, %xmm0
movsd %xmm0, -0x5c0(%rbp)
leaq -0x1a0(%rbp), %rdi
callq 0x35940
movsd (%rax), %xmm0
movsd 0xaf8b4(%rip), %xmm1    # 0xe4490
mulsd %xmm1, %xmm0
movsd %xmm0, -0x5b8(%rbp)
movabsq $0x408f400000000000, %rax # imm = 0x408F400000000000
movq %rax, -0x5b0(%rbp)
leaq -0x5c0(%rbp), %rax
movq %rax, -0x5a8(%rbp)
movq $0x3, -0x5a0(%rbp)
leaq -0x5c1(%rbp), %rdi
movq %rdi, -0x660(%rbp)
callq 0x7ac0
movq -0x640(%rbp), %rdi
movq -0x660(%rbp), %rcx
movq -0x5a8(%rbp), %rsi
movq -0x5a0(%rbp), %rdx
callq 0x35890
jmp 0x34c48
leaq -0x5c1(%rbp), %rdi
callq 0x87a0
movb $0x1, -0x591(%rbp)
testb $0x1, -0x591(%rbp)
jne 0x34c95
jmp 0x34c89
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x5d0(%rbp)
movl %eax, -0x5d4(%rbp)
leaq -0x5c1(%rbp), %rdi
callq 0x87a0
jmp 0x35064
movq -0x640(%rbp), %rdi
callq 0x7760
jmp 0x35054
movl $0x0, -0x5d8(%rbp)
movl $0x19, -0x5dc(%rbp)
cmpl $0x19, -0x5d8(%rbp)
jge 0x34d3b
movl -0x5d8(%rbp), %eax
addl -0x5dc(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x5e0(%rbp)
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x668(%rbp)
movslq -0x5e0(%rbp), %rsi
leaq -0xd8(%rbp), %rdi
callq 0x35910
movsd -0x668(%rbp), %xmm1
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x34d15
movl -0x5e0(%rbp), %eax
movl %eax, -0x5dc(%rbp)
jmp 0x34d21
movl -0x5e0(%rbp), %eax
movl %eax, -0x5d8(%rbp)
movl -0x5dc(%rbp), %eax
movl -0x5d8(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jg 0x34d36
jmp 0x34d3b
jmp 0x34cae
movslq -0x5d8(%rbp), %rsi
leaq -0xd8(%rbp), %rdi
movq %rdi, -0x6b0(%rbp)
callq 0x35910
movsd (%rax), %xmm0
movsd %xmm0, -0x6d8(%rbp)
movslq -0x5d8(%rbp), %rsi
leaq -0x330(%rbp), %rdi
movq %rdi, -0x6e8(%rbp)
callq 0x35910
movsd (%rax), %xmm0
movsd %xmm0, -0x6d0(%rbp)
movslq -0x5d8(%rbp), %rsi
leaq -0x4c8(%rbp), %rdi
movq %rdi, -0x6e0(%rbp)
callq 0x35910
movq -0x6b0(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x6c8(%rbp)
movl -0x5d8(%rbp), %eax
incl %eax
movslq %eax, %rsi
callq 0x35910
movq -0x6e8(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x6c0(%rbp)
movl -0x5d8(%rbp), %eax
incl %eax
movslq %eax, %rsi
callq 0x35910
movq -0x6e0(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x6b8(%rbp)
movl -0x5d8(%rbp), %eax
incl %eax
movslq %eax, %rsi
callq 0x35910
movsd -0x6d8(%rbp), %xmm0
movsd -0x6d0(%rbp), %xmm1
movsd -0x6c8(%rbp), %xmm2
movsd -0x6c0(%rbp), %xmm3
movsd -0x6b8(%rbp), %xmm4
movsd (%rax), %xmm5
movsd -0x10(%rbp), %xmm6
callq 0x354d0
callq 0x6790
movq -0x6b0(%rbp), %rdi
movsd %xmm0, -0x5e8(%rbp)
movslq -0x5d8(%rbp), %rsi
callq 0x35910
movsd (%rax), %xmm0
movsd %xmm0, -0x698(%rbp)
movslq -0x5d8(%rbp), %rsi
leaq -0x3f8(%rbp), %rdi
movq %rdi, -0x6a8(%rbp)
callq 0x35910
movsd (%rax), %xmm0
movsd %xmm0, -0x690(%rbp)
movslq -0x5d8(%rbp), %rsi
leaq -0x590(%rbp), %rdi
movq %rdi, -0x6a0(%rbp)
callq 0x35910
movq -0x6b0(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x688(%rbp)
movl -0x5d8(%rbp), %eax
incl %eax
movslq %eax, %rsi
callq 0x35910
movq -0x6a8(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x680(%rbp)
movl -0x5d8(%rbp), %eax
incl %eax
movslq %eax, %rsi
callq 0x35910
movq -0x6a0(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x678(%rbp)
movl -0x5d8(%rbp), %eax
incl %eax
movslq %eax, %rsi
callq 0x35910
movsd -0x698(%rbp), %xmm0
movsd -0x690(%rbp), %xmm1
movsd -0x688(%rbp), %xmm2
movsd -0x680(%rbp), %xmm3
movsd -0x678(%rbp), %xmm4
movsd (%rax), %xmm5
movsd -0x10(%rbp), %xmm6
callq 0x354d0
callq 0x6790
movsd %xmm0, -0x5f0(%rbp)
movsd -0x10(%rbp), %xmm0
callq 0x355f0
movsd %xmm0, -0x5f8(%rbp)
movb $0x0, -0x5f9(%rbp)
movsd -0x5f0(%rbp), %xmm0
movsd 0xaf517(%rip), %xmm1    # 0xe4498
mulsd %xmm1, %xmm0
movsd %xmm0, -0x628(%rbp)
movsd -0x5e8(%rbp), %xmm0
movsd 0xaf4f3(%rip), %xmm1    # 0xe4490
mulsd %xmm1, %xmm0
movsd %xmm0, -0x620(%rbp)
movsd -0x5f8(%rbp), %xmm0
movsd %xmm0, -0x618(%rbp)
leaq -0x628(%rbp), %rax
movq %rax, -0x610(%rbp)
movq $0x3, -0x608(%rbp)
leaq -0x629(%rbp), %rdi
movq %rdi, -0x670(%rbp)
callq 0x7ac0
movq -0x640(%rbp), %rdi
movq -0x670(%rbp), %rcx
movq -0x610(%rbp), %rsi
movq -0x608(%rbp), %rdx
callq 0x35890
jmp 0x35008
leaq -0x629(%rbp), %rdi
callq 0x87a0
movb $0x1, -0x5f9(%rbp)
testb $0x1, -0x5f9(%rbp)
jne 0x35052
jmp 0x35046
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x5d0(%rbp)
movl %eax, -0x5d4(%rbp)
leaq -0x629(%rbp), %rdi
callq 0x87a0
jmp 0x35064
movq -0x640(%rbp), %rdi
callq 0x7760
jmp 0x35054
movq -0x638(%rbp), %rax
addq $0x6f0, %rsp            # imm = 0x6F0
popq %rbp
retq
movq -0x5d0(%rbp), %rdi
callq 0x6760