// File: sundale[P]gcode/rs274ngc/interp_convert.cc
// Function: Interp::convert_coordinate_system(int, setup_struct*)
int Interp::convert_coordinate_system(int g_code,        //!< g_code called (must be one listed above)     
                                     setup_pointer settings)    //!< pointer to machine settings                  
{
  int origin;
  double *parameters;

  CHKS((settings->cutter_comp_side != OFF),
       (_("Cannot change coordinate systems with cutter radius compensation on")));
  parameters = settings->parameters;
  switch (g_code) {
  case 540:
    origin = 1;
    break;
  case 550:
    origin = 2;
    break;
  case 560:
    origin = 3;
    break;
  case 570:
    origin = 4;
    break;
  case 580:
    origin = 5;
    break;
  case 590:
    origin = 6;
    break;
  case 591:
    origin = 7;
    break;
  case 592:
    origin = 8;
    break;
  case 593:
    origin = 9;
    break;
  default:
    ERS(NCE_BUG_CODE_NOT_IN_RANGE_G54_TO_G593);
  }

  if (origin == settings->origin_index) {       /* already using this origin */
#ifdef DEBUG_EMC
    enqueue_COMMENT("interpreter: continuing to use same coordinate system");
#endif
    return INTERP_OK;
  }

  // move the current point into the new system
  find_current_in_system(settings, origin,
                         &settings->current_x, &settings->current_y, &settings->current_z,
                         &settings->AA_current, &settings->BB_current, &settings->CC_current,
                         &settings->u_current, &settings->v_current, &settings->w_current);

  // remember that this is new system
  settings->origin_index = origin;
  parameters[5220] = (double) origin;

  // load the origin of the newly-selected system
  settings->origin_offset_x = USER_TO_PROGRAM_LEN(parameters[5201 + (origin * 20)]);
  settings->origin_offset_y = USER_TO_PROGRAM_LEN(parameters[5202 + (origin * 20)]);
  settings->origin_offset_z = USER_TO_PROGRAM_LEN(parameters[5203 + (origin * 20)]);
  settings->AA_origin_offset = USER_TO_PROGRAM_ANG(parameters[5204 + (origin * 20)]);
  settings->BB_origin_offset = USER_TO_PROGRAM_ANG(parameters[5205 + (origin * 20)]);
  settings->CC_origin_offset = USER_TO_PROGRAM_ANG(parameters[5206 + (origin * 20)]);
  settings->u_origin_offset = USER_TO_PROGRAM_LEN(parameters[5207 + (origin * 20)]);
  settings->v_origin_offset = USER_TO_PROGRAM_LEN(parameters[5208 + (origin * 20)]);
  settings->w_origin_offset = USER_TO_PROGRAM_LEN(parameters[5209 + (origin * 20)]);
  settings->rotation_xy = parameters[5210 + (origin * 20)];

  SET_ORIGIN_OFFSETS(settings->origin_offset_x + settings->axis_offset_x,
                     settings->origin_offset_y + settings->axis_offset_y,
                     settings->origin_offset_z + settings->axis_offset_z,
                     settings->AA_origin_offset + settings->AA_axis_offset,
                     settings->BB_origin_offset + settings->BB_axis_offset,
                     settings->CC_origin_offset + settings->CC_axis_offset,
                     settings->u_origin_offset + settings->u_axis_offset,
                     settings->v_origin_offset + settings->v_axis_offset,
                     settings->w_origin_offset + settings->w_axis_offset);
  SET_XY_ROTATION(settings->rotation_xy);
  return INTERP_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rdi, -0x28(%rbp)
movl %esi, -0x2c(%rbp)
movq %rdx, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rax
cmpl $0x0, 0x624(%rax)
je 0x1f5d9
jmp 0x1f506
leaq 0x5a7e9(%rip), %rdi     # 0x79cf6
callq 0x10380
movq -0x50(%rbp), %rdi
movq %rax, %rsi
movb $0x0, %al
callq 0x10df0
movq 0x67a39(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x67a28(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x67a1a(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0x5a7df(%rip), %rsi     # 0x79d3a
movl $0x50, %edx
callq 0x10840
movq 0x679f4(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x679e6(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x679ce(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x679be(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x679b1(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x679a3(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x1c(%rbp)
jmp 0x1fe72
jmp 0x1f5db
movq -0x38(%rbp), %rax
addq $0x17c0, %rax           # imm = 0x17C0
movq %rax, -0x48(%rbp)
movl -0x2c(%rbp), %eax
addl $0xfffffde4, %eax       # imm = 0xFFFFFDE4
movl %eax, %ecx
movq %rcx, -0x58(%rbp)
subl $0x35, %eax
ja 0x1f680
movq -0x58(%rbp), %rax
leaq 0x598e5(%rip), %rcx     # 0x78ef0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x1, -0x3c(%rbp)
jmp 0x1f755
movl $0x2, -0x3c(%rbp)
jmp 0x1f755
movl $0x3, -0x3c(%rbp)
jmp 0x1f755
movl $0x4, -0x3c(%rbp)
jmp 0x1f755
movl $0x5, -0x3c(%rbp)
jmp 0x1f755
movl $0x6, -0x3c(%rbp)
jmp 0x1f755
movl $0x7, -0x3c(%rbp)
jmp 0x1f755
movl $0x8, -0x3c(%rbp)
jmp 0x1f755
movl $0x9, -0x3c(%rbp)
jmp 0x1f755
jmp 0x1f682
leaq 0x5a6eb(%rip), %rdi     # 0x79d74
callq 0x10380
movq -0x50(%rbp), %rdi
movq %rax, %rsi
movb $0x0, %al
callq 0x10df0
movq 0x678bd(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x678ac(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x6789e(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0x5a663(%rip), %rsi     # 0x79d3a
movl $0x50, %edx
callq 0x10840
movq 0x67878(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x6786a(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x67852(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x67842(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x67835(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x67827(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x1c(%rbp)
jmp 0x1fe72
movl -0x3c(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpl 0x179c(%rcx), %eax
jne 0x1f77c
leaq 0x5a62b(%rip), %rdi     # 0x79d96
callq 0x11f00
movl $0x0, -0x1c(%rbp)
jmp 0x1fe72
movq -0x50(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x3c(%rbp), %edx
movq -0x38(%rbp), %rcx
addq $0x600, %rcx            # imm = 0x600
movq -0x38(%rbp), %r8
addq $0x608, %r8             # imm = 0x608
movq -0x38(%rbp), %r9
addq $0x610, %r9             # imm = 0x610
movq -0x38(%rbp), %r15
addq $0x8, %r15
movq -0x38(%rbp), %r14
addq $0x20, %r14
movq -0x38(%rbp), %rbx
addq $0x38, %rbx
movq -0x38(%rbp), %r11
addq $0x50, %r11
movq -0x38(%rbp), %r10
addq $0x68, %r10
movq -0x38(%rbp), %rax
addq $0x80, %rax
movq %r15, (%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0x12310
movl -0x3c(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x179c(%rax)
cvtsi2sdl -0x3c(%rbp), %xmm0
movq -0x48(%rbp), %rax
movsd %xmm0, 0xa320(%rax)
movq -0x48(%rbp), %rax
imull $0x14, -0x3c(%rbp), %ecx
addl $0x1451, %ecx           # imm = 0x1451
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x68(%rbp)
callq 0x11030
movaps %xmm0, %xmm1
movsd -0x68(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
movq 0x6770e(%rip), %rax     # 0x86f60
cmpl $0x1, 0x1690(%rax)
jne 0x1f86a
movsd 0x595e5(%rip), %xmm0    # 0x78e48
movsd %xmm0, -0x70(%rbp)
jmp 0x1f8aa
movq 0x676ef(%rip), %rax     # 0x86f60
movl 0x1690(%rax), %eax
movsd 0x595c1(%rip), %xmm0    # 0x78e40
movsd 0x59589(%rip), %xmm1    # 0x78e10
movsd %xmm1, -0x80(%rbp)
cmpl $0x3, %eax
movsd %xmm0, -0x78(%rbp)
je 0x1f8a0
movsd -0x80(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x78(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x60(%rbp), %xmm0
movsd -0x70(%rbp), %xmm1
divsd %xmm1, %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, 0x17a0(%rax)
movq -0x48(%rbp), %rax
imull $0x14, -0x3c(%rbp), %ecx
addl $0x1452, %ecx           # imm = 0x1452
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x90(%rbp)
callq 0x11030
movaps %xmm0, %xmm1
movsd -0x90(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movq 0x6765b(%rip), %rax     # 0x86f60
cmpl $0x1, 0x1690(%rax)
jne 0x1f920
movsd 0x59532(%rip), %xmm0    # 0x78e48
movsd %xmm0, -0x98(%rbp)
jmp 0x1f972
movq 0x67639(%rip), %rax     # 0x86f60
movl 0x1690(%rax), %eax
movsd 0x5950b(%rip), %xmm0    # 0x78e40
movsd 0x594d3(%rip), %xmm1    # 0x78e10
movsd %xmm1, -0xa8(%rbp)
cmpl $0x3, %eax
movsd %xmm0, -0xa0(%rbp)
je 0x1f962
movsd -0xa8(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0xa0(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x88(%rbp), %xmm0
movsd -0x98(%rbp), %xmm1
divsd %xmm1, %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, 0x17a8(%rax)
movq -0x48(%rbp), %rax
imull $0x14, -0x3c(%rbp), %ecx
addl $0x1453, %ecx           # imm = 0x1453
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xb8(%rbp)
callq 0x11030
movaps %xmm0, %xmm1
movsd -0xb8(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0xb0(%rbp)
movq 0x6758d(%rip), %rax     # 0x86f60
cmpl $0x1, 0x1690(%rax)
jne 0x1f9ee
movsd 0x59464(%rip), %xmm0    # 0x78e48
movsd %xmm0, -0xc0(%rbp)
jmp 0x1fa40
movq 0x6756b(%rip), %rax     # 0x86f60
movl 0x1690(%rax), %eax
movsd 0x5943d(%rip), %xmm0    # 0x78e40
movsd 0x59405(%rip), %xmm1    # 0x78e10
movsd %xmm1, -0xd0(%rbp)
cmpl $0x3, %eax
movsd %xmm0, -0xc8(%rbp)
je 0x1fa30
movsd -0xd0(%rbp), %xmm0
movsd %xmm0, -0xc8(%rbp)
movsd -0xc8(%rbp), %xmm0
movsd %xmm0, -0xc0(%rbp)
movsd -0xb0(%rbp), %xmm0
movsd -0xc0(%rbp), %xmm1
divsd %xmm1, %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, 0x17b0(%rax)
movq -0x48(%rbp), %rax
imull $0x14, -0x3c(%rbp), %ecx
addl $0x1454, %ecx           # imm = 0x1454
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xf8(%rbp)
callq 0x11920
movaps %xmm0, %xmm1
movsd -0xf8(%rbp), %xmm0
divsd %xmm1, %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, 0x10(%rax)
movq -0x48(%rbp), %rax
imull $0x14, -0x3c(%rbp), %ecx
addl $0x1455, %ecx           # imm = 0x1455
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xf0(%rbp)
callq 0x11920
movaps %xmm0, %xmm1
movsd -0xf0(%rbp), %xmm0
divsd %xmm1, %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, 0x28(%rax)
movq -0x48(%rbp), %rax
imull $0x14, -0x3c(%rbp), %ecx
addl $0x1456, %ecx           # imm = 0x1456
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xe8(%rbp)
callq 0x11920
movaps %xmm0, %xmm1
movsd -0xe8(%rbp), %xmm0
divsd %xmm1, %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, 0x40(%rax)
movq -0x48(%rbp), %rax
imull $0x14, -0x3c(%rbp), %ecx
addl $0x1457, %ecx           # imm = 0x1457
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xe0(%rbp)
callq 0x11030
movaps %xmm0, %xmm1
movsd -0xe0(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0xd8(%rbp)
movq 0x6740e(%rip), %rax     # 0x86f60
cmpl $0x1, 0x1690(%rax)
jne 0x1fb6d
movsd 0x592e5(%rip), %xmm0    # 0x78e48
movsd %xmm0, -0x100(%rbp)
jmp 0x1fbbf
movq 0x673ec(%rip), %rax     # 0x86f60
movl 0x1690(%rax), %eax
movsd 0x592be(%rip), %xmm0    # 0x78e40
movsd 0x59286(%rip), %xmm1    # 0x78e10
movsd %xmm1, -0x110(%rbp)
cmpl $0x3, %eax
movsd %xmm0, -0x108(%rbp)
je 0x1fbaf
movsd -0x110(%rbp), %xmm0
movsd %xmm0, -0x108(%rbp)
movsd -0x108(%rbp), %xmm0
movsd %xmm0, -0x100(%rbp)
movsd -0xd8(%rbp), %xmm0
movsd -0x100(%rbp), %xmm1
divsd %xmm1, %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, 0x58(%rax)
movq -0x48(%rbp), %rax
imull $0x14, -0x3c(%rbp), %ecx
addl $0x1458, %ecx           # imm = 0x1458
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x120(%rbp)
callq 0x11030
movaps %xmm0, %xmm1
movsd -0x120(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x118(%rbp)
movq 0x67343(%rip), %rax     # 0x86f60
cmpl $0x1, 0x1690(%rax)
jne 0x1fc38
movsd 0x5921a(%rip), %xmm0    # 0x78e48
movsd %xmm0, -0x128(%rbp)
jmp 0x1fc8a
movq 0x67321(%rip), %rax     # 0x86f60
movl 0x1690(%rax), %eax
movsd 0x591f3(%rip), %xmm0    # 0x78e40
movsd 0x591bb(%rip), %xmm1    # 0x78e10
movsd %xmm1, -0x138(%rbp)
cmpl $0x3, %eax
movsd %xmm0, -0x130(%rbp)
je 0x1fc7a
movsd -0x138(%rbp), %xmm0
movsd %xmm0, -0x130(%rbp)
movsd -0x130(%rbp), %xmm0
movsd %xmm0, -0x128(%rbp)
movsd -0x118(%rbp), %xmm0
movsd -0x128(%rbp), %xmm1
divsd %xmm1, %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, 0x70(%rax)
movq -0x48(%rbp), %rax
imull $0x14, -0x3c(%rbp), %ecx
addl $0x1459, %ecx           # imm = 0x1459
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x148(%rbp)
callq 0x11030
movaps %xmm0, %xmm1
movsd -0x148(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x140(%rbp)
movq 0x67278(%rip), %rax     # 0x86f60
cmpl $0x1, 0x1690(%rax)
jne 0x1fd03
movsd 0x5914f(%rip), %xmm0    # 0x78e48
movsd %xmm0, -0x150(%rbp)
jmp 0x1fd55
movq 0x67256(%rip), %rax     # 0x86f60
movl 0x1690(%rax), %eax
movsd 0x59128(%rip), %xmm0    # 0x78e40
movsd 0x590f0(%rip), %xmm1    # 0x78e10
movsd %xmm1, -0x160(%rbp)
cmpl $0x3, %eax
movsd %xmm0, -0x158(%rbp)
je 0x1fd45
movsd -0x160(%rbp), %xmm0
movsd %xmm0, -0x158(%rbp)
movsd -0x158(%rbp), %xmm0
movsd %xmm0, -0x150(%rbp)
movsd -0x140(%rbp), %xmm0
movsd -0x150(%rbp), %xmm1
divsd %xmm1, %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, 0x88(%rax)
movq -0x48(%rbp), %rax
imull $0x14, -0x3c(%rbp), %ecx
addl $0x145a, %ecx           # imm = 0x145A
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, 0x17b8(%rax)
movq -0x38(%rbp), %rax
movsd 0x17a0(%rax), %xmm0
movq -0x38(%rbp), %rax
addsd 0x118(%rax), %xmm0
movq -0x38(%rbp), %rax
movsd 0x17a8(%rax), %xmm1
movq -0x38(%rbp), %rax
addsd 0x120(%rax), %xmm1
movq -0x38(%rbp), %rax
movsd 0x17b0(%rax), %xmm2
movq -0x38(%rbp), %rax
addsd 0x128(%rax), %xmm2
movq -0x38(%rbp), %rax
movsd 0x10(%rax), %xmm3
movq -0x38(%rbp), %rax
addsd (%rax), %xmm3
movq -0x38(%rbp), %rax
movsd 0x28(%rax), %xmm4
movq -0x38(%rbp), %rax
addsd 0x18(%rax), %xmm4
movq -0x38(%rbp), %rax
movsd 0x40(%rax), %xmm5
movq -0x38(%rbp), %rax
addsd 0x30(%rax), %xmm5
movq -0x38(%rbp), %rax
movsd 0x58(%rax), %xmm6
movq -0x38(%rbp), %rax
addsd 0x48(%rax), %xmm6
movq -0x38(%rbp), %rax
movsd 0x70(%rax), %xmm7
movq -0x38(%rbp), %rax
addsd 0x60(%rax), %xmm7
movq -0x38(%rbp), %rax
movsd 0x88(%rax), %xmm8
movq -0x38(%rbp), %rax
addsd 0x78(%rax), %xmm8
movsd %xmm8, (%rsp)
callq 0x107e0
movq -0x38(%rbp), %rax
movsd 0x17b8(%rax), %xmm0
callq 0x11760
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)