// File: /workspace/github_c_makefile_star2_O2/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, %r12
movq %rdi, %rbx
cmpq $0x0, 0x8588(%rip)      # 0xf230
pushq $0x1
popq %rbp
jne 0x6cce
movslq 0x3ac(%r12), %rdi
shlq $0x2, %rdi
callq 0x20d0
movq %rax, 0x856b(%rip)      # 0xf230
testq %rax, %rax
je 0x7025
movl 0x3a8(%r12), %edx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x76b3
testl %eax, %eax
jle 0x7031
movl %eax, %r14d
cmpb $0x0, (%rbx,%r14)
je 0x7045
movq 0x8530(%rip), %rdi      # 0xf230
movsd 0x10(%rsp), %xmm0
movss 0x18(%rsp), %xmm1
movaps %xmm0, 0x40(%rsp)
movss %xmm1, (%rsp)
movq %r12, %rsi
callq 0x25f5
testl %eax, %eax
js 0x6f60
addq %r14, %rbx
movl 0x3ac(%r12), %edx
movq 0x84f8(%rip), %rax      # 0xf230
cmpl $0x5, %edx
pushq $0x4
popq %rcx
cmovgel %edx, %ecx
addl $-0x4, %ecx
xorps %xmm1, %xmm1
xorl %edx, %edx
movss 0x29e3(%rip), %xmm0     # 0x9734
movss 0x12bb(%rip), %xmm2     # 0x8014
movss 0x29d7(%rip), %xmm3     # 0x9738
movss 0x29d3(%rip), %xmm4     # 0x973c
xorps %xmm5, %xmm5
xorps %xmm6, %xmm6
cmpq %rdx, %rcx
je 0x6e02
movss (%rax,%rdx,4), %xmm8
movss 0x10(%rax,%rdx,4), %xmm7
subss %xmm8, %xmm7
mulss %xmm0, %xmm7
ucomiss %xmm8, %xmm2
jbe 0x6dc0
movss 0x4(%rax,%rdx,4), %xmm9
ucomiss %xmm2, %xmm9
jb 0x6dc0
cvtsi2ss %edx, %xmm10
movaps %xmm2, %xmm5
subss %xmm8, %xmm5
subss %xmm8, %xmm9
divss %xmm9, %xmm5
addss %xmm10, %xmm5
mulss %xmm3, %xmm5
ucomiss %xmm8, %xmm4
jbe 0x6df3
movss 0x4(%rax,%rdx,4), %xmm9
ucomiss %xmm4, %xmm9
jb 0x6df3
cvtsi2ss %edx, %xmm10
movaps %xmm4, %xmm1
subss %xmm8, %xmm1
subss %xmm8, %xmm9
divss %xmm9, %xmm1
addss %xmm10, %xmm1
mulss %xmm3, %xmm1
maxss %xmm6, %xmm7
incq %rdx
movaps %xmm7, %xmm6
jmp 0x6d6f
cvtss2sd %xmm1, %xmm0
subss %xmm5, %xmm1
cvtss2sd %xmm1, %xmm1
cvtss2sd %xmm6, %xmm2
leaq 0x29dc(%rip), %rdi      # 0x97f5
movb $0x3, %al
callq 0x2030
movl 0x3ac(%r12), %ecx
xorl %ebp, %ebp
incl %ebp
movl %ecx, %eax
cltd
idivl %ebp
cmpl $0x4000, %eax           # imm = 0x4000
jg 0x6e2a
cmpl $0x2, %ebp
jb 0x6e4d
leaq 0x29d6(%rip), %rdi      # 0x981a
movl %ebp, %esi
xorl %eax, %eax
callq 0x2030
xorl %eax, %eax
cmpq $0x4000, %rax           # imm = 0x4000
je 0x6e61
andl $0x0, 0x50(%rsp,%rax,4)
incq %rax
jmp 0x6e4f
cvtsi2ss %ebp, %xmm0
movl 0x3ac(%r12), %ecx
movq 0x83bc(%rip), %rdi      # 0xf230
xorl %esi, %esi
testl %ecx, %ecx
movl $0x0, %r8d
cmovgl %ecx, %r8d
movss 0x28b6(%rip), %xmm1     # 0x9740
cmpq %rsi, %r8
je 0x6eb3
movss (%rdi,%rsi,4), %xmm2
mulss %xmm1, %xmm2
divss %xmm0, %xmm2
movl %esi, %eax
xorl %edx, %edx
divl %ebp
addss 0x50(%rsp,%rax,4), %xmm2
movss %xmm2, 0x50(%rsp,%rax,4)
incq %rsi
jmp 0x6e8a
movl %ecx, %eax
cltd
idivl %ebp
movl %eax, %r14d
movl $0x18, 0x20(%rsp)
movaps 0x2886(%rip), %xmm0     # 0x9750
movups %xmm0, 0x30(%rsp)
leaq 0x29c1(%rip), %rsi      # 0x9897
movq %rbx, %rdi
callq 0x20e0
testq %rax, %rax
je 0x7063
movq %rax, %r15
movl %r14d, 0x2c(%rsp)
leaq 0x29cc(%rip), %rsi      # 0x98c2
movq %rbx, %rdi
callq 0x21a0
movq %rax, %r13
testq %rax, %rax
jne 0x6f14
movq %rbx, %rdi
callq 0x2160
movq %rax, %r13
addq %rbx, %r13
pushq $0x2f
popq %rsi
movq %rbx, %rdi
callq 0x2110
leaq 0x1(%rax), %rsi
testq %rax, %rax
cmoveq %rbx, %rsi
movq %r13, %rdx
subq %rsi, %rdx
cmpq $0x7, %rdx
movq %r12, 0x8(%rsp)
movq %rbx, (%rsp)
jg 0x6f89
leaq 0x24(%rsp), %r13
movabsq $0x20202020202020, %rax # imm = 0x20202020202020
movq %rax, (%r13)
movq %r13, %rdi
callq 0x22a0
movl 0x8(%r13), %eax
jmp 0x6f95
movaps 0x40(%rsp), %xmm1
cvtss2sd %xmm1, %xmm0
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
cvtss2sd %xmm1, %xmm1
cvtss2sd (%rsp), %xmm2
leaq 0x2832(%rip), %rdi      # 0x97af
movb $0x3, %al
callq 0x2030
jmp 0x7031
movq -0x8(%r13), %rax
movq %rax, 0x24(%rsp)
movl %r14d, %eax
movl %r14d, %r13d
shll $0x2, %eax
leaq 0x1c(%rsp), %rbx
movl %eax, (%rbx)
leaq 0x20(%rsp), %rdi
pushq $0x20
popq %rsi
pushq $0x1
popq %r12
movq %r12, %rdx
movq %r15, %rcx
callq 0x2240
pushq $0x4
popq %r14
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x2240
movslq %r13d, %rdx
leaq 0x50(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rcx
callq 0x2240
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x2240
movq %r15, %rdi
callq 0x2220
movq 0x8(%rsp), %r12
movq (%rsp), %rbx
movl 0x3ac(%r12), %eax
cltd
idivl %ebp
leaq 0x282f(%rip), %rdi      # 0x9844
xorl %ebp, %ebp
movl %eax, %esi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2030
jmp 0x7031
leaq 0x2a9a(%rip), %rdi      # 0x9ac6
callq 0x2090
movl %ebp, %eax
addq $0x10058, %rsp          # imm = 0x10058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4fa4(%rip), %rax      # 0xbff0
movq (%rax), %rcx
leaq 0x2742(%rip), %rdi      # 0x9798
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x2240
jmp 0x7031
movq 0x4f86(%rip), %rax      # 0xbff0
movq (%rax), %rdi
leaq 0x2825(%rip), %rsi      # 0x9899
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2260
jmp 0x7003