// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movsd %xmm2, 0x18(%rsp)
movaps %xmm1, 0x90(%rsp)
movsd %xmm0, 0x78(%rsp)
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %r12
callq 0x52cb
movsd %xmm0, 0x10(%rsp)
movq %r14, %rdi
callq 0x52cb
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
callq 0x52cb
movsd %xmm0, 0x20(%rsp)
movq %rbx, 0x70(%rsp)
movq %rbx, %rdi
callq 0x52cb
movsd 0x8(%rsp), %xmm2
movsd 0x10(%rsp), %xmm1
movsd %xmm0, 0x50(%rsp)
movq (%r14), %r13
movq %r14, 0x60(%rsp)
movq 0x8(%r14), %rdx
movq %rdx, %rbp
subq %r13, %rbp
sarq $0x3, %rbp
movq %r12, 0x58(%rsp)
movq (%r12), %r14
movq %rbp, %xmm0
punpckldq 0x327e(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x3286(%rip), %xmm0     # 0x6070
movapd %xmm0, %xmm6
unpckhpd %xmm0, %xmm6            # xmm6 = xmm6[1],xmm0[1]
addsd %xmm0, %xmm6
movq %r15, 0x68(%rsp)
movq (%r15), %r12
movapd 0x324a(%rip), %xmm0     # 0x6050
xorpd 0x90(%rsp), %xmm0
movapd %xmm0, 0xb0(%rsp)
movsd 0x18(%rsp), %xmm3
mulsd %xmm3, %xmm0
mulsd %xmm3, %xmm0
movapd %xmm0, 0xa0(%rsp)
xorl %eax, %eax
movsd 0x31df(%rip), %xmm7     # 0x6018
movsd 0x31df(%rip), %xmm0     # 0x6020
movsd 0x31be(%rip), %xmm9     # 0x6008
movq %rdx, 0x48(%rsp)
movapd %xmm6, 0xc0(%rsp)
movq 0x60(%rsp), %rbx
movq 0x58(%rsp), %r15
ucomisd 0x31be(%rip), %xmm0     # 0x6028
jbe 0x3196
movq %rax, 0x28(%rsp)
xorpd %xmm8, %xmm8
xorpd %xmm10, %xmm10
cmpq %r13, %rdx
je 0x2eaa
xorl %eax, %eax
movsd (%r13,%rax,8), %xmm0
movsd (%r14,%rax,8), %xmm3
addsd %xmm7, %xmm3
divsd %xmm3, %xmm0
addsd %xmm0, %xmm10
incq %rax
movl %eax, %ecx
cmpq %rcx, %rbp
ja 0x2e86
cmpq %r13, %rdx
movsd 0x20(%rsp), %xmm5
je 0x2ee4
xorpd %xmm8, %xmm8
xorl %eax, %eax
movsd (%r13,%rax,8), %xmm0
movsd (%r14,%rax,8), %xmm3
addsd %xmm7, %xmm3
mulsd %xmm3, %xmm3
divsd %xmm3, %xmm0
addsd %xmm0, %xmm8
incq %rax
movl %eax, %ecx
cmpq %rcx, %rbp
ja 0x2ebc
divsd %xmm6, %xmm10
cmpq %r13, %rdx
je 0x2f41
xorpd %xmm0, %xmm0
xorl %eax, %eax
xorpd %xmm9, %xmm9
movsd (%r13,%rax,8), %xmm3
movsd (%r14,%rax,8), %xmm4
addsd %xmm7, %xmm4
divsd %xmm4, %xmm3
movsd (%r12,%rax,8), %xmm4
subsd %xmm5, %xmm4
mulsd %xmm3, %xmm4
addsd %xmm4, %xmm9
movapd %xmm3, %xmm4
subsd %xmm10, %xmm4
mulsd %xmm3, %xmm4
addsd %xmm4, %xmm0
incq %rax
movl %eax, %ecx
cmpq %rcx, %rbp
ja 0x2ef9
divsd %xmm0, %xmm9
movapd %xmm9, 0xd0(%rsp)
divsd %xmm6, %xmm8
movsd %xmm8, 0x38(%rsp)
movapd %xmm9, %xmm0
mulsd %xmm10, %xmm0
movapd %xmm5, %xmm3
subsd %xmm0, %xmm3
movq %rbx, %rdi
movq 0x70(%rsp), %rsi
movq %r15, %rdx
movsd 0x50(%rsp), %xmm0
movsd %xmm3, 0x88(%rsp)
movsd %xmm7, 0x40(%rsp)
movsd %xmm10, 0x30(%rsp)
callq 0x2c31
movsd %xmm0, 0x80(%rsp)
movq %r15, %rdi
movq %rbx, %rsi
movq 0x68(%rsp), %rdx
movsd 0x20(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
movsd 0x38(%rsp), %xmm2
movsd 0x40(%rsp), %xmm3
callq 0x2afd
movapd 0xd0(%rsp), %xmm12
movsd 0x38(%rsp), %xmm11
movsd 0x8(%rsp), %xmm2
movsd 0x10(%rsp), %xmm1
movapd %xmm12, %xmm6
xorpd 0x3060(%rip), %xmm6     # 0x6050
mulsd %xmm12, %xmm11
movsd 0x30(%rsp), %xmm3
mulsd %xmm0, %xmm3
subsd %xmm3, %xmm11
movsd 0x2ffb(%rip), %xmm9     # 0x6008
movapd %xmm9, %xmm7
movq 0x48(%rsp), %rdx
cmpq %r13, %rdx
movq 0x28(%rsp), %rax
je 0x3067
xorpd %xmm3, %xmm3
xorl %eax, %eax
xorpd %xmm7, %xmm7
movsd (%r13,%rax,8), %xmm4
movsd (%r14,%rax,8), %xmm5
subsd %xmm1, %xmm5
mulsd %xmm4, %xmm5
addsd %xmm5, %xmm7
movapd %xmm4, %xmm5
subsd %xmm2, %xmm5
mulsd %xmm4, %xmm5
addsd %xmm5, %xmm3
incq %rax
movl %eax, %ecx
cmpq %rcx, %rbp
ja 0x302b
divsd %xmm3, %xmm7
movq 0x28(%rsp), %rax
mulsd %xmm11, %xmm7
mulsd 0xb0(%rsp), %xmm11
mulsd %xmm6, %xmm7
movsd 0x80(%rsp), %xmm3
mulsd %xmm3, %xmm0
subsd %xmm7, %xmm0
divsd %xmm3, %xmm12
mulsd %xmm3, %xmm3
divsd %xmm3, %xmm0
movsd 0x40(%rsp), %xmm10
movapd %xmm10, %xmm6
addsd 0x78(%rsp), %xmm6
movsd 0x2f6d(%rip), %xmm8     # 0x6020
movapd %xmm8, %xmm7
subsd %xmm12, %xmm7
movapd 0x90(%rsp), %xmm5
movsd 0x88(%rsp), %xmm4
mulsd %xmm5, %xmm4
movapd %xmm8, %xmm3
subsd %xmm4, %xmm3
mulsd %xmm5, %xmm7
movsd 0x18(%rsp), %xmm4
mulsd %xmm4, %xmm7
mulsd %xmm4, %xmm7
mulsd %xmm3, %xmm7
subsd %xmm7, %xmm6
movapd 0xa0(%rsp), %xmm7
mulsd %xmm11, %xmm7
addsd %xmm8, %xmm7
mulsd %xmm5, %xmm0
mulsd %xmm4, %xmm0
mulsd %xmm4, %xmm0
mulsd %xmm3, %xmm0
addsd %xmm7, %xmm0
mulsd %xmm5, %xmm12
mulsd %xmm4, %xmm12
mulsd %xmm4, %xmm12
mulsd %xmm11, %xmm12
addsd %xmm0, %xmm12
divsd %xmm12, %xmm6
movapd %xmm10, %xmm7
subsd %xmm6, %xmm7
movapd %xmm7, %xmm0
subsd %xmm10, %xmm0
andpd 0x2f2b(%rip), %xmm0     # 0x6080
addsd %xmm7, %xmm10
mulsd 0x2ecd(%rip), %xmm10    # 0x6030
divsd %xmm10, %xmm0
incl %eax
cmpl $0x32, %eax
movapd 0xc0(%rsp), %xmm6
jne 0x2e62
movq 0x5e55(%rip), %rdi      # 0x8fd8
leaq 0x2f3c(%rip), %rsi      # 0x60c6
callq 0x2140
xorl %edi, %edi
callq 0x2190
movapd %xmm7, %xmm0
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq