// File: /workspace/github_c_makefile_star2_O3/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 0x6ff28
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 0x5e941
cmpl $0x1, %eax
jne 0x6ff3b
addq $0xd1c, %r12            # imm = 0xD1C
leaq 0x7e503(%rip), %rsi     # 0xee3f0
leaq 0x9d97c(%rip), %rdx     # 0x10d870
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x30af0
addq $0x1064, %r15           # imm = 0x1064
leaq 0x7e120(%rip), %rsi     # 0xee034
leaq 0xa1da4(%rip), %rdx     # 0x111cbf
movq %r15, %rdi
movl $0xf8b, %ecx            # imm = 0xF8B
jmp 0x705a0
movsd 0x7e330(%rip), %xmm3    # 0xee260
xorl %eax, %eax
movapd %xmm3, %xmm0
movq %r15, %r12
jmp 0x6ff95
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 0x7e2f9(%rip), %xmm0    # 0xee260
divsd %xmm1, %xmm0
divsd %xmm1, %xmm0
divsd %xmm2, %xmm0
movq %r15, %r12
cmpw $0x1, 0xe78(%r15)
sete %al
movsd (%rsp), %xmm2
movq %rbp, %rdi
movq %r13, %rbp
movq %r14, %r13
movq 0x10(%rsp), %r14
movslq 0xc(%rbp), %rcx
movsd 0x7e2bf(%rip), %xmm1    # 0xee260
addsd %xmm2, %xmm1
movsd 0x38(%r14), %xmm4
mulsd %xmm1, %xmm4
addsd (%rdi,%rcx,8), %xmm4
testb %al, %al
je 0x6ffbc
mulsd %xmm0, %xmm4
cmpl %ebx, 0x74(%r13)
movsd %xmm3, 0x28(%rsp)
movsd %xmm4, 0x10(%rsp)
je 0x6ffe4
cmpw $0x0, 0x60(%r14)
je 0x700a3
cmpl %ebx, 0x70(%r13)
jne 0x700a3
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 0x80fae(%rip), %xmm0    # 0xf0fc8
subsd %xmm1, %xmm0
callq 0x8670
movapd 0x40(%rsp), %xmm4
divsd %xmm0, %xmm4
cmpw $0x1, 0xe78(%r12)
jne 0x7003f
divsd 0x28(%rsp), %xmm4
movsd 0x20(%r14), %xmm0
movsd 0x7e213(%rip), %xmm3    # 0xee260
movapd %xmm0, %xmm1
addsd %xmm3, %xmm1
movapd 0x7e1d3(%rip), %xmm5    # 0xee230
xorpd %xmm4, %xmm5
mulsd %xmm4, %xmm1
movapd %xmm3, %xmm2
subsd %xmm1, %xmm2
mulsd 0x813c3(%rip), %xmm4    # 0xf1438
mulsd %xmm2, %xmm2
addsd %xmm0, %xmm0
addsd %xmm3, %xmm0
mulsd %xmm4, %xmm0
addsd %xmm2, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x7014d
xorps %xmm1, %xmm1
sqrtsd %xmm0, %xmm1
jmp 0x7016a
cmpl %ebx, 0x78(%r13)
jne 0x701b2
movsd 0x40(%r14), %xmm0
mulsd %xmm4, %xmm0
divsd %xmm1, %xmm0
divsd %xmm1, %xmm0
callq 0x82c0
movsd 0x58(%r14), %xmm1
divsd 0x10(%rsp), %xmm1
mulsd 0x80ef0(%rip), %xmm0    # 0xf0fc8
subsd %xmm1, %xmm0
callq 0x8670
divsd 0x30(%r14), %xmm0
cmpw $0x1, 0xe78(%r12)
jne 0x700f9
mulsd 0x28(%rsp), %xmm0
movsd 0x7e6e7(%rip), %xmm3    # 0xee7e8
addsd %xmm0, %xmm3
movsd 0x20(%r14), %xmm1
movapd %xmm3, %xmm2
subsd %xmm1, %xmm2
addsd %xmm1, %xmm1
addsd 0x7e141(%rip), %xmm1    # 0xee260
mulsd %xmm2, %xmm2
mulsd 0x8130d(%rip), %xmm1    # 0xf1438
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jb 0x7026c
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0x70281
movapd %xmm5, 0x10(%rsp)
callq 0x82b0
movapd 0x10(%rsp), %xmm5
movsd 0x7e0fa(%rip), %xmm3    # 0xee260
movapd %xmm0, %xmm1
movsd 0x20(%r14), %xmm2
movapd %xmm2, %xmm0
addsd %xmm2, %xmm0
addsd %xmm3, %xmm0
addsd %xmm0, %xmm0
addsd %xmm3, %xmm2
mulsd %xmm2, %xmm5
addsd %xmm3, %xmm5
addsd %xmm1, %xmm5
divsd %xmm5, %xmm0
movsd %xmm0, 0x8(%rsp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%r13)
movsd (%rsp), %xmm2
jmp 0x70525
cmpl %ebx, 0x7c(%r13)
jne 0x702b9
movsd 0x30(%r14), %xmm0
mulsd 0x813b6(%rip), %xmm0    # 0xf1580
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 0x80dcd(%rip), %xmm0    # 0xf0fc8
subsd %xmm1, %xmm0
callq 0x8670
movsd 0x40(%rsp), %xmm2
divsd %xmm0, %xmm2
cmpw $0x1, 0xe78(%r12)
jne 0x70220
divsd 0x28(%rsp), %xmm2
movsd 0x7e038(%rip), %xmm3    # 0xee260
movapd %xmm3, %xmm4
subsd %xmm2, %xmm4
movapd %xmm4, %xmm1
mulsd 0x811fc(%rip), %xmm2    # 0xf1438
mulsd %xmm4, %xmm1
movsd 0x20(%r14), %xmm0
addsd %xmm3, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x70366
xorps %xmm1, %xmm1
sqrtsd %xmm0, %xmm1
jmp 0x70383
movapd %xmm1, %xmm0
movsd %xmm3, 0x10(%rsp)
callq 0x82b0
movsd 0x10(%rsp), %xmm3
movsd (%rsp), %xmm2
subsd 0x20(%r14), %xmm3
subsd %xmm3, %xmm0
mulsd 0x7ea38(%rip), %xmm0    # 0xeecd0
movsd %xmm0, 0x8(%rsp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%r13)
movsd 0x7dfac(%rip), %xmm3    # 0xee260
jmp 0x70525
cmpl %ebx, 0x80(%r13)
jne 0x703b7
movsd 0x40(%r14), %xmm0
mulsd %xmm4, %xmm0
divsd %xmm1, %xmm0
divsd %xmm1, %xmm0
callq 0x82c0
movsd 0x50(%r14), %xmm1
divsd 0x10(%rsp), %xmm1
mulsd 0x80cd7(%rip), %xmm0    # 0xf0fc8
subsd %xmm1, %xmm0
callq 0x8670
movsd 0x81136(%rip), %xmm2    # 0xf1438
mulsd %xmm2, %xmm0
divsd 0x30(%r14), %xmm0
cmpw $0x1, 0xe78(%r12)
jne 0x7031e
mulsd 0x28(%rsp), %xmm0
movsd 0x7e4c2(%rip), %xmm4    # 0xee7e8
addsd %xmm0, %xmm4
movapd %xmm4, %xmm3
mulsd %xmm4, %xmm3
movsd 0x20(%r14), %xmm1
addsd 0x7df20(%rip), %xmm1    # 0xee260
mulsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm3, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jb 0x70461
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0x70476
movsd %xmm4, 0x10(%rsp)
callq 0x82b0
movsd 0x10(%rsp), %xmm4
movsd 0x7dee1(%rip), %xmm3    # 0xee260
movapd %xmm0, %xmm1
movsd (%rsp), %xmm2
movsd 0x20(%r14), %xmm0
addsd %xmm3, %xmm0
addsd %xmm0, %xmm0
addsd %xmm4, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%r13)
jmp 0x70525
cmpl %ebx, 0x84(%r13)
jne 0x704af
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 0x80bd6(%rip), %xmm0    # 0xf0fc8
subsd %xmm1, %xmm0
callq 0x8670
divsd 0x30(%r14), %xmm0
cmpw $0x1, 0xe78(%r12)
jne 0x70413
mulsd 0x28(%rsp), %xmm0
movq %r15, %rcx
movslq 0x8(%rbp), %rax
movsd (%r15,%rax,8), %xmm3
mulsd 0x20(%r14), %xmm3
divsd %xmm0, %xmm3
addsd 0x7de2e(%rip), %xmm3    # 0xee260
movsd 0x80ffe(%rip), %xmm1    # 0xf1438
divsd %xmm0, %xmm1
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x704d9
sqrtsd %xmm0, %xmm0
jmp 0x704ed
movapd %xmm1, %xmm0
movsd %xmm4, 0x10(%rsp)
callq 0x82b0
movsd 0x10(%rsp), %xmm4
movsd (%rsp), %xmm2
subsd %xmm4, %xmm0
mulsd 0x7e849(%rip), %xmm0    # 0xeecd0
movsd %xmm0, 0x8(%rsp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%r13)
movsd 0x7e345(%rip), %xmm3    # 0xee7e8
addsd %xmm0, %xmm3
divsd 0x20(%r14), %xmm3
jmp 0x70525
cmpl %ebx, 0x88(%r13)
je 0x704c5
xorpd %xmm0, %xmm0
cmpl %ebx, 0x8c(%r13)
jne 0x7052b
movslq 0x4(%rbp), %rax
movsd (%rdi,%rax,8), %xmm1
movslq 0x8(%rbp), %rax
movsd (%rdi,%rax,8), %xmm3
jmp 0x7050b
movsd %xmm3, 0x10(%rsp)
callq 0x82b0
movsd 0x10(%rsp), %xmm3
movq %r15, %rcx
movsd (%rsp), %xmm2
addsd %xmm3, %xmm0
movsd 0x7e802(%rip), %xmm1    # 0xeed00
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 %xmm0, 0x10(%r13)
cmpl %ebx, 0x8c(%r13)
jne 0x705b7
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 0x70a70
testl %eax, %eax
je 0x705b1
addq $0x1064, %r12           # imm = 0x1064
leaq 0x7de7f(%rip), %rsi     # 0xee3f0
leaq 0xa176b(%rip), %rdx     # 0x111ce3
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x30af0
leaq 0x7daa3(%rip), %rsi     # 0xee034
leaq 0xa1727(%rip), %rdx     # 0x111cbf
movq %r12, %rdi
movl $0x1014, %ecx           # imm = 0x1014
movq %r14, %r8
xorl %eax, %eax
callq 0x30af0
movl $0x1, %eax
jmp 0x705bf
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