// File: JorisCaze[P]NASG-EOS_Calibration/src/calib_liq-vap.cpp
// Function: computePinfL(std::vector<double, std::allocator<double>> const&, std::vector<double, std::allocator<double>> const&, std::vector<double, std::allocator<double>> const&, std::vector<double, std::allocator<double>> const&, double, double, double)
double computePinfL(vector<double> const& psatExp, vector<double> const& Texp, vector<double> const& vLexp, vector<double> const& hLexp, double p0, double ro0, double c0)
{
    // Purpose : compute pinfL parameter of liquid phase using the Newton-Raphson procedure
    // More : liquid reference state and experimental data are used
    // See eq. (68)
    double fp, dfp, dfp1, dfp2, pinf1(2.e5), pinf2(0.), err(1.);
    double mp, mT, mvL, mhL, mTp, mTp2;
    double diffC, bL, cpL, dDiffC, dbL, dcpL;
    int count(0);

    mp = meanValue(psatExp);
    mT = meanValue(Texp);
    mvL = meanValue(vLexp);
    mhL = meanValue(hLexp);

    while (err > 1.e-5 && count < 50) {
        mTp = computeMeanTp(psatExp,Texp,pinf1);
        mTp2 = computeMeanTp2(psatExp,Texp,pinf1);

        diffC = computeHeatCapDiffL(psatExp,Texp,vLexp,mvL,mTp,pinf1);
        bL = computebL(mvL,mTp,diffC);
        cpL = computeCpL(Texp,hLexp,psatExp,mhL,mp,mT,bL);
        
        dDiffC = computeDHeatCapDiffL(psatExp,Texp,vLexp,mvL,mTp,mTp2,pinf1);
        dbL = computeDbL(mTp,mTp2,diffC,dDiffC); 
        dcpL = computeDcpL(Texp,psatExp,mp,mT,dbL); 
        
        dfp1 = -ro0*dbL;
        dfp2 = (dDiffC*cpL-diffC*dcpL)/(cpL*cpL);

        fp = p0 + pinf1 - (1.-diffC/cpL)*ro0*c0*c0*(1.-bL*ro0);
        dfp = 1. - ro0*c0*c0*dfp1 + dfp2*ro0*c0*c0*(1.-bL*ro0) + (diffC/cpL)*ro0*c0*c0*dfp1;

        pinf2 = pinf1 - fp/dfp;
        err = fabs(pinf2-pinf1)/(0.5*(pinf1+pinf2));
        pinf1 = pinf2;
        count++;
        if (count >= 50) {
            cout << "Warning : newton-raphson of Psat(T) function not converged\n"; exit(0);
        }
    }
    // cout << "Number of iteration NR pinfL : " << count << endl;
    return pinf1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movsd %xmm0, -0x28(%rbp)
movsd %xmm1, -0x30(%rbp)
movsd %xmm2, -0x38(%rbp)
movsd 0x4936(%rip), %xmm0     # 0x8048
movsd %xmm0, -0x60(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
movsd 0x4919(%rip), %xmm0     # 0x8040
movsd %xmm0, -0x70(%rbp)
movl $0x0, -0xd4(%rbp)
movq -0x8(%rbp), %rdi
callq 0x74d0
movsd %xmm0, -0x78(%rbp)
movq -0x10(%rbp), %rdi
callq 0x74d0
movsd %xmm0, -0x80(%rbp)
movq -0x18(%rbp), %rdi
callq 0x74d0
movsd %xmm0, -0x88(%rbp)
movq -0x20(%rbp), %rdi
callq 0x74d0
movsd %xmm0, -0x90(%rbp)
movsd -0x70(%rbp), %xmm0
xorl %eax, %eax
movsd 0x48cd(%rip), %xmm1     # 0x8050
ucomisd %xmm1, %xmm0
movb %al, -0xd5(%rbp)
jbe 0x379f
cmpl $0x32, -0xd4(%rbp)
setl %al
movb %al, -0xd5(%rbp)
movb -0xd5(%rbp), %al
testb $0x1, %al
jne 0x37ae
jmp 0x3b49
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movsd -0x60(%rbp), %xmm0
callq 0x2d40
movsd %xmm0, -0x98(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movsd -0x60(%rbp), %xmm0
callq 0x2e20
movsd %xmm0, -0xa0(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movsd -0x88(%rbp), %xmm0
movsd -0x98(%rbp), %xmm1
movsd -0x60(%rbp), %xmm2
callq 0x2f20
movsd %xmm0, -0xa8(%rbp)
movsd -0x88(%rbp), %xmm0
movsd -0x98(%rbp), %xmm1
movsd -0xa8(%rbp), %xmm2
callq 0x3300
movsd %xmm0, -0xb0(%rbp)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x8(%rbp), %rdx
movsd -0x90(%rbp), %xmm0
movsd -0x78(%rbp), %xmm1
movsd -0x80(%rbp), %xmm2
movsd -0xb0(%rbp), %xmm3
callq 0x33a0
movsd %xmm0, -0xb8(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movsd -0x88(%rbp), %xmm0
movsd -0x98(%rbp), %xmm1
movsd -0xa0(%rbp), %xmm2
movsd -0x60(%rbp), %xmm3
callq 0x3040
movsd %xmm0, -0xc0(%rbp)
movsd -0x98(%rbp), %xmm0
movsd -0xa0(%rbp), %xmm1
movsd -0xa8(%rbp), %xmm2
movsd -0xc0(%rbp), %xmm3
callq 0x3350
movsd %xmm0, -0xc8(%rbp)
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movsd -0x78(%rbp), %xmm0
movsd -0x80(%rbp), %xmm1
movsd -0xc8(%rbp), %xmm2
callq 0x3540
movsd %xmm0, -0xd0(%rbp)
movsd -0x30(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd -0xc8(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0xc0(%rbp), %xmm0
movsd -0xb8(%rbp), %xmm2
movsd -0xa8(%rbp), %xmm1
mulsd -0xd0(%rbp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd -0xb8(%rbp), %xmm1
mulsd -0xb8(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x28(%rbp), %xmm1
addsd -0x60(%rbp), %xmm1
movsd -0xa8(%rbp), %xmm2
divsd -0xb8(%rbp), %xmm2
movsd 0x46ab(%rip), %xmm0     # 0x8040
subsd %xmm2, %xmm0
mulsd -0x30(%rbp), %xmm0
mulsd -0x38(%rbp), %xmm0
mulsd -0x38(%rbp), %xmm0
movsd -0xb0(%rbp), %xmm2
movsd -0x30(%rbp), %xmm3
movq %xmm2, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm2
mulsd %xmm3, %xmm2
movsd 0x4668(%rip), %xmm3     # 0x8040
addsd %xmm3, %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x30(%rbp), %xmm0
mulsd -0x38(%rbp), %xmm0
mulsd -0x38(%rbp), %xmm0
movsd -0x50(%rbp), %xmm1
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm1, %xmm0
movsd 0x4609(%rip), %xmm1     # 0x8040
addsd %xmm1, %xmm0
movsd -0x58(%rbp), %xmm1
mulsd -0x30(%rbp), %xmm1
mulsd -0x38(%rbp), %xmm1
mulsd -0x38(%rbp), %xmm1
movsd -0xb0(%rbp), %xmm2
movsd -0x30(%rbp), %xmm3
movq %xmm2, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm2
mulsd %xmm3, %xmm2
movsd 0x45c1(%rip), %xmm3     # 0x8040
addsd %xmm3, %xmm2
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd -0xa8(%rbp), %xmm0
divsd -0xb8(%rbp), %xmm0
mulsd -0x30(%rbp), %xmm0
mulsd -0x38(%rbp), %xmm0
mulsd -0x38(%rbp), %xmm0
movsd -0x50(%rbp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x60(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
divsd -0x48(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x68(%rbp), %xmm0
subsd -0x60(%rbp), %xmm0
movaps 0x454b(%rip), %xmm1     # 0x8030
pand %xmm1, %xmm0
movsd -0x60(%rbp), %xmm2
addsd -0x68(%rbp), %xmm2
movsd 0x455d(%rip), %xmm1     # 0x8058
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x68(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movl -0xd4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd4(%rbp)
cmpl $0x32, -0xd4(%rbp)
jl 0x3b44
movq 0x74a7(%rip), %rdi      # 0xafd8
leaq 0x456e(%rip), %rsi      # 0x80a6
callq 0x2180
xorl %edi, %edi
callq 0x2210
jmp 0x3774
movsd -0x60(%rbp), %xmm0
addq $0xe0, %rsp
popq %rbp
retq
nopw (%rax,%rax)