// File: /workspace/github_c_makefile_star2_O0/radforddc[P]icpc_siggen/fields.c
// Function: drift_velocity
int drift_velocity(point pt, float q, vector *velo, MJD_Siggen_Setup *setup){
  point  cart_en;
  cyl_pt e, en, cyl;
  cyl_int_pt ipt;
  int   i, sign;
  float abse, absv, f, a, b, c;
  float bp, cp, en4, en6;
  struct velocity_lookup *v_lookup1, *v_lookup2;

  /*  DCR: replaced this with faster code below, saves calls to atan and tan
  cyl = cart_to_cyl(pt);
  if (nearest_field_grid_index(cyl, &ipt, setup) < 0) return -1;
  e = efield(cyl, ipt, setup);
  abse = vector_norm_cyl(e, &en);
  en.phi = cyl.phi;
  cart_en = cyl_to_cart(en);
  */
  cyl.r = sqrt(pt.x*pt.x + pt.y*pt.y);
  cyl.z = pt.z;
  cyl.phi = 0;
  if (nearest_field_grid_index(cyl, &ipt, setup) < 0) return -1;
  e = efield(cyl, ipt, setup);
  abse = vector_norm_cyl(e, &en);
  if (cyl.r > 0.001) {
    cart_en.x = en.r * pt.x/cyl.r;
    cart_en.y = en.r * pt.y/cyl.r;
  } else {
    cart_en.x = cart_en.y = 0;
  }
  cart_en.z = en.z;

  /* find location in table to interpolate from */
  for (i = 0; i < setup->v_lookup_len - 2 && abse > setup->v_lookup[i+1].e; i++);
  v_lookup1 = setup->v_lookup + i;
  v_lookup2 = setup->v_lookup + i+1;
  f = (abse - v_lookup1->e)/(v_lookup2->e - v_lookup1->e);
  if (q > 0){
    a = (v_lookup2->ha - v_lookup1->ha)*f+v_lookup1->ha;
    b = (v_lookup2->hb- v_lookup1->hb)*f+v_lookup1->hb;
    c = (v_lookup2->hc - v_lookup1->hc)*f+v_lookup1->hc;
    bp = (v_lookup2->hbp- v_lookup1->hbp)*f+v_lookup1->hbp;
    cp = (v_lookup2->hcp - v_lookup1->hcp)*f+v_lookup1->hcp;
    setup->dv_dE = (v_lookup2->h100 - v_lookup1->h100)/(v_lookup2->e - v_lookup1->e);
  }else{
    a = (v_lookup2->ea - v_lookup1->ea)*f+v_lookup1->ea;
    b = (v_lookup2->eb- v_lookup1->eb)*f+v_lookup1->eb;
    c = (v_lookup2->ec - v_lookup1->ec)*f+v_lookup1->ec;
    bp = (v_lookup2->ebp- v_lookup1->ebp)*f+v_lookup1->ebp;
    cp = (v_lookup2->ecp - v_lookup1->ecp)*f+v_lookup1->ecp;
    setup->dv_dE = (v_lookup2->e100 - v_lookup1->e100)/(v_lookup2->e - v_lookup1->e);
  }
  /* velocity can vary from the direction of the el. field
     due to effect of crystal axes */
#define POW4(x) ((x)*(x)*(x)*(x))
#define POW6(x) ((x)*(x)*(x)*(x)*(x)*(x))
  en4 = POW4(cart_en.x) + POW4(cart_en.y) + POW4(cart_en.z);
  en6 = POW6(cart_en.x) + POW6(cart_en.y) + POW6(cart_en.z);
  absv = a + b*en4 + c*en6;
  sign = (q < 0 ? -1 : 1);
  setup->v_over_E = absv / abse;
  velo->x = sign*cart_en.x*(absv+bp*4*(cart_en.x*cart_en.x - en4)
			    + cp*6*(POW4(cart_en.x) - en6));
  velo->y = sign*cart_en.y*(absv+bp*4*(cart_en.y*cart_en.y - en4)
			    + cp*6*(POW4(cart_en.y) - en6));
  velo->z = sign*cart_en.z*(absv+bp*4*(cart_en.z*cart_en.z - en4)
			    + cp*6*(POW4(cart_en.z) - en6));
#undef POW4
#undef POW6
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movlpd %xmm0, -0x20(%rbp)
movss %xmm1, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x8(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x10(%rbp)
movss %xmm2, -0x24(%rbp)
movq %rdi, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movss -0x10(%rbp), %xmm0
movss -0xc(%rbp), %xmm1
mulss %xmm1, %xmm1
mulss %xmm0, %xmm0
addss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x2250
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x68(%rbp)
movss -0x8(%rbp), %xmm0
movss %xmm0, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movq -0x38(%rbp), %rsi
movl -0x60(%rbp), %eax
movl %eax, -0xc0(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xc8(%rbp)
movsd -0xc8(%rbp), %xmm0
movss -0xc0(%rbp), %xmm1
leaq -0x74(%rbp), %rdi
callq 0x7930
cmpl $0x0, %eax
jge 0x7d7b
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x858b
movq -0x38(%rbp), %rdx
movl -0x60(%rbp), %eax
movl %eax, -0xe0(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xe8(%rbp)
movsd -0xe8(%rbp), %xmm0
movss -0xe0(%rbp), %xmm1
movl -0x6c(%rbp), %eax
movl %eax, -0xf0(%rbp)
movq -0x74(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rdi
movl -0xf0(%rbp), %esi
callq 0x85a0
movss %xmm1, -0x100(%rbp)
movlpd %xmm0, -0x108(%rbp)
movl -0x100(%rbp), %eax
movl %eax, -0xd0(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl -0xd0(%rbp), %eax
movl %eax, -0x48(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x110(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x118(%rbp)
movsd -0x118(%rbp), %xmm0
movss -0x110(%rbp), %xmm1
leaq -0x5c(%rbp), %rdi
callq 0x4790
movss %xmm0, -0x80(%rbp)
movss -0x68(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x51ce(%rip), %xmm1     # 0xd018
ucomisd %xmm1, %xmm0
jbe 0x7e7a
movss -0x5c(%rbp), %xmm0
mulss -0x10(%rbp), %xmm0
divss -0x68(%rbp), %xmm0
movss %xmm0, -0x44(%rbp)
movss -0x5c(%rbp), %xmm0
mulss -0xc(%rbp), %xmm0
divss -0x68(%rbp), %xmm0
movss %xmm0, -0x40(%rbp)
jmp 0x7e8a
xorps %xmm0, %xmm0
movss %xmm0, -0x40(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x44(%rbp)
movss -0x54(%rbp), %xmm0
movss %xmm0, -0x3c(%rbp)
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %ecx
movq -0x38(%rbp), %rax
movl 0x3d0(%rax), %edx
subl $0x2, %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0x119(%rbp)
jge 0x7ee3
movss -0x80(%rbp), %xmm0
movq -0x38(%rbp), %rax
movq 0x3d8(%rax), %rax
movl -0x78(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x4c, %rcx, %rcx
addq %rcx, %rax
ucomiss (%rax), %xmm0
seta %al
movb %al, -0x119(%rbp)
movb -0x119(%rbp), %al
testb $0x1, %al
jne 0x7eef
jmp 0x7efc
jmp 0x7ef1
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x7e9b
movq -0x38(%rbp), %rax
movq 0x3d8(%rax), %rax
movslq -0x78(%rbp), %rcx
imulq $0x4c, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movq -0x38(%rbp), %rax
movq 0x3d8(%rax), %rax
movslq -0x78(%rbp), %rcx
imulq $0x4c, %rcx, %rcx
addq %rcx, %rax
addq $0x4c, %rax
movq %rax, -0xb8(%rbp)
movss -0x80(%rbp), %xmm0
movq -0xb0(%rbp), %rax
subss (%rax), %xmm0
movq -0xb8(%rbp), %rax
movss (%rax), %xmm1
movq -0xb0(%rbp), %rax
subss (%rax), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x88(%rbp)
movss -0x24(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x80ec
movq -0xb8(%rbp), %rax
movss 0x30(%rax), %xmm0
movq -0xb0(%rbp), %rax
subss 0x30(%rax), %xmm0
movss -0x88(%rbp), %xmm2
movq -0xb0(%rbp), %rax
movss 0x30(%rax), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x8c(%rbp)
movq -0xb8(%rbp), %rax
movss 0x34(%rax), %xmm0
movq -0xb0(%rbp), %rax
subss 0x34(%rax), %xmm0
movss -0x88(%rbp), %xmm2
movq -0xb0(%rbp), %rax
movss 0x34(%rax), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x90(%rbp)
movq -0xb8(%rbp), %rax
movss 0x38(%rax), %xmm0
movq -0xb0(%rbp), %rax
subss 0x38(%rax), %xmm0
movss -0x88(%rbp), %xmm2
movq -0xb0(%rbp), %rax
movss 0x38(%rax), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x94(%rbp)
movq -0xb8(%rbp), %rax
movss 0x3c(%rax), %xmm0
movq -0xb0(%rbp), %rax
subss 0x3c(%rax), %xmm0
movss -0x88(%rbp), %xmm2
movq -0xb0(%rbp), %rax
movss 0x3c(%rax), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x98(%rbp)
movq -0xb8(%rbp), %rax
movss 0x40(%rax), %xmm0
movq -0xb0(%rbp), %rax
subss 0x40(%rax), %xmm0
movss -0x88(%rbp), %xmm2
movq -0xb0(%rbp), %rax
movss 0x40(%rax), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x9c(%rbp)
movq -0xb8(%rbp), %rax
movss 0x10(%rax), %xmm0
movq -0xb0(%rbp), %rax
subss 0x10(%rax), %xmm0
movq -0xb8(%rbp), %rax
movss (%rax), %xmm1
movq -0xb0(%rbp), %rax
subss (%rax), %xmm1
divss %xmm1, %xmm0
movq -0x38(%rbp), %rax
movss %xmm0, 0x159c(%rax)
jmp 0x8256
movq -0xb8(%rbp), %rax
movss 0x1c(%rax), %xmm0
movq -0xb0(%rbp), %rax
subss 0x1c(%rax), %xmm0
movss -0x88(%rbp), %xmm2
movq -0xb0(%rbp), %rax
movss 0x1c(%rax), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x8c(%rbp)
movq -0xb8(%rbp), %rax
movss 0x20(%rax), %xmm0
movq -0xb0(%rbp), %rax
subss 0x20(%rax), %xmm0
movss -0x88(%rbp), %xmm2
movq -0xb0(%rbp), %rax
movss 0x20(%rax), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x90(%rbp)
movq -0xb8(%rbp), %rax
movss 0x24(%rax), %xmm0
movq -0xb0(%rbp), %rax
subss 0x24(%rax), %xmm0
movss -0x88(%rbp), %xmm2
movq -0xb0(%rbp), %rax
movss 0x24(%rax), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x94(%rbp)
movq -0xb8(%rbp), %rax
movss 0x28(%rax), %xmm0
movq -0xb0(%rbp), %rax
subss 0x28(%rax), %xmm0
movss -0x88(%rbp), %xmm2
movq -0xb0(%rbp), %rax
movss 0x28(%rax), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x98(%rbp)
movq -0xb8(%rbp), %rax
movss 0x2c(%rax), %xmm0
movq -0xb0(%rbp), %rax
subss 0x2c(%rax), %xmm0
movss -0x88(%rbp), %xmm2
movq -0xb0(%rbp), %rax
movss 0x2c(%rax), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x9c(%rbp)
movq -0xb8(%rbp), %rax
movss 0x4(%rax), %xmm0
movq -0xb0(%rbp), %rax
subss 0x4(%rax), %xmm0
movq -0xb8(%rbp), %rax
movss (%rax), %xmm1
movq -0xb0(%rbp), %rax
subss (%rax), %xmm1
divss %xmm1, %xmm0
movq -0x38(%rbp), %rax
movss %xmm0, 0x159c(%rax)
movss -0x44(%rbp), %xmm1
mulss -0x44(%rbp), %xmm1
mulss -0x44(%rbp), %xmm1
movss -0x44(%rbp), %xmm2
movss -0x40(%rbp), %xmm0
mulss -0x40(%rbp), %xmm0
mulss -0x40(%rbp), %xmm0
mulss -0x40(%rbp), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss -0x3c(%rbp), %xmm0
mulss -0x3c(%rbp), %xmm0
mulss -0x3c(%rbp), %xmm0
movss -0x3c(%rbp), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0xa0(%rbp)
movss -0x44(%rbp), %xmm1
mulss -0x44(%rbp), %xmm1
mulss -0x44(%rbp), %xmm1
mulss -0x44(%rbp), %xmm1
mulss -0x44(%rbp), %xmm1
movss -0x44(%rbp), %xmm2
movss -0x40(%rbp), %xmm0
mulss -0x40(%rbp), %xmm0
mulss -0x40(%rbp), %xmm0
mulss -0x40(%rbp), %xmm0
mulss -0x40(%rbp), %xmm0
mulss -0x40(%rbp), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss -0x3c(%rbp), %xmm0
mulss -0x3c(%rbp), %xmm0
mulss -0x3c(%rbp), %xmm0
mulss -0x3c(%rbp), %xmm0
mulss -0x3c(%rbp), %xmm0
movss -0x3c(%rbp), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0xa4(%rbp)
movss -0x8c(%rbp), %xmm0
movss -0x90(%rbp), %xmm1
movss -0xa0(%rbp), %xmm2
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss -0x94(%rbp), %xmm0
movss -0xa4(%rbp), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x84(%rbp)
movss -0x24(%rbp), %xmm1
xorps %xmm0, %xmm0
movl $0x1, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
ucomiss %xmm1, %xmm0
cmoval %ecx, %eax
movl %eax, -0x7c(%rbp)
movss -0x84(%rbp), %xmm0
divss -0x80(%rbp), %xmm0
movq -0x38(%rbp), %rax
movss %xmm0, 0x15a0(%rax)
cvtsi2ssl -0x7c(%rbp), %xmm0
mulss -0x44(%rbp), %xmm0
movss -0x84(%rbp), %xmm1
movss 0x5172(%rip), %xmm2     # 0xd51c
mulss -0x98(%rbp), %xmm2
movss -0x44(%rbp), %xmm3
movss -0x44(%rbp), %xmm5
movss -0xa0(%rbp), %xmm4
movd %xmm4, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm4
mulss %xmm5, %xmm3
addss %xmm4, %xmm3
mulss %xmm3, %xmm2
addss %xmm1, %xmm2
movss 0x5137(%rip), %xmm1     # 0xd520
mulss -0x9c(%rbp), %xmm1
movss -0x44(%rbp), %xmm3
mulss -0x44(%rbp), %xmm3
mulss -0x44(%rbp), %xmm3
movss -0x44(%rbp), %xmm5
movss -0xa4(%rbp), %xmm4
movd %xmm4, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm4
mulss %xmm5, %xmm3
addss %xmm4, %xmm3
mulss %xmm3, %xmm1
addss %xmm2, %xmm1
mulss %xmm1, %xmm0
movq -0x30(%rbp), %rax
movss %xmm0, (%rax)
cvtsi2ssl -0x7c(%rbp), %xmm0
mulss -0x40(%rbp), %xmm0
movss -0x84(%rbp), %xmm1
movss 0x50cc(%rip), %xmm2     # 0xd51c
mulss -0x98(%rbp), %xmm2
movss -0x40(%rbp), %xmm3
movss -0x40(%rbp), %xmm5
movss -0xa0(%rbp), %xmm4
movd %xmm4, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm4
mulss %xmm5, %xmm3
addss %xmm4, %xmm3
mulss %xmm3, %xmm2
addss %xmm1, %xmm2
movss 0x5091(%rip), %xmm1     # 0xd520
mulss -0x9c(%rbp), %xmm1
movss -0x40(%rbp), %xmm3
mulss -0x40(%rbp), %xmm3
mulss -0x40(%rbp), %xmm3
movss -0x40(%rbp), %xmm5
movss -0xa4(%rbp), %xmm4
movd %xmm4, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm4
mulss %xmm5, %xmm3
addss %xmm4, %xmm3
mulss %xmm3, %xmm1
addss %xmm2, %xmm1
mulss %xmm1, %xmm0
movq -0x30(%rbp), %rax
movss %xmm0, 0x4(%rax)
cvtsi2ssl -0x7c(%rbp), %xmm0
mulss -0x3c(%rbp), %xmm0
movss -0x84(%rbp), %xmm1
movss 0x5025(%rip), %xmm2     # 0xd51c
mulss -0x98(%rbp), %xmm2
movss -0x3c(%rbp), %xmm3
movss -0x3c(%rbp), %xmm5
movss -0xa0(%rbp), %xmm4
movd %xmm4, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm4
mulss %xmm5, %xmm3
addss %xmm4, %xmm3
mulss %xmm3, %xmm2
addss %xmm1, %xmm2
movss 0x4fea(%rip), %xmm1     # 0xd520
mulss -0x9c(%rbp), %xmm1
movss -0x3c(%rbp), %xmm3
mulss -0x3c(%rbp), %xmm3
mulss -0x3c(%rbp), %xmm3
movss -0x3c(%rbp), %xmm5
movss -0xa4(%rbp), %xmm4
movd %xmm4, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm4
mulss %xmm5, %xmm3
addss %xmm4, %xmm3
mulss %xmm3, %xmm1
addss %xmm2, %xmm1
mulss %xmm1, %xmm0
movq -0x30(%rbp), %rax
movss %xmm0, 0x8(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopw (%rax,%rax)