// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rdx, -0x40(%rbp)
movq %rsi, %rbx
movq %rsi, %rdi
xorl %esi, %esi
callq 0xc3f0
testl %eax, %eax
je 0x2bf6b
leaq 0xb203(%rip), %rdi      # 0x36f81
leaq 0x969a(%rip), %rsi      # 0x3541f
callq 0xbdd0
movq %rax, %r15
leaq 0x6987(%rip), %rsi      # 0x3271b
movq %rbx, %rdi
callq 0xbdd0
movq %rax, -0x38(%rbp)
testq %rax, %rax
je 0x2bfb0
movq 0x111f0(%rip), %rax     # 0x3cfa0
movl (%rax), %r12d
pushq $0x1
popq %r14
leaq -0x150(%rbp), %r13
xorl %ebx, %ebx
movq %r15, %rdi
callq 0xc1b0
testl %eax, %eax
jne 0x2bef8
movq %r13, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdx
callq 0xc230
testq %rax, %rax
je 0x2bef8
movq %r13, %rdi
leaq 0xb134(%rip), %rsi      # 0x36f27
leaq -0x2c(%rbp), %rdx
leaq -0x50(%rbp), %rcx
xorl %eax, %eax
callq 0xbb10
cmpl $0x2, %eax
jne 0x2bdc0
movl -0x2c(%rbp), %eax
movl $0xffffeada, %ecx       # imm = 0xFFFFEADA
addl %ecx, %eax
cmpl $0xffffeadb, %eax       # imm = 0xFFFFEADB
jb 0x2c03e
movslq %ebx, %rbx
movq %rbx, %rcx
shlq $0x20, %rcx
movq %r15, -0x48(%rbp)
cmpq $0x1525, %rbx           # imm = 0x1525
jg 0x2bdc0
movslq -0x2c(%rbp), %rax
cmpq %rax, %rbx
jg 0x2c032
movq %r13, %r15
movslq %r14d, %r13
cmpl %eax, %ebx
je 0x2bea6
cmpl %r12d, %ebx
jne 0x2be8d
movq -0x40(%rbp), %rax
movsd (%rax,%rbx,8), %xmm0
movq %r15, %rdi
leaq 0xb143(%rip), %rsi      # 0x36fa5
movl %r12d, %edx
movb $0x1, %al
movq %rcx, %r12
callq 0xb190
movq %r15, %rdi
movq -0x38(%rbp), %rsi
callq 0xbcf0
movq %r12, %rcx
leal 0x1(%r13), %r14d
movq 0x11117(%rip), %rax     # 0x3cfa0
movl (%rax,%r13,4), %r12d
incq %rbx
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %rcx
movq %r15, %r13
movq -0x48(%rbp), %r15
jmp 0x2be2a
sarq $0x1d, %rcx
movq -0x40(%rbp), %rax
movsd (%rax,%rcx), %xmm0
movq %r15, %rdi
leaq 0xb0e8(%rip), %rsi      # 0x36fa5
movl %ebx, %edx
movb $0x1, %al
callq 0xb190
leaq -0x150(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0xbcf0
cmpl %r12d, %ebx
jne 0x2beea
leal 0x1(%r13), %r14d
movq 0x110ba(%rip), %rax     # 0x3cfa0
movl (%rax,%r13,4), %r12d
incl %ebx
movq %r15, %r13
movq -0x48(%rbp), %r15
jmp 0x2bdc0
movslq %ebx, %r13
movq %r15, %rdi
callq 0xbb40
leaq 0xb09b(%rip), %rbx      # 0x36fa5
leaq -0x150(%rbp), %r15
cmpq $0x1525, %r13           # imm = 0x1525
jg 0x2bf5b
cmpl %r12d, %r13d
jne 0x2bf56
movq -0x40(%rbp), %rax
movsd (%rax,%r13,8), %xmm0
movq %r15, %rdi
movq %rbx, %rsi
movl %r12d, %edx
movb $0x1, %al
callq 0xb190
movq %r15, %rdi
movq -0x38(%rbp), %rsi
callq 0xbcf0
movslq %r14d, %rax
incl %r14d
movq 0x1104e(%rip), %rcx     # 0x3cfa0
movl (%rcx,%rax,4), %r12d
incq %r13
jmp 0x2bf11
movq -0x38(%rbp), %rdi
callq 0xbb40
xorl %eax, %eax
jmp 0x2bff8
leaq -0x150(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0xba80
movq %r14, %rdi
callq 0xb4e0
movl $0x6b61622e, -0x150(%rbp,%rax) # imm = 0x6B61622E
movb $0x0, -0x14c(%rbp,%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xb680
testl %eax, %eax
je 0x2c00a
leaq 0xafa1(%rip), %rdi      # 0x36f4f
jmp 0x2bfb7
leaq 0xafd4(%rip), %rdi      # 0x36f8b
callq 0xb220
movq %rax, %rsi
xorl %eax, %eax
callq 0xb8e0
movq 0x10f93(%rip), %rbx     # 0x3cf60
leaq 0xc6d0(%rbx), %rdi
leaq 0xb103(%rip), %rsi      # 0x370de
pushq $0x50
popq %rdx
callq 0xb940
andw $0x0, 0xc71f(%rbx)
movl $0x1, 0xd670(%rbx)
pushq $0x5
popq %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x940e(%rip), %rsi      # 0x3541f
leaq -0x150(%rbp), %rdi
callq 0xbdd0
movq %rax, %r15
testq %rax, %rax
jne 0x2bd8d
leaq 0xaf39(%rip), %rdi      # 0x36f69
jmp 0x2bfb7
leaq 0xaef5(%rip), %rdi      # 0x36f2e
jmp 0x2bfb7
leaq 0x9936(%rip), %rdi      # 0x3597b
jmp 0x2bfb7