// File: /workspace/github_cpp_makefile_star2_O3/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 0x2ee03
leaq 0xc247(%rip), %rdi      # 0x3af81
leaq 0xa6de(%rip), %rsi      # 0x3941f
callq 0xace0
movq %rax, %r12
leaq 0x79c1(%rip), %rsi      # 0x36711
movq %r14, %rdi
callq 0xace0
movq %rax, -0x38(%rbp)
testq %rax, %rax
je 0x2ee4f
movq 0x12234(%rip), %rax     # 0x40fa0
movl (%rax), %r15d
movq %r12, %rdi
callq 0xb070
testl %eax, %eax
je 0x2ee5b
movq %r12, %rdi
callq 0xaa80
movl $0x1, %r14d
xorl %r13d, %r13d
movslq %r13d, %r12
incq %r12
leaq -0x150(%rbp), %rbx
cmpl %r15d, %r13d
jne 0x2edde
movq -0x48(%rbp), %rax
movsd -0x8(%rax,%r12,8), %xmm0
movq %rbx, %rdi
leaq 0xc1ee(%rip), %rsi      # 0x3afa5
movl %r15d, %edx
movb $0x1, %al
callq 0xa170
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
callq 0xac00
movslq %r14d, %rax
incl %r14d
movq 0x121c6(%rip), %rcx     # 0x40fa0
movl (%rcx,%rax,4), %r15d
incl %r13d
movq %r12, %rax
incq %rax
cmpl $0x1526, %r12d          # imm = 0x1526
movq %rax, %r12
jne 0x2ed9d
movq -0x38(%rbp), %rdi
callq 0xaa80
xorl %eax, %eax
jmp 0x2f026
leaq -0x150(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xa9d0
movq %rbx, %rdi
callq 0xa4a0
movl $0x6b61622e, -0x150(%rbp,%rax) # imm = 0x6B61622E
movb $0x0, -0x14c(%rbp,%rax)
movq %r14, %rdi
movq %rbx, %rsi
callq 0xa640
testl %eax, %eax
je 0x2efb1
leaq 0xc105(%rip), %rdi      # 0x3af4f
jmp 0x2efe0
leaq 0xc135(%rip), %rdi      # 0x3af8b
jmp 0x2efe0
movl $0x1, %r14d
leaq -0x150(%rbp), %rbx
xorl %r13d, %r13d
movq %r12, -0x40(%rbp)
movq %rbx, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r12, %rdx
callq 0xb0f0
testq %rax, %rax
je 0x2ef97
movq %rbx, %rdi
leaq 0xc095(%rip), %rsi      # 0x3af27
leaq -0x2c(%rbp), %rdx
leaq -0x50(%rbp), %rcx
xorl %eax, %eax
callq 0xaa50
cmpl $0x2, %eax
jne 0x2ef87
movl -0x2c(%rbp), %eax
movl $0xffffeada, %ecx       # imm = 0xFFFFEADA
addl %ecx, %eax
cmpl $0xffffeadb, %eax       # imm = 0xFFFFEADB
jb 0x2f038
cmpl $0x1525, %r13d          # imm = 0x1525
jg 0x2ef87
movq %rbx, %r12
movslq %r13d, %rbx
movslq -0x2c(%rbp), %r13
cmpq %r13, %rbx
jg 0x2efd9
cmpl %r13d, %ebx
je 0x2ef3d
cmpl %r15d, %ebx
jne 0x2ef23
movq -0x48(%rbp), %rax
movsd (%rax,%rbx,8), %xmm0
movq %r12, %rdi
leaq 0xc0a9(%rip), %rsi      # 0x3afa5
movl %r15d, %edx
movb $0x1, %al
callq 0xa170
movq %r12, %rdi
movq -0x38(%rbp), %rsi
callq 0xac00
movslq %r14d, %rax
incl %r14d
movq 0x12081(%rip), %rcx     # 0x40fa0
movl (%rcx,%rax,4), %r15d
incq %rbx
cmpl $0x1526, %ebx           # imm = 0x1526
jne 0x2eed2
movl $0x1526, %r13d          # imm = 0x1526
movq %r12, %rbx
movq -0x40(%rbp), %r12
jmp 0x2ef87
movq -0x48(%rbp), %rax
movsd (%rax,%r13,8), %xmm0
movq %r12, %rbx
movq %r12, %rdi
leaq 0xc051(%rip), %rsi      # 0x3afa5
movl %r13d, %edx
movb $0x1, %al
callq 0xa170
movq %r12, %rdi
movq -0x38(%rbp), %rsi
callq 0xac00
cmpl %r15d, %r13d
jne 0x2ef80
movslq %r14d, %rax
incl %r14d
movq 0x12024(%rip), %rcx     # 0x40fa0
movl (%rcx,%rax,4), %r15d
movq -0x40(%rbp), %r12
incl %r13d
movq %r12, %rdi
callq 0xb070
testl %eax, %eax
je 0x2ee6f
movq %r12, %rdi
callq 0xaa80
cmpl $0x1525, %r13d          # imm = 0x1525
jle 0x2ed90
jmp 0x2edf3
leaq 0xa467(%rip), %rsi      # 0x3941f
leaq -0x150(%rbp), %rdi
callq 0xace0
movq %rax, %r12
testq %rax, %rax
jne 0x2ed49
leaq 0xbf92(%rip), %rdi      # 0x3af69
jmp 0x2efe0
leaq 0xbf4e(%rip), %rdi      # 0x3af2e
callq 0xa1f0
movq %rax, %rsi
xorl %eax, %eax
callq 0xa850
movq 0x11f6a(%rip), %rbx     # 0x40f60
leaq 0xc6d0(%rbx), %rdi
leaq 0xc0da(%rip), %rsi      # 0x3b0de
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 0xa93c(%rip), %rdi      # 0x3997b
jmp 0x2efe0
nop