// File: /workspace/github_c_makefile_star2_O0/radforddc[P]icpc_siggen/fields.c
// Function: setup_efield
static int setup_efield(MJD_Siggen_Setup *setup){
  FILE   *fp;
  char   line[MAX_LINE], *cp;
  int    i, j, lineno = 0;
  float  v, eabs, er, ez;
  cyl_pt cyl, **efld;

  if ((fp = fopen(setup->field_name, "r")) == NULL){
    error("failed to open electric field table: %s\n", setup->field_name);
    return 1;
  }
  
  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);

  // here I assume that r, zlen never change from their initial values, which is reasonable
  if ((efld = (cyl_pt **) malloc(setup->rlen*sizeof(*efld))) == NULL) {
    error("Malloc failed in setup_efield\n");
    fclose(fp);
    return 1;
  }
  for (i = 0; i < setup->rlen; i++){
    if ((efld[i] = (cyl_pt *) malloc(setup->zlen*sizeof(*efld[i]))) == NULL){
      error("Malloc failed in setup_efield\n");
      //NB: potential memory leak here.
      fclose(fp);
      return 1;
    }
    memset(efld[i], 0, setup->zlen*sizeof(*efld[i]));
  }
  TELL_NORMAL("Reading electric field data from file: %s\n", setup->field_name);

  if (strstr(setup->field_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 E field 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(efld[i], sizeof(cyl_pt), setup->zlen, fp) != setup->zlen) {
        error("Error while reading %s\n", setup->field_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 %f %f %f", 
                 &cyl.r, &cyl.z, &v, &eabs, &er, &ez) != 6){
        error("failed to read electric field data from line no %d\n"
              "of file %s\n", lineno, setup->field_name);
        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) {
        // error("Error in efield line %d, i = %d, j = %d\n", line, i, j);
        continue;
      }
      cyl.phi = 0;
      if (outside_detector_cyl(cyl, setup)) continue;
      efld[i][j].r = er;
      efld[i][j].z = ez;
      efld[i][j].phi = 0;
    }      

    TELL_NORMAL("Done reading %d lines of electric field data\n", lineno);
  }

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

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x280, %rsp            # imm = 0x280
movq %rdi, -0x10(%rbp)
movl $0x0, -0x234(%rbp)
movq -0x10(%rbp), %rdi
addq $0x188, %rdi            # imm = 0x188
leaq 0x7b5a(%rip), %rsi      # 0xe505
callq 0x20c0
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
jne 0x69df
movq -0x10(%rbp), %rsi
addq $0x188, %rsi            # imm = 0x188
leaq 0x6f7d(%rip), %rdi      # 0xd949
movb $0x0, %al
callq 0x25d0
movl $0x1, -0x4(%rbp)
jmp 0x70ce
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 0x6a7c
movq -0x10(%rbp), %rax
movl 0x3c8(%rax), %esi
movq -0x10(%rbp), %rax
movl 0x3cc(%rax), %edx
leaq 0x6efd(%rip), %rdi      # 0xd972
movb $0x0, %al
callq 0x2530
movq -0x10(%rbp), %rax
movslq 0x3c8(%rax), %rdi
shlq $0x3, %rdi
callq 0x20b0
movq %rax, -0x258(%rbp)
cmpq $0x0, %rax
jne 0x6ac0
leaq 0x6ee2(%rip), %rdi      # 0xd986
movb $0x0, %al
callq 0x25d0
movq -0x18(%rbp), %rdi
callq 0x21e0
movl $0x1, -0x4(%rbp)
jmp 0x70ce
movl $0x0, -0x22c(%rbp)
movl -0x22c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x3c8(%rcx), %eax
jge 0x6b6b
movq -0x10(%rbp), %rax
movslq 0x3cc(%rax), %rax
imulq $0xc, %rax, %rdi
callq 0x20b0
movq -0x258(%rbp), %rcx
movslq -0x22c(%rbp), %rdx
movq %rax, (%rcx,%rdx,8)
cmpq $0x0, %rax
jne 0x6b2f
leaq 0x6e73(%rip), %rdi      # 0xd986
movb $0x0, %al
callq 0x25d0
movq -0x18(%rbp), %rdi
callq 0x21e0
movl $0x1, -0x4(%rbp)
jmp 0x70ce
movq -0x258(%rbp), %rax
movslq -0x22c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x10(%rbp), %rax
movslq 0x3cc(%rax), %rax
imulq $0xc, %rax, %rdx
xorl %esi, %esi
callq 0x2050
movl -0x22c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x22c(%rbp)
jmp 0x6aca
movq -0x10(%rbp), %rax
cmpl $0x1, (%rax)
jl 0x6b8d
movq -0x10(%rbp), %rsi
addq $0x188, %rsi            # imm = 0x188
leaq 0x6e1f(%rip), %rdi      # 0xd9a5
movb $0x0, %al
callq 0x2530
movq -0x10(%rbp), %rdi
addq $0x188, %rdi            # imm = 0x188
leaq 0x6e31(%rip), %rsi      # 0xd9d0
callq 0x2170
cmpq $0x0, %rax
je 0x6d8d
jmp 0x6bb0
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, -0x269(%rbp)
je 0x6c0c
movsbl -0x220(%rbp), %ecx
xorl %eax, %eax
cmpl $0x23, %ecx
movb %al, -0x269(%rbp)
jne 0x6c0c
leaq -0x220(%rbp), %rdi
leaq 0x6ddc(%rip), %rsi      # 0xd9d4
callq 0x2170
cmpq $0x0, %rax
setne %al
xorb $-0x1, %al
movb %al, -0x269(%rbp)
movb -0x269(%rbp), %al
testb $0x1, %al
jne 0x6c18
jmp 0x6c1a
jmp 0x6bb0
movsbl -0x220(%rbp), %eax
cmpl $0x23, %eax
je 0x6c2f
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 0x6c87
movl -0x230(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x3cc(%rcx), %eax
je 0x6ccb
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 0x6d3f(%rip), %rdi      # 0xd9ee
movb $0x0, %al
callq 0x25d0
movq -0x18(%rbp), %rdi
callq 0x21e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x70ce
movl $0x0, -0x22c(%rbp)
movl -0x22c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x3c8(%rcx), %eax
jge 0x6d5d
movq -0x258(%rbp), %rax
movslq -0x22c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x10(%rbp), %rax
movslq 0x3cc(%rax), %rdx
movq -0x18(%rbp), %rcx
movl $0xc, %esi
callq 0x21b0
movq -0x10(%rbp), %rcx
movslq 0x3cc(%rcx), %rcx
cmpq %rcx, %rax
je 0x6d47
movq -0x10(%rbp), %rsi
addq $0x188, %rsi            # imm = 0x188
leaq 0x6cee(%rip), %rdi      # 0xda22
movb $0x0, %al
callq 0x25d0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x70ce
jmp 0x6d49
movl -0x22c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x22c(%rbp)
jmp 0x6cd5
movq -0x10(%rbp), %rax
cmpl $0x1, (%rax)
jl 0x6d88
movq -0x10(%rbp), %rax
movl 0x3c8(%rax), %esi
movq -0x10(%rbp), %rax
movl 0x3cc(%rax), %edx
leaq 0x6cb9(%rip), %rdi      # 0xda3a
movb $0x0, %al
callq 0x2530
jmp 0x7057
jmp 0x6d8f
leaq -0x220(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl $0x200, %esi            # imm = 0x200
callq 0x20e0
cmpq $0x0, %rax
je 0x7038
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, -0x26a(%rbp)
je 0x6e0d
movq -0x228(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x26a(%rbp)
movb -0x26a(%rbp), %al
testb $0x1, %al
jne 0x6e19
jmp 0x6e2f
jmp 0x6e1b
movq -0x228(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x228(%rbp)
jmp 0x6dcb
movq -0x228(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
je 0x6e50
movq -0x228(%rbp), %rdi
callq 0x2130
cmpq $0x0, %rax
jne 0x6e55
jmp 0x6d8f
leaq -0x220(%rbp), %rdi
leaq -0x250(%rbp), %rdx
leaq -0x250(%rbp), %rcx
addq $0x8, %rcx
leaq 0x6959(%rip), %rsi      # 0xd7ce
leaq -0x238(%rbp), %r8
leaq -0x23c(%rbp), %r9
leaq -0x240(%rbp), %r10
leaq -0x244(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x2090
cmpl $0x6, %eax
je 0x6eda
movl -0x234(%rbp), %esi
movq -0x10(%rbp), %rdx
addq $0x188, %rdx            # imm = 0x188
leaq 0x6ba0(%rip), %rdi      # 0xda5e
movb $0x0, %al
callq 0x25d0
movq -0x18(%rbp), %rdi
callq 0x21e0
movl $0x1, -0x4(%rbp)
jmp 0x70ce
movss -0x250(%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 -0x248(%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 0x6f66
movl -0x22c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x3c8(%rcx), %eax
jge 0x6f66
cmpl $0x0, -0x230(%rbp)
jl 0x6f66
movl -0x230(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x3cc(%rcx), %eax
jl 0x6f6b
jmp 0x6d8f
movl $0x0, -0x24c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x248(%rbp), %eax
movl %eax, -0x260(%rbp)
movq -0x250(%rbp), %rax
movq %rax, -0x268(%rbp)
movsd -0x268(%rbp), %xmm0
movss -0x260(%rbp), %xmm1
callq 0x4dc0
cmpl $0x0, %eax
je 0x6fb2
jmp 0x6d8f
movss -0x240(%rbp), %xmm0
movq -0x258(%rbp), %rax
movslq -0x22c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x230(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, (%rax)
movss -0x244(%rbp), %xmm0
movq -0x258(%rbp), %rax
movslq -0x22c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x230(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movss %xmm0, 0x8(%rax)
movq -0x258(%rbp), %rax
movslq -0x22c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x230(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x4(%rax)
jmp 0x6d8f
movq -0x10(%rbp), %rax
cmpl $0x1, (%rax)
jl 0x7055
movl -0x234(%rbp), %esi
leaq 0x6a4f(%rip), %rdi      # 0xda9d
movb $0x0, %al
callq 0x2530
jmp 0x7057
movq -0x18(%rbp), %rdi
callq 0x21e0
movq -0x258(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x1470(%rax)
movl $0x0, -0x22c(%rbp)
movl -0x22c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x3c8(%rcx), %eax
jge 0x70c7
movq -0x258(%rbp), %rax
movslq -0x22c(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x10(%rbp), %rax
movq 0x1470(%rax), %rax
movslq -0x22c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x22c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x22c(%rbp)
jmp 0x707c
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x280, %rsp            # imm = 0x280
popq %rbp
retq
nopw (%rax,%rax)