// File: sundale[P]gcode/rs274ngc/rs274ngc_pre.cc
// Function: Interp::save_parameters(char const*, double const*)
int Interp::save_parameters(const char *filename,      //!< name of file to write
                             const double parameters[]) //!< parameters to save   
{
  FILE *infile;
  FILE *outfile;
  char line[256];
  int variable;
  double value;
  int required;                 // number of next required parameter
  int index;                    // index into _required_parameters
  int k;

  if(access(filename, F_OK)==0) 
  {
    // rename as .bak
    strcpy(line, filename);
    strcat(line, RS274NGC_PARAMETER_FILE_BACKUP_SUFFIX);
    CHKS((rename(filename, line) != 0), NCE_CANNOT_CREATE_BACKUP_FILE);

    // open backup for reading
    infile = fopen(line, "r");
    CHKS((infile == NULL), NCE_CANNOT_OPEN_BACKUP_FILE);
  } else {
    // it's OK if the parameter file doesn't exist yet
    // it will now be created with a default list of parameters
    infile = fopen("/dev/null", "r");
  }
  // open original for writing
  outfile = fopen(filename, "w");
  CHKS((outfile == NULL), NCE_CANNOT_OPEN_VARIABLE_FILE);

  k = 0;
  index = 0;
  required = _required_parameters[index++];
  while (feof(infile) == 0) {
    if (fgets(line, 256, infile) == NULL) {
      break;
    }
    // try for a variable-value match
    if (sscanf(line, "%d %lf", &variable, &value) == 2) {
      CHKS(((variable <= 0)
           || (variable >= RS274NGC_MAX_PARAMETERS)),
          NCE_PARAMETER_NUMBER_OUT_OF_RANGE);
      for (; k < RS274NGC_MAX_PARAMETERS; k++) {
        if (k > variable)
          ERS(NCE_PARAMETER_FILE_OUT_OF_ORDER);
        else if (k == variable) {
          sprintf(line, "%d\t%f\n", k, parameters[k]);
          fputs(line, outfile);
          if (k == required)
            required = _required_parameters[index++];
          k++;
          break;
        } else if (k == required)       // know (k < variable)
        {
          sprintf(line, "%d\t%f\n", k, parameters[k]);
          fputs(line, outfile);
          required = _required_parameters[index++];
        }
      }
    }
  }
  fclose(infile);
  for (; k < RS274NGC_MAX_PARAMETERS; k++) {
    if (k == required) {
      sprintf(line, "%d\t%f\n", k, parameters[k]);
      fputs(line, outfile);
      required = _required_parameters[index++];
    }
  }
  fclose(outfile);
  return INTERP_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x18(%rbp), %rdi
xorl %esi, %esi
callq 0x11dc0
cmpl $0x0, %eax
jne 0x723ba
leaq -0x130(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x11080
leaq -0x130(%rbp), %rdi
leaq 0xdd1f(%rip), %rsi      # 0x7fee3
callq 0x11fb0
movq -0x18(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x109f0
cmpl $0x0, %eax
je 0x722ba
jmp 0x721e4
leaq 0xdcfd(%rip), %rdi      # 0x7fee8
callq 0x10380
movq -0x158(%rbp), %rdi
movq %rax, %rsi
movb $0x0, %al
callq 0x10df0
movq 0x14d58(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x14d47(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x14d39(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0xdcc6(%rip), %rsi      # 0x7ff02
movl $0x50, %edx
callq 0x10840
movq 0x14d13(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x14d05(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x14ced(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x14cdd(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x14cd0(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x14cc2(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x4(%rbp)
jmp 0x7291e
jmp 0x722bc
leaq -0x130(%rbp), %rdi
leaq 0xba35(%rip), %rsi      # 0x7dcff
callq 0x11490
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x723b6
jmp 0x722e0
leaq 0xdc55(%rip), %rdi      # 0x7ff3c
callq 0x10380
movq -0x158(%rbp), %rdi
movq %rax, %rsi
movb $0x0, %al
callq 0x10df0
movq 0x14c5c(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x14c4b(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x14c3d(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0xdbca(%rip), %rsi      # 0x7ff02
movl $0x50, %edx
callq 0x10840
movq 0x14c17(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x14c09(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x14bf1(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x14be1(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x14bd4(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x14bc6(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x4(%rbp)
jmp 0x7291e
jmp 0x723b8
jmp 0x723d1
leaq 0xdb93(%rip), %rdi      # 0x7ff54
leaq 0xb937(%rip), %rsi      # 0x7dcff
callq 0x11490
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x9013(%rip), %rsi      # 0x7b3ef
callq 0x11490
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x724c8
jmp 0x723f2
leaq 0xdb65(%rip), %rdi      # 0x7ff5e
callq 0x10380
movq -0x158(%rbp), %rdi
movq %rax, %rsi
movb $0x0, %al
callq 0x10df0
movq 0x14b4a(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x14b39(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x14b2b(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0xdab8(%rip), %rsi      # 0x7ff02
movl $0x50, %edx
callq 0x10840
movq 0x14b05(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x14af7(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x14adf(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x14acf(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x14ac2(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x14ab4(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x4(%rbp)
jmp 0x7291e
jmp 0x724ca
movl $0x0, -0x14c(%rbp)
movl $0x0, -0x148(%rbp)
movl -0x148(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x148(%rbp)
movslq %eax, %rcx
movq 0x14aa7(%rip), %rax     # 0x86fa0
movl (%rax,%rcx,4), %eax
movl %eax, -0x144(%rbp)
movq -0x28(%rbp), %rdi
callq 0x11ac0
cmpl $0x0, %eax
jne 0x72872
leaq -0x130(%rbp), %rdi
movq -0x28(%rbp), %rdx
movl $0x100, %esi            # imm = 0x100
callq 0x11b60
cmpq $0x0, %rax
jne 0x72534
jmp 0x72872
leaq -0x130(%rbp), %rdi
leaq 0xd97e(%rip), %rsi      # 0x7fec0
leaq -0x134(%rbp), %rdx
leaq -0x140(%rbp), %rcx
movb $0x0, %al
callq 0x11130
cmpl $0x2, %eax
jne 0x7286d
jmp 0x72562
cmpl $0x0, -0x134(%rbp)
jle 0x7257b
cmpl $0x1526, -0x134(%rbp)   # imm = 0x1526
jl 0x72653
jmp 0x7257d
leaq 0xbe51(%rip), %rdi      # 0x7e3d5
callq 0x10380
movq -0x158(%rbp), %rdi
movq %rax, %rsi
movb $0x0, %al
callq 0x10df0
movq 0x149bf(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x149ae(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x149a0(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0xd92d(%rip), %rsi      # 0x7ff02
movl $0x50, %edx
callq 0x10840
movq 0x1497a(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x1496c(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x14954(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x14944(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x14937(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x14929(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x4(%rbp)
jmp 0x7291e
jmp 0x72655
jmp 0x72657
cmpl $0x1526, -0x14c(%rbp)   # imm = 0x1526
jge 0x7286b
movl -0x14c(%rbp), %eax
cmpl -0x134(%rbp), %eax
jle 0x72751
jmp 0x7267b
leaq 0xd845(%rip), %rdi      # 0x7fec7
callq 0x10380
movq -0x158(%rbp), %rdi
movq %rax, %rsi
movb $0x0, %al
callq 0x10df0
movq 0x148c1(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x148b0(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x148a2(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0xd82f(%rip), %rsi      # 0x7ff02
movl $0x50, %edx
callq 0x10840
movq 0x1487c(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x1486e(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x14856(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x14846(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x14839(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x1482b(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x4(%rbp)
jmp 0x7291e
movl -0x14c(%rbp), %eax
cmpl -0x134(%rbp), %eax
jne 0x727e4
leaq -0x130(%rbp), %rdi
movl -0x14c(%rbp), %edx
movq -0x20(%rbp), %rax
movslq -0x14c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0xd7f1(%rip), %rsi      # 0x7ff78
movb $0x1, %al
callq 0x10280
leaq -0x130(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x11370
movl -0x14c(%rbp), %eax
cmpl -0x144(%rbp), %eax
jne 0x727d0
movl -0x148(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x148(%rbp)
movslq %eax, %rcx
movq 0x147d9(%rip), %rax     # 0x86fa0
movl (%rax,%rcx,4), %eax
movl %eax, -0x144(%rbp)
movl -0x14c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14c(%rbp)
jmp 0x7286b
movl -0x14c(%rbp), %eax
cmpl -0x144(%rbp), %eax
jne 0x72851
leaq -0x130(%rbp), %rdi
movl -0x14c(%rbp), %edx
movq -0x20(%rbp), %rax
movslq -0x14c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0xd762(%rip), %rsi      # 0x7ff78
movb $0x1, %al
callq 0x10280
leaq -0x130(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x11370
movl -0x148(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x148(%rbp)
movslq %eax, %rcx
movq 0x14758(%rip), %rax     # 0x86fa0
movl (%rax,%rcx,4), %eax
movl %eax, -0x144(%rbp)
jmp 0x72853
jmp 0x72855
jmp 0x72857
movl -0x14c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14c(%rbp)
jmp 0x72657
jmp 0x7286d
jmp 0x72502
movq -0x28(%rbp), %rdi
callq 0x11160
cmpl $0x1526, -0x14c(%rbp)   # imm = 0x1526
jge 0x7290e
movl -0x14c(%rbp), %eax
cmpl -0x144(%rbp), %eax
jne 0x728f8
leaq -0x130(%rbp), %rdi
movl -0x14c(%rbp), %edx
movq -0x20(%rbp), %rax
movslq -0x14c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0xd6bb(%rip), %rsi      # 0x7ff78
movb $0x1, %al
callq 0x10280
leaq -0x130(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x11370
movl -0x148(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x148(%rbp)
movslq %eax, %rcx
movq 0x146b1(%rip), %rax     # 0x86fa0
movl (%rax,%rcx,4), %eax
movl %eax, -0x144(%rbp)
jmp 0x728fa
movl -0x14c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14c(%rbp)
jmp 0x7287b
movq -0x30(%rbp), %rdi
callq 0x11160
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
nopw (%rax,%rax)