// File: /workspace/github_c_makefile_star2_O0/radforddc[P]icpc_siggen/icpc_fieldgen_opt.c
// Function: write_wp
int write_wp(MJD_Siggen_Setup *setup) {
  int    i, j, new=1;;
  float  grid = setup->xtal_grid;
  //int    L  = setup->xtal_length/grid + 2.99;
  //int    R  = setup->xtal_radius/grid + 2.99;
  int    L  = lrint(setup->xtal_length/grid)+2;
  int    R  = lrint(setup->xtal_radius/grid)+2;
  float  r, z, w;
  FILE *file;

  if (!(file = fopen(setup->wp_name, "w"))) {
    printf("ERROR: Cannot open file %s for weighting potential...\n", setup->wp_name);
    return 1;
  } else {
    printf("Writing weighting potential to file %s\n\n", setup->wp_name);
  }

  /* copy configuration parameters to output file */
  report_config(file, setup->config_file_name);
  fprintf(file, "#\n# HV bias in fieldgen: %.1f V\n", setup->xtal_HV);
  if (setup->fully_depleted) {
    fprintf(file, "# Detector is fully depleted.\n");
  } else {
    fprintf(file, "# Detector is not fully depleted.\n");
    if (setup->bubble_volts > 0.0f)
      fprintf(file, "# Pinch-off bubble at %.0f V potential\n", setup->bubble_volts);
  }

  if (strstr(setup->field_name, "unf")) {
    fprintf(file, "#\n## start of unformatted data\n");
    i = R-1; j = L-1;
    fwrite(&i, sizeof(int), 1, file);
    fwrite(&j, sizeof(int), 1, file);
    for (i = 1; i < R; i++) {
      for (j = 1; j < L; j++) {
        w = setup->v[new][j][i];
        fwrite(&w, sizeof(float), 1, file);
      }
    }
  } else {
    fprintf(file, "#\n## r (mm), z (mm), WP\n");
    for (j = 1; j < R; j++) {
      r = (j-1) * grid;
      for (i = 1; i < L; i++) {
        z = (i-1) * grid;
        fprintf(file, "%7.2f %7.2f %12.6e\n", r, z, setup->v[new][i][j]);
      }
      fprintf(file, "\n");
    }
  }
  fclose(file);

  return 0;
 }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl $0x1, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movss 0x54(%rax), %xmm0
movss %xmm0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movss 0x4(%rax), %xmm0
divss -0x20(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x10f0
addq $0x2, %rax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movss 0x8(%rax), %xmm0
divss -0x20(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x10f0
addq $0x2, %rax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
addq $0x288, %rdi            # imm = 0x288
leaq 0x3b25(%rip), %rsi      # 0xd654
callq 0x1180
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
jne 0x9b63
movq -0x10(%rbp), %rsi
addq $0x288, %rsi            # imm = 0x288
leaq 0x42a2(%rip), %rdi      # 0xddf2
movb $0x0, %al
callq 0x10e0
movl $0x1, -0x4(%rbp)
jmp 0x9de7
movq -0x10(%rbp), %rsi
addq $0x288, %rsi            # imm = 0x288
leaq 0x42b4(%rip), %rdi      # 0xde29
movb $0x0, %al
callq 0x10e0
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x1480, %rsi           # imm = 0x1480
callq 0x9df0
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rax
movss 0x74(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x42aa(%rip), %rsi      # 0xde52
movb $0x1, %al
callq 0x1130
movq -0x10(%rbp), %rax
cmpl $0x0, 0x440(%rax)
je 0x9bd0
movq -0x40(%rbp), %rdi
leaq 0x42ac(%rip), %rsi      # 0xde73
movb $0x0, %al
callq 0x1130
jmp 0x9c1a
movq -0x40(%rbp), %rdi
leaq 0x42b7(%rip), %rsi      # 0xde92
movb $0x0, %al
callq 0x1130
movq -0x10(%rbp), %rax
movss 0x444(%rax), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x9c18
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rax
movss 0x444(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x42a4(%rip), %rsi      # 0xdeb5
movb $0x1, %al
callq 0x1130
jmp 0x9c1a
movq -0x10(%rbp), %rdi
addq $0x188, %rdi            # imm = 0x188
leaq 0x42b1(%rip), %rsi      # 0xdedd
callq 0x11c0
cmpq $0x0, %rax
je 0x9d09
movq -0x40(%rbp), %rdi
leaq 0x429b(%rip), %rsi      # 0xdee1
movb $0x0, %al
callq 0x1130
movl -0x28(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x14(%rbp)
movl -0x24(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x18(%rbp)
movq -0x40(%rbp), %rcx
leaq -0x14(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x11a0
movq -0x40(%rbp), %rcx
leaq -0x18(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x11a0
movl $0x1, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x9d04
movl $0x1, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x9cf7
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq 0x3e0(%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x14(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x34(%rbp)
movq -0x40(%rbp), %rcx
leaq -0x34(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x11a0
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x9ca3
jmp 0x9cf9
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x9c94
jmp 0x9dd7
movq -0x40(%rbp), %rdi
leaq 0x41ed(%rip), %rsi      # 0xdf01
movb $0x0, %al
callq 0x1130
movl $0x1, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x9dd5
movl -0x18(%rbp), %eax
subl $0x1, %eax
cvtsi2ss %eax, %xmm0
mulss -0x20(%rbp), %xmm0
movss %xmm0, -0x2c(%rbp)
movl $0x1, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x9db5
movl -0x14(%rbp), %eax
subl $0x1, %eax
cvtsi2ss %eax, %xmm0
mulss -0x20(%rbp), %xmm0
movss %xmm0, -0x30(%rbp)
movq -0x40(%rbp), %rdi
movss -0x2c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movss -0x30(%rbp), %xmm1
cvtss2sd %xmm1, %xmm1
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq 0x3e0(%rax,%rcx,8), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
leaq 0x4177(%rip), %rsi      # 0xdf1a
movb $0x3, %al
callq 0x1130
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x9d49
movq -0x40(%rbp), %rdi
leaq 0x4dbe(%rip), %rsi      # 0xeb7e
movb $0x0, %al
callq 0x1130
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x9d22
jmp 0x9dd7
movq -0x40(%rbp), %rdi
callq 0x10b0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq