// File: /workspace/github_cpp_makefile_star2_O1/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 0x485ab(%rip), %rcx     # 0x63370
xorl %eax, %eax
cmpq 0x485aa(%rip), %rcx     # 0x63378
je 0x1b167
callq 0xa210
movq 0x48598(%rip), %rax     # 0x63378
cmpq 0x48589(%rip), %rax     # 0x63370
je 0x1b07b
movl $0x1, %r12d
xorl %r14d, %r14d
movabsq $-0x1111111111111111, %r13 # imm = 0xEEEEEEEEEEEEEEEF
callq 0xa210
movq 0x48564(%rip), %rax     # 0x63370
imulq $0x78, %r14, %rcx
leaq (%rax,%rcx), %r14
movl (%rax,%rcx), %eax
testl %eax, %eax
je 0x1af61
cmpl $0x1, %eax
je 0x1af40
cmpl $0x2, %eax
jne 0x1b051
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 0x4850b(%rip), %xmm0    # 0x633a0
movsd 0x48513(%rip), %xmm1    # 0x633b0
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 0x1a14f(%rip), %xmm1    # 0x35020
ucomisd 0x1e967(%rip), %xmm1    # 0x39840
ja 0x1b0ab
movapd -0x60(%rbp), %xmm2
ucomisd 0x1e95c(%rip), %xmm2    # 0x39848
jne 0x1aef0
jnp 0x1af2c
testb $0x1, 0x48499(%rip)     # 0x63390
je 0x1af2c
movapd 0x1a11f(%rip), %xmm1    # 0x35020
andpd %xmm1, %xmm0
andpd %xmm1, %xmm2
xorl %eax, %eax
cmpl $0x2, 0x1690(%r15)
sete %al
leaq 0x1e913(%rip), %rcx     # 0x39830
addsd (%rcx,%rax,8), %xmm2
ucomisd %xmm2, %xmm0
ja 0x1b10d
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x18(%r14)
addq $0x20, %r14
jmp 0x1b047
movl 0xc67c(%r15), %eax
cmpl $0x1, %eax
je 0x1af82
cmpl $0x3, %eax
jne 0x1b0e2
movl $0x10, %edx
movl $0x20, %ecx
jmp 0x1af8c
movl 0xc67c(%r15), %eax
cmpl $0x1, %eax
je 0x1afd2
cmpl $0x3, %eax
jne 0x1b104
movl $0x10, %edx
movl $0x20, %ecx
jmp 0x1afdc
movl $0x18, %edx
movl $0x10, %ecx
cmpb $0x1, 0x483fd(%rip)     # 0x63390
jne 0x1b01d
movsd -0x30(%rbp), %xmm0
subsd 0x483fa(%rip), %xmm0    # 0x633a0
movsd -0x38(%rbp), %xmm1
subsd 0x483fd(%rip), %xmm1    # 0x633b0
mulsd (%r14,%rdx), %xmm1
mulsd (%r14,%rcx), %xmm0
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x1b01d
jmp 0x1b0d9
movl $0x18, %edx
movl $0x10, %ecx
cmpb $0x1, 0x483ad(%rip)     # 0x63390
jne 0x1b01d
movsd -0x30(%rbp), %xmm0
subsd 0x483ae(%rip), %xmm0    # 0x633a0
movsd -0x38(%rbp), %xmm1
subsd 0x483b1(%rip), %xmm1    # 0x633b0
mulsd (%r14,%rdx), %xmm1
mulsd (%r14,%rcx), %xmm0
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x1b0d0
cmpl $0x3, %eax
je 0x1b038
cmpl $0x1, %eax
jne 0x1b051
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x28(%r14)
addq $0x30, %r14
jmp 0x1b047
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x38(%r14)
addq $0x28, %r14
movsd -0x38(%rbp), %xmm0
movsd %xmm0, (%r14)
movl %r12d, %r14d
callq 0xa210
movq 0x48318(%rip), %rax     # 0x63378
subq 0x48309(%rip), %rax     # 0x63370
sarq $0x3, %rax
imulq %r13, %rax
incl %r12d
cmpq %r14, %rax
ja 0x1ae00
movq %r15, %rdi
callq 0xa300
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x48310(%rip)    # 0x633a0
movsd -0x38(%rbp), %xmm0
movsd %xmm0, 0x48313(%rip)    # 0x633b0
movb $0x1, 0x482ec(%rip)     # 0x63390
xorl %eax, %eax
jmp 0x1b167
leaq 0x1e79e(%rip), %rdi     # 0x39850
callq 0xa1f0
movq %rbx, %rdi
movq %rax, %rsi
movsd -0x48(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
movb $0x2, %al
callq 0xa850
jmp 0x1b126
leaq 0x1e800(%rip), %rdi     # 0x398d7
jmp 0x1b114
leaq 0x1e87c(%rip), %rdi     # 0x3995c
jmp 0x1b114
leaq 0x1e840(%rip), %rdi     # 0x39929
callq 0xa1f0
movl 0xc67c(%r15), %edx
movq %rbx, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0xa850
jmp 0x1b126
leaq 0x1e79e(%rip), %rdi     # 0x398a9
jmp 0x1b114
leaq 0x1d17f(%rip), %rdi     # 0x38293
callq 0xa1f0
movq %rbx, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0xa850
movq 0x28e33(%rip), %rbx     # 0x43f60
movl $0x0, 0xd670(%rbx)
leaq 0xc6d0(%rbx), %rdi
leaq 0x1e68b(%rip), %rsi     # 0x397d0
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