// File: /workspace/github_c_makefile_star2_O2/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
pushq %rax
movq %rsi, %r12
movq %rdi, %rbx
movss 0x4(%rdi), %xmm0
movss 0x54(%rdi), %xmm1
movss %xmm1, 0x4(%rsp)
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1100
movq %rax, %r15
leal 0x3(%rax), %ebp
movss 0x8(%rbx), %xmm0
divss 0x4(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1100
movq %rax, %r14
leal 0x3(%rax), %r13d
testq %r12, %r12
je 0x3995
movslq %r13d, %r13
movslq %ebp, %rbp
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4f81
pushq $0x1
popq %rax
movq $0x1, 0x440(%rbx)
movq %rax, %rcx
cmpq %rbp, %rcx
jge 0x38ff
movq %rax, %rdx
cmpq %r13, %rdx
jge 0x38fa
movq 0x430(%rbx), %rsi
movq (%rsi,%rcx,8), %rsi
movb (%rsi,%rdx), %dil
cmpb $0x1, %dil
jne 0x38d0
cvtss2sd 0x74(%rbx), %xmm0
movq 0x3e8(%rbx), %rdi
movq (%rdi,%rcx,8), %rdi
movsd %xmm0, (%rdi,%rdx,8)
movq 0x3e0(%rbx), %rdi
movq (%rdi,%rcx,8), %rdi
movsd %xmm0, (%rdi,%rdx,8)
movb (%rsi,%rdx), %dil
testb %dil, %dil
jne 0x38f5
movq 0x3e8(%rbx), %rsi
movq (%rsi,%rcx,8), %rsi
andq $0x0, (%rsi,%rdx,8)
movq 0x3e0(%rbx), %rsi
movq (%rsi,%rcx,8), %rsi
andq $0x0, (%rsi,%rdx,8)
incq %rdx
jmp 0x388d
incq %rcx
jmp 0x3885
testq %r12, %r12
je 0x391c
cmpl $0x0, 0x440(%r12)
je 0x391c
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x5a26
jmp 0x3924
movq %rbx, %rdi
callq 0x51c4
cmpl $0x0, 0x7c(%rbx)
je 0x3932
movq %rbx, %rdi
callq 0x462a
cmpl $0x0, 0x440(%rbx)
je 0x39f9
leaq 0x576c(%rip), %rdi      # 0x90b2
callq 0x1070
pushq $0x1
popq %rax
movaps 0x481b(%rip), %xmm0     # 0x8170
movq %rax, %rcx
cmpq %rbp, %rcx
jge 0x3a2c
movq %rax, %rdx
cmpq %r13, %rdx
jge 0x3990
movq 0x3e8(%rbx), %rsi
movq 0x428(%rbx), %rdi
movq (%rsi,%rcx,8), %rsi
movsd (%rsi,%rdx,8), %xmm1
andps %xmm0, %xmm1
movq (%rdi,%rcx,8), %rsi
movlps %xmm1, (%rsi,%rdx,8)
incq %rdx
jmp 0x3964
incq %rcx
jmp 0x3958
leaq 0x56d1(%rip), %rdi      # 0x906d
callq 0x1070
movslq %r13d, %r13
movslq %ebp, %rbp
pushq $0x1
popq %rax
movsd 0x46be(%rip), %xmm0     # 0x8070
movq %rax, %rcx
cmpq %rbp, %rcx
jge 0x3874
movq %rax, %rdx
cmpq %r13, %rdx
jge 0x39f4
cvtss2sd 0x74(%rbx), %xmm1
mulsd %xmm0, %xmm1
movq 0x3e8(%rbx), %rsi
movq (%rsi,%rcx,8), %rsi
movsd %xmm1, (%rsi,%rdx,8)
movq 0x3e0(%rbx), %rsi
movq (%rsi,%rcx,8), %rsi
movsd %xmm1, (%rsi,%rdx,8)
incq %rdx
jmp 0x39c1
incq %rcx
jmp 0x39b5
leaq 0x5692(%rip), %rdi      # 0x9092
callq 0x1070
movss 0x444(%rbx), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x3a27
cvtss2sd %xmm0, %xmm0
leaq 0x5232(%rip), %rdi      # 0x8c52
movb $0x1, %al
callq 0x10f0
testq %r12, %r12
je 0x3a3b
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5236(%rip), %rdi      # 0x8c78
leaq 0x4c5b(%rip), %rsi      # 0x86a4
callq 0x11b0
movq %rax, %r12
addl $0x2, %r14d
addl $0x2, %r15d
movslq %r15d, %r13
leaq 0x5402(%rip), %r15      # 0x8e65
testl %r14d, %r14d
jle 0x3a98
movq 0x438(%rbx), %rax
movq (%rax,%r14,8), %rax
movb $0x0, (%rax,%r13)
movq 0x438(%rbx), %rax
movq (%rax,%r14,8), %rdx
incq %rdx
movq %r12, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x1150
decq %r14
jmp 0x3a63
movq %r12, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10c0