// File: /workspace/github_cpp_makefile_star2_O2/sundale[P]gcode/rs274ngc/interp_convert.cc
// Function: Interp::convert_stop(block_struct*, setup_struct*)
int Interp::convert_stop(block_pointer block,    //!< pointer to a block of RS274/NGC instructions
                        setup_pointer settings) //!< pointer to machine settings                 
{
  int index;
  char *line;
  int length;

  double cx, cy, cz;
  comp_get_current(settings, &cx, &cy, &cz);
  CHP(move_endpoint_and_flush(settings, cx, cy));
  dequeue_canons(settings);

  if (block->m_modes[4] == 0) {
    PROGRAM_STOP();
  } else if (block->m_modes[4] == 60) {
    PALLET_SHUTTLE();
    PROGRAM_STOP();
  } else if (block->m_modes[4] == 1) {
    OPTIONAL_PROGRAM_STOP();
  } else if ((block->m_modes[4] == 2) || (block->m_modes[4] == 30)) {   /* reset stuff here */
/*1*/
    settings->current_x += settings->origin_offset_x;
    settings->current_y += settings->origin_offset_y;
    settings->current_z += settings->origin_offset_z;
    settings->AA_current += settings->AA_origin_offset;
    settings->BB_current += settings->BB_origin_offset;
    settings->CC_current += settings->CC_origin_offset;
    settings->u_current += settings->u_origin_offset;
    settings->v_current += settings->v_origin_offset;
    settings->w_current += settings->w_origin_offset;
    rotate(&settings->current_x, &settings->current_y, settings->rotation_xy);

    settings->origin_index = 1;
    settings->parameters[5220] = 1.0;
    settings->origin_offset_x = USER_TO_PROGRAM_LEN(settings->parameters[5221]);
    settings->origin_offset_y = USER_TO_PROGRAM_LEN(settings->parameters[5222]);
    settings->origin_offset_z = USER_TO_PROGRAM_LEN(settings->parameters[5223]);
    settings->AA_origin_offset = USER_TO_PROGRAM_ANG(settings->parameters[5224]);
    settings->BB_origin_offset = USER_TO_PROGRAM_ANG(settings->parameters[5225]);
    settings->CC_origin_offset = USER_TO_PROGRAM_ANG(settings->parameters[5226]);
    settings->u_origin_offset = USER_TO_PROGRAM_LEN(settings->parameters[5227]);
    settings->v_origin_offset = USER_TO_PROGRAM_LEN(settings->parameters[5228]);
    settings->w_origin_offset = USER_TO_PROGRAM_LEN(settings->parameters[5229]);
    settings->rotation_xy = settings->parameters[5230];

    rotate(&settings->current_x, &settings->current_y, -settings->rotation_xy);
    settings->current_x -= settings->origin_offset_x;
    settings->current_y -= settings->origin_offset_y;
    settings->current_z -= settings->origin_offset_z;
    settings->AA_current -= settings->AA_origin_offset;
    settings->BB_current -= settings->BB_origin_offset;
    settings->CC_current -= settings->CC_origin_offset;
    settings->u_current -= settings->u_origin_offset;
    settings->v_current -= settings->v_origin_offset;
    settings->w_current -= settings->w_origin_offset;

    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);

/*2*/ if (settings->plane != CANON_PLANE_XY) {
      SELECT_PLANE(CANON_PLANE_XY);
      settings->plane = CANON_PLANE_XY;
    }

/*3*/
    settings->distance_mode = MODE_ABSOLUTE;

/*4*/ settings->feed_mode = UNITS_PER_MINUTE;
    SET_FEED_MODE(0);
    settings->feed_rate = block->f_number;
    SET_FEED_RATE(0);

/*5*/ if (settings->feed_override != ON) {
      ENABLE_FEED_OVERRIDE();
      settings->feed_override = ON;
    }
    if (settings->speed_override != ON) {
      ENABLE_SPEED_OVERRIDE();
      settings->speed_override = ON;
    }

/*6*/
    settings->cutter_comp_side = OFF;
    settings->cutter_comp_firstmove = ON;

/*7*/ STOP_SPINDLE_TURNING();
    settings->spindle_turning = CANON_STOPPED;

    /* turn off FPR */
    SET_SPINDLE_MODE(0);

/*8*/ settings->motion_mode = G_1;

/*9*/ if (settings->mist == ON) {
      MIST_OFF();
      settings->mist = OFF;
    }
    if (settings->flood == ON) {
      FLOOD_OFF();
      settings->flood = OFF;
    }

    if (block->m_modes[4] == 30)
      PALLET_SHUTTLE();
    PROGRAM_END();
    if (_setup.percent_flag == ON && _setup.file_pointer) {
      line = _setup.linetext;
      for (;;) {                /* check for ending percent sign and comment if missing */
        if (fgets(line, LINELEN, _setup.file_pointer) == NULL) {
          enqueue_COMMENT("interpreter: percent sign missing from end of file");
          break;
        }
        length = strlen(line);
        if (length == (LINELEN - 1)) {       // line is too long. need to finish reading the line
          for (; fgetc(_setup.file_pointer) != '\n';);
          continue;
        }
        for (index = (length - 1);      // index set on last char
             (index >= 0) && (isspace(line[index])); index--);
        if (line[index] == '%') // found line with % at end
        {
          for (index--; (index >= 0) && (isspace(line[index])); index--);
          if (index == -1)      // found line with only percent sign
            break;
        }
      }
    }
    return INTERP_EXIT;
  } else
    ERS(NCE_BUG_CODE_NOT_M0_M1_M2_M30_M60);
  return INTERP_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r12
leaq -0x58(%rbp), %r15
leaq -0x50(%rbp), %r13
leaq -0x48(%rbp), %r8
movq %rdx, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0xc820
movsd (%r15), %xmm0
movsd (%r13), %xmm1
movq %r12, %rdi
movq %r14, %rsi
callq 0xb7c0
testl %eax, %eax
je 0x171d9
movl %eax, %r15d
movq 0x25dde(%rip), %rbx     # 0x3cf60
movslq 0xd670(%rbx), %rax
cmpq $0x30, %rax
jg 0x1793a
imulq $0x50, %rax, %rax
leaq (%rbx,%rax), %rdi
addq $0xc6d0, %rdi           # imm = 0xC6D0
leaq 0x198c4(%rip), %rsi     # 0x30a6d
pushq $0x50
popq %rdx
callq 0xb940
movslq 0xd670(%rbx), %rax
imulq $0x50, %rax, %rcx
xorl %edx, %edx
movb %dl, 0xc71f(%rbx,%rcx)
incl %eax
movl %eax, 0xd670(%rbx)
movb %dl, 0xc720(%rbx,%rcx)
jmp 0x1793a
movq %r14, %rdi
callq 0xb340
movl 0x1f0(%rbx), %eax
cmpl $0x3c, %eax
je 0x1791b
cmpl $0x1, %eax
je 0x17914
cmpl $0x2, %eax
je 0x1725a
cmpl $0x1e, %eax
je 0x1725a
testl %eax, %eax
je 0x17920
leaq 0x1b060(%rip), %rdi     # 0x32272
callq 0xb220
movq %r12, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0xb8e0
movq 0x25d35(%rip), %rbx     # 0x3cf60
leaq 0xc6d0(%rbx), %rdi
leaq 0x19834(%rip), %rsi     # 0x30a6d
pushq $0x50
popq %rdx
callq 0xb940
andw $0x0, 0xc71f(%rbx)
movl $0x1, 0xd670(%rbx)
pushq $0x5
jmp 0x17938
movsd 0x17a0(%r14), %xmm0
addsd 0x600(%r14), %xmm0
leaq 0x600(%r14), %r15
movsd %xmm0, 0x600(%r14)
movsd 0x17a8(%r14), %xmm0
leaq 0x608(%r14), %r12
addsd 0x608(%r14), %xmm0
movsd %xmm0, 0x608(%r14)
movsd 0x17b0(%r14), %xmm0
addsd 0x610(%r14), %xmm0
movsd %xmm0, 0x610(%r14)
movsd 0x10(%r14), %xmm0
movsd 0x28(%r14), %xmm1
addsd 0x8(%r14), %xmm0
movsd %xmm0, 0x8(%r14)
addsd 0x20(%r14), %xmm1
movsd %xmm1, 0x20(%r14)
movsd 0x40(%r14), %xmm0
addsd 0x38(%r14), %xmm0
movsd %xmm0, 0x38(%r14)
movsd 0x58(%r14), %xmm0
addsd 0x50(%r14), %xmm0
movsd %xmm0, 0x50(%r14)
movsd 0x70(%r14), %xmm0
addsd 0x68(%r14), %xmm0
movsd %xmm0, 0x68(%r14)
movsd 0x88(%r14), %xmm0
addsd 0x80(%r14), %xmm0
movsd %xmm0, 0x80(%r14)
movsd 0x17b8(%r14), %xmm0
movq %r15, %rsi
movq %r12, %rdx
callq 0xc300
movl $0x1, 0x179c(%r14)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xbae0(%r14)
movsd 0xbae8(%r14), %xmm0
movsd %xmm0, -0x38(%rbp)
callq 0xba40
movq 0x25be8(%rip), %r13     # 0x3cf60
movl 0x1690(%r13), %eax
movsd 0x18b39(%rip), %xmm1    # 0x2fec0
movsd %xmm1, -0x40(%rbp)
cmpl $0x3, %eax
je 0x17399
movsd 0x18b07(%rip), %xmm1    # 0x2fea0
movsd -0x38(%rbp), %xmm2
divsd %xmm0, %xmm2
movsd 0x18b1e(%rip), %xmm0    # 0x2fec8
movsd %xmm0, -0x38(%rbp)
cmpl $0x1, %eax
je 0x173b8
movapd %xmm1, %xmm0
divsd %xmm0, %xmm2
movsd %xmm2, 0x17a0(%r14)
movsd 0xbaf0(%r14), %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0xba40
movl 0x1690(%r13), %eax
movsd 0x18ad9(%rip), %xmm1    # 0x2fec0
cmpl $0x3, %eax
je 0x173f4
movsd 0x18aac(%rip), %xmm1    # 0x2fea0
movsd -0x30(%rbp), %xmm2
divsd %xmm0, %xmm2
movsd 0x18ac3(%rip), %xmm0    # 0x2fec8
cmpl $0x1, %eax
je 0x1740e
movapd %xmm1, %xmm0
divsd %xmm0, %xmm2
movsd %xmm2, 0x17a8(%r14)
movsd 0xbaf8(%r14), %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0xba40
movl 0x1690(%r13), %eax
movsd 0x18a83(%rip), %xmm1    # 0x2fec0
cmpl $0x3, %eax
je 0x1744a
movsd 0x18a56(%rip), %xmm1    # 0x2fea0
movsd -0x30(%rbp), %xmm2
divsd %xmm0, %xmm2
movsd 0x18a6d(%rip), %xmm0    # 0x2fec8
cmpl $0x1, %eax
je 0x17464
movapd %xmm1, %xmm0
divsd %xmm0, %xmm2
movsd %xmm2, 0x17b0(%r14)
movsd 0xbb00(%r14), %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0xc090
movsd -0x30(%rbp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%r14)
movsd 0xbb08(%r14), %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0xc090
movsd -0x30(%rbp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x28(%r14)
movsd 0xbb10(%r14), %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0xc090
movsd -0x30(%rbp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x40(%r14)
movsd 0xbb18(%r14), %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0xba40
movl 0x1690(%r13), %eax
movsd 0x189c7(%rip), %xmm1    # 0x2fec0
cmpl $0x3, %eax
je 0x17506
movsd 0x1899a(%rip), %xmm1    # 0x2fea0
movsd -0x30(%rbp), %xmm2
divsd %xmm0, %xmm2
movsd 0x189b1(%rip), %xmm0    # 0x2fec8
cmpl $0x1, %eax
je 0x17520
movapd %xmm1, %xmm0
divsd %xmm0, %xmm2
movsd %xmm2, 0x58(%r14)
movsd 0xbb20(%r14), %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0xba40
movl 0x1690(%r13), %eax
movsd 0x18974(%rip), %xmm1    # 0x2fec0
cmpl $0x3, %eax
je 0x17559
movsd 0x18947(%rip), %xmm1    # 0x2fea0
movsd -0x30(%rbp), %xmm2
divsd %xmm0, %xmm2
movsd 0x1895e(%rip), %xmm0    # 0x2fec8
cmpl $0x1, %eax
je 0x17573
movapd %xmm1, %xmm0
divsd %xmm0, %xmm2
movsd %xmm2, 0x70(%r14)
movsd 0xbb28(%r14), %xmm0
movsd %xmm0, -0x30(%rbp)
callq 0xba40
movl 0x1690(%r13), %eax
cmpl $0x3, %eax
je 0x175a9
movsd 0x188fc(%rip), %xmm1    # 0x2fea0
movsd %xmm1, -0x40(%rbp)
movsd -0x30(%rbp), %xmm1
divsd %xmm0, %xmm1
cmpl $0x1, %eax
je 0x175c1
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
divsd -0x38(%rbp), %xmm1
movsd %xmm1, 0x88(%r14)
movsd 0xbb30(%r14), %xmm0
movsd %xmm0, 0x17b8(%r14)
xorps 0x18928(%rip), %xmm0    # 0x2ff10
movq %r15, %rsi
movq %r12, %rdx
callq 0xc300
movsd 0x17a0(%r14), %xmm0
movsd 0x600(%r14), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x600(%r14)
movsd 0x17a8(%r14), %xmm1
movsd 0x608(%r14), %xmm2
subsd %xmm1, %xmm2
movsd %xmm2, 0x608(%r14)
movsd 0x17b0(%r14), %xmm2
movsd 0x610(%r14), %xmm3
subsd %xmm2, %xmm3
movsd %xmm3, 0x610(%r14)
movsd 0x8(%r14), %xmm4
movsd 0x10(%r14), %xmm3
subsd %xmm3, %xmm4
movsd %xmm4, 0x8(%r14)
movsd 0x28(%r14), %xmm4
movsd 0x20(%r14), %xmm5
subsd %xmm4, %xmm5
movsd %xmm5, 0x20(%r14)
movsd 0x40(%r14), %xmm5
movsd 0x38(%r14), %xmm6
subsd %xmm5, %xmm6
movsd %xmm6, 0x38(%r14)
movsd 0x58(%r14), %xmm6
movsd 0x50(%r14), %xmm7
subsd %xmm6, %xmm7
movsd %xmm7, 0x50(%r14)
movsd 0x68(%r14), %xmm8
movsd 0x70(%r14), %xmm7
subsd %xmm7, %xmm8
movsd %xmm8, 0x68(%r14)
movsd 0x80(%r14), %xmm8
movsd 0x88(%r14), %xmm9
subsd %xmm9, %xmm8
movsd %xmm8, 0x80(%r14)
addsd 0x118(%r14), %xmm0
addsd 0x120(%r14), %xmm1
addsd 0x128(%r14), %xmm2
addsd (%r14), %xmm3
addsd 0x18(%r14), %xmm4
addsd 0x30(%r14), %xmm5
addsd 0x48(%r14), %xmm6
addsd 0x60(%r14), %xmm7
addsd 0x78(%r14), %xmm9
movsd %xmm9, (%rsp)
callq 0xb550
movsd 0x17b8(%r14), %xmm0
callq 0xbf80
cmpl $0x1, 0xc67c(%r14)
je 0x17753
pushq $0x1
popq %rdi
callq 0xbcb0
movl $0x1, 0xc67c(%r14)
andl $0x0, 0x668(%r14)
andl $0x0, 0x670(%r14)
xorl %edi, %edi
callq 0xc340
movsd 0x148(%rbx), %xmm0
movsd %xmm0, 0x678(%r14)
xorps %xmm0, %xmm0
callq 0xbc60
cmpb $0x0, 0x674(%r14)
jne 0x1779a
callq 0xb5b0
movb $0x1, 0x674(%r14)
cmpb $0x0, 0xc6c8(%r14)
jne 0x177b1
callq 0xc830
movb $0x1, 0xc6c8(%r14)
andl $0x0, 0x624(%r14)
movb $0x1, 0xc689(%r14)
callq 0xbca0
movl $0x1, 0xc6cc(%r14)
xorps %xmm0, %xmm0
callq 0xbc70
movl $0xa, 0x1798(%r14)
cmpb $0x1, 0x1797(%r14)
jne 0x177fb
callq 0xb100
movb $0x0, 0x1797(%r14)
cmpb $0x1, 0x1688(%r14)
jne 0x17812
callq 0xb760
movb $0x0, 0x1688(%r14)
cmpl $0x1e, 0x1f0(%rbx)
jne 0x17820
callq 0xc700
callq 0xb860
movq 0x1680(%r13), %rdx
testq %rdx, %rdx
setne %al
andb 0xc678(%r13), %al
cmpb $0x1, %al
jne 0x17936
movl $0x1698, %ebx           # imm = 0x1698
addq 0x25713(%rip), %rbx     # 0x3cf60
movq %rbx, %rdi
movl $0xff, %esi
callq 0xc230
testq %rax, %rax
je 0x1792a
movq %rbx, %rdi
callq 0xb4e0
movq %rax, %r14
cmpl $0xfe, %r14d
jne 0x1788a
movq 0x1680(%r13), %rdi
callq 0xc210
cmpl $0xa, %eax
jne 0x17877
jmp 0x17908
movl %r14d, %r15d
movl %r14d, %r12d
sarl $0x1f, %r12d
andl %r14d, %r12d
leal -0x1(%r12), %eax
movl %eax, -0x38(%rbp)
decq %r15
testl %r14d, %r14d
jle 0x178c8
movl %r15d, %eax
movsbl 0x1698(%r13,%rax), %edi
callq 0xb0c0
decl %r14d
decq %r15
testl %eax, %eax
jne 0x178a2
leal 0x1(%r14), %r12d
jmp 0x178ce
movl -0x38(%rbp), %eax
movl %eax, %r14d
movslq %r14d, %rax
cmpb $0x25, 0x1698(%r13,%rax)
jne 0x17908
addl $-0x2, %r12d
movl %r12d, %r14d
testl %r14d, %r14d
js 0x17902
movl %r14d, %eax
movsbl 0x1698(%r13,%rax), %edi
callq 0xb0c0
decl %r14d
testl %eax, %eax
jne 0x178e3
jmp 0x17908
cmpl $-0x2, %r12d
jg 0x17936
movq 0x1680(%r13), %rdx
jmp 0x1784d
callq 0xba30
jmp 0x17925
callq 0xc700
callq 0xbd70
xorl %r15d, %r15d
jmp 0x1793a
leaq 0x1a90e(%rip), %rdi     # 0x3223f
callq 0xc4d0
pushq $0x1
popq %r15
movl %r15d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq