// File: /workspace/github_c_makefile_star2_O2/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 $0xd8, %rsp
movq %rcx, %r13
movq %rdx, %rbp
movq %rsi, %r15
movq %rdi, %r12
movl $0x200, %edi            # imm = 0x200
callq 0x8490
movq %rax, %r14
movb $0x0, (%rax)
movq %rax, %rdi
movq %rbp, %rsi
callq 0x8690
movq %r14, %rdi
callq 0x8190
movabsq $0x6e695f6168706c41, %rcx # imm = 0x6E695F6168706C41
movq %rcx, (%r14,%rax)
movabsq $0x7461642e666e69, %rcx # imm = 0x7461642E666E69
movq %rcx, 0x6(%r14,%rax)
leaq 0x3675e(%rip), %rsi     # 0x103c64
movq %r14, %rdi
callq 0x8420
testq %rax, %rax
je 0xcd8d1
movq %rax, %rbx
movl $0x200, %edi            # imm = 0x200
callq 0x8490
movq %rbp, %rsi
movq %rax, %rbp
movb $0x0, (%rax)
movq %rax, %rdi
movq %rsi, 0x20(%rsp)
callq 0x8690
movq %rbp, %rdi
callq 0x8190
movabsq $0x61642e666e695f52, %rcx # imm = 0x61642E666E695F52
movq %rcx, (%rbp,%rax)
movw $0x74, 0x8(%rbp,%rax)
leaq 0x36705(%rip), %rsi     # 0x103c64
movq %rbp, %rdi
callq 0x8420
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0xcd763
movq %rbp, 0x40(%rsp)
movq %r14, 0x48(%rsp)
pushq $0x28
popq %r14
pushq $0x64
popq %rbp
movl %r14d, %edi
movl %ebp, %esi
movq %r15, %rdx
movq %r13, %rcx
callq 0xcbec4
movq %rax, 0x460(%r12)
movl %r14d, %edi
movl %ebp, %esi
movq %r15, %rdx
movq %r13, %rcx
callq 0xcbec4
movq %rax, 0x468(%r12)
movl %r14d, %edi
movl %ebp, %esi
movq %r15, %rdx
movq %r13, %rcx
callq 0xcbec4
movq %rax, 0x470(%r12)
movl %r14d, %edi
movl %ebp, %esi
movq %r15, %rdx
movq %r13, %rcx
callq 0xcbec4
movq %rax, 0x478(%r12)
movsd 0x53c3a(%rip), %xmm0    # 0x121228
movsd 0x53c3a(%rip), %xmm1    # 0x121230
movl %ebp, %edi
movq %r12, %rsi
callq 0xcc06f
leaq 0x320(%r12), %rsi
movsd 0x17c80(%rip), %xmm0    # 0xe5290
movsd 0x17c48(%rip), %xmm1    # 0xe5260
movl %r14d, %edi
callq 0xcc06f
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, 0x18(%rsp)
xorl %ebp, %ebp
movq %r15, 0x10(%rsp)
movq %r13, 0x28(%rsp)
cmpq $0x64, %rbp
je 0xcd77a
leaq (,%rbp,8), %r13
xorl %r15d, %r15d
cmpq $0x28, %r15
je 0xcd6e2
xorl %r14d, %r14d
cmpq $0x4, %r14
je 0xcd6dd
movq 0x460(%r12,%r14,8), %rax
movq (%rax,%r15,8), %rdx
addq %r13, %rdx
movq %rbx, %rdi
leaq 0x5039d(%rip), %rsi     # 0x11da42
xorl %eax, %eax
callq 0x86c0
cmpl $0x1, %eax
jne 0xcd72b
movq 0x460(%r12,%r14,8), %rax
movq (%rax,%r15,8), %rax
movsd (%rax,%rbp,8), %xmm0
callq 0x82e0
movq 0x460(%r12,%r14,8), %rax
movq (%rax,%r15,8), %rax
movsd %xmm0, (%rax,%rbp,8)
incq %r14
jmp 0xcd686
incq %r15
jmp 0xcd67d
movq 0x18(%rsp), %rax
leaq (%rax,%rbp,8), %r14
movq 0x8(%rsp), %rdi
leaq 0x5034b(%rip), %rsi     # 0x11da42
movq %r14, %rdx
xorl %eax, %eax
callq 0x86c0
cmpl $0x1, %eax
jne 0xcd99d
movsd (%r14), %xmm0
callq 0x82e0
movsd %xmm0, (%r14)
incq %rbp
movq 0x10(%rsp), %r15
movq 0x28(%rsp), %r13
jmp 0xcd668
leaq 0x53c04(%rip), %rsi     # 0x121336
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq 0x48(%rsp), %rdx
xorl %eax, %eax
callq 0x8090
movq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x8690
movq 0x10(%rsp), %rax
movl $0x1, (%rax)
jmp 0xcd8fc
leaq 0x53b8c(%rip), %rsi     # 0x1212f6
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rdx
jmp 0xcd8e3
movq %rbx, %rdi
callq 0x83c0
movq 0x8(%rsp), %rdi
callq 0x83c0
movl $0x200, %edi            # imm = 0x200
callq 0x8490
movq %rax, %r14
movb $0x0, (%rax)
movq %rax, %rdi
movq 0x20(%rsp), %rsi
callq 0x8690
movq %r14, %rdi
callq 0x8190
movabsq $0x7461642e73656c, %rcx # imm = 0x7461642E73656C
movq %rcx, 0xe(%r14,%rax)
movupd 0x53bcd(%rip), %xmm0    # 0x121395
movupd %xmm0, (%r14,%rax)
leaq 0x3648f(%rip), %rsi     # 0x103c64
movq %r14, %rdi
callq 0x8420
movq %rax, 0x38(%rsp)
testq %rax, %rax
je 0xcd8d1
movq %r14, 0x30(%rsp)
leaq 0x2e90(%r12), %rbx
movl $0x138, %eax            # imm = 0x138
decq %rax
je 0xcd90e
leaq 0x8(%rbx), %rcx
movq %rcx, 0x20(%rsp)
leaq -0x26e0(%rbx), %rdx
movq 0x38(%rsp), %r15
movq %r15, %rdi
leaq 0x5021d(%rip), %r14     # 0x11da42
movq %r14, %rsi
movq %rax, 0x8(%rsp)
xorl %eax, %eax
callq 0x86c0
movl %eax, %ebp
leaq -0x1d28(%rbx), %rdx
movq %r15, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x86c0
movl %eax, %r14d
addl %ebp, %r14d
leaq -0x1370(%rbx), %rdx
movq %r15, %rdi
leaq 0x501e1(%rip), %rsi     # 0x11da42
xorl %eax, %eax
callq 0x86c0
movl %eax, 0x18(%rsp)
leaq -0x9b8(%rbx), %rdx
movq %r15, %rdi
leaq 0x501c5(%rip), %rsi     # 0x11da42
xorl %eax, %eax
callq 0x86c0
movl %eax, %ebp
addl 0x18(%rsp), %ebp
addl %r14d, %ebp
movq %r15, %rdi
movq 0x10(%rsp), %r15
leaq 0x501a6(%rip), %rsi     # 0x11da42
movq %rbx, %rdx
xorl %eax, %eax
callq 0x86c0
addl %ebp, %eax
movq 0x20(%rsp), %rbx
cmpl $0x5, %eax
movq 0x8(%rsp), %rax
je 0xcd7fd
leaq 0x53a74(%rip), %rsi     # 0x121336
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq 0x30(%rsp), %rdx
jmp 0xcd8e3
leaq 0x53a1e(%rip), %rsi     # 0x1212f6
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x8090
movq %r13, %rdi
movq %rbx, %rsi
callq 0x8690
movl $0x1, (%r15)
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38(%rsp), %rdi
callq 0x83c0
xorpd %xmm0, %xmm0
xorl %eax, %eax
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rbx
movq 0x30(%rsp), %r14
cmpq $0x8c, %rax
je 0xcd944
addsd 0x1b20(%r12,%rax,8), %xmm0
incq %rax
jmp 0xcd92d
movsd 0x538b4(%rip), %xmm1    # 0x121200
divsd %xmm0, %xmm1
xorl %eax, %eax
cmpq $0x8c, %rax
je 0xcd977
movsd 0x1b20(%r12,%rax,8), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x1b20(%r12,%rax,8)
incq %rax
jmp 0xcd952
callq 0x8430
movq %rbx, %rdi
callq 0x8430
movq %r14, %rdi
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8430
leaq 0x53992(%rip), %rsi     # 0x121336
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq 0x40(%rsp), %rdx
jmp 0xcd73f