// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movss 0x54(%rax), %xmm0
movss %xmm0, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movss 0x4(%rax), %xmm0
divss -0x1c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x10f0
addq $0x3, %rax
movl %eax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movss 0x8(%rax), %xmm0
divss -0x1c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x10f0
addq $0x3, %rax
movl %eax, -0x24(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x509c
leaq 0x8c95(%rip), %rdi      # 0xdca1
movb $0x0, %al
callq 0x10e0
movl $0x1, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x509a
movl $0x1, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x508d
movq -0x8(%rbp), %rax
movss 0x74(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x8002(%rip), %xmm1     # 0xd048
divsd %xmm1, %xmm0
movq -0x8(%rbp), %rax
movq 0x3e8(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x8(%rbp), %rax
movq 0x3e0(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x5029
jmp 0x508f
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x501a
jmp 0x509c
cmpq $0x0, -0x10(%rbp)
je 0x50b0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x6930
movq -0x8(%rbp), %rax
movl $0x1, 0x440(%rax)
movq -0x8(%rbp), %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x444(%rax)
movl $0x1, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x51d6
movl $0x1, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x51c6
movq -0x8(%rbp), %rax
movq 0x430(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x1, %eax
jne 0x5158
movq -0x8(%rbp), %rax
movss 0x74(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x8(%rbp), %rax
movq 0x3e8(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x8(%rbp), %rax
movq 0x3e0(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x8(%rbp), %rax
movq 0x430(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x51b6
movq -0x8(%rbp), %rax
movq 0x3e8(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x8(%rbp), %rax
movq 0x3e0(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x51b8
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x50e7
jmp 0x51c8
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x50d4
cmpq $0x0, -0x10(%rbp)
je 0x51ea
movq -0x10(%rbp), %rax
cmpl $0x0, 0x440(%rax)
jne 0x51f5
movq -0x8(%rbp), %rdi
callq 0x6cb0
jmp 0x5203
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x7c40
movq -0x8(%rbp), %rax
cmpl $0x0, 0x7c(%rax)
je 0x5216
movq -0x8(%rbp), %rdi
callq 0x56c0
movq -0x8(%rbp), %rax
cmpl $0x0, 0x440(%rax)
je 0x52b3
leaq 0x8c47(%rip), %rdi      # 0xde75
movb $0x0, %al
callq 0x10e0
movl $0x1, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x52ae
movl $0x1, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x52a1
movq -0x8(%rbp), %rax
movq 0x3e8(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movaps 0x7eca(%rip), %xmm1     # 0xd140
pand %xmm1, %xmm0
movq -0x8(%rbp), %rax
movq 0x428(%rax), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x524b
jmp 0x52a3
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x523c
jmp 0x5383
leaq 0x8bda(%rip), %rdi      # 0xde94
movb $0x0, %al
callq 0x10e0
movq -0x8(%rbp), %rax
movss 0x444(%rax), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x52f3
movq -0x8(%rbp), %rax
movss 0x444(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x89db(%rip), %rdi      # 0xdcc7
movb $0x1, %al
callq 0x10e0
cmpq $0x0, -0x10(%rbp)
jne 0x5381
leaq 0x89e8(%rip), %rdi      # 0xdced
leaq 0x8348(%rip), %rsi      # 0xd654
callq 0x1180
movq %rax, -0x30(%rbp)
movl -0x24(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jle 0x5378
movq -0x8(%rbp), %rax
movq 0x438(%rax), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl -0x20(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movq -0x30(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x438(%rax), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
leaq 0x8bdd(%rip), %rsi      # 0xdf43
movb $0x0, %al
callq 0x1130
movl -0x18(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x531e
movq -0x30(%rbp), %rdi
callq 0x10b0
jmp 0x5383
xorl %eax, %eax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)