// File: /workspace/github_c_makefile_star2_O1/PoulinV[P]AxiCLASS/build/../source/thermodynamics.c
// Function: thermodynamics_ionization_fractions
int thermodynamics_ionization_fractions(
                                        double z,
                                        double * y,
                                        struct background * pba,
                                        struct thermodynamics * pth,
                                        struct thermo_workspace * ptw,
                                        int current_ap
                                        ) {

  /** Summary: */

  /** Define local variables */
  struct thermo_diffeq_workspace * ptdw = ptw->ptdw;
  struct thermo_vector * ptv = ptdw->ptv;

  /* Thermo quantities */
  double x_H, x_He, xHeII, x=0., Tmat;
  /* Analytical quantities */
  double rhs, sqrt_val;

  /* Varying fundamental constants (according to 1705.03925) */
  double rescale_rhs = 1., rescale_T = 1.;
  double alpha = 1., me = 1.;

  if (pth->has_varconst == _TRUE_) {
    class_call(background_varconst_of_z(pba, z, &alpha, &me),
               pba->error_message,
               pth->error_message);
    rescale_rhs = alpha*alpha*alpha*me*me*me;
    rescale_T = 1./alpha/alpha/me;
  }

  /** - Calculate x_noreio from Recfast/Hyrec in each approximation
      regime. Store the results in the workspace. */
  /** - --> For credits, see external/wrap_recfast.c */

  /* Set Tmat from the y vector (it is always evolved). */
  Tmat = y[ptv->index_ti_D_Tmat] + ptw->Tcmb*(1.+z);
  if (pth->has_varconst == _TRUE_) {
    Tmat *= rescale_T;
  }

  /** - --> first regime: H and Helium fully ionized */
  if (current_ap == ptdw->index_ap_brec || (ptw->has_ap_idmtca && current_ap == ptdw->index_ap_idmtca)) {

    /* This is equivalent to the formula for HeIII --> HeII in Saha, just using rhs' = 1/rhs */
    rhs = ptw->SIunit_nH0/exp( 1.5*log(ptw->const_NR_numberdens*Tmat/(1.+z)/(1.+z)) - ptw->const_Tion_HeII/Tmat );
    if (pth->has_varconst == _TRUE_) {
      rhs /= rescale_rhs;
    }

    sqrt_val = sqrt(pow(1.-rhs*(1.+ptw->fHe),2) + 4.*rhs*(1.+2*ptw->fHe));

    x = 2.*(1+2.*ptw->fHe)/(1.-rhs*(1.+ptw->fHe) + sqrt_val);

    ptdw->x_H = 1.;
    ptdw->x_He = 1.;

  }
  /** - --> second regime: first Helium recombination (analytic approximation) */
  else if (current_ap == ptdw->index_ap_He1) {

    /* Assuming Saha equilibrium for HeIII --> HeII */
    rhs = exp( 1.5*log(ptw->const_NR_numberdens*Tmat/(1.+z)/(1.+z)) - ptw->const_Tion_HeII/Tmat ) / ptw->SIunit_nH0;
    if (pth->has_varconst == _TRUE_) {
      rhs *= rescale_rhs;
    }

    sqrt_val = sqrt(pow((rhs-1.-ptw->fHe),2) + 4.*(1.+2.*ptw->fHe)*rhs);

    x = 0.5*(sqrt_val - (rhs-1.-ptw->fHe));

    ptdw->x_H = 1.;
    ptdw->x_He = 1.;

  }
  /** - --> third regime: first Helium recombination finished, H and Helium fully ionized */
  else if (current_ap == ptdw->index_ap_He1f) {

    /* Assuming Saha equilibrium for HeII --> HeI with HII fully ionized, again expanding in rhs' = 1/rhs compared to below */
    rhs = 0.25*ptw->SIunit_nH0/exp( 1.5*log(ptw->const_NR_numberdens*Tmat/(1.+z)/(1.+z)) - ptw->const_Tion_HeI/Tmat );
    if (pth->has_varconst == _TRUE_) {
      rhs /= rescale_rhs;
    }

    sqrt_val = sqrt(pow(1.-rhs,2) + 4.*rhs*(1.+ptw->fHe));

    x = 2.*(1+ptw->fHe)/(1.-rhs + sqrt_val);

    ptdw->x_H = 1.;
    ptdw->x_He = 1.;

  }
  /** - --> fourth regime: second Helium recombination starts (analytic approximation) */
  else if (current_ap == ptdw->index_ap_He2) {

    /* Assuming Saha equilibrium for HeII --> HeI with HII fully ionized */
    rhs = 4.*exp(1.5*log(ptw->const_NR_numberdens*Tmat/(1.+z)/(1.+z)) - ptw->const_Tion_HeI/Tmat ) / ptw->SIunit_nH0;
    if (pth->has_varconst == _TRUE_) {
      rhs *= rescale_rhs;
    }

    sqrt_val = sqrt(pow((rhs-1.),2) + 4.*(1.+ptw->fHe)*rhs );

    x = 0.5*(sqrt_val - (rhs-1.));

    ptdw->x_H = 1.;
    ptdw->x_He = (x-1.)/ptw->fHe;

  }
  /** - --> fifth regime: Hydrogen recombination starts (analytic approximation)
      while Helium recombination continues (full equation) */
  else if (current_ap == ptdw->index_ap_H) {

    rhs = exp(1.5*log(ptw->const_NR_numberdens*Tmat/(1.+z)/(1.+z)) - ptw->const_Tion_H/Tmat)/ptw->SIunit_nH0;
    if (pth->has_varconst == _TRUE_) {
      rhs *= rescale_rhs;
    }

    /* Assuming Saha equilibrium for HII->HI. Includes xHeII corrections from incomplete recombination of HeII --> HeI (non-zero x_HeII) */
    xHeII = y[ptv->index_ti_x_He]*ptw->fHe;
    x_H = 2./(1.+xHeII/rhs + sqrt((1.+xHeII/rhs)*(1.+xHeII/rhs)+4./rhs));

    x_He = y[ptv->index_ti_x_He];
    x = x_H + ptw->fHe * x_He;

    ptdw->x_H = x_H;
    ptdw->x_He = x_He;

  }
  /** - --> sixth regime: full Hydrogen and Helium equations */
  else if (current_ap == ptdw->index_ap_frec) {
    x_H = y[ptv->index_ti_x_H];
    x_He = y[ptv->index_ti_x_He];
    x = x_H + ptw->fHe * x_He;

    ptdw->x_H = x_H;
    ptdw->x_He = x_He;

  }
  /** - --> seventh regime: calculate x_noreio during reionization
      (i.e. x before taking reionisation into account) */
  else if (current_ap == ptdw->index_ap_reio) {

    x_H = y[ptv->index_ti_x_H];
    x_He = y[ptv->index_ti_x_He];
    x = x_H + ptw->fHe * x_He;

    ptdw->x_H = x_H;
    ptdw->x_He = x_He;

  }

  ptdw->x_noreio = x;

  /** - If z is during reionization, also calculate the reionized x */
  if (current_ap == ptdw->index_ap_reio) {

    /* set x from the evolver (which is very low ~10^-4) as 'xe_before' */
    ptw->ptrp->reionization_parameters[ptw->ptrp->index_re_xe_before] = x;

    /* compute x */
    class_call(thermodynamics_reionization_function(z,pth,ptw->ptrp,&x),
               pth->error_message,
               pth->error_message);
  }

  ptdw->x_reio = x;

  return _SUCCESS_;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x858, %rsp            # imm = 0x858
movl %r8d, %ebx
movq %rcx, %r14
movapd %xmm0, %xmm2
movq 0x80(%rcx), %r13
movq 0xb8(%r13), %rbp
movq $0x0, 0x8(%rsp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x38(%rsp)
movq %rax, 0x30(%rsp)
movq %rdx, %r15
cmpw $0x1, 0xe78(%rdx)
movsd %xmm0, (%rsp)
jne 0x72a16
movq %rsi, %r12
movq %r14, 0x10(%rsp)
movq %r13, %r14
movq %rbp, %r13
movq %rdi, %rbp
leaq 0x38(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %r12, %rdi
movapd %xmm2, %xmm0
callq 0x60fe9
cmpl $0x1, %eax
jne 0x72a24
addq $0xd1c, %r12            # imm = 0xD1C
leaq 0x7ea15(%rip), %rsi     # 0xf13f0
leaq 0x9de3e(%rip), %rdx     # 0x110820
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x30c2c
addq $0x1064, %r15           # imm = 0x1064
leaq 0x7e632(%rip), %rsi     # 0xf1034
leaq 0xa2266(%rip), %rdx     # 0x114c6f
movq %r15, %rdi
movl $0xf8b, %ecx            # imm = 0xF8B
jmp 0x73099
movsd 0x7e842(%rip), %xmm3    # 0xf1260
movapd %xmm3, %xmm0
jmp 0x72a6f
movsd 0x38(%rsp), %xmm1
movapd %xmm1, %xmm3
mulsd %xmm1, %xmm3
mulsd %xmm1, %xmm3
movsd 0x30(%rsp), %xmm2
mulsd %xmm2, %xmm3
mulsd %xmm2, %xmm3
mulsd %xmm2, %xmm3
movsd 0x7e810(%rip), %xmm0    # 0xf1260
divsd %xmm1, %xmm0
divsd %xmm1, %xmm0
divsd %xmm2, %xmm0
movsd (%rsp), %xmm2
movq %rbp, %rdi
movq %r13, %rbp
movq %r14, %r13
movq 0x10(%rsp), %r14
movslq 0xc(%rbp), %rax
movsd 0x7e7e5(%rip), %xmm1    # 0xf1260
addsd %xmm2, %xmm1
movsd 0x38(%r14), %xmm4
mulsd %xmm1, %xmm4
addsd (%rdi,%rax,8), %xmm4
movq %r15, %r12
cmpw $0x1, 0xe78(%r15)
jne 0x72aa0
mulsd %xmm0, %xmm4
cmpl %ebx, 0x74(%r13)
movsd %xmm3, 0x28(%rsp)
movsd %xmm4, 0x10(%rsp)
je 0x72ac8
cmpw $0x0, 0x60(%r14)
je 0x72b84
cmpl %ebx, 0x70(%r13)
jne 0x72b84
movsd 0x30(%r14), %xmm0
movaps %xmm0, 0x40(%rsp)
movsd 0x40(%r14), %xmm0
mulsd %xmm4, %xmm0
divsd %xmm1, %xmm0
divsd %xmm1, %xmm0
callq 0x82c0
movsd 0x58(%r14), %xmm1
divsd 0x10(%rsp), %xmm1
mulsd 0x814ba(%rip), %xmm0    # 0xf3fb8
subsd %xmm1, %xmm0
callq 0x8670
movapd 0x40(%rsp), %xmm4
divsd %xmm0, %xmm4
cmpw $0x1, 0xe78(%r12)
jne 0x72b23
divsd 0x28(%rsp), %xmm4
movsd 0x20(%r14), %xmm0
movsd 0x7e72f(%rip), %xmm3    # 0xf1260
movapd %xmm0, %xmm1
addsd %xmm3, %xmm1
movapd 0x7e6ef(%rip), %xmm5    # 0xf1230
xorpd %xmm4, %xmm5
mulsd %xmm4, %xmm1
movapd %xmm3, %xmm2
subsd %xmm1, %xmm2
mulsd 0x818cf(%rip), %xmm4    # 0xf4428
mulsd %xmm2, %xmm2
addsd %xmm0, %xmm0
addsd %xmm3, %xmm0
mulsd %xmm4, %xmm0
addsd %xmm2, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x72c2e
sqrtsd %xmm0, %xmm0
jmp 0x72c47
cmpl %ebx, 0x78(%r13)
jne 0x72c8f
movsd 0x40(%r14), %xmm0
mulsd %xmm4, %xmm0
divsd %xmm1, %xmm0
divsd %xmm1, %xmm0
callq 0x82c0
movsd 0x58(%r14), %xmm1
divsd 0x10(%rsp), %xmm1
mulsd 0x813ff(%rip), %xmm0    # 0xf3fb8
subsd %xmm1, %xmm0
callq 0x8670
divsd 0x30(%r14), %xmm0
cmpw $0x1, 0xe78(%r12)
jne 0x72bda
mulsd 0x28(%rsp), %xmm0
movsd 0x7ec06(%rip), %xmm3    # 0xf17e8
addsd %xmm0, %xmm3
movsd 0x20(%r14), %xmm1
movapd %xmm3, %xmm2
subsd %xmm1, %xmm2
addsd %xmm1, %xmm1
addsd 0x7e660(%rip), %xmm1    # 0xf1260
mulsd %xmm2, %xmm2
mulsd 0x8181c(%rip), %xmm1    # 0xf4428
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jb 0x72d46
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0x72d5b
movapd %xmm5, 0x10(%rsp)
callq 0x82b0
movapd 0x10(%rsp), %xmm5
movsd 0x7e619(%rip), %xmm3    # 0xf1260
movsd 0x20(%r14), %xmm1
movapd %xmm1, %xmm2
addsd %xmm1, %xmm2
addsd %xmm3, %xmm2
addsd %xmm2, %xmm2
addsd %xmm3, %xmm1
mulsd %xmm1, %xmm5
addsd %xmm3, %xmm5
addsd %xmm0, %xmm5
divsd %xmm5, %xmm2
movsd %xmm2, 0x8(%rsp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%r13)
movsd (%rsp), %xmm2
jmp 0x73018
cmpl %ebx, 0x7c(%r13)
jne 0x72d93
movsd 0x30(%r14), %xmm0
mulsd 0x818c9(%rip), %xmm0    # 0xf4570
movsd %xmm0, 0x40(%rsp)
movsd 0x40(%r14), %xmm0
mulsd %xmm4, %xmm0
divsd %xmm1, %xmm0
divsd %xmm1, %xmm0
callq 0x82c0
movsd 0x50(%r14), %xmm1
divsd 0x10(%rsp), %xmm1
mulsd 0x812e0(%rip), %xmm0    # 0xf3fb8
subsd %xmm1, %xmm0
callq 0x8670
movsd 0x40(%rsp), %xmm2
divsd %xmm0, %xmm2
cmpw $0x1, 0xe78(%r12)
jne 0x72cfd
divsd 0x28(%rsp), %xmm2
movsd 0x7e55b(%rip), %xmm3    # 0xf1260
movapd %xmm3, %xmm4
subsd %xmm2, %xmm4
movapd %xmm4, %xmm1
mulsd 0x8170f(%rip), %xmm2    # 0xf4428
mulsd %xmm4, %xmm1
movsd 0x20(%r14), %xmm0
addsd %xmm3, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x72e40
sqrtsd %xmm0, %xmm0
jmp 0x72e59
movapd %xmm1, %xmm0
movsd %xmm3, 0x10(%rsp)
callq 0x82b0
movsd 0x10(%rsp), %xmm3
movsd (%rsp), %xmm2
subsd 0x20(%r14), %xmm3
subsd %xmm3, %xmm0
mulsd 0x7ef5e(%rip), %xmm0    # 0xf1cd0
movsd %xmm0, 0x8(%rsp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%r13)
movsd 0x7e4d2(%rip), %xmm3    # 0xf1260
jmp 0x73018
cmpl %ebx, 0x80(%r13)
jne 0x72e8d
movsd 0x40(%r14), %xmm0
mulsd %xmm4, %xmm0
divsd %xmm1, %xmm0
divsd %xmm1, %xmm0
callq 0x82c0
movsd 0x50(%r14), %xmm1
divsd 0x10(%rsp), %xmm1
mulsd 0x811ed(%rip), %xmm0    # 0xf3fb8
subsd %xmm1, %xmm0
callq 0x8670
movsd 0x8164c(%rip), %xmm2    # 0xf4428
mulsd %xmm2, %xmm0
divsd 0x30(%r14), %xmm0
cmpw $0x1, 0xe78(%r12)
jne 0x72df8
mulsd 0x28(%rsp), %xmm0
movsd 0x7e9e8(%rip), %xmm4    # 0xf17e8
addsd %xmm0, %xmm4
movapd %xmm4, %xmm3
mulsd %xmm4, %xmm3
movsd 0x20(%r14), %xmm1
addsd 0x7e446(%rip), %xmm1    # 0xf1260
mulsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm3, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jb 0x72f37
xorps %xmm3, %xmm3
sqrtsd %xmm1, %xmm3
jmp 0x72f50
movsd %xmm4, 0x10(%rsp)
callq 0x82b0
movsd 0x10(%rsp), %xmm4
movsd 0x7e407(%rip), %xmm3    # 0xf1260
movsd (%rsp), %xmm2
movsd 0x20(%r14), %xmm1
addsd %xmm3, %xmm1
addsd %xmm1, %xmm1
addsd %xmm4, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%r13)
jmp 0x73018
cmpl %ebx, 0x84(%r13)
jne 0x72f88
movq %rdi, %r15
movsd 0x40(%r14), %xmm0
mulsd %xmm4, %xmm0
divsd %xmm1, %xmm0
divsd %xmm1, %xmm0
callq 0x82c0
movsd 0x48(%r14), %xmm1
divsd 0x10(%rsp), %xmm1
mulsd 0x810f0(%rip), %xmm0    # 0xf3fb8
subsd %xmm1, %xmm0
callq 0x8670
divsd 0x30(%r14), %xmm0
cmpw $0x1, 0xe78(%r12)
jne 0x72ee9
mulsd 0x28(%rsp), %xmm0
movq %r15, %rcx
movslq 0x8(%rbp), %rax
movsd (%r15,%rax,8), %xmm3
mulsd 0x20(%r14), %xmm3
divsd %xmm0, %xmm3
addsd 0x7e358(%rip), %xmm3    # 0xf1260
movsd 0x81518(%rip), %xmm1    # 0xf4428
divsd %xmm0, %xmm1
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x72fcc
sqrtsd %xmm0, %xmm0
jmp 0x72fe0
movapd %xmm1, %xmm0
movsd %xmm4, 0x10(%rsp)
callq 0x82b0
movsd 0x10(%rsp), %xmm4
movapd %xmm0, %xmm3
movsd (%rsp), %xmm2
subsd %xmm4, %xmm3
mulsd 0x7ed6f(%rip), %xmm3    # 0xf1cd0
movsd %xmm3, 0x8(%rsp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%r13)
addsd 0x7e86b(%rip), %xmm3    # 0xf17e8
divsd 0x20(%r14), %xmm3
jmp 0x73018
cmpl %ebx, 0x88(%r13)
je 0x72f9e
cmpl %ebx, 0x8c(%r13)
jne 0x7301e
movslq 0x4(%rbp), %rax
movsd (%rdi,%rax,8), %xmm0
movslq 0x8(%rbp), %rax
movsd (%rdi,%rax,8), %xmm3
movsd 0x20(%r14), %xmm1
mulsd %xmm3, %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, (%r13)
jmp 0x73018
movsd %xmm3, 0x10(%rsp)
callq 0x82b0
movsd 0x10(%rsp), %xmm3
movq %r15, %rcx
movsd (%rsp), %xmm2
addsd %xmm3, %xmm0
movsd 0x7ed0f(%rip), %xmm1    # 0xf1d00
divsd %xmm0, %xmm1
movslq 0x8(%rbp), %rax
movsd (%rcx,%rax,8), %xmm3
movsd 0x20(%r14), %xmm0
mulsd %xmm3, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd %xmm1, (%r13)
movsd %xmm3, 0x8(%r13)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x10(%r13)
cmpl %ebx, 0x8c(%r13)
jne 0x730aa
movq 0x88(%r14), %rsi
movq 0x38(%rsi), %rax
movslq 0xc(%rsi), %rcx
movsd %xmm0, (%rax,%rcx,8)
leaq 0x8(%rsp), %rdx
movapd %xmm2, %xmm0
movq %r12, %rdi
callq 0x73546
testl %eax, %eax
je 0x730aa
addq $0x1064, %r12           # imm = 0x1064
leaq 0x7e386(%rip), %rsi     # 0xf13f0
leaq 0xa1c22(%rip), %rdx     # 0x114c93
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x30c2c
leaq 0x7dfaa(%rip), %rsi     # 0xf1034
leaq 0xa1bde(%rip), %rdx     # 0x114c6f
movq %r12, %rdi
movl $0x1014, %ecx           # imm = 0x1014
movq %r14, %r8
xorl %eax, %eax
callq 0x30c2c
movl $0x1, %eax
jmp 0x730b8
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x18(%r13)
xorl %eax, %eax
addq $0x858, %rsp            # imm = 0x858
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq