// File: /workspace/github_c_makefile_star2_O3/radforddc[P]icpc_siggen/icpc_fieldgen_opt.c
// Function: ev_calc
int ev_calc(MJD_Siggen_Setup *setup, MJD_Siggen_Setup *old_setup) {
  int    i, j;
  float  grid = setup->xtal_grid;
  int    L  = lrint(setup->xtal_length/grid)+3;
  int    R  = lrint(setup->xtal_radius/grid)+3;

  if (!old_setup) {
    printf("\n\n ---- starting EV calculation --- \n");
    for (i = 1; i < L; i++) {
      for (j = 1; j < R; j++) {
        setup->v[0][i][j] = setup->v[1][i][j] = setup->xtal_HV/2.0;
      }
    }
  }
  if (old_setup) interpolate(setup, old_setup);
  setup->fully_depleted = 1;
  setup->bubble_volts = 0;

  /* set boundary voltages */
  for (i = 1; i < L; i++) {
    for (j = 1; j < R; j++) {
      if (setup->point_type[i][j] == HVC)
        setup->v[0][i][j] = setup->v[1][i][j] = setup->xtal_HV;
      if (setup->point_type[i][j] == PC)
        setup->v[0][i][j] = setup->v[1][i][j] = 0.0;
    }
  }

  if (!old_setup || !old_setup->fully_depleted) ev_relax_undep(setup);
  else do_relax(setup, 1);
  if (setup->write_field) write_ev(setup);

  if (setup->fully_depleted) {
    printf("Detector is fully depleted.\n");
    /* save potential close to point contact, to use later when calculating depletion voltage */
    for (i = 1; i < L; i++) {
      for (j = 1; j < R; j++) {
        setup->vsave[i][j] = fabs(setup->v[1][i][j]);
      }
    }
  } else {
    printf("Detector is not fully depleted.\n");
    if (setup->bubble_volts > 0)
      printf("Pinch-off bubble at %.1f V potential\n", setup->bubble_volts);
    if (!old_setup) {
      // write a little file that shows any undepleted voxels in the crystal
      FILE *file = fopen("undepleted.txt", "w");
      for (j = R-1; j > 0; j--) {
	setup->undepleted[j][L-1] = '\0';
	fprintf(file, "%s\n", setup->undepleted[j]+1);
      }
      fclose(file);
    }
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r12
movq %rdi, %rbx
movss 0x4(%rdi), %xmm0
movss 0x54(%rdi), %xmm1
movss %xmm1, (%rsp)
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1100
movq %rax, 0x10(%rsp)
leal 0x3(%rax), %r14d
movss 0x8(%rbx), %xmm0
divss (%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1100
movq %rax, (%rsp)
leal 0x3(%rax), %ebp
testq %r12, %r12
movl %r14d, 0xc(%rsp)
je 0x3c7a
movq %rbx, %rdi
movq %r12, %rsi
callq 0x57e9
jmp 0x3ceb
leaq 0x645c(%rip), %rdi      # 0xa0dd
callq 0x1070
cmpl $0x1, %r14d
jle 0x3dc7
movl %r14d, %eax
movl %ebp, %ecx
movl $0x1, %edx
movsd 0x5396(%rip), %xmm0     # 0x9038
cmpl $0x2, %ebp
jl 0x3ce3
xorps %xmm1, %xmm1
cvtss2sd 0x74(%rbx), %xmm1
mulsd %xmm0, %xmm1
movq 0x3e0(%rbx), %rdi
movq 0x3e8(%rbx), %rsi
movq (%rsi,%rdx,8), %rsi
movq (%rdi,%rdx,8), %rdi
movl $0x1, %r8d
movsd %xmm1, (%rsi,%r8,8)
movsd %xmm1, (%rdi,%r8,8)
incq %r8
cmpq %r8, %rcx
jne 0x3ccf
incq %rdx
cmpq %rax, %rdx
jne 0x3ca2
movq $0x1, 0x440(%rbx)
cmpl $0x2, %r14d
setge %r13b
jl 0x3d97
movl %r14d, %eax
movl %ebp, %ecx
movl $0x1, %edx
xorl %esi, %esi
cmpl $0x2, %ebp
jl 0x3d89
movq 0x430(%rbx), %rdi
movq (%rdi,%rdx,8), %rdi
movl $0x1, %r8d
movb (%rdi,%r8), %r9b
cmpb $0x1, %r9b
jne 0x3d5e
xorps %xmm0, %xmm0
cvtss2sd 0x74(%rbx), %xmm0
movq 0x3e8(%rbx), %r9
movq (%r9,%rdx,8), %r9
movsd %xmm0, (%r9,%r8,8)
movq 0x3e0(%rbx), %r9
movq (%r9,%rdx,8), %r9
movsd %xmm0, (%r9,%r8,8)
movb (%rdi,%r8), %r9b
testb %r9b, %r9b
jne 0x3d81
movq 0x3e8(%rbx), %r9
movq (%r9,%rdx,8), %r9
movq %rsi, (%r9,%r8,8)
movq 0x3e0(%rbx), %r9
movq (%r9,%rdx,8), %r9
movq %rsi, (%r9,%r8,8)
incq %r8
cmpq %r8, %rcx
jne 0x3d26
incq %rdx
cmpq %rax, %rdx
jne 0x3d10
jmp 0x3d9a
xorl %r13d, %r13d
leaq 0x440(%rbx), %r14
leaq 0x444(%rbx), %r15
testq %r12, %r12
je 0x3de3
cmpl $0x0, 0x440(%r12)
je 0x3de3
movq %rbx, %rdi
movl $0x1, %esi
callq 0x643d
jmp 0x3deb
leaq 0x440(%rbx), %r14
movq $0x1, 0x440(%rbx)
leaq 0x444(%rbx), %r15
xorl %r13d, %r13d
movq %rbx, %rdi
callq 0x5a8a
cmpl $0x0, 0x7c(%rbx)
je 0x3df9
movq %rbx, %rdi
callq 0x4bf4
cmpl $0x0, (%r14)
je 0x3e67
leaq 0x631c(%rip), %rdi      # 0xa122
callq 0x1070
testb %r13b, %r13b
je 0x3e97
movl 0xc(%rsp), %eax
movl %ebp, %ecx
movl $0x1, %edx
movaps 0x539a(%rip), %xmm0     # 0x91c0
cmpl $0x2, %ebp
jl 0x3e5d
movq 0x3e8(%rbx), %rsi
movq 0x428(%rbx), %rdi
movq (%rsi,%rdx,8), %rsi
movq (%rdi,%rdx,8), %rdi
movl $0x1, %r8d
movsd (%rsi,%r8,8), %xmm1
andps %xmm0, %xmm1
movlps %xmm1, (%rdi,%r8,8)
incq %r8
cmpq %r8, %rcx
jne 0x3e47
incq %rdx
cmpq %rax, %rdx
jne 0x3e26
jmp 0x3e97
leaq 0x6294(%rip), %rdi      # 0xa102
callq 0x1070
movss (%r15), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x3e92
cvtss2sd %xmm0, %xmm0
leaq 0x5e37(%rip), %rdi      # 0x9cc2
movb $0x1, %al
callq 0x10f0
testq %r12, %r12
je 0x3ea6
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5e3b(%rip), %rdi      # 0x9ce8
leaq 0x5860(%rip), %rsi      # 0x9714
callq 0x11a0
movq %rax, %r12
movq (%rsp), %rcx
addl $0x2, %ecx
testl %ecx, %ecx
jle 0x3f15
movq 0x10(%rsp), %rax
addl $0x2, %eax
movslq %eax, %r15
movl %ecx, %r13d
incq %r13
leaq 0x5ff6(%rip), %r14      # 0x9ed5
movq 0x438(%rbx), %rax
movq -0x8(%rax,%r13,8), %rax
movb $0x0, (%rax,%r15)
movq 0x438(%rbx), %rax
movq -0x8(%rax,%r13,8), %rdx
incq %rdx
movq %r12, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1150
decq %r13
cmpq $0x1, %r13
ja 0x3edf
movq %r12, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10c0