// File: /workspace/github_cpp_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, %r14
movq %rdx, %r12
movq %rdi, %r15
xorl %ebx, %ebx
cmpl $0x35, %esi
je 0xdb5f
cmpl $0x34, %esi
jne 0xde75
movb 0x270(%r12), %al
movb 0x280(%r12), %cl
cmpb $0x1, %al
jne 0xdbb8
testb %cl, %cl
je 0xdbbc
jmp 0xdc04
cmpl $0x34, 0x1798(%r14)
jne 0xdbc8
movq 0x4e8a0(%rip), %rcx     # 0x5c410
movq 0x4e891(%rip), %rsi     # 0x5c408
subq %rsi, %rcx
pushq $0x18
popq %rdi
movq %rcx, %rax
cqto
idivq %rdi
movl 0x4e895(%rip), %edx     # 0x5c420
cmpq %rdx, %rax
jae 0xdda1
leaq 0x23135(%rip), %rdi     # 0x30cd0
callq 0xb220
movl 0x4e87a(%rip), %edx     # 0x5c420
movq %r15, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0xb8e0
jmp 0xde43
testb %cl, %cl
je 0xdbd4
leaq 0x23013(%rip), %rsi     # 0x30bd6
jmp 0xde39
leaq 0x230de(%rip), %rsi     # 0x30cad
jmp 0xde39
movsd 0x210(%r12), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xdc04
movq 0x4e819(%rip), %rdx     # 0x5c408
cmpq 0x4e81a(%rip), %rdx     # 0x5c410
je 0xdc04
leaq 0x23014(%rip), %rsi     # 0x30c13
jmp 0xde39
movsd 0x210(%r12), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jb 0xdc34
movq 0x4e7e9(%rip), %rdx     # 0x5c408
cmpq 0x4e7ea(%rip), %rdx     # 0x5c410
je 0xdc34
leaq 0x23023(%rip), %rsi     # 0x30c52
jmp 0xde39
cmpl $0x0, 0x670(%r14)
je 0xde15
movq 0x4e7bf(%rip), %rdx     # 0x5c408
cmpq 0x4e7c0(%rip), %rdx     # 0x5c410
jne 0xdcc3
movsd 0x600(%r14), %xmm1
movsd %xmm1, -0x40(%rbp)
movsd 0x608(%r14), %xmm1
movsd %xmm1, -0x38(%rbp)
movsd 0x2222a(%rip), %xmm1    # 0x2fea0
movapd %xmm1, %xmm2
testb $0x1, %cl
jne 0xdc83
movapd %xmm0, %xmm2
testb %al, %al
jne 0xdca4
xorpd %xmm3, %xmm3
cmpnltsd %xmm0, %xmm3
movapd %xmm3, %xmm0
andnpd %xmm2, %xmm0
andpd %xmm1, %xmm3
orpd %xmm0, %xmm3
movapd %xmm3, %xmm1
leaq -0x40(%rbp), %rsi
movsd %xmm1, 0x10(%rsi)
movl $0x3, 0x4e769(%rip)     # 0x5c420
leaq 0x4e74a(%rip), %rdi     # 0x5c408
callq 0xb070
movl 0x1c8(%r12), %eax
cmpl $0x4, %eax
jl 0xdcd6
movl %eax, 0x4e74a(%rip)     # 0x5c420
cmpb $0x1, 0x270(%r12)
jne 0xdea5
cmpb $0x1, 0x280(%r12)
jne 0xdea5
leaq -0x38(%rbp), %r8
leaq -0x60(%rbp), %rax
leaq -0x68(%rbp), %r10
leaq -0x70(%rbp), %r11
leaq -0x78(%rbp), %r13
leaq -0x40(%rbp), %rcx
leaq -0x90(%rbp), %r9
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
pushq %rax
pushq %r10
pushq %r11
pushq %r13
leaq -0x80(%rbp), %rax
pushq %rax
leaq -0x88(%rbp), %rax
pushq %rax
callq 0xbb30
addq $0x30, %rsp
testl %eax, %eax
je 0xde86
movl %eax, %r15d
movq 0x2f215(%rip), %rbx     # 0x3cf60
movslq 0xd670(%rbx), %rax
cmpq $0x30, %rax
jg 0xdd99
imulq $0x50, %rax, %rax
leaq (%rbx,%rax), %rdi
addq $0xc6d0, %rdi           # imm = 0xC6D0
leaq 0x223d2(%rip), %rsi     # 0x30140
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)
movl %r15d, %ebx
jmp 0xde75
movsd -0x18(%rsi,%rcx), %xmm0
movsd %xmm0, 0x600(%r14)
movsd -0x10(%rsi,%rcx), %xmm0
movsd %xmm0, 0x608(%r14)
movl 0x1d0(%r12), %r15d
leaq 0x4e63a(%rip), %rsi     # 0x5c408
leaq -0x58(%rbp), %r12
movq %r12, %rdi
callq 0xb620
movl 0x4e640(%rip), %edx     # 0x5c420
movl %r15d, %edi
movq %r12, %rsi
callq 0xb470
leaq -0x58(%rbp), %rdi
callq 0xb180
movq 0x4e60d(%rip), %rax     # 0x5c408
cmpq %rax, 0x4e60e(%rip)     # 0x5c410
je 0xde0b
movq %rax, 0x4e605(%rip)     # 0x5c410
orl $-0x1, 0x1798(%r14)
jmp 0xde75
movsd 0x678(%r14), %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jne 0xdc42
jp 0xdc42
leaq 0x22e50(%rip), %rsi     # 0x30c89
movq %r15, %rdi
xorl %eax, %eax
callq 0xb8e0
movq 0x2f116(%rip), %rbx     # 0x3cf60
leaq 0xc6d0(%rbx), %rdi
leaq 0x222e8(%rip), %rsi     # 0x30140
pushq $0x50
popq %rdx
callq 0xb940
andw $0x0, 0xc71f(%rbx)
movl $0x1, 0xd670(%rbx)
pushq $0x5
popq %rbx
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsd 0x210(%r12), %xmm0
leaq -0x40(%rbp), %rsi
movsd %xmm0, 0x10(%rsi)
leaq 0x4e568(%rip), %rdi     # 0x5c408
callq 0xb070
movl $0x34, 0x1798(%r14)
jmp 0xde75
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0xb180
movq %rbx, %rdi
callq 0xc520