// File: /workspace/github_c_makefile_star2_O1/PoulinV[P]AxiCLASS/build/../external/HyRec2020/history.c
// Function: rec_xH1_stiff
void rec_xH1_stiff(HYREC_DATA *data, int model, double z, double xHeII, double *xH1, unsigned iz_rad, double H){

  REC_COSMOPARAMS *cosmo = data->cosmo;
  int *error = &data->error;

  double ainv, xH1sSaha, xHIISaha, dxH1sSaha_dlna, dxH1sdlna_Saha, DdxH1sdlna_DxH1s, T, nH, Dxe;
  int model_stiff;	// To use EMLA2p2s model for PostSaha in FULL and SWIFT mode
  char sub_message[SIZE_ErrorM];

  // Set model for rec_xH1_stiff. FULL mode uses EMLA2s2p for stiff.
  // SWIFT mode uses EMLA2s2p for stiff when z is not in the range of redshifts for SWIFT fitting function.

  T = kBoltz*cosmo->T0 * (ainv=1.+z);

  if (model == FULL) {
    model_stiff = EMLA2s2p;
  }
  else{
    model_stiff = model;
    if (model == SWIFT){
      if (T/kBoltz/cosmo->fsR/cosmo->fsR/cosmo->meR > data->fit->swift_func[0][DKK_SIZE-1] ) model_stiff = EMLA2s2p;
    }
  }

  nH = cosmo->nH0 *cube(1.+z);
  xH1sSaha = rec_saha_xH1s(cosmo, z, xHeII);
  xHIISaha = 1.-xH1sSaha;

  dxH1sSaha_dlna = (1.+z)*(rec_saha_xH1s(cosmo, z-0.5, xHeII)
                          -rec_saha_xH1s(cosmo, z+0.5, xHeII));
                  /* (partial xHII)/(partial lna). Use xH1s = 1-xHII for better accuracy. */
  if (xHeII != 0.) {
    dxH1sSaha_dlna = (1.+z)*(rec_saha_xH1s(cosmo, z-0.5, xHeII)
                            -rec_saha_xH1s(cosmo, z+0.5, xHeII));
    Dxe = 0.01*(cosmo->fHe - xHeII);
    dxH1sSaha_dlna += (rec_saha_xH1s(cosmo, z, xHeII+Dxe)
                      -rec_saha_xH1s(cosmo, z, xHeII-Dxe))/2./Dxe
                      *rec_helium_dxHeIIdlna(data, z, xH1sSaha, xHeII, H);
                   /* (partial xHII)/(partial xHeII).dxHeII/dlna */
  }

  dxH1sdlna_Saha = -rec_dxHIIdlna(data, model_stiff, xHIISaha + xHeII, xHIISaha, nH, H, T, T, iz_rad, z);
  Dxe            = 0.01*xH1sSaha;
  DdxH1sdlna_DxH1s = (rec_dxHIIdlna(data, model_stiff, xHIISaha+Dxe + xHeII, xHIISaha+Dxe, nH, H, T, T, iz_rad, z)
                       -rec_dxHIIdlna(data, model_stiff, xHIISaha-Dxe + xHeII, xHIISaha-Dxe, nH, H, T, T, iz_rad, z))/2./Dxe;
  *xH1 = xH1sSaha + (dxH1sSaha_dlna - dxH1sdlna_Saha)/DdxH1sdlna_DxH1s;


  if (fabs(*xH1 - xH1sSaha) > DXHII_MAX)  data->quasi_eq = 0;
  //if (z<1700.) *stiff = 0;   /* Used when calculating the correction function for SWIFT mode. */

  /* Update photon population when MODEL = FULL */
  if (model == FULL) rec_dxHIIdlna(data, model, xHeII + 1.-*xH1, 1.-*xH1, nH, H, T, T, iz_rad, z);

  if (*xH1 < 0. || *xH1 != *xH1) {
    sprintf(sub_message, "xH1 < 0 in rec_xH1_stiff: at z = %f, xH1 = %E\n", z, *xH1);
    strcat(data->error_message, sub_message);
    *error = 1;
  }

  if (*error == 1) {
    sprintf(sub_message, "  called from rec_xH1_stiff at z = %f\n", z);
    strcat(data->error_message, sub_message);
    return;
  }
  return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x868, %rsp            # imm = 0x868
movsd %xmm2, 0x40(%rsp)
movl %ecx, %ebp
movq %rdx, %r14
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, 0x10(%rsp)
movl %esi, %r15d
movq %rdi, %rbx
movq 0x8(%rdi), %r13
movsd 0x8(%r13), %xmm1
mulsd 0x4fd6c(%rip), %xmm1    # 0x12d338
movsd 0x13c8c(%rip), %xmm2    # 0xf1260
addsd %xmm0, %xmm2
mulsd %xmm2, %xmm1
movsd %xmm1, 0x38(%rsp)
movl $0x2, %r12d
cmpl $0x3, %esi
je 0xdd63b
movl %r15d, %r12d
cmpl $0x4, %r15d
jne 0xdd63b
movsd 0x38(%rsp), %xmm0
divsd 0x4fd34(%rip), %xmm0    # 0x12d338
movsd 0x98(%r13), %xmm1
divsd %xmm1, %xmm0
divsd %xmm1, %xmm0
divsd 0xa0(%r13), %xmm0
movq 0x60(%rbx), %rax
movq (%rax), %rax
movl $0x4, %r12d
ucomisd 0x840(%rax), %xmm0
jbe 0xdd63b
movl $0x2, %r12d
movsd 0x88(%r13), %xmm0
movsd %xmm0, 0x30(%rsp)
movapd %xmm2, %xmm0
movsd %xmm2, 0x20(%rsp)
callq 0xd6f51
mulsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movq %r13, %rdi
movsd 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
callq 0xd7b30
movsd 0x13bdf(%rip), %xmm1    # 0xf1260
movsd %xmm0, 0x50(%rsp)
subsd %xmm0, %xmm1
movsd %xmm1, 0x58(%rsp)
movsd 0x16dcf(%rip), %xmm0    # 0xf4468
addsd 0x10(%rsp), %xmm0
movq %r13, %rdi
movsd %xmm0, 0x18(%rsp)
movsd 0x8(%rsp), %xmm1
callq 0xd7b30
movsd %xmm0, 0x48(%rsp)
movsd 0x1460f(%rip), %xmm0    # 0xf1cd0
addsd 0x10(%rsp), %xmm0
movq %r13, %rdi
movsd %xmm0, 0x28(%rsp)
movsd 0x8(%rsp), %xmm1
callq 0xd7b30
xorpd %xmm1, %xmm1
movsd 0x8(%rsp), %xmm3
ucomisd %xmm1, %xmm3
jne 0xdd6f1
jnp 0xdd7e6
movq %r13, %rdi
movsd 0x18(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
callq 0xd7b30
movsd %xmm0, 0x18(%rsp)
movq %r13, %rdi
movsd 0x28(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
callq 0xd7b30
movsd 0x18(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x80(%r13), %xmm1
subsd 0x8(%rsp), %xmm1
mulsd 0x16860(%rip), %xmm1    # 0xf3fb0
movsd %xmm1, 0x28(%rsp)
addsd 0x8(%rsp), %xmm1
movq %r13, %rdi
movsd 0x10(%rsp), %xmm0
callq 0xd7b30
movsd %xmm0, 0x18(%rsp)
movsd 0x8(%rsp), %xmm1
subsd 0x28(%rsp), %xmm1
movq %r13, %rdi
movsd 0x10(%rsp), %xmm0
callq 0xd7b30
movsd 0x18(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd 0x14534(%rip), %xmm1    # 0xf1cd0
divsd 0x28(%rsp), %xmm1
movsd %xmm1, 0x18(%rsp)
movq %rbx, %rdi
movsd 0x10(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
movsd 0x40(%rsp), %xmm3
callq 0xd7cf2
movsd 0x8(%rsp), %xmm3
mulsd 0x18(%rsp), %xmm0
movsd 0x20(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x20(%rsp)
jmp 0xdd800
movsd 0x48(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x58(%rsp), %xmm1
movapd %xmm1, %xmm0
addsd %xmm3, %xmm0
movq %rbx, %rdi
movl %r12d, %esi
movsd 0x30(%rsp), %xmm2
movsd 0x40(%rsp), %xmm3
movsd 0x38(%rsp), %xmm4
movaps %xmm4, %xmm5
movl %ebp, %edx
movsd 0x10(%rsp), %xmm6
callq 0xdbd61
movsd %xmm0, 0x28(%rsp)
movsd 0x1676c(%rip), %xmm0    # 0xf3fb0
mulsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x58(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
addsd 0x8(%rsp), %xmm0
movq %rbx, %rdi
movl %r12d, %esi
movsd 0x30(%rsp), %xmm2
movsd 0x40(%rsp), %xmm3
movsd 0x38(%rsp), %xmm4
movaps %xmm4, %xmm5
movl %ebp, %edx
movsd 0x10(%rsp), %xmm6
callq 0xdbd61
movsd %xmm0, 0x48(%rsp)
movsd 0x58(%rsp), %xmm1
subsd 0x18(%rsp), %xmm1
movapd %xmm1, %xmm0
addsd 0x8(%rsp), %xmm0
movq %rbx, %rdi
movl %r12d, %esi
movsd 0x30(%rsp), %xmm2
movsd 0x40(%rsp), %xmm3
movsd 0x38(%rsp), %xmm4
movaps %xmm4, %xmm5
movl %ebp, %edx
movsd 0x10(%rsp), %xmm6
callq 0xdbd61
movsd 0x28(%rsp), %xmm2
movsd 0x48(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd 0x143e8(%rip), %xmm1    # 0xf1cd0
divsd 0x18(%rsp), %xmm1
addsd 0x20(%rsp), %xmm2
divsd %xmm1, %xmm2
movsd 0x50(%rsp), %xmm1
addsd %xmm1, %xmm2
movapd %xmm2, %xmm0
subsd %xmm1, %xmm0
andpd 0x1392e(%rip), %xmm0    # 0xf1240
movsd %xmm2, (%r14)
ucomisd 0x4fe99(%rip), %xmm0    # 0x12d7b8
jbe 0xdd928
movl $0x0, 0x3c(%rbx)
cmpl $0x3, %r15d
jne 0xdd972
movsd 0x8(%rsp), %xmm0
movsd 0x13924(%rip), %xmm1    # 0xf1260
addsd %xmm1, %xmm0
subsd %xmm2, %xmm0
subsd %xmm2, %xmm1
movq %rbx, %rdi
movl $0x3, %esi
movsd 0x30(%rsp), %xmm2
movsd 0x40(%rsp), %xmm3
movsd 0x38(%rsp), %xmm4
movaps %xmm4, %xmm5
movl %ebp, %edx
movsd 0x10(%rsp), %xmm6
callq 0xdbd61
movsd (%r14), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jae 0xdd9b0
leaq 0x507af(%rip), %rsi     # 0x12e137
leaq 0x60(%rsp), %r14
movq %r14, %rdi
movsd 0x10(%rsp), %xmm0
movb $0x2, %al
callq 0x8090
movq 0x48(%rbx), %rdi
movq %r14, %rsi
callq 0x8660
movl $0x1, 0x38(%rbx)
cmpl $0x1, 0x38(%rbx)
jne 0xdd9de
leaq 0x507a9(%rip), %rsi     # 0x12e166
leaq 0x60(%rsp), %r14
movq %r14, %rdi
movsd 0x10(%rsp), %xmm0
movb $0x1, %al
callq 0x8090
movq 0x48(%rbx), %rdi
movq %r14, %rsi
callq 0x8660
addq $0x868, %rsp            # imm = 0x868
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq