// File: /workspace/github_c_makefile_star2_O2/PoulinV[P]AxiCLASS/build/../source/perturbations.c
// Function: perturbations_rsa_delta_and_theta
int perturbations_rsa_delta_and_theta(
                                      struct precision * ppr,
                                      struct background * pba,
                                      struct thermodynamics * pth,
                                      struct perturbations * ppt,
                                      double k,
                                      double * y,
                                      double a_prime_over_a,
                                      double * pvecthermo,
                                      struct perturbations_workspace * ppw,
                                      ErrorMsg error_message
                                      ) {
  /* - define local variables */

  double k2;

  k2 = k*k;

  class_test(ppw->approx[ppw->index_ap_rsa] == (int)rsa_off,
             "this function should not have been called now, bug was introduced",
             ppt->error_message,
             ppt->error_message);

  // formulas below TBC for curvaturema

  /* newtonian gauge */
  if (ppt->gauge == newtonian) {

    if (ppr->radiation_streaming_approximation == rsa_null) {
      ppw->rsa_delta_g = 0.;
      ppw->rsa_theta_g = 0.;
    }
    else {
      ppw->rsa_delta_g = -4.*y[ppw->pv->index_pt_phi];
      ppw->rsa_theta_g = 6.*ppw->pvecmetric[ppw->index_mt_phi_prime];
    }

    if (ppr->radiation_streaming_approximation == rsa_MD_with_reio) {

      ppw->rsa_delta_g +=
        -4./k2*ppw->pvecthermo[pth->index_th_dkappa]*y[ppw->pv->index_pt_theta_b];

      ppw->rsa_theta_g +=
        3./k2*(ppw->pvecthermo[pth->index_th_ddkappa]*y[ppw->pv->index_pt_theta_b]
               +ppw->pvecthermo[pth->index_th_dkappa]*
               (-a_prime_over_a*y[ppw->pv->index_pt_theta_b]
                +ppw->pvecthermo[pth->index_th_cb2]*k2*y[ppw->pv->index_pt_delta_b]
                +k2*y[ppw->pv->index_pt_phi]));
    }

    if (pba->has_ur == _TRUE_) {

      if (ppr->radiation_streaming_approximation == rsa_null) {
        ppw->rsa_delta_ur = 0.;
        ppw->rsa_theta_ur = 0.;
      }
      else {
        ppw->rsa_delta_ur = -4.*y[ppw->pv->index_pt_phi];
        ppw->rsa_theta_ur = 6.*ppw->pvecmetric[ppw->index_mt_phi_prime];
      }
    }
  }

  /* synchronous gauge */
  if (ppt->gauge == synchronous) {

    if (ppr->radiation_streaming_approximation == rsa_null) {
      ppw->rsa_delta_g = 0.;
      ppw->rsa_theta_g = 0.;
    }
    else {

      ppw->rsa_delta_g = 4./k2*(a_prime_over_a*ppw->pvecmetric[ppw->index_mt_h_prime]
                                -k2*y[ppw->pv->index_pt_eta]);
      ppw->rsa_theta_g = -0.5*ppw->pvecmetric[ppw->index_mt_h_prime];
    }

    if (ppr->radiation_streaming_approximation == rsa_MD_with_reio) {

      ppw->rsa_delta_g +=
        -4./k2*ppw->pvecthermo[pth->index_th_dkappa]*(y[ppw->pv->index_pt_theta_b]+0.5*ppw->pvecmetric[ppw->index_mt_h_prime]);

      ppw->rsa_theta_g +=
        3./k2*(ppw->pvecthermo[pth->index_th_ddkappa]*
               (y[ppw->pv->index_pt_theta_b]
                +0.5*ppw->pvecmetric[ppw->index_mt_h_prime])
               +ppw->pvecthermo[pth->index_th_dkappa]*
               (-a_prime_over_a*y[ppw->pv->index_pt_theta_b]
                + ppw->pvecthermo[pth->index_th_cb2]*k2*y[ppw->pv->index_pt_delta_b]
                -a_prime_over_a*ppw->pvecmetric[ppw->index_mt_h_prime]
                +k2*y[ppw->pv->index_pt_eta]));
    }

    if (pba->has_ur == _TRUE_) {

      if (ppr->radiation_streaming_approximation == rsa_null) {
        ppw->rsa_delta_ur = 0.;
        ppw->rsa_theta_ur = 0.;
      }
      else {
        ppw->rsa_delta_ur = 4./k2*(a_prime_over_a*ppw->pvecmetric[ppw->index_mt_h_prime]
                                   -k2*y[ppw->pv->index_pt_eta]);
        ppw->rsa_theta_ur = -0.5*ppw->pvecmetric[ppw->index_mt_h_prime];
      }
    }
  }

  /* update total delta and theta given rsa approximation results */

  ppw->delta_rho += ppw->pvecback[pba->index_bg_rho_g]*ppw->rsa_delta_g;
  ppw->delta_p += 1./3.*ppw->pvecback[pba->index_bg_rho_g]*ppw->rsa_delta_g;
  ppw->rho_plus_p_theta += 4./3.*ppw->pvecback[pba->index_bg_rho_g]*ppw->rsa_theta_g;

  if (pba->has_ur == _TRUE_) {
    ppw->delta_rho += ppw->pvecback[pba->index_bg_rho_ur]*ppw->rsa_delta_ur;
    ppw->delta_p += 1./3.*ppw->pvecback[pba->index_bg_rho_ur]*ppw->rsa_delta_ur;
    ppw->rho_plus_p_theta += 4./3.*ppw->pvecback[pba->index_bg_rho_ur]*ppw->rsa_theta_ur;
  }

  return _SUCCESS_;

}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x1000, %rsp           # imm = 0x1000
movq 0x1020(%rsp), %r9
movq 0x180(%r9), %rax
movslq 0x164(%r9), %r10
cmpl $0x0, (%rax,%r10,4)
je 0x8cdc1
mulsd %xmm0, %xmm0
movl 0x83c(%rcx), %eax
testl %eax, %eax
je 0x8ce2a
cmpl $0x1, %eax
jne 0x8cf5a
movl 0x620(%rdi), %r11d
testl %r11d, %r11d
je 0x8cf79
movsd 0x5b7a6(%rip), %xmm4    # 0xe83f8
divsd %xmm0, %xmm4
movq 0x40(%r9), %rax
movq 0x48(%r9), %r10
movslq 0x8(%r9), %rcx
movslq 0xd0(%r10), %rdi
movsd (%r8,%rdi,8), %xmm2
mulsd %xmm0, %xmm2
movsd (%rax,%rcx,8), %xmm3
mulsd %xmm1, %xmm3
subsd %xmm2, %xmm3
mulsd %xmm4, %xmm3
movsd %xmm3, 0xa8(%r9)
movsd (%rax,%rcx,8), %xmm2
mulsd 0x5b79e(%rip), %xmm2    # 0xe8438
movsd %xmm2, 0xb0(%r9)
cmpl $0x2, %r11d
jne 0x8cd7d
movsd 0x5b773(%rip), %xmm6    # 0xe8428
divsd %xmm0, %xmm6
movq 0x38(%r9), %r11
movslq 0xe80(%rdx), %rbx
mulsd (%r11,%rbx,8), %xmm6
movslq 0x2c(%r10), %r15
movsd 0x58fda(%rip), %xmm5    # 0xe5cb0
movsd (%rax,%rcx,8), %xmm7
mulsd %xmm5, %xmm7
addsd (%r8,%r15,8), %xmm7
mulsd %xmm6, %xmm7
addsd %xmm7, %xmm3
movsd %xmm3, 0xa8(%r9)
movsd 0x59a4a(%rip), %xmm6    # 0xe6748
divsd %xmm0, %xmm6
movslq 0xe88(%rdx), %r14
movsd (%r8,%r15,8), %xmm7
movsd (%rax,%rcx,8), %xmm8
mulsd %xmm8, %xmm5
movslq 0xef0(%rdx), %rdx
movsd (%r11,%rdx,8), %xmm9
mulsd %xmm0, %xmm9
movslq 0x28(%r10), %rdx
mulsd (%r8,%rdx,8), %xmm9
addsd %xmm7, %xmm5
mulsd %xmm1, %xmm7
subsd %xmm7, %xmm9
mulsd %xmm1, %xmm8
subsd %xmm8, %xmm9
movsd (%r8,%rdi,8), %xmm7
mulsd %xmm0, %xmm7
addsd %xmm9, %xmm7
mulsd (%r11,%rbx,8), %xmm7
mulsd (%r11,%r14,8), %xmm5
addsd %xmm7, %xmm5
mulsd %xmm6, %xmm5
addsd %xmm5, %xmm2
movsd %xmm2, 0xb0(%r9)
cmpw $0x1, 0x4c4(%rsi)
jne 0x8cfa1
xorpd 0x5849d(%rip), %xmm0    # 0xe5230
mulsd (%r8,%rdi,8), %xmm0
mulsd (%rax,%rcx,8), %xmm1
addsd %xmm0, %xmm1
mulsd %xmm1, %xmm4
movsd %xmm4, 0xb8(%r9)
movsd (%rax,%rcx,8), %xmm0
mulsd 0x5b67c(%rip), %xmm0    # 0xe8438
jmp 0x8cf4d
addq $0x6c8a, %rcx           # imm = 0x6C8A
leaq 0x800(%rsp), %rbx
movq %rbx, %rdi
movq %rcx, %rsi
movq %rcx, %rdx
xorl %eax, %eax
callq 0x2e014
leaq 0x5829b(%rip), %rsi     # 0xe5082
leaq 0x8211b(%rip), %rdx     # 0x10ef09
movq %rsp, %r14
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x2e014
leaq 0x82133(%rip), %rdi     # 0x10ef38
leaq 0x58228(%rip), %rsi     # 0xe5034
leaq 0x82167(%rip), %rdx     # 0x10ef7a
movl $0x2cff, %ecx           # imm = 0x2CFF
movq %r14, %r8
xorl %eax, %eax
callq 0x2e014
pushq $0x1
popq %rax
jmp 0x8d069
movl 0x620(%rdi), %r11d
testl %r11d, %r11d
je 0x8cf79
movq 0x48(%r9), %r10
movslq 0xd4(%r10), %rax
movsd (%r8,%rax,8), %xmm3
mulsd 0x5b5d5(%rip), %xmm3    # 0xe8428
movq 0x40(%r9), %rcx
movsd %xmm3, 0xa8(%r9)
movslq 0x4(%r9), %rdi
movsd (%rcx,%rdi,8), %xmm2
mulsd 0x597df(%rip), %xmm2    # 0xe6650
movsd %xmm2, 0xb0(%r9)
cmpl $0x2, %r11d
jne 0x8cf1f
movsd 0x5b59c(%rip), %xmm4    # 0xe8428
divsd %xmm0, %xmm4
movq 0x38(%r9), %r11
movslq 0xe80(%rdx), %rbx
mulsd (%r11,%rbx,8), %xmm4
movslq 0x2c(%r10), %r14
mulsd (%r8,%r14,8), %xmm4
addsd %xmm4, %xmm3
movsd %xmm3, 0xa8(%r9)
movsd 0x59888(%rip), %xmm4    # 0xe6748
divsd %xmm0, %xmm4
movslq 0xe88(%rdx), %r15
movsd (%r8,%r14,8), %xmm5
movslq 0xef0(%rdx), %rdx
movsd (%r11,%rdx,8), %xmm6
mulsd %xmm0, %xmm6
movslq 0x28(%r10), %rdx
mulsd (%r8,%rdx,8), %xmm6
mulsd %xmm5, %xmm1
subsd %xmm1, %xmm6
mulsd (%r8,%rax,8), %xmm0
addsd %xmm6, %xmm0
mulsd (%r11,%rbx,8), %xmm0
mulsd (%r11,%r15,8), %xmm5
addsd %xmm0, %xmm5
mulsd %xmm4, %xmm5
addsd %xmm5, %xmm2
movsd %xmm2, 0xb0(%r9)
cmpw $0x1, 0x4c4(%rsi)
jne 0x8cfa1
movsd (%r8,%rax,8), %xmm0
mulsd 0x5b4f1(%rip), %xmm0    # 0xe8428
movsd %xmm0, 0xb8(%r9)
movsd (%rcx,%rdi,8), %xmm0
mulsd 0x59703(%rip), %xmm0    # 0xe6650
movsd %xmm0, 0xc0(%r9)
movb $0x1, %cl
jmp 0x8cfab
movsd 0xa8(%r9), %xmm3
movsd 0xb0(%r9), %xmm2
cmpw $0x1, 0x4c4(%rsi)
sete %cl
jmp 0x8cfab
xorpd %xmm0, %xmm0
movupd %xmm0, 0xa8(%r9)
xorpd %xmm2, %xmm2
cmpw $0x1, 0x4c4(%rsi)
jne 0x8cfa5
movupd %xmm0, 0xb8(%r9)
movb $0x1, %cl
jmp 0x8cfa7
xorl %ecx, %ecx
jmp 0x8cfab
xorl %ecx, %ecx
xorpd %xmm3, %xmm3
movq 0x30(%r9), %rdx
movslq 0x38c(%rsi), %rax
movsd (%rdx,%rax,8), %xmm4
mulsd %xmm3, %xmm4
addsd 0x50(%r9), %xmm4
movsd %xmm4, 0x50(%r9)
movsd (%rdx,%rax,8), %xmm0
mulsd 0x5b430(%rip), %xmm0    # 0xe8408
mulsd %xmm3, %xmm0
addsd 0x68(%r9), %xmm0
movsd %xmm0, 0x68(%r9)
movsd (%rdx,%rax,8), %xmm1
mulsd 0x5b48b(%rip), %xmm1    # 0xe8480
mulsd %xmm2, %xmm1
addsd 0x58(%r9), %xmm1
movsd %xmm1, 0x58(%r9)
xorl %eax, %eax
testb %cl, %cl
je 0x8d069
movslq 0x3ac(%rsi), %rcx
movsd 0xb8(%r9), %xmm2
movsd (%rdx,%rcx,8), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm4, %xmm3
movsd %xmm3, 0x50(%r9)
movsd (%rdx,%rcx,8), %xmm3
mulsd 0x5b3cd(%rip), %xmm3    # 0xe8408
mulsd %xmm2, %xmm3
addsd %xmm0, %xmm3
movsd %xmm3, 0x68(%r9)
movsd (%rdx,%rcx,8), %xmm0
mulsd 0x5b42a(%rip), %xmm0    # 0xe8480
mulsd 0xc0(%r9), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x58(%r9)
addq $0x1000, %rsp           # imm = 0x1000
popq %rbx
popq %r14
popq %r15
retq