// File: sundale[P]gcode/rs274ngc/interp_convert.cc
// Function: Interp::convert_straight_comp1(int, block_struct*, setup_struct*, double, double, double, double, double, double, double, double, double)
int Interp::convert_straight_comp1(int move,     //!< either G_0 or G_1                        
                                   block_pointer block,  //!< pointer to a block of RS274 instructions 
                                   setup_pointer settings,       //!< pointer to machine settings              
                                   double px,    //!< X coordinate of end point                
                                   double py,    //!< Y coordinate of end point                
                                   double pz,    //!< Z coordinate of end point                
                                   double AA_end,        //!< A coordinate of end point          
                                   double BB_end,        //!< B coordinate of end point          
                                   double CC_end,        //!< C coordinate of end point          
                                   double u_end, double v_end, double w_end)
{
    double alpha;
    double distance;
    double radius = settings->cutter_comp_radius; /* always will be positive */
    double end_x, end_y;

    int side = settings->cutter_comp_side;
    double cx, cy, cz;

    comp_get_current(settings, &cx, &cy, &cz);
    distance = hypot((px - cx), (py - cy));

    CHKS(((side != LEFT) && (side != RIGHT)), NCE_BUG_SIDE_NOT_RIGHT_OR_LEFT);
    CHKS((distance <= radius), "Length of cutter compensation entry move is not greater than the tool radius");

    alpha = atan2(py - cy, px - cx) + (side == LEFT ? M_PIl/2. : -M_PIl/2.);

    end_x = (px + (radius * cos(alpha)));
    end_y = (py + (radius * sin(alpha)));

    // with these moves we don't need to record the direction vector.
    // they cannot get reversed because they are guaranteed to be long
    // enough.

    set_endpoint(cx, cy);

    if (move == G_0) {
        enqueue_STRAIGHT_TRAVERSE(settings, block->line_number, 
                                  cos(alpha), sin(alpha), 0, 
                                  end_x, end_y, pz,
                                  AA_end, BB_end, CC_end, u_end, v_end, w_end);
    }
    else if (move == G_1) {
        enqueue_STRAIGHT_FEED(settings, block->line_number, 
                              cos(alpha), sin(alpha), 0,
                              end_x, end_y, pz,
                              AA_end, BB_end, CC_end, u_end, v_end, w_end);
    } else
        ERS(NCE_BUG_CODE_NOT_G0_OR_G1);

    settings->cutter_comp_firstmove = OFF;

    comp_set_current(settings, end_x, end_y, pz);
    settings->AA_current = AA_end;
    settings->BB_current = BB_end;
    settings->CC_current = CC_end;
    settings->u_current = u_end;
    settings->v_current = v_end;
    settings->w_current = w_end;
    comp_set_programmed(settings, px, py, pz);
    return INTERP_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movsd 0x10(%rbp), %xmm8
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movsd %xmm0, -0x30(%rbp)
movsd %xmm1, -0x38(%rbp)
movsd %xmm2, -0x40(%rbp)
movsd %xmm3, -0x48(%rbp)
movsd %xmm4, -0x50(%rbp)
movsd %xmm5, -0x58(%rbp)
movsd %xmm6, -0x60(%rbp)
movsd %xmm7, -0x68(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
movq -0x28(%rbp), %rax
movsd 0x618(%rax), %xmm0
movsd %xmm0, -0x80(%rbp)
movq -0x28(%rbp), %rax
movl 0x624(%rax), %eax
movl %eax, -0x94(%rbp)
movq -0x28(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
leaq -0xa8(%rbp), %rcx
leaq -0xb0(%rbp), %r8
callq 0x12360
movsd -0x30(%rbp), %xmm0
subsd -0xa0(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
subsd -0xa8(%rbp), %xmm1
callq 0x10460
movsd %xmm0, -0x78(%rbp)
cmpl $0x2, -0x94(%rbp)
je 0x30108
cmpl $0x1, -0x94(%rbp)
je 0x30108
jmp 0x30032
leaq 0x4b0fd(%rip), %rdi     # 0x7b136
callq 0x10380
movq -0xc8(%rbp), %rdi
movq %rax, %rsi
movb $0x0, %al
callq 0x10df0
movq 0x56f0a(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x56ef9(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x56eeb(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0x4b0c7(%rip), %rsi     # 0x7b151
movl $0x50, %edx
callq 0x10840
movq 0x56ec5(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x56eb7(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x56e9f(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x56e8f(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x56e82(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x56e74(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x4(%rbp)
jmp 0x305f2
jmp 0x3010a
jmp 0x3010c
movsd -0x78(%rbp), %xmm1
movsd -0x80(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x301f0
jmp 0x30122
movq -0xc8(%rbp), %rdi
leaq 0x4b0af(%rip), %rsi     # 0x7b1df
movb $0x0, %al
callq 0x10df0
movq 0x56e22(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x56e11(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x56e03(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0x4afdf(%rip), %rsi     # 0x7b151
movl $0x50, %edx
callq 0x10840
movq 0x56ddd(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x56dcf(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x56db7(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x56da7(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x56d9a(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x56d8c(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x4(%rbp)
jmp 0x305f2
jmp 0x301f2
movsd -0x38(%rbp), %xmm0
movsd -0xa8(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd -0x30(%rbp), %xmm1
movsd -0xa0(%rbp), %xmm2
subsd %xmm2, %xmm1
callq 0x101d0
movsd %xmm0, -0xb8(%rbp)
fldl -0xb8(%rbp)
movl -0x94(%rbp), %eax
subl $0x2, %eax
sete %al
movzbl %al, %eax
shll $0x4, %eax
leaq 0x48d90(%rip), %rcx     # 0x78fd0
fldt (%rax,%rcx)
faddp %st, %st(1)
fstpl -0xc0(%rbp)
movsd -0xc0(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0xe8(%rbp)
movsd -0x80(%rbp), %xmm0
movsd %xmm0, -0xe0(%rbp)
movsd -0x70(%rbp), %xmm0
callq 0x12160
movsd -0xe8(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0xe0(%rbp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0xd8(%rbp)
movsd -0x80(%rbp), %xmm0
movsd %xmm0, -0xd0(%rbp)
movsd -0x70(%rbp), %xmm0
callq 0x10330
movsd -0xd8(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0xd0(%rbp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
movsd -0xa0(%rbp), %xmm0
movsd -0xa8(%rbp), %xmm1
callq 0x11060
cmpl $0x0, -0x14(%rbp)
jne 0x303b3
movq -0x28(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x20(%rbp), %rax
movl 0x1d0(%rax), %eax
movl %eax, -0xf4(%rbp)
movsd -0x70(%rbp), %xmm0
callq 0x12160
movsd %xmm0, -0xf0(%rbp)
movsd -0x70(%rbp), %xmm0
callq 0x10330
movq -0x100(%rbp), %rdi
movl -0xf4(%rbp), %esi
movaps %xmm0, %xmm1
movsd -0xf0(%rbp), %xmm0
movsd -0x88(%rbp), %xmm3
movsd -0x90(%rbp), %xmm4
movsd -0x40(%rbp), %xmm5
movsd -0x48(%rbp), %xmm6
movsd -0x50(%rbp), %xmm7
movsd -0x58(%rbp), %xmm11
movsd -0x60(%rbp), %xmm10
movsd -0x68(%rbp), %xmm9
movsd 0x10(%rbp), %xmm8
xorps %xmm2, %xmm2
movsd %xmm11, (%rsp)
movsd %xmm10, 0x8(%rsp)
movsd %xmm9, 0x10(%rsp)
movsd %xmm8, 0x18(%rsp)
callq 0x123e8
jmp 0x30545
cmpl $0xa, -0x14(%rbp)
jne 0x3046b
movq -0x28(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x20(%rbp), %rax
movl 0x1d0(%rax), %eax
movl %eax, -0x10c(%rbp)
movsd -0x70(%rbp), %xmm0
callq 0x12160
movsd %xmm0, -0x108(%rbp)
movsd -0x70(%rbp), %xmm0
callq 0x10330
movq -0x118(%rbp), %rdi
movl -0x10c(%rbp), %esi
movaps %xmm0, %xmm1
movsd -0x108(%rbp), %xmm0
movsd -0x88(%rbp), %xmm3
movsd -0x90(%rbp), %xmm4
movsd -0x40(%rbp), %xmm5
movsd -0x48(%rbp), %xmm6
movsd -0x50(%rbp), %xmm7
movsd -0x58(%rbp), %xmm11
movsd -0x60(%rbp), %xmm10
movsd -0x68(%rbp), %xmm9
movsd 0x10(%rbp), %xmm8
xorps %xmm2, %xmm2
movsd %xmm11, (%rsp)
movsd %xmm10, 0x8(%rsp)
movsd %xmm9, 0x10(%rsp)
movsd %xmm8, 0x18(%rsp)
callq 0x123d0
jmp 0x30543
jmp 0x3046d
leaq 0x4abb6(%rip), %rdi     # 0x7b02a
callq 0x10380
movq -0xc8(%rbp), %rdi
movq %rax, %rsi
movb $0x0, %al
callq 0x10df0
movq 0x56acf(%rip), %rax     # 0x86f60
movl $0x0, 0xd670(%rax)
movq 0x56abe(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rax
movq 0x56ab0(%rip), %rdi     # 0x86f60
addq $0xc6d0, %rdi           # imm = 0xC6D0
imulq $0x50, %rax, %rax
addq %rax, %rdi
leaq 0x4ac8c(%rip), %rsi     # 0x7b151
movl $0x50, %edx
callq 0x10840
movq 0x56a8a(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x56a7c(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, 0x4f(%rax)
movq 0x56a64(%rip), %rax     # 0x86f60
movl 0xd670(%rax), %ecx
addl $0x1, %ecx
movq 0x56a54(%rip), %rax     # 0x86f60
movl %ecx, 0xd670(%rax)
movq 0x56a47(%rip), %rax     # 0x86f60
movslq 0xd670(%rax), %rcx
movq 0x56a39(%rip), %rax     # 0x86f60
addq $0xc6d0, %rax           # imm = 0xC6D0
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movl $0x5, -0x4(%rbp)
jmp 0x305f2
jmp 0x30545
movq -0xc8(%rbp), %rdi
movq -0x28(%rbp), %rax
movb $0x0, 0xc689(%rax)
movq -0x28(%rbp), %rsi
movsd -0x88(%rbp), %xmm0
movsd -0x90(%rbp), %xmm1
movsd -0x40(%rbp), %xmm2
callq 0x11c70
movq -0xc8(%rbp), %rdi
movsd -0x48(%rbp), %xmm0
movq -0x28(%rbp), %rax
movsd %xmm0, 0x8(%rax)
movsd -0x50(%rbp), %xmm0
movq -0x28(%rbp), %rax
movsd %xmm0, 0x20(%rax)
movsd -0x58(%rbp), %xmm0
movq -0x28(%rbp), %rax
movsd %xmm0, 0x38(%rax)
movsd -0x60(%rbp), %xmm0
movq -0x28(%rbp), %rax
movsd %xmm0, 0x50(%rax)
movsd -0x68(%rbp), %xmm0
movq -0x28(%rbp), %rax
movsd %xmm0, 0x68(%rax)
movsd 0x10(%rbp), %xmm0
movq -0x28(%rbp), %rax
movsd %xmm0, 0x80(%rax)
movq -0x28(%rbp), %rsi
movsd -0x30(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
movsd -0x40(%rbp), %xmm2
callq 0x115b0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
nop