// File: /workspace/github_c_makefile_star2_O3/PoulinV[P]AxiCLASS/build/../external/HyRec2020/hydrogen.c
// Function: allocate_and_read_atomic
void allocate_and_read_atomic(HYREC_ATOMIC *atomic, int *error, char *path_to_hyrec, char error_message[SIZE_ErrorM]){

  /*********** Effective rates *************/

  char *alpha_file, *rr_file, *twog_file;
  char sub_message[128];
  alpha_file = malloc(SIZE_InputFile);
  alpha_file[0] = 0;
  strcat(alpha_file, path_to_hyrec);
  strcat(alpha_file, ALPHA_FILE);

  FILE *fA = fopen(alpha_file, "r");
  if (fA == NULL) {
    sprintf(sub_message, "in allocate_and_read_atomic: could not open file %s \n", alpha_file);
    strcat(error_message, sub_message);
    *error = 1;
    return;
  }

  rr_file = malloc(SIZE_InputFile);
  rr_file[0] = 0;
  strcat(rr_file, path_to_hyrec);
  strcat(rr_file, RR_FILE);

  FILE *fR = fopen(rr_file, "r");
  if (fR == NULL) {
    sprintf(sub_message, "in allocate_and_read_atomic: could not open file %s \n", rr_file);
    strcat(error_message, sub_message);
    *error = 1;
    return;
  }

  unsigned i, j, l;

  /* Allocate memory */
  atomic->logAlpha_tab[0] = create_2D_array(NTM, NTR, error, error_message);
  atomic->logAlpha_tab[1] = create_2D_array(NTM, NTR, error, error_message);
  atomic->logAlpha_tab[2] = create_2D_array(NTM, NTR, error, error_message);
  atomic->logAlpha_tab[3] = create_2D_array(NTM, NTR, error, error_message);

  maketab(log(TR_MIN), log(TR_MAX), NTR, atomic->logTR_tab);
  maketab(T_RATIO_MIN, T_RATIO_MAX, NTM, atomic->T_RATIO_tab);
  atomic->DlogTR = atomic->logTR_tab[1] - atomic->logTR_tab[0];
  atomic->DT_RATIO = atomic->T_RATIO_tab[1] - atomic->T_RATIO_tab[0];

  for (i = 0; i < NTR; i++) {
    for (j = 0; j < NTM; j++) for (l = 0; l <= 3; l++) {
      if( fscanf(fA, "%le", &(atomic->logAlpha_tab[l][j][i])) != 1){
        sprintf(sub_message, "in allocate_and_read_atomic: could not read file %s completely -- The file might be corrupted\n", alpha_file);
        strcat(error_message, sub_message);
        *error = 1;
        return;
      }
      atomic->logAlpha_tab[l][j][i] = log(atomic->logAlpha_tab[l][j][i]);
    }
    if ( fscanf(fR, "%le", &(atomic->logR2p2s_tab[i])) != 1){
        sprintf(sub_message, "in allocate_and_read_atomic: could not read file %s completely -- The file might be corrupted\n", rr_file);
        strcat(error_message, sub_message);
        *error = 1;
        return;
    }
    atomic->logR2p2s_tab[i] = log(atomic->logR2p2s_tab[i]);
  }
  fclose(fA);
  fclose(fR);

  /************ Two-photon rates ************/

  FILE *f2g;
  unsigned b;
  double L2s1s_current;
  int fscanf_counter;

  twog_file = malloc(SIZE_InputFile);
  twog_file[0] = 0;
  strcat(twog_file, path_to_hyrec);
  strcat(twog_file, TWOG_FILE);

  f2g = fopen(twog_file, "r");
  if (f2g == NULL) {
    sprintf(sub_message, "in allocate_and_read_atomic: could not open file %s \n", twog_file);
    strcat(error_message, sub_message);
    *error = 1;
    return;
  }

  for (b = 0; b < NVIRT; b++) {
    fscanf_counter = 0;
    fscanf_counter+= fscanf(f2g, "%le", &(atomic->Eb_tab[b]));
    fscanf_counter+= fscanf(f2g, "%le", &(atomic->A1s_tab[b]));
    fscanf_counter+= fscanf(f2g, "%le", &(atomic->A2s_tab[b]));
    fscanf_counter+= fscanf(f2g, "%le", &(atomic->A3s3d_tab[b]));
    fscanf_counter+= fscanf(f2g, "%le", &(atomic->A4s4d_tab[b]));
    if(fscanf_counter!=5){
      sprintf(sub_message, "in allocate_and_read_atomic: could not read file %s completely -- The file might be corrupted\n", twog_file);
      strcat(error_message, sub_message);
      *error = 1;
      return;
    }
  }
  fclose(f2g);

  /* Normalize 2s--1s differential decay rate to L2s1s (can be set by user in hydrogen.h) */
  L2s1s_current = 0.;
  for (b = 0; b < NSUBLYA; b++) L2s1s_current += atomic->A2s_tab[b];
  for (b = 0; b < NSUBLYA; b++) atomic->A2s_tab[b] *= L2s1s/L2s1s_current;

  /* Switches for the various effects considered in Hirata (2008) and diffusion:
      Effect A: correct 2s-->1s rate, with stimulated decays and absorptions of non-thermal photons
      Effect B: Sub-Lyman-alpha two-photon decays
      Effect C: Super-Lyman-alpha two-photon decays
      Effect D: Raman scattering */

  #if (EFFECT_A == 0)
    for (b = 0; b < NSUBLYA; b++) atomic->A2s_tab[b] = 0;
  #endif
  #if (EFFECT_B == 0)
    for (b = 0; b < NSUBLYA; b++) atomic->A3s3d_tab[b] = atomic->A4s4d_tab[b] = 0;
  #endif
  #if (EFFECT_C == 0)
    for (b = NSUBLYA; b < NVIRT; b++) atomic->A3s3d_tab[b] = atomic->A4s4d_tab[b] = 0;
  #endif
  #if (EFFECT_D == 0)
    for (b = NSUBLYA; b < NVIRT; b++) atomic->A2s_tab[b] = 0;
    for (b = NSUBLYB; b < NVIRT; b++) atomic->A3s3d_tab[b] = 0;
  #endif
  #if (DIFFUSION == 0)
    for (b = 0; b < NVIRT; b++) atomic->A1s_tab[b] = 0;
  #endif

  free(alpha_file);
  free(rr_file);
  free(twog_file);

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rcx, %r15
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %r12
movl $0x200, %edi            # imm = 0x200
callq 0x8460
movq %rax, %r13
movb $0x0, (%rax)
movq %rax, %rdi
movq %rbp, %rsi
callq 0x8660
movq %r13, %rdi
callq 0x8180
movabsq $0x6e695f6168706c41, %rcx # imm = 0x6E695F6168706C41
movq %rcx, (%r13,%rax)
movabsq $0x7461642e666e69, %rcx # imm = 0x7461642E666E69
movq %rcx, 0x6(%r13,%rax)
leaq 0x37d43(%rip), %rsi     # 0x10cda4
movq %r13, %rdi
callq 0x8400
testq %rax, %rax
je 0xd545f
movq %rax, %rbx
movl $0x200, %edi            # imm = 0x200
callq 0x8460
movq %rbp, %rsi
movq %rax, %rbp
movb $0x0, (%rax)
movq %rax, %rdi
movq %rsi, 0x28(%rsp)
callq 0x8660
movq %rbp, %rdi
callq 0x8180
movabsq $0x61642e666e695f52, %rcx # imm = 0x61642E666E695F52
movq %rcx, (%rbp,%rax)
movw $0x74, 0x8(%rbp,%rax)
leaq 0x37cea(%rip), %rsi     # 0x10cda4
movq %rbp, %rdi
callq 0x8400
testq %rax, %rax
je 0xd5473
movq %rax, 0x10(%rsp)
movq %rbp, 0x30(%rsp)
movq %r13, 0x38(%rsp)
movl $0x28, %edi
movl $0x64, %esi
movq %r14, %rdx
movq %r15, %rcx
callq 0xd37d2
movq %rax, 0x460(%r12)
movl $0x28, %edi
movl $0x64, %esi
movq %r14, %rdx
movq %r15, %rcx
callq 0xd37d2
movq %rax, 0x468(%r12)
movl $0x28, %edi
movl $0x64, %esi
movq %r14, %rdx
movq %r15, %rcx
callq 0xd37d2
movq %rax, 0x470(%r12)
movl $0x28, %edi
movl $0x64, %esi
movq %r14, 0x20(%rsp)
movq %r14, %rdx
movq %r15, 0x18(%rsp)
movq %r15, %rcx
callq 0xd37d2
movq %rax, 0x478(%r12)
movsd 0x55228(%rip), %xmm0    # 0x12a388
movsd 0x55228(%rip), %xmm1    # 0x12a390
movl $0x64, %edi
movq %r12, %rsi
callq 0xd399d
leaq 0x320(%r12), %rsi
movsd 0x1910b(%rip), %xmm0    # 0xee290
movsd 0x190d3(%rip), %xmm1    # 0xee260
movl $0x28, %edi
callq 0xd399d
movsd 0x8(%r12), %xmm0
movhpd 0x328(%r12), %xmm0      # xmm0 = xmm0[0],mem[0]
movsd (%r12), %xmm1
movhpd 0x320(%r12), %xmm1      # xmm1 = xmm1[0],mem[0]
subpd %xmm1, %xmm0
movupd %xmm0, 0x7a0(%r12)
leaq 0x480(%r12), %rax
movq %rax, 0x8(%rsp)
xorl %ebp, %ebp
leaq (,%rbp,8), %r13
xorl %r15d, %r15d
xorl %r14d, %r14d
movq 0x460(%r12,%r14,8), %rax
movq (%rax,%r15,8), %rdx
addq %r13, %rdx
movq %rbx, %rdi
leaq 0x51996(%rip), %rsi     # 0x126b92
xorl %eax, %eax
callq 0x8690
cmpl $0x1, %eax
jne 0xd542a
movq 0x460(%r12,%r14,8), %rax
movq (%rax,%r15,8), %rax
movsd (%rax,%rbp,8), %xmm0
callq 0x82c0
movq 0x460(%r12,%r14,8), %rax
movq (%rax,%r15,8), %rax
movsd %xmm0, (%rax,%rbp,8)
incq %r14
cmpq $0x4, %r14
jne 0xd51e3
incq %r15
cmpq $0x28, %r15
jne 0xd51e0
movq 0x8(%rsp), %rax
leaq (%rax,%rbp,8), %r14
movq 0x10(%rsp), %rdi
leaq 0x51938(%rip), %rsi     # 0x126b92
movq %r14, %rdx
xorl %eax, %eax
callq 0x8690
cmpl $0x1, %eax
jne 0xd54b0
movsd (%r14), %xmm0
callq 0x82c0
movsd %xmm0, (%r14)
incq %rbp
cmpq $0x64, %rbp
jne 0xd51d5
movq %rbx, %rdi
callq 0x83a0
movq 0x10(%rsp), %rdi
callq 0x83a0
movl $0x200, %edi            # imm = 0x200
callq 0x8460
movq %rax, %r14
movb $0x0, (%rax)
movq %rax, %rdi
movq 0x28(%rsp), %rsi
callq 0x8660
movq %r14, %rdi
callq 0x8180
movabsq $0x7461642e73656c, %rcx # imm = 0x7461642E73656C
movq %rcx, 0xe(%r14,%rax)
movupd 0x55246(%rip), %xmm0    # 0x12a51d
movupd %xmm0, (%r14,%rax)
leaq 0x37ac0(%rip), %rsi     # 0x10cda4
movq %r14, %rdi
callq 0x8400
testq %rax, %rax
je 0xd54c9
movq %rax, %r13
movq %r14, 0x8(%rsp)
leaq 0x2e90(%r12), %r14
movl $0x137, %eax            # imm = 0x137
movq %rax, 0x10(%rsp)
leaq -0x26e0(%r14), %rdx
movq %r13, %rdi
leaq 0x51872(%rip), %r15     # 0x126b92
movq %r15, %rsi
xorl %eax, %eax
callq 0x8690
movl %eax, %ebx
leaq -0x1d28(%r14), %rdx
movq %r13, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x8690
movl %eax, %ebp
addl %ebx, %ebp
leaq -0x1370(%r14), %rdx
movq %r13, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x8690
movl %eax, 0x28(%rsp)
leaq -0x9b8(%r14), %rdx
movq %r13, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x8690
movl %eax, %ebx
addl 0x28(%rsp), %ebx
addl %ebp, %ebx
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x8690
addl %ebx, %eax
cmpl $0x5, %eax
jne 0xd54e0
addq $0x8, %r14
decq 0x10(%rsp)
jne 0xd530f
movq %r13, %rdi
callq 0x83a0
xorpd %xmm0, %xmm0
xorl %eax, %eax
addsd 0x1b20(%r12,%rax,8), %xmm0
incq %rax
cmpq $0x8c, %rax
jne 0xd53b0
movsd 0x54f93(%rip), %xmm1    # 0x12a360
divsd %xmm0, %xmm1
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
xorl %eax, %eax
movupd 0x1b20(%r12,%rax,8), %xmm0
mulpd %xmm1, %xmm0
movupd %xmm0, 0x1b20(%r12,%rax,8)
addq $0x2, %rax
cmpq $0x8c, %rax
jne 0xd53d7
movq 0x38(%rsp), %rdi
callq 0x8410
movq 0x30(%rsp), %rdi
callq 0x8410
movq 0x8(%rsp), %rdi
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8410
leaq 0x5508d(%rip), %rsi     # 0x12a4be
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq 0x38(%rsp), %rdx
xorl %eax, %eax
callq 0x8090
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x8660
movq 0x20(%rsp), %rax
movl $0x1, (%rax)
jmp 0xd549e
leaq 0x55018(%rip), %rsi     # 0x12a47e
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rdx
jmp 0xd5485
leaq 0x55004(%rip), %rsi     # 0x12a47e
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x8090
movq %r15, %rdi
movq %rbx, %rsi
callq 0x8660
movl $0x1, (%r14)
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x55007(%rip), %rsi     # 0x12a4be
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq 0x30(%rsp), %rdx
jmp 0xd543e
leaq 0x54fae(%rip), %rsi     # 0x12a47e
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
jmp 0xd543e
leaq 0x54fd7(%rip), %rsi     # 0x12a4be
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq 0x8(%rsp), %rdx
jmp 0xd543e