// File: JorisCaze[P]NASG-EOS_Calibration/src/calib_liq-vap.cpp
// Function: computePsatTh(double, double, double, double, double, double, double)
double computePsatTh(double A, double B, double C, double D, double E, double pinfL, double T)
{
    // Purpose : compute saturated pressure Psat at a given temperature
    // See eq. (41), fp is obtained with exp((41))
    // More : Newton-Raphson algo. is used
    double fp, dfp, p1(1.e5), p2(0.), err(1.), k(0.);
    int count(0);

    while (err > 1.e-5 && count < 50) {
        k = exp(A+B/T+C*log(T));
        fp = p1 - k*exp(E*p1/T)*pow((p1+pinfL),D);
        dfp = 1. - k*exp(E*p1/T)*((E/T)*pow((p1+pinfL),D) + D*pow((p1+pinfL),(D-1.)));
        p2 = p1 - fp/dfp;
        err = fabs(p2-p1)/(0.5*(p1+p2));
        p1 = p2;
        count++;
        if (count >= 50) 
            cout << "Warning : newton-raphson of Psat(T) function not converged\n";
    }
    if (p2 < 1.e-6)
        return 0.;
    else
        return p2;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movsd %xmm0, -0x10(%rbp)
movsd %xmm1, -0x18(%rbp)
movsd %xmm2, -0x20(%rbp)
movsd %xmm3, -0x28(%rbp)
movsd %xmm4, -0x30(%rbp)
movsd %xmm5, -0x38(%rbp)
movsd %xmm6, -0x40(%rbp)
movsd 0x43ca(%rip), %xmm0     # 0x8060
movsd %xmm0, -0x58(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
movsd 0x4395(%rip), %xmm0     # 0x8040
movsd %xmm0, -0x68(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
movsd -0x68(%rbp), %xmm0
xorl %eax, %eax
movsd 0x4382(%rip), %xmm1     # 0x8050
ucomisd %xmm1, %xmm0
movb %al, -0x75(%rbp)
jbe 0x3ce1
cmpl $0x32, -0x74(%rbp)
setl %al
movb %al, -0x75(%rbp)
movb -0x75(%rbp), %al
testb $0x1, %al
jne 0x3ced
jmp 0x3f39
movsd -0x10(%rbp), %xmm0
movsd -0x18(%rbp), %xmm1
divsd -0x40(%rbp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0xc8(%rbp)
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0xc0(%rbp)
movsd -0x40(%rbp), %xmm0
callq 0x2130
movsd -0xc8(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0xc0(%rbp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
callq 0x22f0
movsd %xmm0, -0x70(%rbp)
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd -0x70(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
movsd -0x30(%rbp), %xmm0
mulsd -0x58(%rbp), %xmm0
divsd -0x40(%rbp), %xmm0
callq 0x22f0
movaps %xmm0, %xmm1
movsd -0xb8(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0x58(%rbp), %xmm0
addsd -0x38(%rbp), %xmm0
movsd -0x28(%rbp), %xmm1
callq 0x22c0
movsd -0xb0(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0xa8(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x70(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0x30(%rbp), %xmm0
mulsd -0x58(%rbp), %xmm0
divsd -0x40(%rbp), %xmm0
callq 0x22f0
movaps %xmm0, %xmm1
movsd -0xa0(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x30(%rbp), %xmm0
divsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
movsd -0x58(%rbp), %xmm0
addsd -0x38(%rbp), %xmm0
movsd -0x28(%rbp), %xmm1
callq 0x22c0
movsd %xmm0, -0x88(%rbp)
movsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x58(%rbp), %xmm0
addsd -0x38(%rbp), %xmm0
movsd -0x28(%rbp), %xmm1
movsd 0x41e5(%rip), %xmm2     # 0x8040
subsd %xmm2, %xmm1
callq 0x22c0
movsd -0x98(%rbp), %xmm2
movsd -0x90(%rbp), %xmm1
movsd -0x88(%rbp), %xmm3
movaps %xmm0, %xmm4
movsd -0x80(%rbp), %xmm0
mulsd %xmm4, %xmm2
mulsd %xmm3, %xmm1
addsd %xmm2, %xmm1
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm1, %xmm0
movsd 0x418d(%rip), %xmm1     # 0x8040
addsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x58(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
divsd -0x50(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
movsd -0x60(%rbp), %xmm0
subsd -0x58(%rbp), %xmm0
movaps 0x414b(%rip), %xmm1     # 0x8030
pand %xmm1, %xmm0
movsd -0x58(%rbp), %xmm2
addsd -0x60(%rbp), %xmm2
movsd 0x415d(%rip), %xmm1     # 0x8058
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
cmpl $0x32, -0x74(%rbp)
jl 0x3f34
movq 0x70b0(%rip), %rdi      # 0xafd8
leaq 0x4177(%rip), %rsi      # 0x80a6
callq 0x2180
jmp 0x3cbf
movsd 0x4127(%rip), %xmm0     # 0x8068
ucomisd -0x60(%rbp), %xmm0
jbe 0x3f52
xorps %xmm0, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x3f5c
movsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
movsd -0x8(%rbp), %xmm0
addq $0xd0, %rsp
popq %rbp
retq
nopw (%rax,%rax)