// File: istellartech[P]OpenTsiolkovsky/src/rocket.cpp
// Function: RocketStage::update_from_time_and_altitude(double, double)
void RocketStage::update_from_time_and_altitude(double time, double altitude){
    Air air;
    //    air = air.altitude(altitude);
    if (air_density_file_exist){
        air = air.altitude_with_variation_table(posLLH_[2], air_density_mat);
    } else{
        air = air.altitude_with_variation(altitude, variation_ratio_of_air_density);
    }

    if (Isp_file_exist){
        Isp_vac = interp_matrix((time - previous_stage_separation_time) * thrust_coeff, Isp_mat);  // Isp vac
    } else {
        Isp_vac = Isp_const;  // Isp vac
    }
    if (thrust_file_exist){
        thrust_vac = interp_matrix((time - previous_stage_separation_time) * thrust_coeff, thrust_mat, 1);
        // nozzle_exhaust_pressure = interp_matrix((time - previous_stage_separation_time) * thrust_coeff, thrust_mat, 2);
        if (thrust_vac != 0 &&
            time >= previous_stage_separation_time + burn_start_time &&
            time < previous_stage_separation_time + burn_start_time + burn_time / thrust_coeff &&
            time < previous_stage_separation_time + forced_cutoff_time) {
            is_powered = true;
        } else {
            is_powered = false;
        }
    } else {
        if (time >= previous_stage_separation_time + burn_start_time &&
            time < previous_stage_separation_time + burn_start_time + burn_time / thrust_coeff &&
            time < previous_stage_separation_time + forced_cutoff_time){
            thrust_vac = thrust_const;
            is_powered = true;
        } else {
            is_powered = false;
        }
    }
    if (is_powered){ // ---- powered flight ----
        Isp_vac                 *= Isp_coeff;
        thrust_vac              *= Isp_coeff * thrust_coeff;
        // nozzle_exhaust_pressure *= Isp_coeff * thrust_coeff;

        m_dot = thrust_vac / Isp_vac / g0;
        // thrust_momentum = thrust_vac - nozzle_exhaust_area * nozzle_exhaust_pressure;
        thrust = thrust_vac - nozzle_exhaust_area * air.pressure;
        if (m_dot > 0.0001) {
            Isp = thrust / m_dot / g0;
        } else {
            Isp = 0.0;
        }
    } else {
        thrust = 0.0;
        m_dot = 0.0;
        Isp = 0.0;
    }

    if (attitude_file_exist) {
        azimuth_target = deg2rad(interp_matrix(time, attitude_mat, 1));
        elevation_target = deg2rad(interp_matrix(time, attitude_mat, 2));
        if (attitude_mat.cols() == 4) {
            roll_target = deg2rad(interp_matrix(time, attitude_mat, 3));
        } else {
            roll_target = deg2rad(attitude_roll_const_deg);
        }
    } else {
        azimuth_target = deg2rad(attitude_azimuth_const_deg);
        elevation_target = deg2rad(attitude_elevation_const_deg);
        roll_target = deg2rad(attitude_roll_const_deg);
    }

    if (wind_file_exist) {
        double wind_u = interp_matrix(altitude, wind_mat_uv, 1);
        double wind_v = interp_matrix(altitude, wind_mat_uv, 2);
        wind_speed = sqrt(wind_u * wind_u + wind_v * wind_v);
        wind_direction = rad2deg(atan2(wind_u, wind_v)) + 180.;
    } else {
        wind_speed = wind_const[0];
        wind_direction = wind_const[1];
    }

    if (is_consider_neutrality) {
        pos_CG         = interp_matrix(time, CGXt_mat, 1);
        pos_Controller = interp_matrix(time, CGXt_mat, 2);

        pos_CG -= Xcg_offset;               // +X direction = -STA direction
        pos_Controller -= Xt_offset;        // +X direction = -STA direction
    }

    if (time >= later_stage_separation_time && is_separated == false){
        is_separated = true;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x590, %rsp            # imm = 0x590
movq %rdi, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
movsd %xmm1, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x490(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x8060
movq -0x490(%rbp), %rax
testb $0x1, 0x290(%rax)
je 0x18946
movq -0x490(%rbp), %rdi
addq $0xa80, %rdi            # imm = 0xA80
movl $0x2, %esi
callq 0x1de40
movq -0x490(%rbp), %rsi
movsd (%rax), %xmm0
movsd %xmm0, -0x4a0(%rbp)
addq $0x2b8, %rsi            # imm = 0x2B8
leaq -0x270(%rbp), %rdi
movq %rdi, -0x498(%rbp)
callq 0x8170
movsd -0x4a0(%rbp), %xmm0
movq -0x498(%rbp), %rdx
leaq -0x258(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x7840
jmp 0x188fd
leaq -0x138(%rbp), %rdi
leaq -0x258(%rbp), %rsi
movl $0x120, %edx            # imm = 0x120
callq 0x63b0
leaq -0x270(%rbp), %rdi
callq 0x8220
jmp 0x18985
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
leaq -0x270(%rbp), %rdi
callq 0x8220
jmp 0x19434
movq -0x490(%rbp), %rax
movsd -0x18(%rbp), %xmm0
movsd 0x270(%rax), %xmm1
leaq -0x3a0(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x7190
leaq -0x138(%rbp), %rdi
leaq -0x3a0(%rbp), %rsi
movl $0x120, %edx            # imm = 0x120
callq 0x63b0
movq -0x490(%rbp), %rax
testb $0x1, 0x430(%rax)
je 0x18a4a
movq -0x490(%rbp), %rsi
movsd -0x10(%rbp), %xmm0
movsd 0x748(%rsi), %xmm1
subsd %xmm1, %xmm0
movsd 0x4c0(%rsi), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x4b8(%rbp)
addq $0x458, %rsi            # imm = 0x458
leaq -0x3b8(%rbp), %rdi
movq %rdi, -0x4b0(%rbp)
callq 0x8170
movsd -0x4b8(%rbp), %xmm0
movq -0x4b0(%rbp), %rdi
movl $0x1, %esi
callq 0x3b470
movsd %xmm0, -0x4a8(%rbp)
jmp 0x18a02
movq -0x490(%rbp), %rax
movsd -0x4a8(%rbp), %xmm0
movsd %xmm0, 0x788(%rax)
leaq -0x3b8(%rbp), %rdi
callq 0x8220
jmp 0x18a61
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
leaq -0x3b8(%rbp), %rdi
callq 0x8220
jmp 0x19434
movq -0x490(%rbp), %rax
movsd 0x478(%rax), %xmm0
movsd %xmm0, 0x788(%rax)
movq -0x490(%rbp), %rax
testb $0x1, 0x480(%rax)
je 0x18be4
movq -0x490(%rbp), %rsi
movsd -0x10(%rbp), %xmm0
movsd 0x748(%rsi), %xmm1
subsd %xmm1, %xmm0
movsd 0x4c0(%rsi), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x4d0(%rbp)
addq $0x4a8, %rsi            # imm = 0x4A8
leaq -0x3d0(%rbp), %rdi
movq %rdi, -0x4c8(%rbp)
callq 0x8170
movsd -0x4d0(%rbp), %xmm0
movq -0x4c8(%rbp), %rdi
movl $0x1, %esi
callq 0x3b470
movsd %xmm0, -0x4c0(%rbp)
jmp 0x18ade
movq -0x490(%rbp), %rax
movsd -0x4c0(%rbp), %xmm0
movsd %xmm0, 0x778(%rax)
leaq -0x3d0(%rbp), %rdi
callq 0x8220
movq -0x490(%rbp), %rax
movsd 0x778(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x18b20
jp 0x18b20
jmp 0x18bd1
movq -0x490(%rbp), %rax
movsd -0x10(%rbp), %xmm0
movsd 0x748(%rax), %xmm1
addsd 0x4d0(%rax), %xmm1
ucomisd %xmm1, %xmm0
jb 0x18bd1
movq -0x490(%rbp), %rax
movsd -0x10(%rbp), %xmm1
movsd 0x748(%rax), %xmm0
addsd 0x4d0(%rax), %xmm0
movsd 0x4e0(%rax), %xmm2
divsd 0x4c0(%rax), %xmm2
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x18bd1
movq -0x490(%rbp), %rax
movsd -0x10(%rbp), %xmm1
movsd 0x748(%rax), %xmm0
addsd 0x4e8(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x18bd1
movq -0x490(%rbp), %rax
movb $0x1, 0x254(%rax)
jmp 0x18bdf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
leaq -0x3d0(%rbp), %rdi
callq 0x8220
jmp 0x19434
movq -0x490(%rbp), %rax
movb $0x0, 0x254(%rax)
jmp 0x18c8e
movq -0x490(%rbp), %rax
movsd -0x10(%rbp), %xmm0
movsd 0x748(%rax), %xmm1
addsd 0x4d0(%rax), %xmm1
ucomisd %xmm1, %xmm0
jb 0x18c7e
movq -0x490(%rbp), %rax
movsd -0x10(%rbp), %xmm1
movsd 0x748(%rax), %xmm0
addsd 0x4d0(%rax), %xmm0
movsd 0x4e0(%rax), %xmm2
divsd 0x4c0(%rax), %xmm2
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x18c7e
movq -0x490(%rbp), %rax
movsd -0x10(%rbp), %xmm1
movsd 0x748(%rax), %xmm0
addsd 0x4e8(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x18c7e
movq -0x490(%rbp), %rax
movsd 0x4c8(%rax), %xmm0
movsd %xmm0, 0x778(%rax)
movb $0x1, 0x254(%rax)
jmp 0x18c8c
movq -0x490(%rbp), %rax
movb $0x0, 0x254(%rax)
jmp 0x18c8e
movq -0x490(%rbp), %rax
testb $0x1, 0x254(%rax)
je 0x18d90
movq -0x490(%rbp), %rax
movsd 0x470(%rax), %xmm0
mulsd 0x788(%rax), %xmm0
movsd %xmm0, 0x788(%rax)
movsd 0x470(%rax), %xmm0
mulsd 0x4c0(%rax), %xmm0
mulsd 0x778(%rax), %xmm0
movsd %xmm0, 0x778(%rax)
movsd 0x778(%rax), %xmm0
divsd 0x788(%rax), %xmm0
divsd 0x758(%rax), %xmm0
movsd %xmm0, 0x790(%rax)
movsd 0x778(%rax), %xmm1
movsd 0x798(%rax), %xmm0
movsd -0x28(%rbp), %xmm2
movq %xmm0, %rcx
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
xorq %rdx, %rcx
movq %rcx, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x760(%rax)
movsd 0x790(%rax), %xmm0
movsd 0x3a103(%rip), %xmm1    # 0x52e50
ucomisd %xmm1, %xmm0
jbe 0x18d7c
movq -0x490(%rbp), %rax
movsd 0x760(%rax), %xmm0
divsd 0x790(%rax), %xmm0
divsd 0x758(%rax), %xmm0
movsd %xmm0, 0x780(%rax)
jmp 0x18d8e
movq -0x490(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x780(%rax)
jmp 0x18db8
movq -0x490(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x760(%rax)
xorps %xmm0, %xmm0
movsd %xmm0, 0x790(%rax)
xorps %xmm0, %xmm0
movsd %xmm0, 0x780(%rax)
movq -0x490(%rbp), %rax
testb $0x1, 0x5b8(%rax)
je 0x1901d
movq -0x490(%rbp), %rsi
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x4e8(%rbp)
addq $0x5e0, %rsi            # imm = 0x5E0
leaq -0x3e8(%rbp), %rdi
movq %rdi, -0x4e0(%rbp)
callq 0x8170
movsd -0x4e8(%rbp), %xmm0
movq -0x4e0(%rbp), %rdi
movl $0x1, %esi
callq 0x3b470
movsd %xmm0, -0x4d8(%rbp)
jmp 0x18e1d
movsd -0x4d8(%rbp), %xmm0
callq 0x1e000
movsd %xmm0, -0x4f0(%rbp)
jmp 0x18e34
movq -0x490(%rbp), %rax
movsd -0x4f0(%rbp), %xmm0
movsd %xmm0, 0x940(%rax)
leaq -0x3e8(%rbp), %rdi
callq 0x8220
movq -0x490(%rbp), %rsi
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x508(%rbp)
addq $0x5e0, %rsi            # imm = 0x5E0
leaq -0x400(%rbp), %rdi
movq %rdi, -0x500(%rbp)
callq 0x8170
movsd -0x508(%rbp), %xmm0
movq -0x500(%rbp), %rdi
movl $0x2, %esi
callq 0x3b470
movsd %xmm0, -0x4f8(%rbp)
jmp 0x18ea8
movsd -0x4f8(%rbp), %xmm0
callq 0x1e000
movsd %xmm0, -0x510(%rbp)
jmp 0x18ebf
movq -0x490(%rbp), %rax
movsd -0x510(%rbp), %xmm0
movsd %xmm0, 0x948(%rax)
leaq -0x400(%rbp), %rdi
callq 0x8220
movq -0x490(%rbp), %rdi
addq $0x5e0, %rdi            # imm = 0x5E0
callq 0x87e0
cmpq $0x4, %rax
jne 0x18ff8
movq -0x490(%rbp), %rsi
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x528(%rbp)
addq $0x5e0, %rsi            # imm = 0x5E0
leaq -0x418(%rbp), %rdi
movq %rdi, -0x520(%rbp)
callq 0x8170
movsd -0x528(%rbp), %xmm0
movq -0x520(%rbp), %rdi
movl $0x3, %esi
callq 0x3b470
movsd %xmm0, -0x518(%rbp)
jmp 0x18f50
movsd -0x518(%rbp), %xmm0
callq 0x1e000
movsd %xmm0, -0x530(%rbp)
jmp 0x18f67
movq -0x490(%rbp), %rax
movsd -0x530(%rbp), %xmm0
movsd %xmm0, 0x950(%rax)
leaq -0x418(%rbp), %rdi
callq 0x8220
jmp 0x1901b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
leaq -0x3e8(%rbp), %rdi
callq 0x8220
jmp 0x19434
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
leaq -0x400(%rbp), %rdi
callq 0x8220
jmp 0x19434
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
leaq -0x418(%rbp), %rdi
callq 0x8220
jmp 0x19434
movq -0x490(%rbp), %rax
movsd 0x608(%rax), %xmm0
callq 0x1e000
movq -0x490(%rbp), %rax
movsd %xmm0, 0x950(%rax)
jmp 0x19078
movq -0x490(%rbp), %rax
movsd 0x5f8(%rax), %xmm0
callq 0x1e000
movq -0x490(%rbp), %rax
movsd %xmm0, 0x940(%rax)
movsd 0x600(%rax), %xmm0
callq 0x1e000
movq -0x490(%rbp), %rax
movsd %xmm0, 0x948(%rax)
movsd 0x608(%rax), %xmm0
callq 0x1e000
movq -0x490(%rbp), %rax
movsd %xmm0, 0x950(%rax)
movq -0x490(%rbp), %rax
testb $0x1, 0x2d0(%rax)
je 0x19222
movq -0x490(%rbp), %rsi
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x548(%rbp)
addq $0x328, %rsi            # imm = 0x328
leaq -0x438(%rbp), %rdi
movq %rdi, -0x540(%rbp)
callq 0x8170
movsd -0x548(%rbp), %xmm0
movq -0x540(%rbp), %rdi
movl $0x1, %esi
callq 0x3b470
movsd %xmm0, -0x538(%rbp)
jmp 0x190dd
leaq -0x438(%rbp), %rdi
callq 0x8220
movsd -0x538(%rbp), %xmm0
movq -0x490(%rbp), %rsi
movsd %xmm0, -0x420(%rbp)
movsd -0x18(%rbp), %xmm0
movsd %xmm0, -0x560(%rbp)
addq $0x328, %rsi            # imm = 0x328
leaq -0x458(%rbp), %rdi
movq %rdi, -0x558(%rbp)
callq 0x8170
movsd -0x560(%rbp), %xmm0
movq -0x558(%rbp), %rdi
movl $0x2, %esi
callq 0x3b470
movsd %xmm0, -0x550(%rbp)
jmp 0x1914a
leaq -0x458(%rbp), %rdi
callq 0x8220
movsd -0x550(%rbp), %xmm0
movsd %xmm0, -0x440(%rbp)
movsd -0x420(%rbp), %xmm0
movsd -0x420(%rbp), %xmm2
movsd -0x440(%rbp), %xmm1
mulsd -0x440(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
callq 0x6390
movq -0x490(%rbp), %rax
movsd %xmm0, 0x918(%rax)
movsd -0x420(%rbp), %xmm0
movsd -0x440(%rbp), %xmm1
callq 0x60c0
callq 0x205a0
movq -0x490(%rbp), %rax
movsd 0x39c75(%rip), %xmm1    # 0x52e40
addsd %xmm1, %xmm0
movsd %xmm0, 0x920(%rax)
jmp 0x19273
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
leaq -0x438(%rbp), %rdi
callq 0x8220
jmp 0x19434
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
leaq -0x458(%rbp), %rdi
callq 0x8220
jmp 0x19434
movq -0x490(%rbp), %rdi
addq $0x2f8, %rdi            # imm = 0x2F8
xorl %eax, %eax
movl %eax, %esi
callq 0x1de40
movq -0x490(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, 0x918(%rdi)
addq $0x2f8, %rdi            # imm = 0x2F8
movl $0x1, %esi
callq 0x1de40
movq %rax, %rcx
movq -0x490(%rbp), %rax
movsd (%rcx), %xmm0
movsd %xmm0, 0x920(%rax)
movq -0x490(%rbp), %rax
testb $0x1, 0x680(%rax)
je 0x193f0
movq -0x490(%rbp), %rsi
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x578(%rbp)
addq $0x710, %rsi            # imm = 0x710
leaq -0x470(%rbp), %rdi
movq %rdi, -0x570(%rbp)
callq 0x8170
movsd -0x578(%rbp), %xmm0
movq -0x570(%rbp), %rdi
movl $0x1, %esi
callq 0x3b470
movsd %xmm0, -0x568(%rbp)
jmp 0x192d8
movq -0x490(%rbp), %rax
movsd -0x568(%rbp), %xmm0
movsd %xmm0, 0x978(%rax)
leaq -0x470(%rbp), %rdi
callq 0x8220
movq -0x490(%rbp), %rsi
movsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x590(%rbp)
addq $0x710, %rsi            # imm = 0x710
leaq -0x488(%rbp), %rdi
movq %rdi, -0x588(%rbp)
callq 0x8170
movsd -0x590(%rbp), %xmm0
movq -0x588(%rbp), %rdi
movl $0x2, %esi
callq 0x3b470
movsd %xmm0, -0x580(%rbp)
jmp 0x1934c
movq -0x490(%rbp), %rax
movsd -0x580(%rbp), %xmm0
movsd %xmm0, 0x990(%rax)
leaq -0x488(%rbp), %rdi
callq 0x8220
movq -0x490(%rbp), %rax
movsd 0x6c8(%rax), %xmm1
movsd 0x978(%rax), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x978(%rax)
movsd 0x6f8(%rax), %xmm1
movsd 0x990(%rax), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x990(%rax)
jmp 0x193f0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
leaq -0x470(%rbp), %rdi
callq 0x8220
jmp 0x19434
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
leaq -0x488(%rbp), %rdi
callq 0x8220
jmp 0x19434
movq -0x490(%rbp), %rax
movsd -0x10(%rbp), %xmm0
ucomisd 0x750(%rax), %xmm0
jb 0x1942b
movq -0x490(%rbp), %rax
movb 0x255(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x1942b
movq -0x490(%rbp), %rax
movb $0x1, 0x255(%rax)
addq $0x590, %rsp            # imm = 0x590
popq %rbp
retq
movq -0x278(%rbp), %rdi
callq 0x67a0