// File: /workspace/github_c_makefile_star2_O3/radforddc[P]icpc_siggen/signal_tester.c
// Function: save_signal
static int save_signal(char *cmd, MJD_Siggen_Setup *setup){
  float spec[MAX_SPE_CHS], d, a_over_e=0, t10, t90;
  struct point cart;
  int   comp_f;
  int   i, j, k, a_over_e_dt=4;
  char  *cp;
  static float *s;

  if (s == NULL){//first call
    if ((s = (float *) malloc(setup->ntsteps_out*sizeof(*s))) == NULL){
      printf("Malloc failed\n");
      return 1;
    }
  }

  if ((k = get_cart(cmd, &cart, setup->coord_type)) <= 0) return 1;
  cp = cmd + k;
  if (strlen(cp) == 0){
    fprintf(stderr, "must supply file name\n");
    return 1;
  }

  if (get_signal(cart, s, setup) < 0) {
    printf("point not in crystal or has no field: (x = %.1f, y = %.1f, z = %.1f)\n",
	   cart.x, cart.y, cart.z);
    return 1;
  }

  /* calculate t10, t90, and A/E */
  a_over_e = t10 = t90 = 0;
  for (i = 0; i < setup->ntsteps_out - a_over_e_dt; i++) {
    d = (s[i+a_over_e_dt] - s[i]) / (float) a_over_e_dt;
    if (d > a_over_e) a_over_e = d;
    if (s[i] < 0.1f && s[i+1] >= 0.1f)
      t10 = 10.0f*((float) i + (0.1f - s[i]) / (s[i+1] - s[i]));
    if (s[i] < 0.9f && s[i+1] >= 0.9f)
      t90 = 10.0f*((float) i + (0.9f - s[i]) / (s[i+1] - s[i]));
  }
  printf("t_90, t_10-90, A/E = %4.0f %4.0f %f\n", t90, t90-t10, a_over_e);

  comp_f = 1;
  while (setup->ntsteps_out/comp_f > MAX_SPE_CHS) comp_f++;
  if (comp_f > 1) printf("spectrum will be compressed by factor %d\n", comp_f);
  for (i = 0; i < (int) (sizeof(spec)/sizeof(spec[0])); i++) spec[i] = 0.0;

  /*copy signal data to spectrum array*/
  for (j = 0; j < setup->ntsteps_out; j++) spec[j/comp_f] += s[j]*1000/comp_f;
  write_spectrum(spec, setup->ntsteps_out/comp_f, cp);
  printf("%d channels of data saved in spectrum %s\n",
	 setup->ntsteps_out/comp_f, cp);

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10058, %rsp          # imm = 0x10058
movq %rsi, %r14
movq %rdi, %rbx
cmpq $0x0, 0x81b4(%rip)      # 0xf230
jne 0x709e
movslq 0x3ac(%r14), %rdi
shlq $0x2, %rdi
callq 0x20d0
movq %rax, 0x819b(%rip)      # 0xf230
testq %rax, %rax
je 0x73ff
movl 0x3a8(%r14), %edx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x7acd
movl $0x1, %ebp
testl %eax, %eax
jle 0x7410
movl %eax, %r15d
cmpb $0x0, (%rbx,%r15)
je 0x7424
movq 0x815c(%rip), %rdi      # 0xf230
movsd 0x10(%rsp), %xmm0
movss 0x18(%rsp), %xmm1
movaps %xmm0, 0x40(%rsp)
movss %xmm1, 0xc(%rsp)
movq %r14, %rsi
callq 0x263a
testl %eax, %eax
js 0x71df
movl 0x3ac(%r14), %eax
cmpl $0x5, %eax
jl 0x720c
addl $-0x4, %eax
movq 0x811b(%rip), %rcx      # 0xf230
xorps %xmm0, %xmm0
xorl %edx, %edx
movss 0x2612(%rip), %xmm2     # 0x9734
movss 0xeea(%rip), %xmm4      # 0x8014
movss 0x2606(%rip), %xmm5     # 0x9738
movss 0x2602(%rip), %xmm6     # 0x973c
xorps %xmm3, %xmm3
xorps %xmm1, %xmm1
movaps %xmm0, %xmm7
movss (%rcx,%rdx,4), %xmm8
movss 0x10(%rcx,%rdx,4), %xmm0
subss %xmm8, %xmm0
mulss %xmm2, %xmm0
ucomiss %xmm8, %xmm4
jbe 0x718f
movss 0x4(%rcx,%rdx,4), %xmm9
ucomiss %xmm4, %xmm9
jb 0x718f
xorps %xmm10, %xmm10
cvtsi2ss %edx, %xmm10
movaps %xmm4, %xmm3
subss %xmm8, %xmm3
subss %xmm8, %xmm9
divss %xmm9, %xmm3
addss %xmm10, %xmm3
mulss %xmm5, %xmm3
ucomiss %xmm8, %xmm6
jbe 0x71c6
movss 0x4(%rcx,%rdx,4), %xmm9
ucomiss %xmm6, %xmm9
jb 0x71c6
xorps %xmm10, %xmm10
cvtsi2ss %edx, %xmm10
movaps %xmm6, %xmm1
subss %xmm8, %xmm1
subss %xmm8, %xmm9
divss %xmm9, %xmm1
addss %xmm10, %xmm1
mulss %xmm5, %xmm1
maxss %xmm7, %xmm0
incq %rdx
cmpq %rdx, %rax
jne 0x7140
xorps %xmm2, %xmm2
cvtss2sd %xmm0, %xmm2
jmp 0x7215
movaps 0x40(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
cvtss2sd %xmm1, %xmm1
cvtss2sd 0xc(%rsp), %xmm2
leaq 0x25af(%rip), %rdi      # 0x97af
movb $0x3, %al
callq 0x2030
jmp 0x7410
xorps %xmm2, %xmm2
xorps %xmm1, %xmm1
xorps %xmm3, %xmm3
addq %r15, %rbx
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
subss %xmm3, %xmm1
cvtss2sd %xmm1, %xmm1
leaq 0x25c7(%rip), %rdi      # 0x97f5
movb $0x3, %al
callq 0x2030
movl 0x3ac(%r14), %r15d
xorl %ebp, %ebp
incl %ebp
movl %r15d, %eax
cltd
idivl %ebp
cmpl $0x4000, %eax           # imm = 0x4000
jg 0x723e
cmpl $0x2, %ebp
jb 0x7269
leaq 0x25c1(%rip), %rdi      # 0x981a
movl %ebp, %esi
xorl %eax, %eax
callq 0x2030
movl 0x3ac(%r14), %r15d
leaq 0x50(%rsp), %rdi
movl $0x10000, %edx          # imm = 0x10000
xorl %esi, %esi
callq 0x2050
testl %r15d, %r15d
jle 0x72c1
xorps %xmm0, %xmm0
cvtsi2ss %ebp, %xmm0
movq 0x7fa3(%rip), %rsi      # 0xf230
movl %r15d, %edi
xorl %ecx, %ecx
movss 0x24a6(%rip), %xmm1     # 0x9740
movss (%rsi,%rcx,4), %xmm2
mulss %xmm1, %xmm2
divss %xmm0, %xmm2
movl %ecx, %eax
xorl %edx, %edx
divl %ebp
addss 0x50(%rsp,%rax,4), %xmm2
movss %xmm2, 0x50(%rsp,%rax,4)
incq %rcx
cmpq %rcx, %rdi
jne 0x729a
movl %r15d, %eax
cltd
idivl %ebp
movl %eax, %r12d
movl $0x18, 0x20(%rsp)
movaps 0x2477(%rip), %xmm0     # 0x9750
movups %xmm0, 0x30(%rsp)
leaq 0x25b2(%rip), %rsi      # 0x9897
movq %rbx, %rdi
callq 0x20e0
testq %rax, %rax
je 0x742b
movq %rax, %r15
movl %r12d, 0x2c(%rsp)
leaq 0x25bd(%rip), %rsi      # 0x98c2
movq %rbx, %rdi
callq 0x21a0
movq %rax, %r13
testq %rax, %rax
jne 0x7323
movq %rbx, %rdi
callq 0x2160
movq %rax, %r13
addq %rbx, %r13
movq %rbx, %rdi
movl $0x2f, %esi
callq 0x2110
leaq 0x1(%rax), %rsi
testq %rax, %rax
cmoveq %rbx, %rsi
movq %r13, %rdx
subq %rsi, %rdx
cmpq $0x7, %rdx
jg 0x7368
leaq 0x24(%rsp), %r13
movabsq $0x20202020202020, %rax # imm = 0x20202020202020
movq %rax, (%r13)
movq %r13, %rdi
callq 0x22a0
movl 0x8(%r13), %eax
jmp 0x7374
movq -0x8(%r13), %rax
movq %rax, 0x24(%rsp)
movl %r12d, %eax
shll $0x2, %eax
leaq 0x1c(%rsp), %r13
movl %eax, (%r13)
leaq 0x20(%rsp), %rdi
movl $0x20, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x2240
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r15, %rcx
callq 0x2240
movslq %r12d, %rdx
leaq 0x50(%rsp), %rdi
movl $0x4, %esi
movq %r15, %rcx
callq 0x2240
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r15, %rcx
callq 0x2240
movq %r15, %rdi
callq 0x2220
movl 0x3ac(%r14), %eax
cltd
idivl %ebp
leaq 0x2455(%rip), %rdi      # 0x9844
xorl %ebp, %ebp
movl %eax, %esi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2030
jmp 0x7410
leaq 0x26c0(%rip), %rdi      # 0x9ac6
callq 0x2090
movl $0x1, %ebp
movl %ebp, %eax
addq $0x10058, %rsp          # imm = 0x10058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2378
jmp 0x7410
movq 0x4bbe(%rip), %rax      # 0xbff0
movq (%rax), %rdi
leaq 0x245d(%rip), %rsi      # 0x9899
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2260
jmp 0x73de