// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Relate.cpp
// Function: gaps::R2Splits(gaps::R2Line const&, gaps::R2Span const&, gaps::R2Span*, gaps::R2Span*)
int 
R2Splits(const R2Line& line, const R2Span& span, R2Span *below_result, R2Span *above_result)
{
    // Classify span
    // Result values: 0 = on, 1 = below, 2 = above, 3 = crossing
    RNScalar d1 = R2SignedDistance(line, span.Start());
    RNScalar d2 = R2SignedDistance(line, span.End());
    if (RNIsNegative(d1)) {
        if (RNIsPositive(d2)) {
  	    // Span crosses line
	    if (below_result || above_result) {
	        // Compute intersection
		R2Point point = span.Point(-d1);
		if (below_result) *below_result = R2Span(span.Start(), point);
		if (above_result) *above_result = R2Span(point, span.End());
	    }
	    return 3;
	}
	else {
  	    // Span is below line
	    if (below_result) *below_result = span; 
	    return 1;
	}
    }
    else if (RNIsPositive(d1)) {
        if (RNIsNegative(d2)) {
  	    // Span crosses line
	    if (below_result || above_result) {
	        // Compute intersection
		R2Point point = span.Point(d1);
		if (below_result) *below_result = R2Span(span.End(), point);
		if (above_result) *above_result = R2Span(point, span.Start());
	    }
	    return 3;
	}
	else {
  	    // Span is above line
	    if (above_result) *above_result = span; 
	    return 2;
	}
    }
    else {
        if (RNIsNegative(d2)) {
  	    // Span is below line
	    if (below_result) *below_result = span; 
	    return 1;
	}
        else if (RNIsPositive(d2)) {
  	    // Span is above line
	    if (above_result) *above_result = span; 
	    return 2;
	}
	else {
  	    // Span is on line
	    if (below_result) *below_result = span; 
	    if (above_result) *above_result = span; 
	    return 0;
	}
    }
}

// Assembly:
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rdi, 0x198(%rsp)
movq %rsi, 0x190(%rsp)
movq %rdx, 0x188(%rsp)
movq %rcx, 0x180(%rsp)
movq 0x198(%rsp), %rax
movq %rax, (%rsp)
movq 0x190(%rsp), %rdi
callq 0xa3500
movq (%rsp), %rdi
movq %rax, %rsi
callq 0xcb900
movsd %xmm0, 0x178(%rsp)
movq 0x198(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x190(%rsp), %rdi
callq 0xa3510
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0xcb900
movsd %xmm0, 0x170(%rsp)
movsd 0x178(%rsp), %xmm0
leaq 0xaa93b(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x38fd0
cmpl $0x0, %eax
je 0xcdcf4
movsd 0x170(%rsp), %xmm0
leaq 0xaa919(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
cmpl $0x0, %eax
je 0xcdcc4
cmpq $0x0, 0x188(%rsp)
jne 0xcdbf3
cmpq $0x0, 0x180(%rsp)
je 0xcdcb4
movq 0x190(%rsp), %rsi
movsd 0x178(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
leaq 0x160(%rsp), %rdi
callq 0xc1620
cmpq $0x0, 0x188(%rsp)
je 0xcdc6d
movq 0x190(%rsp), %rdi
callq 0xa3500
movq %rax, %rsi
leaq 0x110(%rsp), %rdi
leaq 0x160(%rsp), %rdx
callq 0xc1470
movq 0x188(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0xcdf30
cmpq $0x0, 0x180(%rsp)
je 0xcdcb2
movq 0x190(%rsp), %rdi
callq 0xa3510
movq %rax, %rdx
leaq 0xc0(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0xc1470
movq 0x180(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0xcdf30
jmp 0xcdcb4
movl $0x3, 0x1a4(%rsp)
jmp 0xcdf14
cmpq $0x0, 0x188(%rsp)
je 0xcdce4
movq 0x190(%rsp), %rsi
movq 0x188(%rsp), %rdi
callq 0xcdf30
movl $0x1, 0x1a4(%rsp)
jmp 0xcdf14
movsd 0x178(%rsp), %xmm0
leaq 0xaa7dc(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
cmpl $0x0, %eax
je 0xcde30
movsd 0x170(%rsp), %xmm0
leaq 0xaa7ba(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x38fd0
cmpl $0x0, %eax
je 0xcde00
cmpq $0x0, 0x188(%rsp)
jne 0xcdd52
cmpq $0x0, 0x180(%rsp)
je 0xcddf0
movq 0x190(%rsp), %rsi
movsd 0x178(%rsp), %xmm0
leaq 0xb0(%rsp), %rdi
callq 0xc1620
cmpq $0x0, 0x188(%rsp)
je 0xcddaf
movq 0x190(%rsp), %rdi
callq 0xa3510
movq %rax, %rsi
leaq 0x60(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
callq 0xc1470
movq 0x188(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xcdf30
cmpq $0x0, 0x180(%rsp)
je 0xcddee
movq 0x190(%rsp), %rdi
callq 0xa3500
movq %rax, %rdx
leaq 0x10(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0xc1470
movq 0x180(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xcdf30
jmp 0xcddf0
movl $0x3, 0x1a4(%rsp)
jmp 0xcdf14
cmpq $0x0, 0x180(%rsp)
je 0xcde20
movq 0x190(%rsp), %rsi
movq 0x180(%rsp), %rdi
callq 0xcdf30
movl $0x2, 0x1a4(%rsp)
jmp 0xcdf14
movsd 0x170(%rsp), %xmm0
leaq 0xaa6a0(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x38fd0
cmpl $0x0, %eax
je 0xcde7e
cmpq $0x0, 0x188(%rsp)
je 0xcde6e
movq 0x190(%rsp), %rsi
movq 0x188(%rsp), %rdi
callq 0xcdf30
movl $0x1, 0x1a4(%rsp)
jmp 0xcdf14
movsd 0x170(%rsp), %xmm0
leaq 0xaa652(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
cmpl $0x0, %eax
je 0xcdec9
cmpq $0x0, 0x180(%rsp)
je 0xcdebc
movq 0x190(%rsp), %rsi
movq 0x180(%rsp), %rdi
callq 0xcdf30
movl $0x2, 0x1a4(%rsp)
jmp 0xcdf14
cmpq $0x0, 0x188(%rsp)
je 0xcdee9
movq 0x190(%rsp), %rsi
movq 0x188(%rsp), %rdi
callq 0xcdf30
cmpq $0x0, 0x180(%rsp)
je 0xcdf09
movq 0x190(%rsp), %rsi
movq 0x180(%rsp), %rdi
callq 0xcdf30
movl $0x0, 0x1a4(%rsp)
movl 0x1a4(%rsp), %eax
addq $0x1a8, %rsp            # imm = 0x1A8
retq
nopw %cs:(%rax,%rax)
nopl (%rax)