// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Relate.cpp
// Function: gaps::R3Splits(gaps::R3Plane const&, gaps::R3Span const&, gaps::R3Span*, gaps::R3Span*)
int 
R3Splits(const R3Plane& plane, const R3Span& span, R3Span *below_result, R3Span *above_result)
{
    // Characterize endpoints with respect to plane
    RNScalar d1 = R3SignedDistance(plane, span.Start());
    RNScalar d2 = R3SignedDistance(plane, span.End());

    // Clip span to plane
    if (RNIsNegative(d1)) {
	if (RNIsPositive(d2)) {
	    // Start is below, end is above
	    R3Point intersection_point = (span.Start() * d2 + span.End() * -d1) / (d2 - d1);
	    if (below_result) below_result->Reset(span.Start(), intersection_point);
	    if (above_result) above_result->Reset(intersection_point, span.End());
	    return 0x3;
	}
	else {
	    // Both points are on or below plane 
	    if (below_result) *below_result = span;
	    return 0x1;
	}
    }
    else if (RNIsPositive(d1)) {
	if (RNIsNegative(d2)) {
	    // Start is above, end is below
	    R3Point intersection_point = (span.Start() * -d2 + span.End() * d1) / (d1 - d2);
	    if (below_result) below_result->Reset(intersection_point, span.End());
	    if (above_result) above_result->Reset(span.Start(), intersection_point);
	    return 0x3;
	}
	else {
	    // Start is above, end is on or above
	    if (above_result) *above_result = span;
	    return 0x2;
	}
    }
    else {
	if (RNIsNegative(d2)) {
	    // Start is on, end is below -- move end to start
	    if (below_result) *below_result = span;
	    return 0x1;
	}
	else {
	    // Start is on, end is on or above
	    if (above_result) *above_result = span;
	    return 0x2;
	}
    }
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x118(%rsp)
movq %rsi, 0x110(%rsp)
movq %rdx, 0x108(%rsp)
movq %rcx, 0x100(%rsp)
movq 0x118(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x110(%rsp), %rdi
callq 0x39250
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x86f90
movsd %xmm0, 0xf8(%rsp)
movq 0x118(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x110(%rsp), %rdi
callq 0x39260
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x86f90
movsd %xmm0, 0xf0(%rsp)
movsd 0xf8(%rsp), %xmm0
leaq 0xebd09(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x38fd0
cmpl $0x0, %eax
je 0x8c966
movsd 0xf0(%rsp), %xmm0
leaq 0xebce7(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
cmpl $0x0, %eax
je 0x8c936
movq 0x110(%rsp), %rdi
callq 0x39250
movq %rax, %rsi
movsd 0xf0(%rsp), %xmm0
leaq 0xa8(%rsp), %rdi
callq 0x6fb80
movq 0x110(%rsp), %rdi
callq 0x39260
movq %rax, %rsi
movsd 0xf8(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
leaq 0x90(%rsp), %rdi
callq 0x6fb80
leaq 0xc0(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0x6fa40
movsd 0xf0(%rsp), %xmm0
subsd 0xf8(%rsp), %xmm0
leaq 0xd8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x6fc00
cmpq $0x0, 0x108(%rsp)
je 0x8c8ec
movq 0x108(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x110(%rsp), %rdi
callq 0x39250
movq 0x18(%rsp), %rdi
movq %rax, %rsi
leaq 0xd8(%rsp), %rdx
callq 0x6dcb0
cmpq $0x0, 0x100(%rsp)
je 0x8c926
movq 0x100(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x110(%rsp), %rdi
callq 0x39260
movq 0x10(%rsp), %rdi
movq %rax, %rdx
leaq 0xd8(%rsp), %rsi
callq 0x6dcb0
movl $0x3, 0x124(%rsp)
jmp 0x8cb5b
cmpq $0x0, 0x108(%rsp)
je 0x8c956
movq 0x110(%rsp), %rsi
movq 0x108(%rsp), %rdi
callq 0x39330
movl $0x1, 0x124(%rsp)
jmp 0x8cb5b
movsd 0xf8(%rsp), %xmm0
leaq 0xebb6a(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
cmpl $0x0, %eax
je 0x8cae5
movsd 0xf0(%rsp), %xmm0
leaq 0xebb48(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x38fd0
cmpl $0x0, %eax
je 0x8cab8
movq 0x110(%rsp), %rdi
callq 0x39250
movq %rax, %rsi
movsd 0xf0(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
leaq 0x48(%rsp), %rdi
callq 0x6fb80
movq 0x110(%rsp), %rdi
callq 0x39260
movq %rax, %rsi
movsd 0xf8(%rsp), %xmm0
leaq 0x30(%rsp), %rdi
callq 0x6fb80
leaq 0x60(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x6fa40
movsd 0xf8(%rsp), %xmm0
subsd 0xf0(%rsp), %xmm0
leaq 0x78(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x6fc00
cmpq $0x0, 0x108(%rsp)
je 0x8ca73
movq 0x108(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x110(%rsp), %rdi
callq 0x39260
movq 0x8(%rsp), %rdi
movq %rax, %rdx
leaq 0x78(%rsp), %rsi
callq 0x6dcb0
cmpq $0x0, 0x100(%rsp)
je 0x8caa8
movq 0x100(%rsp), %rax
movq %rax, (%rsp)
movq 0x110(%rsp), %rdi
callq 0x39250
movq (%rsp), %rdi
movq %rax, %rsi
leaq 0x78(%rsp), %rdx
callq 0x6dcb0
movl $0x3, 0x124(%rsp)
jmp 0x8cb5b
cmpq $0x0, 0x100(%rsp)
je 0x8cad8
movq 0x110(%rsp), %rsi
movq 0x100(%rsp), %rdi
callq 0x39330
movl $0x2, 0x124(%rsp)
jmp 0x8cb5b
movsd 0xf0(%rsp), %xmm0
leaq 0xeb9eb(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x38fd0
cmpl $0x0, %eax
je 0x8cb30
cmpq $0x0, 0x108(%rsp)
je 0x8cb23
movq 0x110(%rsp), %rsi
movq 0x108(%rsp), %rdi
callq 0x39330
movl $0x1, 0x124(%rsp)
jmp 0x8cb5b
cmpq $0x0, 0x100(%rsp)
je 0x8cb50
movq 0x110(%rsp), %rsi
movq 0x100(%rsp), %rdi
callq 0x39330
movl $0x2, 0x124(%rsp)
movl 0x124(%rsp), %eax
addq $0x128, %rsp            # imm = 0x128
retq
nopw (%rax,%rax)