// File: /workspace/github_cpp_makefile_star2_O1/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, -0x48(%rbp)
movq %rsi, %r14
movq %rsi, %rdi
xorl %esi, %esi
callq 0xb290
testl %eax, %eax
je 0x32804
leaq 0xb89d(%rip), %rdi      # 0x3dfd1
leaq 0x9cec(%rip), %rsi      # 0x3c427
callq 0xace0
movq %rax, %r12
leaq 0x6fc7(%rip), %rsi      # 0x39711
movq %r14, %rdi
callq 0xace0
movq %rax, -0x38(%rbp)
testq %rax, %rax
je 0x32850
movq 0x1183a(%rip), %rax     # 0x43fa0
movl (%rax), %r15d
movq %r12, %rdi
callq 0xb070
testl %eax, %eax
je 0x3285c
xorl %r13d, %r13d
movl $0x1, %ebx
movq %r12, %rdi
callq 0xaa80
cmpl $0x1525, %r13d          # imm = 0x1525
jg 0x327f4
movslq %r13d, %r14
incq %r14
leaq -0x150(%rbp), %r12
cmpl %r15d, %r13d
jne 0x327df
movq -0x48(%rbp), %rax
movsd -0x8(%rax,%r14,8), %xmm0
movq %r12, %rdi
leaq 0xb83c(%rip), %rsi      # 0x3dff5
movl %r13d, %edx
movb $0x1, %al
callq 0xa170
movq %r12, %rdi
movq -0x38(%rbp), %rsi
callq 0xac00
movslq %ebx, %rax
incl %ebx
movq 0x117c5(%rip), %rcx     # 0x43fa0
movl (%rcx,%rax,4), %r15d
incl %r13d
movq %r14, %rax
incq %rax
cmpl $0x1526, %r14d          # imm = 0x1526
movq %rax, %r14
jne 0x3279f
movq -0x38(%rbp), %rdi
callq 0xaa80
xorl %eax, %eax
jmp 0x32a2a
leaq -0x150(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0xa9d0
movq %r15, %rdi
callq 0xa4a0
movl $0x6b61622e, -0x150(%rbp,%rax) # imm = 0x6B61622E
movb $0x0, -0x14c(%rbp,%rax)
movq %r14, %rdi
movq %r15, %rsi
callq 0xa640
testl %eax, %eax
je 0x329ab
leaq 0xb754(%rip), %rdi      # 0x3df9f
jmp 0x329da
leaq 0xb784(%rip), %rdi      # 0x3dfdb
jmp 0x329da
movl $0x1, %ebx
leaq -0x150(%rbp), %r14
xorl %r13d, %r13d
movq %r12, -0x40(%rbp)
movq %r14, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r12, %rdx
callq 0xb0f0
testq %rax, %rax
je 0x32781
movq %r14, %rdi
leaq 0xb6e5(%rip), %rsi      # 0x3df77
leaq -0x2c(%rbp), %rdx
leaq -0x50(%rbp), %rcx
xorl %eax, %eax
callq 0xaa50
cmpl $0x2, %eax
jne 0x32996
movl -0x2c(%rbp), %eax
movl $0xffffeada, %ecx       # imm = 0xFFFFEADA
addl %ecx, %eax
cmpl $0xffffeadb, %eax       # imm = 0xFFFFEADB
jb 0x32a3c
cmpl $0x1525, %r13d          # imm = 0x1525
jg 0x32996
movslq %r13d, %r13
movq %r13, %r12
shlq $0x20, %r12
movslq -0x2c(%rbp), %rax
cmpq %rax, %r13
jg 0x329d3
cmpl %eax, %r13d
je 0x3294c
cmpl %r15d, %r13d
jne 0x32927
movq -0x48(%rbp), %rax
movsd (%rax,%r13,8), %xmm0
movq %r14, %rdi
leaq 0xb6f4(%rip), %rsi      # 0x3dff5
movl %r13d, %edx
movb $0x1, %al
callq 0xa170
movq %r14, %rdi
movq -0x38(%rbp), %rsi
callq 0xac00
movslq %ebx, %rax
incl %ebx
movq 0x1167d(%rip), %rcx     # 0x43fa0
movl (%rcx,%rax,4), %r15d
incq %r13
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %r12
cmpl $0x1526, %r13d          # imm = 0x1526
jne 0x328d6
movl $0x1526, %r13d          # imm = 0x1526
movq -0x40(%rbp), %r12
jmp 0x32996
sarq $0x1d, %r12
movq -0x48(%rbp), %rax
movsd (%rax,%r12), %xmm0
movq %r14, %rdi
leaq 0xb691(%rip), %rsi      # 0x3dff5
movl %r13d, %edx
movb $0x1, %al
callq 0xa170
movq %r14, %rdi
movq -0x38(%rbp), %rsi
callq 0xac00
cmpl %r15d, %r13d
jne 0x3298f
movslq %ebx, %rax
incl %ebx
movq 0x11615(%rip), %rcx     # 0x43fa0
movl (%rcx,%rax,4), %r15d
movq -0x40(%rbp), %r12
incl %r13d
movq %r12, %rdi
callq 0xb070
testl %eax, %eax
je 0x3286f
jmp 0x32781
leaq 0x9a75(%rip), %rsi      # 0x3c427
leaq -0x150(%rbp), %rdi
callq 0xace0
movq %rax, %r12
testq %rax, %rax
jne 0x32743
leaq 0xb5e8(%rip), %rdi      # 0x3dfb9
jmp 0x329da
leaq 0xb5a4(%rip), %rdi      # 0x3df7e
callq 0xa1f0
movq %rax, %rsi
xorl %eax, %eax
callq 0xa850
movq 0x11570(%rip), %rbx     # 0x43f60
movl $0x0, 0xd670(%rbx)
leaq 0xc6d0(%rbx), %rdi
leaq 0xb726(%rip), %rsi      # 0x3e12e
movl $0x50, %edx
callq 0xa8a0
movw $0x0, 0xc71f(%rbx)
movl $0x1, 0xd670(%rbx)
movl $0x5, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9f43(%rip), %rdi      # 0x3c986
jmp 0x329da
nop