// File: /workspace/github_c_makefile_star2_O0/radforddc[P]icpc_siggen/calc_signal.c
// Function: get_signal
int get_signal(point pt, float *signal_out, MJD_Siggen_Setup *setup) {
  static float *signal, *sum, *tmp;
  static int tsteps = 0;
  float w, x, y;
  char  tmpstr[MAX_LINE];
  int   j, k, l, dt, err, comp_f;

  /* first time -- allocate signal and sum arrays */
  if (tsteps != setup->time_steps_calc) {
    tsteps = setup->time_steps_calc;
    if ((signal = (float *) malloc(tsteps*sizeof(*signal))) == NULL ||
	(tmp    = (float *) malloc(tsteps*sizeof(*tmp))) == NULL ||
	(sum    = (float *) malloc(tsteps*sizeof(*sum))) == NULL) {
      error("malloc failed in get_signal\n");
      return -1;
    }
  }

  for (j = 0; j < tsteps; j++) signal[j] = 0.0;

  if (outside_detector(pt, setup)) {
    TELL_CHATTY("Point %s is outside detector!\n", pt_to_str(tmpstr, MAX_LINE, pt));
    return -1;
  }
  TELL_CHATTY("Calculating signal for %s...\n", pt_to_str(tmpstr, MAX_LINE, pt));

  memset(setup->dpath_e, 0, tsteps*sizeof(point));
  memset(setup->dpath_h, 0, tsteps*sizeof(point));

  err = make_signal(pt, signal, ELECTRON_CHARGE, setup);
  err = make_signal(pt, signal, HOLE_CHARGE, setup);
  /* make_signal returns 1 for success; require hole signal but not electron */

  /* change from current signal to charge signal, i.e.
     each time step contains the summed signals of all previous time steps */
  for (j = 1; j < tsteps; j++) signal[j] += signal[j-1];

  if (signal_out != NULL) {

    if (setup->charge_cloud_size > 0.001 || setup->use_diffusion) {
      /* convolute with a Gaussian to correct for charge cloud size
	 and initial velocity
	 charge_cloud_size = initial FWHM of charge cloud, in mm,
	 NOTE this uses initial velocity of holes only;
	 this may not be quite right if electron signal is strong */
      /* difference in time between center and edge of charge cloud */
      dt = (int) (1.5f + setup->charge_cloud_size /
		  (setup->step_time_calc * setup->initial_vel));
      if (setup->initial_vel < 0.00001f) dt = 0;
      TELL_CHATTY("Initial vel, size, dt = %f mm/ns, %f mm, %d steps\n",
		  setup->initial_vel, setup->charge_cloud_size, dt);
      if (setup->use_diffusion) {
	dt = (int) (1.5f + setup->final_charge_size /
		    (setup->step_time_calc * setup->final_vel));
	TELL_CHATTY("  Final vel, size, dt = %f mm/ns, %f mm, %d steps\n",
		    setup->final_vel, setup->final_charge_size, dt);
      }
      if (dt > 1) {
	/* Gaussian */
	w = ((float) dt) / 2.355;
	l = dt/10;     // use l to speed up convolution of waveform with gaussian;
	if (l < 1) {   // instead of using every 1-ns step, use steps of FWHM/10
	  l = 1;
	} else if (setup->step_time_out > setup->preamp_tau) {
	  if (l > setup->step_time_out/setup->step_time_calc)
	    l = setup->step_time_out/setup->step_time_calc;
	} else {
	  if (l > setup->preamp_tau/setup->step_time_calc)
	    l = setup->preamp_tau/setup->step_time_calc;
	}
	// TELL_CHATTY(">> l: %d\n", l);
	for (j = 0; j < tsteps; j++) {
	  sum[j] = 1.0;
	  tmp[j] = signal[j];
	}
	for (k = l; k < 2*dt; k+=l) {
	  x = ((float) k)/w;
	  y = exp(-x*x/2.0);
	  for (j = 0; j < tsteps - k; j++){
	    sum[j] += y;
	    tmp[j] += signal[j+k] * y;
	    sum[j+k] += y;
	    tmp[j+k] += signal[j] * y;
	  }
	}
        for (j = 0; j < tsteps; j++){
          signal[j] = tmp[j]/sum[j];
        }
      }
    }

    /* now, compress the signal and place it in the signal_out array;
       truncate the signal if time_steps_calc % ntsteps_out != 0 */
    comp_f = setup->time_steps_calc/setup->ntsteps_out;
    for (j = 0; j < setup->ntsteps_out; j++) signal_out[j] = 0;
    for (j = 0; j < setup->ntsteps_out*comp_f; j++)
      signal_out[j/comp_f] += signal[j]/comp_f;

    /* do RC integration for preamp risetime */
    if (setup->preamp_tau/setup->step_time_out >= 0.1f)
      rc_integrate(signal_out, signal_out,
		   setup->preamp_tau/setup->step_time_out, setup->ntsteps_out);
  }

  /* make_signal returns 1 for success; require hole signal but not electron */
  if (err) return -1;
  return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2c0, %rsp            # imm = 0x2C0
movlpd %xmm0, -0x20(%rbp)
movss %xmm1, -0x18(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x10(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x8(%rbp)
movq %rdi, -0x28(%rbp)
movq %rsi, -0x30(%rbp)
movl 0x11b4f(%rip), %eax     # 0x141f0
movq -0x30(%rbp), %rcx
cmpl 0x390(%rcx), %eax
je 0x2734
movq -0x30(%rbp), %rax
movl 0x390(%rax), %eax
movl %eax, 0x11b2f(%rip)     # 0x141f0
movslq 0x11b28(%rip), %rdi     # 0x141f0
shlq $0x2, %rdi
callq 0x20b0
movq %rax, 0x11b00(%rip)     # 0x141d8
cmpq $0x0, %rax
je 0x2718
movslq 0x11b0b(%rip), %rdi     # 0x141f0
shlq $0x2, %rdi
callq 0x20b0
movq %rax, 0x11af3(%rip)     # 0x141e8
cmpq $0x0, %rax
je 0x2718
movslq 0x11aee(%rip), %rdi     # 0x141f0
shlq $0x2, %rdi
callq 0x20b0
movq %rax, 0x11ace(%rip)     # 0x141e0
cmpq $0x0, %rax
jne 0x2732
leaq 0xaa7a(%rip), %rdi      # 0xd199
movb $0x0, %al
callq 0x25d0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2f76
jmp 0x2734
movl $0x0, -0x244(%rbp)
movl -0x244(%rbp), %eax
cmpl 0x11aa6(%rip), %eax     # 0x141f0
jge 0x2773
movq 0x11a85(%rip), %rax     # 0x141d8
movslq -0x244(%rbp), %rcx
xorps %xmm0, %xmm0
movss %xmm0, (%rax,%rcx,4)
movl -0x244(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x244(%rbp)
jmp 0x273e
movq -0x30(%rbp), %rdi
movl -0x8(%rbp), %eax
movl %eax, -0x260(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x268(%rbp)
movsd -0x268(%rbp), %xmm0
movss -0x260(%rbp), %xmm1
callq 0x4820
cmpl $0x0, %eax
je 0x2800
movq -0x30(%rbp), %rax
cmpl $0x2, (%rax)
jl 0x27f4
leaq -0x240(%rbp), %rdi
movl -0x8(%rbp), %eax
movl %eax, -0x270(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x278(%rbp)
movsd -0x278(%rbp), %xmm0
movss -0x270(%rbp), %xmm1
movl $0x200, %esi            # imm = 0x200
callq 0x9160
movq %rax, %rsi
leaq 0xa9c9(%rip), %rdi      # 0xd1b6
movb $0x0, %al
callq 0x2530
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2f76
movq -0x30(%rbp), %rax
cmpl $0x2, (%rax)
jl 0x284f
leaq -0x240(%rbp), %rdi
movl -0x8(%rbp), %eax
movl %eax, -0x280(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x288(%rbp)
movsd -0x288(%rbp), %xmm0
movss -0x280(%rbp), %xmm1
movl $0x200, %esi            # imm = 0x200
callq 0x9160
movq %rax, %rsi
leaq 0xa98d(%rip), %rdi      # 0xd1d5
movb $0x0, %al
callq 0x2530
movq -0x30(%rbp), %rax
movq 0x1580(%rax), %rdi
movslq 0x1198f(%rip), %rax     # 0x141f0
shlq $0x2, %rax
leaq (%rax,%rax,2), %rdx
xorl %esi, %esi
movl %esi, -0x2ac(%rbp)
callq 0x2050
movl -0x2ac(%rbp), %esi
movq -0x30(%rbp), %rax
movq 0x1588(%rax), %rdi
movslq 0x11962(%rip), %rax     # 0x141f0
shlq $0x2, %rax
leaq (%rax,%rax,2), %rdx
callq 0x2050
movq 0x11936(%rip), %rdi     # 0x141d8
movq -0x30(%rbp), %rsi
movl -0x8(%rbp), %eax
movl %eax, -0x290(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x298(%rbp)
movsd -0x298(%rbp), %xmm0
movss -0x290(%rbp), %xmm1
movss 0xa736(%rip), %xmm2     # 0xd008
callq 0x2f90
movl %eax, -0x254(%rbp)
movq 0x118f4(%rip), %rdi     # 0x141d8
movq -0x30(%rbp), %rsi
movl -0x8(%rbp), %eax
movl %eax, -0x2a0(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x2a8(%rbp)
movsd -0x2a8(%rbp), %xmm0
movss -0x2a0(%rbp), %xmm1
movss 0xa6f0(%rip), %xmm2     # 0xd004
callq 0x2f90
movl %eax, -0x254(%rbp)
movl $0x1, -0x244(%rbp)
movl -0x244(%rbp), %eax
cmpl 0x118bb(%rip), %eax     # 0x141f0
jge 0x2978
movq 0x1189a(%rip), %rax     # 0x141d8
movl -0x244(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movq 0x11882(%rip), %rax     # 0x141d8
movslq -0x244(%rbp), %rcx
addss (%rax,%rcx,4), %xmm0
movss %xmm0, (%rax,%rcx,4)
movl -0x244(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x244(%rbp)
jmp 0x2929
cmpq $0x0, -0x28(%rbp)
je 0x2f5d
movq -0x30(%rbp), %rax
movss 0x39c(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0xa67d(%rip), %xmm1     # 0xd018
ucomisd %xmm1, %xmm0
ja 0x29b2
movq -0x30(%rbp), %rax
cmpl $0x0, 0x3a0(%rax)
je 0x2e29
movq -0x30(%rbp), %rax
movss 0x39c(%rax), %xmm1
movq -0x30(%rbp), %rax
movss 0x394(%rax), %xmm0
movq -0x30(%rbp), %rax
mulss 0x1594(%rax), %xmm0
divss %xmm0, %xmm1
movss 0xa62e(%rip), %xmm0     # 0xd010
addss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0x250(%rbp)
movq -0x30(%rbp), %rax
movss 0xa610(%rip), %xmm0     # 0xd00c
ucomiss 0x1594(%rax), %xmm0
jbe 0x2a0f
movl $0x0, -0x250(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x2, (%rax)
jl 0x2a4c
movq -0x30(%rbp), %rax
movss 0x1594(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x30(%rbp), %rax
movss 0x39c(%rax), %xmm1
cvtss2sd %xmm1, %xmm1
movl -0x250(%rbp), %esi
leaq 0xa7ae(%rip), %rdi      # 0xd1f3
movb $0x2, %al
callq 0x2530
movq -0x30(%rbp), %rax
cmpl $0x0, 0x3a0(%rax)
je 0x2ad6
movq -0x30(%rbp), %rax
movsd 0x15a8(%rax), %xmm1
movq -0x30(%rbp), %rax
movss 0x394(%rax), %xmm0
movq -0x30(%rbp), %rax
mulss 0x1598(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
divsd %xmm0, %xmm1
movsd 0xa593(%rip), %xmm0     # 0xd020
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x250(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x2, (%rax)
jl 0x2ad4
movq -0x30(%rbp), %rax
movss 0x1598(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x30(%rbp), %rax
movsd 0x15a8(%rax), %xmm1
movl -0x250(%rbp), %esi
leaq 0xa759(%rip), %rdi      # 0xd226
movb $0x2, %al
callq 0x2530
jmp 0x2ad6
cmpl $0x1, -0x250(%rbp)
jle 0x2e27
cvtsi2ssl -0x250(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0xa531(%rip), %xmm1     # 0xd028
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x34(%rbp)
movl -0x250(%rbp), %eax
movl $0xa, %ecx
cltd
idivl %ecx
movl %eax, -0x24c(%rbp)
cmpl $0x1, -0x24c(%rbp)
jge 0x2b30
movl $0x1, -0x24c(%rbp)
jmp 0x2bdd
movq -0x30(%rbp), %rax
movss 0x398(%rax), %xmm0
movq -0x30(%rbp), %rax
ucomiss 0x38c(%rax), %xmm0
jbe 0x2b92
cvtsi2ssl -0x24c(%rbp), %xmm0
movq -0x30(%rbp), %rax
movss 0x398(%rax), %xmm1
movq -0x30(%rbp), %rax
divss 0x394(%rax), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x2b90
movq -0x30(%rbp), %rax
movss 0x398(%rax), %xmm0
movq -0x30(%rbp), %rax
divss 0x394(%rax), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0x24c(%rbp)
jmp 0x2bdb
cvtsi2ssl -0x24c(%rbp), %xmm0
movq -0x30(%rbp), %rax
movss 0x38c(%rax), %xmm1
movq -0x30(%rbp), %rax
divss 0x394(%rax), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x2bd9
movq -0x30(%rbp), %rax
movss 0x38c(%rax), %xmm0
movq -0x30(%rbp), %rax
divss 0x394(%rax), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0x24c(%rbp)
jmp 0x2bdb
jmp 0x2bdd
movl $0x0, -0x244(%rbp)
movl -0x244(%rbp), %eax
cmpl 0x115fd(%rip), %eax     # 0x141f0
jge 0x2c47
movq 0x115e4(%rip), %rax     # 0x141e0
movslq -0x244(%rbp), %rcx
movss 0xa3f9(%rip), %xmm0     # 0xd004
movss %xmm0, (%rax,%rcx,4)
movq 0x115c1(%rip), %rax     # 0x141d8
movslq -0x244(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movq 0x115be(%rip), %rax     # 0x141e8
movslq -0x244(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x244(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x244(%rbp)
jmp 0x2be7
movl -0x24c(%rbp), %eax
movl %eax, -0x248(%rbp)
movl -0x248(%rbp), %eax
movl -0x250(%rbp), %ecx
shll %ecx
cmpl %ecx, %eax
jge 0x2dc3
cvtsi2ssl -0x248(%rbp), %xmm0
divss -0x34(%rbp), %xmm0
movss %xmm0, -0x38(%rbp)
movss -0x38(%rbp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
mulss -0x38(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0xa392(%rip), %xmm1     # 0xd030
divsd %xmm1, %xmm0
callq 0x2070
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x3c(%rbp)
movl $0x0, -0x244(%rbp)
movl -0x244(%rbp), %eax
movl 0x1152a(%rip), %ecx     # 0x141f0
subl -0x248(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2daa
movss -0x3c(%rbp), %xmm0
movq 0x11500(%rip), %rax     # 0x141e0
movslq -0x244(%rbp), %rcx
addss (%rax,%rcx,4), %xmm0
movss %xmm0, (%rax,%rcx,4)
movq 0x114e0(%rip), %rax     # 0x141d8
movl -0x244(%rbp), %ecx
addl -0x248(%rbp), %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movss -0x3c(%rbp), %xmm2
movq 0x114d0(%rip), %rax     # 0x141e8
movslq -0x244(%rbp), %rcx
movss (%rax,%rcx,4), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, (%rax,%rcx,4)
movss -0x3c(%rbp), %xmm0
movq 0x114a3(%rip), %rax     # 0x141e0
movl -0x244(%rbp), %ecx
addl -0x248(%rbp), %ecx
movslq %ecx, %rcx
addss (%rax,%rcx,4), %xmm0
movss %xmm0, (%rax,%rcx,4)
movq 0x1147b(%rip), %rax     # 0x141d8
movslq -0x244(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movss -0x3c(%rbp), %xmm2
movq 0x11473(%rip), %rax     # 0x141e8
movl -0x244(%rbp), %ecx
addl -0x248(%rbp), %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, (%rax,%rcx,4)
movl -0x244(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x244(%rbp)
jmp 0x2cba
jmp 0x2dac
movl -0x24c(%rbp), %eax
addl -0x248(%rbp), %eax
movl %eax, -0x248(%rbp)
jmp 0x2c53
movl $0x0, -0x244(%rbp)
movl -0x244(%rbp), %eax
cmpl 0x11417(%rip), %eax     # 0x141f0
jge 0x2e25
movq 0x11406(%rip), %rax     # 0x141e8
movslq -0x244(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movq 0x113eb(%rip), %rax     # 0x141e0
movslq -0x244(%rbp), %rcx
divss (%rax,%rcx,4), %xmm0
movq 0x113d0(%rip), %rax     # 0x141d8
movslq -0x244(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x244(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x244(%rbp)
jmp 0x2dcd
jmp 0x2e27
jmp 0x2e29
movq -0x30(%rbp), %rax
movl 0x390(%rax), %eax
movq -0x30(%rbp), %rcx
cltd
idivl 0x3ac(%rcx)
movl %eax, -0x258(%rbp)
movl $0x0, -0x244(%rbp)
movl -0x244(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0x3ac(%rcx), %eax
jge 0x2e84
movq -0x28(%rbp), %rax
movslq -0x244(%rbp), %rcx
xorps %xmm0, %xmm0
movss %xmm0, (%rax,%rcx,4)
movl -0x244(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x244(%rbp)
jmp 0x2e4e
movl $0x0, -0x244(%rbp)
movl -0x244(%rbp), %eax
movq -0x30(%rbp), %rcx
movl 0x3ac(%rcx), %ecx
imull -0x258(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2f07
movq 0x11328(%rip), %rax     # 0x141d8
movslq -0x244(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtsi2ssl -0x258(%rbp), %xmm1
divss %xmm1, %xmm0
movq -0x28(%rbp), %rax
movq %rax, -0x2b8(%rbp)
movl -0x244(%rbp), %eax
cltd
idivl -0x258(%rbp)
movl %eax, %ecx
movq -0x2b8(%rbp), %rax
movslq %ecx, %rcx
addss (%rax,%rcx,4), %xmm0
movss %xmm0, (%rax,%rcx,4)
movl -0x244(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x244(%rbp)
jmp 0x2e8e
movq -0x30(%rbp), %rax
movss 0x38c(%rax), %xmm0
movq -0x30(%rbp), %rax
divss 0x398(%rax), %xmm0
movss 0xa0ed(%rip), %xmm1     # 0xd014
ucomiss %xmm1, %xmm0
jb 0x2f5b
movq -0x28(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rax
movss 0x38c(%rax), %xmm0
movq -0x30(%rbp), %rax
divss 0x398(%rax), %xmm0
movq -0x30(%rbp), %rax
movl 0x3ac(%rax), %edx
callq 0x41f0
jmp 0x2f5d
cmpl $0x0, -0x254(%rbp)
je 0x2f6f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2f76
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x2c0, %rsp            # imm = 0x2C0
popq %rbp
retq
nopw %cs:(%rax,%rax)