// File: /workspace/github_cpp_makefile_star2_O3/sundale[P]gcode/rs274ngc/interp_queue.cc
// Function: Interp::move_endpoint_and_flush(setup_struct*, double, double)
int Interp::move_endpoint_and_flush(setup_pointer settings, double x, double y) {
    double x1;
    double y1;
    double x2;
    double y2;
    double dot;

    if(qc().empty()) return 0;
    
    for(unsigned int i = 0; i<qc().size(); i++) {
        // there may be several moves in the queue, and we need to
        // change all of them.  consider moving into a concave corner,
        // then up and back down, then continuing on.  there will be
        // three moves to change.

        queued_canon &q = qc()[i];

        switch(q.type) {
        case QARC_FEED:
            double r1, r2, l1, l2;
            r1 = hypot(q.data.arc_feed.end1 - q.data.arc_feed.center1,
                       q.data.arc_feed.end2 - q.data.arc_feed.center2);
            l1 = q.data.arc_feed.original_turns;
            q.data.arc_feed.end1 = x;
            q.data.arc_feed.end2 = y;
            r2 = hypot(x - q.data.arc_feed.center1,
                       y - q.data.arc_feed.center2);
            l2 = find_turn(endpoint[0], endpoint[1],
                           q.data.arc_feed.center1, q.data.arc_feed.center2,
                           q.data.arc_feed.turn,
                           x, y);
            if(debug_qc) printf("moving endpoint of arc lineno %d old sweep %f new speed %f\n", q.data.arc_feed.line_number, l1, l2);

            if(fabs(r1-r2) > .01) 
                ERS(_("BUG: cutter compensation has generated an invalid arc with mismatched radii r1 %f r2 %f\n"), r1, r2);
            if(l1 && endpoint_valid && fabs(l2) > fabs(l1) + (settings->length_units == CANON_UNITS_MM? .0254 : .001)) {
                ERS(_("Arc move in concave corner cannot be reached by the tool without gouging"));
            }
            q.data.arc_feed.end1 = x;
            q.data.arc_feed.end2 = y;
            break;
        case QSTRAIGHT_TRAVERSE:
            switch(settings->plane) {
            case CANON_PLANE_XY:
                x1 = q.data.straight_traverse.dx; // direction of original motion
                y1 = q.data.straight_traverse.dy;                
                x2 = x - endpoint[0];         // new direction after clipping
                y2 = y - endpoint[1];
                break;
            case CANON_PLANE_XZ:
                x1 = q.data.straight_traverse.dz; // direction of original motion
                y1 = q.data.straight_traverse.dx;                
                x2 = x - endpoint[0];         // new direction after clipping
                y2 = y - endpoint[1];
                break;
            default:
                ERS(_("BUG: Unsupported plane in cutter compensation"));
            }
            
            dot = x1 * x2 + y1 * y2; // not normalized; we only care about the angle
            if(debug_qc) printf("moving endpoint of traverse old dir %f new dir %f dot %f endpoint_valid %d\n", atan2(y1,x1), atan2(y2,x2), dot, endpoint_valid);

            if(endpoint_valid && dot<0) {
                // oops, the move is the wrong way.  this means the
                // path has crossed because we backed up further
                // than the line is long.  this will gouge.
                ERS(_("Straight traverse in concave corner cannot be reached by the tool without gouging"));
            }
            switch(settings->plane) {
            case CANON_PLANE_XY:
                q.data.straight_traverse.x = x;
                q.data.straight_traverse.y = y;
                break;
            case CANON_PLANE_XZ:
                q.data.straight_traverse.z = x;
                q.data.straight_traverse.x = y;
                break;
            }
            break;
        case QSTRAIGHT_FEED: 
            switch(settings->plane) {
            case CANON_PLANE_XY:
                x1 = q.data.straight_feed.dx; // direction of original motion
                y1 = q.data.straight_feed.dy;                
                x2 = x - endpoint[0];         // new direction after clipping
                y2 = y - endpoint[1];
                break;
            case CANON_PLANE_XZ:
                x1 = q.data.straight_feed.dz; // direction of original motion
                y1 = q.data.straight_feed.dx;                
                x2 = x - endpoint[0];         // new direction after clipping
                y2 = y - endpoint[1];
                break;
            default:
                ERS(_("BUG: Unsupported plane [%d] in cutter compensation"),
			settings->plane);
            }

            dot = x1 * x2 + y1 * y2;
            if(debug_qc) printf("moving endpoint of feed old dir %f new dir %f dot %f endpoint_valid %d\n", atan2(y1,x1), atan2(y2,x2), dot, endpoint_valid);

            if(endpoint_valid && dot<0) {
                // oops, the move is the wrong way.  this means the
                // path has crossed because we backed up further
                // than the line is long.  this will gouge.
                ERS(_("Straight feed in concave corner cannot be reached by the tool without gouging"));
            }
            switch(settings->plane) {
            case CANON_PLANE_XY:
                q.data.straight_feed.x = x;
                q.data.straight_feed.y = y;
                break;
            case CANON_PLANE_XZ:
                q.data.straight_feed.z = x;
                q.data.straight_feed.x = y;
                break;
            }
            break;
        default:
            // other things are not moves - we don't have to mess with them.
            ;
        }
    }
    dequeue_canons(settings);
    set_endpoint(x, y);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movsd %xmm1, -0x38(%rbp)
movsd %xmm0, -0x30(%rbp)
movq %rsi, %r15
movq %rdi, %rbx
callq 0xa210
movq 0x463e7(%rip), %rcx     # 0x60370
xorl %eax, %eax
cmpq 0x463e6(%rip), %rcx     # 0x60378
je 0x1a321
callq 0xa210
movq 0x463d4(%rip), %rax     # 0x60378
cmpq 0x463c5(%rip), %rax     # 0x60370
je 0x1a23f
movl $0x1, %r12d
xorl %r14d, %r14d
movabsq $-0x1111111111111111, %r13 # imm = 0xEEEEEEEEEEEEEEEF
callq 0xa210
movq 0x463a0(%rip), %rax     # 0x60370
imulq $0x78, %r14, %rcx
leaq (%rax,%rcx), %r14
movl (%rax,%rcx), %eax
testl %eax, %eax
je 0x1a125
cmpl $0x1, %eax
je 0x1a104
cmpl $0x2, %eax
jne 0x1a215
movsd 0x18(%r14), %xmm0
movsd 0x20(%r14), %xmm1
subsd 0x28(%r14), %xmm0
subsd 0x30(%r14), %xmm1
callq 0xa290
movsd 0x10(%r14), %xmm1
movaps %xmm1, -0x60(%rbp)
movsd -0x30(%rbp), %xmm2
movsd %xmm2, 0x18(%r14)
movsd -0x38(%rbp), %xmm1
movsd %xmm1, 0x20(%r14)
subsd 0x28(%r14), %xmm2
movsd %xmm0, -0x48(%rbp)
subsd 0x30(%r14), %xmm1
movapd %xmm2, %xmm0
callq 0xa290
movsd %xmm0, -0x40(%rbp)
movsd 0x46347(%rip), %xmm0    # 0x603a0
movsd 0x4634f(%rip), %xmm1    # 0x603b0
movsd 0x28(%r14), %xmm2
movsd 0x30(%r14), %xmm3
movl 0x38(%r14), %esi
movq %rbx, %rdi
movsd -0x30(%rbp), %xmm4
movsd -0x38(%rbp), %xmm5
callq 0xab90
movsd -0x48(%rbp), %xmm1
subsd -0x40(%rbp), %xmm1
andpd 0x17f8b(%rip), %xmm1    # 0x32020
ucomisd 0x1c7a3(%rip), %xmm1    # 0x36840
ja 0x1a26f
movapd -0x60(%rbp), %xmm2
ucomisd 0x19e20(%rip), %xmm2    # 0x33ed0
jne 0x1a0b4
jnp 0x1a0f0
testb $0x1, 0x462d5(%rip)     # 0x60390
je 0x1a0f0
movapd 0x17f5b(%rip), %xmm1    # 0x32020
andpd %xmm1, %xmm0
andpd %xmm1, %xmm2
xorl %eax, %eax
cmpl $0x2, 0x1690(%r15)
sete %al
leaq 0x1c74f(%rip), %rcx     # 0x36830
addsd (%rcx,%rax,8), %xmm2
ucomisd %xmm2, %xmm0
ja 0x1a2d1
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x18(%r14)
addq $0x20, %r14
jmp 0x1a20b
movl 0xc67c(%r15), %eax
cmpl $0x1, %eax
je 0x1a146
cmpl $0x3, %eax
jne 0x1a2a6
movl $0x10, %edx
movl $0x20, %ecx
jmp 0x1a150
movl 0xc67c(%r15), %eax
cmpl $0x1, %eax
je 0x1a196
cmpl $0x3, %eax
jne 0x1a2c8
movl $0x10, %edx
movl $0x20, %ecx
jmp 0x1a1a0
movl $0x18, %edx
movl $0x10, %ecx
cmpb $0x1, 0x46239(%rip)     # 0x60390
jne 0x1a1e1
movsd -0x30(%rbp), %xmm0
subsd 0x46236(%rip), %xmm0    # 0x603a0
movsd -0x38(%rbp), %xmm1
subsd 0x46239(%rip), %xmm1    # 0x603b0
mulsd (%r14,%rdx), %xmm1
mulsd (%r14,%rcx), %xmm0
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x1a1e1
jmp 0x1a29d
movl $0x18, %edx
movl $0x10, %ecx
cmpb $0x1, 0x461e9(%rip)     # 0x60390
jne 0x1a1e1
movsd -0x30(%rbp), %xmm0
subsd 0x461ea(%rip), %xmm0    # 0x603a0
movsd -0x38(%rbp), %xmm1
subsd 0x461ed(%rip), %xmm1    # 0x603b0
mulsd (%r14,%rdx), %xmm1
mulsd (%r14,%rcx), %xmm0
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x1a294
cmpl $0x3, %eax
je 0x1a1fc
cmpl $0x1, %eax
jne 0x1a215
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x28(%r14)
addq $0x30, %r14
jmp 0x1a20b
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x38(%r14)
addq $0x28, %r14
movsd -0x38(%rbp), %xmm0
movsd %xmm0, (%r14)
movl %r12d, %r14d
callq 0xa210
movq 0x46154(%rip), %rax     # 0x60378
subq 0x46145(%rip), %rax     # 0x60370
sarq $0x3, %rax
imulq %r13, %rax
incl %r12d
cmpq %r14, %rax
ja 0x19fc4
movq %r15, %rdi
callq 0xa300
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x4614c(%rip)    # 0x603a0
movsd -0x38(%rbp), %xmm0
movsd %xmm0, 0x4614f(%rip)    # 0x603b0
movb $0x1, 0x46128(%rip)     # 0x60390
xorl %eax, %eax
jmp 0x1a321
leaq 0x1c5d2(%rip), %rdi     # 0x36848
callq 0xa1f0
movq %rbx, %rdi
movq %rax, %rsi
movsd -0x48(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
movb $0x2, %al
callq 0xa850
jmp 0x1a2ea
leaq 0x1c634(%rip), %rdi     # 0x368cf
jmp 0x1a2d8
leaq 0x1c6b0(%rip), %rdi     # 0x36954
jmp 0x1a2d8
leaq 0x1c674(%rip), %rdi     # 0x36921
callq 0xa1f0
movl 0xc67c(%r15), %edx
movq %rbx, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0xa850
jmp 0x1a2ea
leaq 0x1c5d2(%rip), %rdi     # 0x368a1
jmp 0x1a2d8
leaq 0x1afbb(%rip), %rdi     # 0x35293
callq 0xa1f0
movq %rbx, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0xa850
movq 0x26c6f(%rip), %rbx     # 0x40f60
leaq 0xc6d0(%rbx), %rdi
leaq 0x1c4d1(%rip), %rsi     # 0x367d0
movl $0x50, %edx
callq 0xa8a0
movw $0x0, 0xc71f(%rbx)
movl $0x1, 0xd670(%rbx)
movl $0x5, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq