// File: /workspace/github_c_makefile_star2_O0/radforddc[P]icpc_siggen/fields.c
// Function: setup_wp
static int setup_wp(MJD_Siggen_Setup *setup){
  FILE   *fp;
  char   line[MAX_LINE], *cp;
  int    i, j, lineno;
  cyl_pt cyl;
  float  wp, **wpot;

  setup->rlen = lrintf((setup->rmax - setup->rmin)/setup->rstep) + 1;
  setup->zlen = lrintf((setup->zmax - setup->zmin)/setup->zstep) + 1;
  TELL_CHATTY("rlen, zlen: %d, %d\n", setup->rlen, setup->zlen);

  //assuming rlen, zlen never change as for setup_efld
  if ((wpot = (float **) malloc(setup->rlen*sizeof(*wpot))) == NULL){
    error("Malloc failed in setup_wp\n");
    return 1;
  }
  for (i = 0; i < setup->rlen; i++){
    if ((wpot[i] = (float *) malloc(setup->zlen*sizeof(*wpot[i]))) == NULL){  
      error("Malloc failed in setup_wp\n");
      //NB: memory leak here.
      return 1;
    }
    memset(wpot[i], 0, setup->zlen*sizeof(*wpot[i]));
  }
  if ((fp = fopen(setup->wp_name, "r")) == NULL){
    error("failed to open file: %s\n", setup->wp_name);
    return -1;
  }
  lineno = 0;
  TELL_NORMAL("Reading weighting potential from file: %s\n", setup->wp_name);

  if (strstr(setup->wp_name, "unf")) {
    /* try to read from unformatted file */
    while (fgets(line, sizeof(line), fp) && line[0] == '#' &&
           !strstr(line, "start of unformatted data"))
      ;
    if (line[0] != '#') rewind(fp);
    fread(&i, sizeof(int), 1, fp);
    fread(&j, sizeof(int), 1, fp);
    if (i != setup->rlen || j != setup->zlen) {
      error("Error in WP dimensions: %d != %d, or %d != %d\n", i, setup->rlen, j, setup->zlen);
      fclose(fp);
      return -1;
    }
    for (i = 0; i < setup->rlen; i++) {
      if (fread(wpot[i], sizeof(float), setup->zlen, fp) != setup->zlen) {
        error("Error while reading %s\n", setup->wp_name);
        return -1;
      }
    }
    TELL_NORMAL("Done reading field, %d x %d points\n", setup->rlen, setup->zlen);

  } else {

    /* read the table from a text file*/
    while (fgets(line, MAX_LINE, fp) != NULL){
      lineno++;
      for (cp = line; isspace(*cp) && *cp != '\0'; cp++);
      if (*cp == '#' || !strlen(cp)) continue;
      if (sscanf(line, "%f %f %f\n",&cyl.r, &cyl.z, &wp) != 3){ 
        error("failed to read weighting potential from line %d\n"
              "line: %s", lineno, line);
        fclose(fp);
        return 1;
      }
      i = lrintf((cyl.r - setup->rmin)/setup->rstep);
      j = lrintf((cyl.z - setup->zmin)/setup->zstep);
      if (i < 0 || i >= setup->rlen || j < 0 || j >= setup->zlen) continue;
      if (outside_detector_cyl(cyl, setup)) continue;
      wpot[i][j] = wp;
    }
    TELL_NORMAL("Done reading %d lines of WP data\n", lineno);
  }

  fclose(fp);
  setup->wpot = wpot;
  for (i = 0; i < setup->rlen; i++) setup->wpot[i] = wpot[i];

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x270, %rsp            # imm = 0x270
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movss 0x3b4(%rax), %xmm0
movq -0x10(%rbp), %rax
subss 0x3b0(%rax), %xmm0
movq -0x10(%rbp), %rax
divss 0x3b8(%rax), %xmm0
callq 0x2150
addq $0x1, %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x3c8(%rax)
movq -0x10(%rbp), %rax
movss 0x3c0(%rax), %xmm0
movq -0x10(%rbp), %rax
subss 0x3bc(%rax), %xmm0
movq -0x10(%rbp), %rax
divss 0x3c4(%rax), %xmm0
callq 0x2150
addq $0x1, %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x3cc(%rax)
movq -0x10(%rbp), %rax
cmpl $0x2, (%rax)
jl 0x718c
movq -0x10(%rbp), %rax
movl 0x3c8(%rax), %esi
movq -0x10(%rbp), %rax
movl 0x3cc(%rax), %edx
leaq 0x67ed(%rip), %rdi      # 0xd972
movb $0x0, %al
callq 0x2530
movq -0x10(%rbp), %rax
movslq 0x3c8(%rax), %rdi
shlq $0x3, %rdi
callq 0x20b0
movq %rax, -0x250(%rbp)
cmpq $0x0, %rax
jne 0x71c7
leaq 0x6917(%rip), %rdi      # 0xdacb
movb $0x0, %al
callq 0x25d0
movl $0x1, -0x4(%rbp)
jmp 0x7795
movl $0x0, -0x22c(%rbp)
movl -0x22c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x3c8(%rcx), %eax
jge 0x7269
movq -0x10(%rbp), %rax
movslq 0x3cc(%rax), %rdi
shlq $0x2, %rdi
callq 0x20b0
movq -0x250(%rbp), %rcx
movslq -0x22c(%rbp), %rdx
movq %rax, (%rcx,%rdx,8)
cmpq $0x0, %rax
jne 0x722d
leaq 0x68b1(%rip), %rdi      # 0xdacb
movb $0x0, %al
callq 0x25d0
movl $0x1, -0x4(%rbp)
jmp 0x7795
movq -0x250(%rbp), %rax
movslq -0x22c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x10(%rbp), %rax
movslq 0x3cc(%rax), %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x2050
movl -0x22c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x22c(%rbp)
jmp 0x71d1
movq -0x10(%rbp), %rdi
addq $0x288, %rdi            # imm = 0x288
leaq 0x728a(%rip), %rsi      # 0xe505
callq 0x20c0
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
jne 0x72af
movq -0x10(%rbp), %rsi
addq $0x288, %rsi            # imm = 0x288
leaq 0x684a(%rip), %rdi      # 0xdae6
movb $0x0, %al
callq 0x25d0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7795
movl $0x0, -0x234(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x1, (%rax)
jl 0x72db
movq -0x10(%rbp), %rsi
addq $0x288, %rsi            # imm = 0x288
leaq 0x682b(%rip), %rdi      # 0xdaff
movb $0x0, %al
callq 0x2530
movq -0x10(%rbp), %rdi
addq $0x288, %rdi            # imm = 0x288
leaq 0x66e3(%rip), %rsi      # 0xd9d0
callq 0x2170
cmpq $0x0, %rax
je 0x74db
jmp 0x72fe
leaq -0x220(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl $0x200, %esi            # imm = 0x200
callq 0x20e0
movq %rax, %rcx
xorl %eax, %eax
cmpq $0x0, %rcx
movb %al, -0x261(%rbp)
je 0x735a
movsbl -0x220(%rbp), %ecx
xorl %eax, %eax
cmpl $0x23, %ecx
movb %al, -0x261(%rbp)
jne 0x735a
leaq -0x220(%rbp), %rdi
leaq 0x668e(%rip), %rsi      # 0xd9d4
callq 0x2170
cmpq $0x0, %rax
setne %al
xorb $-0x1, %al
movb %al, -0x261(%rbp)
movb -0x261(%rbp), %al
testb $0x1, %al
jne 0x7366
jmp 0x7368
jmp 0x72fe
movsbl -0x220(%rbp), %eax
cmpl $0x23, %eax
je 0x737d
movq -0x18(%rbp), %rdi
callq 0x2180
movq -0x18(%rbp), %rcx
leaq -0x22c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x21b0
movq -0x18(%rbp), %rcx
leaq -0x230(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x21b0
movl -0x22c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x3c8(%rcx), %eax
jne 0x73d5
movl -0x230(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x3cc(%rcx), %eax
je 0x7419
movl -0x22c(%rbp), %esi
movq -0x10(%rbp), %rax
movl 0x3c8(%rax), %edx
movl -0x230(%rbp), %ecx
movq -0x10(%rbp), %rax
movl 0x3cc(%rax), %r8d
leaq 0x672d(%rip), %rdi      # 0xdb2a
movb $0x0, %al
callq 0x25d0
movq -0x18(%rbp), %rdi
callq 0x21e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7795
movl $0x0, -0x22c(%rbp)
movl -0x22c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x3c8(%rcx), %eax
jge 0x74ab
movq -0x250(%rbp), %rax
movslq -0x22c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x10(%rbp), %rax
movslq 0x3cc(%rax), %rdx
movq -0x18(%rbp), %rcx
movl $0x4, %esi
callq 0x21b0
movq -0x10(%rbp), %rcx
movslq 0x3cc(%rcx), %rcx
cmpq %rcx, %rax
je 0x7495
movq -0x10(%rbp), %rsi
addq $0x288, %rsi            # imm = 0x288
leaq 0x65a0(%rip), %rdi      # 0xda22
movb $0x0, %al
callq 0x25d0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7795
jmp 0x7497
movl -0x22c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x22c(%rbp)
jmp 0x7423
movq -0x10(%rbp), %rax
cmpl $0x1, (%rax)
jl 0x74d6
movq -0x10(%rbp), %rax
movl 0x3c8(%rax), %esi
movq -0x10(%rbp), %rax
movl 0x3cc(%rax), %edx
leaq 0x656b(%rip), %rdi      # 0xda3a
movb $0x0, %al
callq 0x2530
jmp 0x771e
jmp 0x74dd
leaq -0x220(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl $0x200, %esi            # imm = 0x200
callq 0x20e0
cmpq $0x0, %rax
je 0x76ff
movl -0x234(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x234(%rbp)
leaq -0x220(%rbp), %rax
movq %rax, -0x228(%rbp)
callq 0x2140
movq (%rax), %rax
movq -0x228(%rbp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %ecx
andl $0x2000, %ecx           # imm = 0x2000
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x262(%rbp)
je 0x755b
movq -0x228(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x262(%rbp)
movb -0x262(%rbp), %al
testb $0x1, %al
jne 0x7567
jmp 0x757d
jmp 0x7569
movq -0x228(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x228(%rbp)
jmp 0x7519
movq -0x228(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
je 0x759e
movq -0x228(%rbp), %rdi
callq 0x2130
cmpq $0x0, %rax
jne 0x75a3
jmp 0x74dd
leaq -0x220(%rbp), %rdi
leaq -0x240(%rbp), %rdx
leaq -0x240(%rbp), %rcx
addq $0x8, %rcx
leaq 0x6596(%rip), %rsi      # 0xdb59
leaq -0x244(%rbp), %r8
movb $0x0, %al
callq 0x2090
cmpl $0x3, %eax
je 0x7606
movl -0x234(%rbp), %esi
leaq -0x220(%rbp), %rdx
leaq 0x6579(%rip), %rdi      # 0xdb63
movb $0x0, %al
callq 0x25d0
movq -0x18(%rbp), %rdi
callq 0x21e0
movl $0x1, -0x4(%rbp)
jmp 0x7795
movss -0x240(%rbp), %xmm0
movq -0x10(%rbp), %rax
subss 0x3b0(%rax), %xmm0
movq -0x10(%rbp), %rax
divss 0x3b8(%rax), %xmm0
callq 0x2150
movl %eax, -0x22c(%rbp)
movss -0x238(%rbp), %xmm0
movq -0x10(%rbp), %rax
subss 0x3bc(%rax), %xmm0
movq -0x10(%rbp), %rax
divss 0x3c4(%rax), %xmm0
callq 0x2150
movl %eax, -0x230(%rbp)
cmpl $0x0, -0x22c(%rbp)
jl 0x7692
movl -0x22c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x3c8(%rcx), %eax
jge 0x7692
cmpl $0x0, -0x230(%rbp)
jl 0x7692
movl -0x230(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x3cc(%rcx), %eax
jl 0x7697
jmp 0x74dd
movq -0x10(%rbp), %rdi
movl -0x238(%rbp), %eax
movl %eax, -0x258(%rbp)
movq -0x240(%rbp), %rax
movq %rax, -0x260(%rbp)
movsd -0x260(%rbp), %xmm0
movss -0x258(%rbp), %xmm1
callq 0x4dc0
cmpl $0x0, %eax
je 0x76d4
jmp 0x74dd
movss -0x244(%rbp), %xmm0
movq -0x250(%rbp), %rax
movslq -0x22c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x230(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
jmp 0x74dd
movq -0x10(%rbp), %rax
cmpl $0x1, (%rax)
jl 0x771c
movl -0x234(%rbp), %esi
leaq 0x6487(%rip), %rdi      # 0xdb9c
movb $0x0, %al
callq 0x2530
jmp 0x771e
movq -0x18(%rbp), %rdi
callq 0x21e0
movq -0x250(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x1478(%rax)
movl $0x0, -0x22c(%rbp)
movl -0x22c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x3c8(%rcx), %eax
jge 0x778e
movq -0x250(%rbp), %rax
movslq -0x22c(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x10(%rbp), %rax
movq 0x1478(%rax), %rax
movslq -0x22c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x22c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x22c(%rbp)
jmp 0x7743
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x270, %rsp            # imm = 0x270
popq %rbp
retq
nopw %cs:(%rax,%rax)