// File: /workspace/github_c_makefile_star2_O2/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
andq $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 0x6b8f3
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 0x5a991
cmpl $0x1, %eax
jne 0x6b906
addq $0xd1c, %r12            # imm = 0xD1C
leaq 0x79b38(%rip), %rsi     # 0xe53f0
leaq 0x98e51(%rip), %rdx     # 0x104710
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x2e014
addq $0x1064, %r15           # imm = 0x1064
leaq 0x79755(%rip), %rsi     # 0xe5034
leaq 0x9d279(%rip), %rdx     # 0x108b5f
movq %r15, %rdi
movl $0xf8b, %ecx            # imm = 0xF8B
jmp 0x6bf5f
movsd 0x79965(%rip), %xmm3    # 0xe5260
xorl %eax, %eax
movapd %xmm3, %xmm0
movq %r15, %r12
jmp 0x6b960
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 0x7992e(%rip), %xmm0    # 0xe5260
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 0x798f4(%rip), %xmm1    # 0xe5260
addsd %xmm2, %xmm1
movsd 0x38(%r14), %xmm4
mulsd %xmm1, %xmm4
addsd (%rdi,%rcx,8), %xmm4
testb %al, %al
je 0x6b987
mulsd %xmm0, %xmm4
cmpl %ebx, 0x74(%r13)
movsd %xmm3, 0x28(%rsp)
movsd %xmm4, 0x10(%rsp)
je 0x6b9af
cmpw $0x0, 0x60(%r14)
je 0x6ba6b
cmpl %ebx, 0x70(%r13)
jne 0x6ba6b
movsd 0x30(%r14), %xmm0
movaps %xmm0, 0x40(%rsp)
movsd 0x40(%r14), %xmm0
mulsd %xmm4, %xmm0
divsd %xmm1, %xmm0
divsd %xmm1, %xmm0
callq 0x82e0
movsd 0x58(%r14), %xmm1
divsd 0x10(%rsp), %xmm1
mulsd 0x7c5a3(%rip), %xmm0    # 0xe7f88
subsd %xmm1, %xmm0
callq 0x86a0
movapd 0x40(%rsp), %xmm4
divsd %xmm0, %xmm4
cmpw $0x1, 0xe78(%r12)
jne 0x6ba0a
divsd 0x28(%rsp), %xmm4
movsd 0x20(%r14), %xmm0
movsd 0x79848(%rip), %xmm3    # 0xe5260
movapd %xmm0, %xmm1
addsd %xmm3, %xmm1
movapd 0x79808(%rip), %xmm5    # 0xe5230
xorpd %xmm4, %xmm5
mulsd %xmm4, %xmm1
movapd %xmm3, %xmm2
subsd %xmm1, %xmm2
mulsd 0x7c9b8(%rip), %xmm4    # 0xe83f8
mulsd %xmm2, %xmm2
addsd %xmm0, %xmm0
addsd %xmm3, %xmm0
mulsd %xmm4, %xmm0
addsd %xmm2, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x6bb12
sqrtsd %xmm0, %xmm1
jmp 0x6bb2f
cmpl %ebx, 0x78(%r13)
jne 0x6bb77
movsd 0x40(%r14), %xmm0
mulsd %xmm4, %xmm0
divsd %xmm1, %xmm0
divsd %xmm1, %xmm0
callq 0x82e0
movsd 0x58(%r14), %xmm1
divsd 0x10(%rsp), %xmm1
mulsd 0x7c4e8(%rip), %xmm0    # 0xe7f88
subsd %xmm1, %xmm0
callq 0x86a0
divsd 0x30(%r14), %xmm0
cmpw $0x1, 0xe78(%r12)
jne 0x6bac1
mulsd 0x28(%rsp), %xmm0
movsd 0x79d1f(%rip), %xmm3    # 0xe57e8
addsd %xmm0, %xmm3
movsd 0x20(%r14), %xmm1
movapd %xmm3, %xmm2
subsd %xmm1, %xmm2
addsd %xmm1, %xmm1
addsd 0x79779(%rip), %xmm1    # 0xe5260
mulsd %xmm2, %xmm2
mulsd 0x7c905(%rip), %xmm1    # 0xe83f8
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jb 0x6bc2e
sqrtsd %xmm1, %xmm0
jmp 0x6bc43
movapd %xmm5, 0x10(%rsp)
callq 0x82d0
movapd 0x10(%rsp), %xmm5
movsd 0x79735(%rip), %xmm3    # 0xe5260
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 0x6bee4
cmpl %ebx, 0x7c(%r13)
jne 0x6bc7b
movsd 0x30(%r14), %xmm0
mulsd 0x7c971(%rip), %xmm0    # 0xe8500
movsd %xmm0, 0x40(%rsp)
movsd 0x40(%r14), %xmm0
mulsd %xmm4, %xmm0
divsd %xmm1, %xmm0
divsd %xmm1, %xmm0
callq 0x82e0
movsd 0x50(%r14), %xmm1
divsd 0x10(%rsp), %xmm1
mulsd 0x7c3c8(%rip), %xmm0    # 0xe7f88
subsd %xmm1, %xmm0
callq 0x86a0
movsd 0x40(%rsp), %xmm2
divsd %xmm0, %xmm2
cmpw $0x1, 0xe78(%r12)
jne 0x6bbe5
divsd 0x28(%rsp), %xmm2
movsd 0x79673(%rip), %xmm3    # 0xe5260
movapd %xmm3, %xmm4
subsd %xmm2, %xmm4
movapd %xmm4, %xmm1
mulsd 0x7c7f7(%rip), %xmm2    # 0xe83f8
mulsd %xmm4, %xmm1
movsd 0x20(%r14), %xmm0
addsd %xmm3, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x6bd25
sqrtsd %xmm0, %xmm1
jmp 0x6bd42
movapd %xmm1, %xmm0
movsd %xmm3, 0x10(%rsp)
callq 0x82d0
movsd 0x10(%rsp), %xmm3
movsd (%rsp), %xmm2
subsd 0x20(%r14), %xmm3
subsd %xmm3, %xmm0
mulsd 0x7a056(%rip), %xmm0    # 0xe5cb0
movsd %xmm0, 0x8(%rsp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%r13)
movsd 0x795ea(%rip), %xmm3    # 0xe5260
jmp 0x6bee4
cmpl %ebx, 0x80(%r13)
jne 0x6bd76
movsd 0x40(%r14), %xmm0
mulsd %xmm4, %xmm0
divsd %xmm1, %xmm0
divsd %xmm1, %xmm0
callq 0x82e0
movsd 0x50(%r14), %xmm1
divsd 0x10(%rsp), %xmm1
mulsd 0x7c2d5(%rip), %xmm0    # 0xe7f88
subsd %xmm1, %xmm0
callq 0x86a0
movsd 0x7c734(%rip), %xmm2    # 0xe83f8
mulsd %xmm2, %xmm0
divsd 0x30(%r14), %xmm0
cmpw $0x1, 0xe78(%r12)
jne 0x6bce0
mulsd 0x28(%rsp), %xmm0
movsd 0x79b00(%rip), %xmm4    # 0xe57e8
addsd %xmm0, %xmm4
movapd %xmm4, %xmm3
mulsd %xmm4, %xmm3
movsd 0x20(%r14), %xmm1
addsd 0x7955e(%rip), %xmm1    # 0xe5260
mulsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm3, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jb 0x6be20
sqrtsd %xmm1, %xmm0
jmp 0x6be35
movsd %xmm4, 0x10(%rsp)
callq 0x82d0
movsd 0x10(%rsp), %xmm4
movsd 0x79522(%rip), %xmm3    # 0xe5260
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 0x6bee4
cmpl %ebx, 0x84(%r13)
jne 0x6be6e
movq %rdi, %r15
movsd 0x40(%r14), %xmm0
mulsd %xmm4, %xmm0
divsd %xmm1, %xmm0
divsd %xmm1, %xmm0
callq 0x82e0
movsd 0x48(%r14), %xmm1
divsd 0x10(%rsp), %xmm1
mulsd 0x7c1d7(%rip), %xmm0    # 0xe7f88
subsd %xmm1, %xmm0
callq 0x86a0
divsd 0x30(%r14), %xmm0
cmpw $0x1, 0xe78(%r12)
jne 0x6bdd2
mulsd 0x28(%rsp), %xmm0
movq %r15, %rcx
movslq 0x8(%rbp), %rax
movsd (%r15,%rax,8), %xmm3
mulsd 0x20(%r14), %xmm3
divsd %xmm0, %xmm3
addsd 0x7946f(%rip), %xmm3    # 0xe5260
movsd 0x7c5ff(%rip), %xmm1    # 0xe83f8
divsd %xmm0, %xmm1
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x6be98
sqrtsd %xmm0, %xmm0
jmp 0x6beac
movapd %xmm1, %xmm0
movsd %xmm4, 0x10(%rsp)
callq 0x82d0
movsd 0x10(%rsp), %xmm4
movsd (%rsp), %xmm2
subsd %xmm4, %xmm0
mulsd 0x79e6a(%rip), %xmm0    # 0xe5cb0
movsd %xmm0, 0x8(%rsp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%r13)
movsd 0x79986(%rip), %xmm3    # 0xe57e8
addsd %xmm0, %xmm3
divsd 0x20(%r14), %xmm3
jmp 0x6bee4
cmpl %ebx, 0x88(%r13)
je 0x6be84
xorpd %xmm0, %xmm0
cmpl %ebx, 0x8c(%r13)
jne 0x6beea
movslq 0x4(%rbp), %rax
movsd (%rdi,%rax,8), %xmm1
movslq 0x8(%rbp), %rax
movsd (%rdi,%rax,8), %xmm3
jmp 0x6beca
movsd %xmm3, 0x10(%rsp)
callq 0x82d0
movsd 0x10(%rsp), %xmm3
movq %r15, %rcx
movsd (%rsp), %xmm2
addsd %xmm3, %xmm0
movsd 0x79e23(%rip), %xmm1    # 0xe5ce0
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 0x6bf74
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 0x6c427
testl %eax, %eax
je 0x6bf6e
addq $0x1064, %r12           # imm = 0x1064
leaq 0x794c0(%rip), %rsi     # 0xe53f0
leaq 0x9cc4c(%rip), %rdx     # 0x108b83
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x790e4(%rip), %rsi     # 0xe5034
leaq 0x9cc08(%rip), %rdx     # 0x108b5f
movq %r12, %rdi
movl $0x1014, %ecx           # imm = 0x1014
movq %r14, %r8
xorl %eax, %eax
callq 0x2e014
pushq $0x1
popq %rax
jmp 0x6bf7c
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