// File: /workspace/github_c_makefile_star2_O1/PoulinV[P]AxiCLASS/build/../source/output.c
// Function: output_tk
int output_tk(
              struct background * pba,
              struct perturbations * ppt,
              struct output * pop
              ) {

  /** Summary: */

  /** - define local variables */
  char titles[_MAXTITLESTRINGLENGTH_]={0};
  double * data;
  int size_data, number_of_titles;

  FILE * tkfile;

  int index_md;
  int index_ic;
  int index_z;

  double z;

  FileName file_name;
  char redshift_suffix[7]; // 7 is enough to write "z%d_" as long as there are at most 10'000 bins
  char first_line[_LINE_LENGTH_MAX_];
  char ic_suffix[_SUFFIXNAMESIZE_];   // 4 is enough to write "ad", "bi", "cdi", "nid", "niv", ...


  index_md=ppt->index_md_scalars;

  if (pop->output_format == camb_format) {

    class_test(pba->N_ncdm>1,
               pop->error_message,
               "you wish to output the transfer functions in CMBFAST/CAMB format but you have more than one non-cold dark matter (ncdm) species. The two are not compatible (since CMBFAST/CAMB only have one ncdm species): switch to CLASS output format or keep only on ncdm species");

    class_test(ppt->has_velocity_transfers == _TRUE_,
               pop->error_message,
               "you wish to output the transfer functions in CMBFAST/CAMB format, but you requested velocity transfer functions. The two are not compatible (since CMBFAST/CAMB do not compute velocity transfer functions): switch to CLASS output format, or ask only for density transfer function");
  }


  class_call(perturbations_output_titles(pba,ppt,pop->output_format,titles),
             pba->error_message,
             pop->error_message);
  number_of_titles = get_number_of_titles(titles);
  size_data = number_of_titles*ppt->k_size[index_md];

  class_alloc(data, sizeof(double)*ppt->ic_size[index_md]*size_data, pop->error_message);

  for (index_z = 0; index_z < pop->z_pk_num; index_z++) {

    z = pop->z_pk[index_z];

    /** - first, check that requested redshift z_pk is consistent */

    class_test((pop->z_pk[index_z] > ppt->z_max_pk),
               pop->error_message,
               "T_i(k,z) computed up to z=%f but requested at z=%f. Must increase z_max_pk in precision file.",ppt->z_max_pk,pop->z_pk[index_z]);

    if (pop->z_pk_num == 1)
      redshift_suffix[0]='\0';
    else
      class_sprintf(redshift_suffix,"z%d_",index_z+1);

    /** - second, open only the relevant files, and write a heading in each of them */

    class_call(perturbations_output_data_at_z(pba,
                                              ppt,
                                              pop->output_format,
                                              pop->z_pk[index_z],
                                              number_of_titles,
                                              data
                                              ),
               ppt->error_message,
               pop->error_message);

    for (index_ic = 0; index_ic < ppt->ic_size[index_md]; index_ic++) {

      class_call(perturbations_output_firstline_and_ic_suffix(ppt, index_ic, first_line, ic_suffix),
                 ppt->error_message, pop->error_message);

      if ((ppt->has_ad == _TRUE_) && (ppt->ic_size[index_md] == 1) )
        class_sprintf(file_name,"%s%s%s",pop->root,redshift_suffix,"tk.dat");
      else
        class_sprintf(file_name,"%s%s%s%s%s",pop->root,redshift_suffix,"tk_",ic_suffix,".dat");

      class_open(tkfile, file_name, "w", pop->error_message);

      if (pop->write_header == _TRUE_) {
        if (pop->output_format == class_format) {
          fprintf(tkfile,"# Transfer functions T_i(k) %sat redshift z=%g\n",first_line,z);
          fprintf(tkfile,"# for k=%g to %g h/Mpc,\n",ppt->k[index_md][0]/pba->h,ppt->k[index_md][ppt->k_size[index_md]-1]/pba->h);
          fprintf(tkfile,"# number of wavenumbers equal to %d\n",ppt->k_size[index_md]);
          if (ppt->has_density_transfers == _TRUE_) {
            fprintf(tkfile,"# d_i   stands for (delta rho_i/rho_i)(k,z) with above normalization \n");
            fprintf(tkfile,"# d_tot stands for (delta rho_tot/rho_tot)(k,z) with rho_Lambda NOT included in rho_tot\n");
            fprintf(tkfile,"# (note that this differs from the transfer function output from CAMB/CMBFAST, which gives the same\n");
            fprintf(tkfile,"#  quantities divided by -k^2 with k in Mpc^-1; use format=camb to match CAMB)\n");
          }
          if (ppt->has_velocity_transfers == _TRUE_) {
            fprintf(tkfile,"# t_i   stands for theta_i(k,z) with above normalization \n");
            fprintf(tkfile,"# t_tot stands for (sum_i [rho_i+p_i] theta_i)/(sum_i [rho_i+p_i]))(k,z)\n");
          }
          fprintf(tkfile,"#\n");
        }
        else if (pop->output_format == camb_format) {

          fprintf(tkfile,"# Rescaled matter transfer functions [-T_i(k)/k^2] %sat redshift z=%g\n",first_line,z);
          fprintf(tkfile,"# for k=%g to %g h/Mpc,\n",ppt->k[index_md][0]/pba->h,ppt->k[index_md][ppt->k_size[index_md]-1]/pba->h);
          fprintf(tkfile,"# number of wavenumbers equal to %d\n",ppt->k_size[index_md]);
          fprintf(tkfile,"# T_i   stands for (delta rho_i/rho_i)(k,z) with above normalization \n");
          fprintf(tkfile,"# The rescaling factor [-1/k^2] with k in 1/Mpc is here to match the CMBFAST/CAMB output convention\n");
          fprintf(tkfile,"#\n");
          fprintf(tkfile,"#");
          fprintf(tkfile,"\n");

        }
      }

      output_print_data(tkfile,
                        titles,
                        data+index_ic*size_data,
                        size_data);

      /** - free memory and close files */
      fclose(tkfile);

    }

  }

  free(data);

  return _SUCCESS_;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x34a8, %rsp           # imm = 0x34A8
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r13
leaq 0x1560(%rsp), %rdi
movl $0x1f40, %edx           # imm = 0x1F40
xorl %esi, %esi
callq 0x8200
movslq 0x848(%r14), %rax
movq %rax, (%rsp)
movl 0x40c(%rbx), %edx
cmpl $0x1, %edx
jne 0xed408
cmpl $0x2, 0x248(%r13)
jl 0xed3a2
leaq 0x4517f(%rip), %rsi     # 0x1324ca
leaq 0x160(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x30c2c
leaq 0x3d1e(%rip), %rsi      # 0xf1082
leaq 0x45267(%rip), %rdx     # 0x1325d2
leaq 0xd60(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x30c2c
addq $0x420, %rbx            # imm = 0x420
leaq 0x3ca6(%rip), %rsi      # 0xf1034
leaq 0x4524b(%rip), %rdx     # 0x1325e0
movq %rbx, %rdi
movl $0x421, %ecx            # imm = 0x421
jmp 0xeda18
cmpw $0x1, 0x36(%r14)
jne 0xed408
leaq 0x45239(%rip), %rsi     # 0x1325ea
leaq 0x160(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x30c2c
leaq 0x3cb8(%rip), %rsi      # 0xf1082
leaq 0x10941(%rip), %rdx     # 0xfdd12
leaq 0xd60(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x30c2c
addq $0x420, %rbx            # imm = 0x420
leaq 0x3c40(%rip), %rsi      # 0xf1034
leaq 0x451e5(%rip), %rdx     # 0x1325e0
movq %rbx, %rdi
movl $0x425, %ecx            # imm = 0x425
jmp 0xeda18
leaq 0x1560(%rsp), %rcx
movq %r13, %rdi
movq %r14, %rsi
callq 0x7558e
cmpl $0x1, %eax
jne 0xed46c
addq $0xd1c, %r13            # imm = 0xD1C
leaq 0x3fc2(%rip), %rsi      # 0xf13f0
leaq 0x452cb(%rip), %rdx     # 0x132700
leaq 0x160(%rsp), %r14
movq %r14, %rdi
movq %r13, %rcx
xorl %eax, %eax
callq 0x30c2c
addq $0x420, %rbx            # imm = 0x420
leaq 0x3bdc(%rip), %rsi      # 0xf1034
leaq 0x45181(%rip), %rdx     # 0x1325e0
movq %rbx, %rdi
movl $0x42b, %ecx            # imm = 0x42B
jmp 0xeda68
leaq 0x1560(%rsp), %rdi
callq 0x30d6e
movq 0x870(%r14), %rdx
movq 0x9c8(%r14), %rcx
movq (%rsp), %rsi
movslq (%rcx,%rsi,4), %rcx
movl %eax, 0x34(%rsp)
movslq %eax, %rbp
imulq %rcx, %rbp
movslq (%rdx,%rsi,4), %r12
movq %rbp, %rdi
imulq %r12, %rdi
shlq $0x3, %rdi
callq 0x8460
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0xed984
movl 0xe0(%rbx), %eax
testl %eax, %eax
jle 0xed973
movslq %ebp, %rcx
shlq $0x3, %rcx
movq %rcx, 0x50(%rsp)
xorl %ecx, %ecx
movq %rbp, 0x58(%rsp)
movq %rbx, 0x40(%rsp)
movq %r14, 0x48(%rsp)
movq %r13, 0x18(%rsp)
movsd 0xe8(%rbx,%rcx,8), %xmm1
movsd 0x818(%r14), %xmm0
movsd %xmm1, 0x20(%rsp)
ucomisd %xmm0, %xmm1
ja 0xeda89
cmpl $0x1, %eax
movq %rcx, 0x38(%rsp)
jne 0xed51f
movb $0x0, 0x11(%rsp)
jmp 0xed554
leal 0x1(%rcx), %ebp
xorl %edi, %edi
xorl %esi, %esi
leaq 0x44bed(%rip), %r15     # 0x13211a
movq %r15, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x8550
incl %eax
movslq %eax, %rsi
leaq 0x11(%rsp), %rdi
movq %r15, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x8550
movq 0x38(%rsp), %rcx
movl 0x40c(%rbx), %edx
movsd 0xe8(%rbx,%rcx,8), %xmm0
movq %r13, %rdi
movq %r14, %rsi
movl 0x34(%rsp), %ecx
movq 0x28(%rsp), %r8
callq 0x747e4
cmpl $0x1, %eax
je 0xedaed
movq 0x870(%r14), %rax
movq (%rsp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jle 0xed956
movq 0x28(%rsp), %r13
xorl %ebp, %ebp
movq %r14, %rdi
movl %ebp, %esi
leaq 0x960(%rsp), %rdx
leaq 0xd(%rsp), %rcx
callq 0x75e1d
cmpl $0x1, %eax
je 0xed9d1
cmpw $0x1, 0x14(%r14)
jne 0xed621
movq 0x870(%r14), %rax
movq (%rsp), %rcx
cmpl $0x1, (%rax,%rcx,4)
jne 0xed621
xorl %edi, %edi
xorl %esi, %esi
leaq 0x4527e(%rip), %r14     # 0x13285e
movq %r14, %rdx
movq %rbx, %rcx
leaq 0x11(%rsp), %r12
movq %r12, %r8
leaq 0x8a7e(%rip), %r15      # 0xf6073
movq %r15, %r9
xorl %eax, %eax
callq 0x8550
incl %eax
movslq %eax, %rsi
movq %rbx, %rcx
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
movq %r12, %r8
movq %r15, %r9
xorl %eax, %eax
callq 0x8550
jmp 0xed69f
xorl %edi, %edi
xorl %esi, %esi
movq %rbp, %r12
leaq 0x4522b(%rip), %rbp     # 0x13285a
movq %rbp, %rdx
movq %rbx, %rcx
leaq 0x11(%rsp), %r15
movq %r15, %r8
movq %r13, %r14
leaq 0x4521e(%rip), %r13     # 0x132865
movq %r13, %r9
xorl %eax, %eax
leaq 0x8a68(%rip), %r10      # 0xf60bb
pushq %r10
leaq 0x15(%rsp), %r10
pushq %r10
callq 0x8550
addq $0x10, %rsp
incl %eax
movslq %eax, %rsi
movq %rbx, %rcx
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rdx
movq %r12, %rbp
movq %r15, %r8
movq %r13, %r9
movq %r14, %r13
xorl %eax, %eax
leaq 0x8a2e(%rip), %r10      # 0xf60bb
pushq %r10
leaq 0x15(%rsp), %r10
pushq %r10
callq 0x8550
addq $0x10, %rsp
movq %rbx, %rdi
leaq 0x6d68(%rip), %rsi      # 0xf4411
callq 0x8400
testq %rax, %rax
je 0xeda1d
movq %rax, %r12
movq 0x40(%rsp), %rbx
cmpw $0x1, 0x408(%rbx)
movq 0x48(%rsp), %r14
jne 0xed916
movl 0x40c(%rbx), %eax
cmpl $0x1, %eax
je 0xed82f
testl %eax, %eax
jne 0xed916
movq %r12, %rdi
leaq 0x4517d(%rip), %rsi     # 0x132870
leaq 0x960(%rsp), %rdx
movsd 0x20(%rsp), %xmm0
movb $0x1, %al
callq 0x84e0
movq 0x9c8(%r14), %rax
movq 0x9d0(%r14), %rcx
movq (%rsp), %r15
movq (%rcx,%r15,8), %rcx
movsd (%rcx), %xmm0
movq 0x18(%rsp), %rdx
movsd 0x8(%rdx), %xmm2
divsd %xmm2, %xmm0
movslq (%rax,%r15,4), %rax
movsd -0x8(%rcx,%rax,8), %xmm1
divsd %xmm2, %xmm1
movq %r12, %rdi
leaq 0x45158(%rip), %rsi     # 0x1328a0
movb $0x2, %al
callq 0x84e0
movq 0x9c8(%r14), %rax
movl (%rax,%r15,4), %edx
movq %r12, %rdi
leaq 0x45155(%rip), %rsi     # 0x1328b9
xorl %eax, %eax
callq 0x84e0
cmpw $0x1, 0x34(%r14)
jne 0xed7d7
movl $0x46, %esi
movl $0x1, %edx
leaq 0x4515a(%rip), %rdi     # 0x1328de
movq %r12, %rcx
callq 0x86d0
movl $0x58, %esi
movl $0x1, %edx
leaq 0x45188(%rip), %rdi     # 0x132925
movq %r12, %rcx
callq 0x86d0
movl $0x64, %esi
movl $0x1, %edx
leaq 0x451c8(%rip), %rdi     # 0x13297e
movq %r12, %rcx
callq 0x86d0
movl $0x4f, %esi
movl $0x1, %edx
leaq 0x45214(%rip), %rdi     # 0x1329e3
movq %r12, %rcx
callq 0x86d0
cmpw $0x1, 0x36(%r14)
jne 0xed811
movl $0x3a, %esi
movl $0x1, %edx
leaq 0x45243(%rip), %rdi     # 0x132a33
movq %r12, %rcx
callq 0x86d0
movl $0x49, %esi
movl $0x1, %edx
leaq 0x45265(%rip), %rdi     # 0x132a6e
movq %r12, %rcx
callq 0x86d0
movl $0x2, %esi
movl $0x1, %edx
leaq 0x1fc8b(%rip), %rdi     # 0x10d4ad
movq %r12, %rcx
callq 0x86d0
jmp 0xed916
movq %r12, %rdi
leaq 0x4527f(%rip), %rsi     # 0x132ab8
leaq 0x960(%rsp), %rdx
movsd 0x20(%rsp), %xmm0
movb $0x1, %al
callq 0x84e0
movq 0x9c8(%r14), %rax
movq 0x9d0(%r14), %rcx
movq (%rsp), %r15
movq (%rcx,%r15,8), %rcx
movsd (%rcx), %xmm0
movq 0x18(%rsp), %rdx
movsd 0x8(%rdx), %xmm2
divsd %xmm2, %xmm0
movslq (%rax,%r15,4), %rax
movsd -0x8(%rcx,%rax,8), %xmm1
divsd %xmm2, %xmm1
movq %r12, %rdi
leaq 0x45012(%rip), %rsi     # 0x1328a0
movb $0x2, %al
callq 0x84e0
movq 0x9c8(%r14), %rax
movl (%rax,%r15,4), %edx
movq %r12, %rdi
leaq 0x4500f(%rip), %rsi     # 0x1328b9
xorl %eax, %eax
callq 0x84e0
movl $0x46, %esi
movl $0x1, %edx
leaq 0x4523d(%rip), %rdi     # 0x132aff
movq %r12, %rcx
callq 0x86d0
movl $0x64, %esi
movl $0x1, %edx
leaq 0x4526b(%rip), %rdi     # 0x132b46
movq %r12, %rcx
callq 0x86d0
movl $0x2, %esi
movl $0x1, %edx
leaq 0x1fbb9(%rip), %rdi     # 0x10d4ad
movq %r12, %rcx
callq 0x86d0
movl $0x23, %edi
movq %r12, %rsi
callq 0x8440
movl $0xa, %edi
movq %r12, %rsi
callq 0x8440
movq %r12, %rdi
leaq 0x1560(%rsp), %rsi
movq %r13, %rdx
movq 0x58(%rsp), %rcx
callq 0xf0579
movq %r12, %rdi
callq 0x83a0
incq %rbp
movq 0x870(%r14), %rax
movq (%rsp), %rcx
movslq (%rax,%rcx,4), %rax
addq 0x50(%rsp), %r13
cmpq %rax, %rbp
jl 0xed59c
movq 0x38(%rsp), %rcx
incq %rcx
movslq 0xe0(%rbx), %rax
cmpq %rax, %rcx
movq 0x18(%rsp), %r13
jl 0xed4ec
movq 0x28(%rsp), %rdi
callq 0x8410
xorl %eax, %eax
jmp 0xeda77
imull %ebp, %r12d
shll $0x3, %r12d
leaq 0x3671(%rip), %rsi      # 0xf1004
leaq 0x409b3(%rip), %rdx     # 0x12e34d
leaq 0x160(%rsp), %r14
movq %r14, %rdi
movl %r12d, %ecx
xorl %eax, %eax
callq 0x30c2c
addq $0x420, %rbx            # imm = 0x420
leaq 0x3677(%rip), %rsi      # 0xf1034
leaq 0x44c1c(%rip), %rdx     # 0x1325e0
movq %rbx, %rdi
movl $0x42f, %ecx            # imm = 0x42F
jmp 0xeda68
addq $0x6c8a, %r14           # imm = 0x6C8A
leaq 0x3a11(%rip), %rsi      # 0xf13f0
leaq 0x44e21(%rip), %rdx     # 0x132807
leaq 0x160(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x30c2c
addq $0x420, %rbx            # imm = 0x420
leaq 0x362b(%rip), %rsi      # 0xf1034
leaq 0x44bd0(%rip), %rdx     # 0x1325e0
movq %rbx, %rdi
movl $0x44f, %ecx            # imm = 0x44F
movq %r15, %r8
jmp 0xeda6b
leaq 0x5fb4(%rip), %rsi      # 0xf39d8
leaq 0x44e3e(%rip), %rdx     # 0x132869
leaq 0x1f9d2(%rip), %r8      # 0x10d404
leaq 0x160(%rsp), %r14
leaq 0x60(%rsp), %rcx
movq %r14, %rdi
xorl %eax, %eax
callq 0x30c2c
movq 0x40(%rsp), %rdi
addq $0x420, %rdi            # imm = 0x420
leaq 0x35d8(%rip), %rsi      # 0xf1034
leaq 0x44b7d(%rip), %rdx     # 0x1325e0
movl $0x456, %ecx            # imm = 0x456
movq %r14, %r8
xorl %eax, %eax
callq 0x30c2c
movl $0x1, %eax
addq $0x34a8, %rsp           # imm = 0x34A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x44caf(%rip), %rsi     # 0x13273f
leaq 0x160(%rsp), %r14
movq %r14, %rdi
movsd 0x20(%rsp), %xmm1
movb $0x2, %al
callq 0x30c2c
leaq 0x35d3(%rip), %rsi      # 0xf1082
leaq 0x4463f(%rip), %rdx     # 0x1320f5
leaq 0xd60(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x30c2c
addq $0x420, %rbx            # imm = 0x420
leaq 0x355b(%rip), %rsi      # 0xf1034
leaq 0x44b00(%rip), %rdx     # 0x1325e0
movq %rbx, %rdi
movl $0x439, %ecx            # imm = 0x439
jmp 0xeda18
addq $0x6c8a, %r14           # imm = 0x6C8A
leaq 0x38f5(%rip), %rsi      # 0xf13f0
leaq 0x44c9b(%rip), %rdx     # 0x13279d
leaq 0x160(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x30c2c
addq $0x420, %rbx            # imm = 0x420
leaq 0x350f(%rip), %rsi      # 0xf1034
leaq 0x44ab4(%rip), %rdx     # 0x1325e0
movq %rbx, %rdi
movl $0x44a, %ecx            # imm = 0x44A
jmp 0xeda18