// File: /workspace/github_c_makefile_star2_O1/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, %r14
leal 0x3(%rax), %r13d
movss 0x8(%rbx), %xmm0
divss 0x4(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x1100
movq %rax, %r15
leal 0x3(%rax), %ebp
testq %r12, %r12
je 0x3c9c
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5808
jmp 0x3d09
leaq 0x63ba(%rip), %rdi      # 0xa05d
callq 0x1070
cmpl $0x2, %r13d
jl 0x3d09
movl %r13d, %eax
movl %ebp, %ecx
movl $0x1, %edx
movsd 0x5378(%rip), %xmm0     # 0x9038
cmpl $0x2, %ebp
jl 0x3d01
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 0x3ced
incq %rdx
cmpq %rax, %rdx
jne 0x3cc0
movq $0x1, 0x440(%rbx)
cmpl $0x2, %r13d
jl 0x3da6
movl %r13d, %eax
movl %ebp, %ecx
movl $0x1, %edx
xorl %esi, %esi
cmpl $0x2, %ebp
jl 0x3d9e
movq 0x430(%rbx), %rdi
movq (%rdi,%rdx,8), %rdi
movl $0x1, %r8d
cmpb $0x1, (%rdi,%r8)
jne 0x3d71
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)
cmpb $0x0, (%rdi,%r8)
jne 0x3d96
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 0x3d40
incq %rdx
cmpq %rax, %rdx
jne 0x3d2a
testq %r12, %r12
je 0x3dc5
cmpl $0x0, 0x440(%r12)
je 0x3dc5
movq %rbx, %rdi
movl $0x1, %esi
callq 0x6462
jmp 0x3dcd
movq %rbx, %rdi
callq 0x5a9a
cmpl $0x0, 0x7c(%rbx)
je 0x3ddb
movq %rbx, %rdi
callq 0x4c67
cmpl $0x0, 0x440(%rbx)
je 0x3e4c
leaq 0x62b7(%rip), %rdi      # 0xa0a2
callq 0x1070
cmpl $0x2, %r13d
jl 0x3e7f
movl %r13d, %eax
movl %ebp, %ecx
movl $0x1, %edx
movaps 0x5345(%rip), %xmm0     # 0x9150
cmpl $0x2, %ebp
jl 0x3e42
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 0x3e2c
incq %rdx
cmpq %rax, %rdx
jne 0x3e0b
jmp 0x3e7f
leaq 0x622f(%rip), %rdi      # 0xa082
callq 0x1070
movss 0x444(%rbx), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x3e7a
cvtss2sd %xmm0, %xmm0
leaq 0x5dcf(%rip), %rdi      # 0x9c42
movb $0x1, %al
callq 0x10f0
testq %r12, %r12
je 0x3e8e
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5dd3(%rip), %rdi      # 0x9c68
leaq 0x57f8(%rip), %rsi      # 0x9694
callq 0x11b0
movq %rax, %r12
addl $0x2, %r15d
testl %r15d, %r15d
jle 0x3ef7
addl $0x2, %r14d
movslq %r14d, %r13
movl %r15d, %r15d
incq %r15
leaq 0x5f94(%rip), %r14      # 0x9e55
movq 0x438(%rbx), %rax
movq -0x8(%rax,%r15,8), %rax
movb $0x0, (%rax,%r13)
movq 0x438(%rbx), %rax
movq -0x8(%rax,%r15,8), %rdx
incq %rdx
movq %r12, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1150
decq %r15
cmpq $0x1, %r15
jg 0x3ec1
movq %r12, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10c0