// File: sundale[P]gcode/rs274ngc/interp_convert.cc
// Function: Interp::convert_nurbs(int, block_struct*, setup_struct*)
int Interp::convert_nurbs(int mode,
      block_pointer block,     //!< pointer to a block of RS274 instructions
      setup_pointer settings)  //!< pointer to machine settings
{
    double end_z, AA_end, BB_end, CC_end, u_end, v_end, w_end;
    CONTROL_POINT CP;   

    if (mode == G_5_2)  {
	CHKS((((block->x_flag) && !(block->y_flag)) || (!(block->x_flag) && (block->y_flag))), (
             "You must specify both X and Y coordinates for Control Points"));
	CHKS((!(block->x_flag) && !(block->y_flag) && (block->p_number > 0) && 
             (!nurbs_control_points.empty())), (
             "Can specify P without X and Y only for the first control point"));

        CHKS(((block->p_number <= 0) && (!nurbs_control_points.empty())), (
             "Must specify positive weight P for every Control Point"));
        if (settings->feed_mode == UNITS_PER_MINUTE) {
            CHKS((settings->feed_rate == 0.0), (
                 "Cannot make a NURBS with 0 feedrate"));
        }
        if (nurbs_control_points.empty()) {
            CP.X = settings->current_x;
            CP.Y = settings->current_y;
            if (!(block->x_flag) && !(block->y_flag) && (block->p_number > 0)) {
                CP.W = block->p_number;
            } else {
                CP.W = 1;
            }
            nurbs_order = 3;
            nurbs_control_points.push_back(CP);
        } 
        if (block->l_number != -1 && block->l_number > 3) {
            nurbs_order = block->l_number;  
        } 
        if ((block->x_flag) && (block->y_flag)) {
            CHP(find_ends(block, settings, &CP.X, &CP.Y, &end_z, &AA_end, &BB_end, &CC_end,
                          &u_end, &v_end, &w_end));
            CP.W = block->p_number;
            nurbs_control_points.push_back(CP);
            }

//for (i=0;i<nurbs_control_points.size();i++){
//                printf( "X %8.4f, Y %8.4f, W %8.4f\n",
//              nurbs_control_points[i].X,
//               nurbs_control_points[i].Y,
//               nurbs_control_points[i].W);
//       }
//        printf("*-----------------------------------------*\n");
        settings->motion_mode = mode;
    }
    
    else if (mode == G_5_3){
        CHKS((settings->motion_mode != G_5_2), (
             "Cannot use G5.3 without G5.2 first"));
        CHKS((nurbs_control_points.size()<nurbs_order), _("You must specify a number of control points at least equal to the order L = %d"), nurbs_order);
	settings->current_x = nurbs_control_points[nurbs_control_points.size()-1].X;
        settings->current_y = nurbs_control_points[nurbs_control_points.size()-1].Y;
        NURBS_FEED(block->line_number, nurbs_control_points, nurbs_order);
	//printf("hello\n");
	nurbs_control_points.clear();
	//printf("%d\n", 	nurbs_control_points.size());
	settings->motion_mode = -1;
    }
    return INTERP_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0xf8, %rsp
movq %rdi, -0x28(%rbp)
movl %esi, -0x2c(%rbp)
movq %rdx, -0x38(%rbp)
movq %rcx, -0x40(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0xc8(%rbp)
cmpl $0x34, -0x2c(%rbp)
jne 0x1683b
jmp 0x161a6
movq -0x38(%rbp), %rax
testb $0x1, 0x270(%rax)
je 0x161c0
movq -0x38(%rbp), %rax
testb $0x1, 0x280(%rax)
je 0x161e2
movq -0x38(%rbp), %rax
testb $0x1, 0x270(%rax)
jne 0x162b2
movq -0x38(%rbp), %rax
testb $0x1, 0x280(%rax)
je 0x162b2
jmp 0x161e4
movq -0xc8(%rbp), %rdi
leaq 0x62fc6(%rip), %rsi     # 0x791b8
movb $0x0, %al
callq 0x10df0
movq 0x70d60(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x70d4f(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x70d41(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0x62fc1(%rip), %rsi     # 0x791f5
movl $0x50, %edx
callq 0x10840
movq 0x70d1b(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x70d0d(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x70cf5(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x70ce5(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x70cd8(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x70cca(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x1c(%rbp)
jmp 0x16b20
jmp 0x162b4
jmp 0x162b6
movq -0x38(%rbp), %rax
testb $0x1, 0x270(%rax)
jne 0x163d5
movq -0x38(%rbp), %rax
testb $0x1, 0x280(%rax)
jne 0x163d5
movq -0x38(%rbp), %rax
movsd 0x210(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x163d5
leaq 0x906d0(%rip), %rdi     # 0xa69c8
callq 0x10c10
testb $0x1, %al
jne 0x163d5
jmp 0x16307
movq -0xc8(%rbp), %rdi
leaq 0x62f1d(%rip), %rsi     # 0x79232
movb $0x0, %al
callq 0x10df0
movq 0x70c3d(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x70c2c(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x70c1e(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0x62e9e(%rip), %rsi     # 0x791f5
movl $0x50, %edx
callq 0x10840
movq 0x70bf8(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x70bea(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x70bd2(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x70bc2(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x70bb5(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x70ba7(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x1c(%rbp)
jmp 0x16b20
jmp 0x163d7
jmp 0x163d9
movq -0x38(%rbp), %rax
xorps %xmm0, %xmm0
ucomisd 0x210(%rax), %xmm0
jb 0x164d2
leaq 0x905d3(%rip), %rdi     # 0xa69c8
callq 0x10c10
testb $0x1, %al
jne 0x164d2
jmp 0x16404
movq -0xc8(%rbp), %rdi
leaq 0x62e5f(%rip), %rsi     # 0x79271
movb $0x0, %al
callq 0x10df0
movq 0x70b40(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x70b2f(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x70b21(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0x62da1(%rip), %rsi     # 0x791f5
movl $0x50, %edx
callq 0x10840
movq 0x70afb(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x70aed(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x70ad5(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x70ac5(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x70ab8(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x70aaa(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x1c(%rbp)
jmp 0x16b20
jmp 0x164d4
movq -0x40(%rbp), %rax
cmpl $0x0, 0x670(%rax)
jne 0x165da
jmp 0x164e7
movq -0x40(%rbp), %rax
movsd 0x678(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x165d6
jp 0x165d6
jmp 0x16508
movq -0xc8(%rbp), %rdi
leaq 0x62d92(%rip), %rsi     # 0x792a8
movb $0x0, %al
callq 0x10df0
movq 0x70a3c(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x70a2b(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x70a1d(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0x62c9d(%rip), %rsi     # 0x791f5
movl $0x50, %edx
callq 0x10840
movq 0x709f7(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x709e9(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x709d1(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x709c1(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x709b4(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x709a6(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x1c(%rbp)
jmp 0x16b20
jmp 0x165d8
jmp 0x165da
leaq 0x903e7(%rip), %rdi     # 0xa69c8
callq 0x10c10
testb $0x1, %al
jne 0x165ef
jmp 0x16683
movq -0x40(%rbp), %rax
movsd 0x600(%rax), %xmm0
movsd %xmm0, -0x90(%rbp)
movq -0x40(%rbp), %rax
movsd 0x608(%rax), %xmm0
movsd %xmm0, -0x88(%rbp)
movq -0x38(%rbp), %rax
testb $0x1, 0x270(%rax)
jne 0x16659
movq -0x38(%rbp), %rax
testb $0x1, 0x280(%rax)
jne 0x16659
movq -0x38(%rbp), %rax
movsd 0x210(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x16659
movq -0x38(%rbp), %rax
movsd 0x210(%rax), %xmm0
movsd %xmm0, -0x80(%rbp)
jmp 0x16666
movsd 0x627af(%rip), %xmm0    # 0x78e10
movsd %xmm0, -0x80(%rbp)
movl $0x3, 0x90370(%rip)     # 0xa69e0
leaq 0x90351(%rip), %rdi     # 0xa69c8
leaq -0x90(%rbp), %rsi
callq 0x100a0
movq -0x38(%rbp), %rax
cmpl $-0x1, 0x1c8(%rax)
je 0x166ad
movq -0x38(%rbp), %rax
cmpl $0x3, 0x1c8(%rax)
jle 0x166ad
movq -0x38(%rbp), %rax
movl 0x1c8(%rax), %eax
movl %eax, 0x90333(%rip)     # 0xa69e0
movq -0x38(%rbp), %rax
testb $0x1, 0x270(%rax)
je 0x16829
movq -0x38(%rbp), %rax
testb $0x1, 0x280(%rax)
je 0x16829
jmp 0x166d1
movq -0xc8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
leaq -0x90(%rbp), %rcx
leaq -0x90(%rbp), %r8
addq $0x8, %r8
leaq -0x48(%rbp), %r9
leaq -0x50(%rbp), %r15
leaq -0x58(%rbp), %r14
leaq -0x60(%rbp), %rbx
leaq -0x68(%rbp), %r11
leaq -0x70(%rbp), %r10
leaq -0x78(%rbp), %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 0x11150
movl %eax, -0x94(%rbp)
cmpl $0x0, -0x94(%rbp)
je 0x16803
jmp 0x16745
movq 0x70814(%rip), %rax     # 0x86f60
cmpl $0x31, 0xd670(%rax)
jge 0x167f5
movq 0x70800(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x707f2(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0x62a72(%rip), %rsi     # 0x791f5
movl $0x50, %edx
callq 0x10840
movq 0x707cc(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x707be(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x707a6(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x70796(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x70789(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x7077b(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl -0x94(%rbp), %eax
movl %eax, -0x1c(%rbp)
jmp 0x16b20
jmp 0x16805
movq -0x38(%rbp), %rax
movsd 0x210(%rax), %xmm0
movsd %xmm0, -0x80(%rbp)
leaq 0x901ab(%rip), %rdi     # 0xa69c8
leaq -0x90(%rbp), %rsi
callq 0x100a0
movl -0x2c(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x1798(%rax)
jmp 0x16b19
cmpl $0x35, -0x2c(%rbp)
jne 0x16b17
jmp 0x16847
movq -0x40(%rbp), %rax
cmpl $0x34, 0x1798(%rax)
je 0x16928
jmp 0x1685a
movq -0xc8(%rbp), %rdi
leaq 0x62a64(%rip), %rsi     # 0x792cc
movb $0x0, %al
callq 0x10df0
movq 0x706ea(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x706d9(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x706cb(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0x6294b(%rip), %rsi     # 0x791f5
movl $0x50, %edx
callq 0x10840
movq 0x706a5(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x70697(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x7067f(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x7066f(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x70662(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x70654(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x1c(%rbp)
jmp 0x16b20
jmp 0x1692a
jmp 0x1692c
leaq 0x90095(%rip), %rdi     # 0xa69c8
callq 0x10bf0
movl 0x900a2(%rip), %ecx     # 0xa69e0
cmpq %rcx, %rax
jae 0x16a25
jmp 0x16949
leaq 0x6299f(%rip), %rdi     # 0x792ef
callq 0x10380
movq -0xc8(%rbp), %rdi
movq %rax, %rsi
movl 0x9007b(%rip), %edx     # 0xa69e0
movb $0x0, %al
callq 0x10df0
movq 0x705ed(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x705dc(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x705ce(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0x6284e(%rip), %rsi     # 0x791f5
movl $0x50, %edx
callq 0x10840
movq 0x705a8(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x7059a(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x70582(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x70572(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x70565(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x70557(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x1c(%rbp)
jmp 0x16b20
jmp 0x16a27
leaq 0x8ff9a(%rip), %rdi     # 0xa69c8
movq %rdi, -0xe0(%rbp)
callq 0x10bf0
movq -0xe0(%rbp), %rdi
movq %rax, %rsi
decq %rsi
callq 0x12250
movq -0xe0(%rbp), %rdi
movsd (%rax), %xmm0
movq -0x40(%rbp), %rax
movsd %xmm0, 0x600(%rax)
callq 0x10bf0
movq -0xe0(%rbp), %rdi
movq %rax, %rsi
decq %rsi
callq 0x12250
movq -0xe0(%rbp), %rsi
movsd 0x8(%rax), %xmm0
movq -0x40(%rbp), %rax
movsd %xmm0, 0x608(%rax)
movq -0x38(%rbp), %rax
movl 0x1d0(%rax), %eax
movl %eax, -0xd4(%rbp)
leaq -0xb0(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
callq 0x10970
movl -0xd4(%rbp), %edi
movq -0xd0(%rbp), %rsi
movl 0x8ff18(%rip), %edx     # 0xa69e0
callq 0x10690
jmp 0x16acf
leaq -0xb0(%rbp), %rdi
callq 0x123e0
leaq 0x8fee6(%rip), %rdi     # 0xa69c8
callq 0x10b30
movq -0x40(%rbp), %rax
movl $0xffffffff, 0x1798(%rax) # imm = 0xFFFFFFFF
jmp 0x16b17
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb8(%rbp)
movl %eax, -0xbc(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x123e0
jmp 0x16b31
jmp 0x16b19
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
addq $0xf8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq -0xb8(%rbp), %rdi
callq 0x11f50
nopl (%rax)