// File: realuptime[P]PolyDetector/PolyDetector.cpp
// Function: PolyLine::iCompareLineOrder(PolyLine const&, PolyLine&)
int PolyLine::iCompareLineOrder(const PolyLine &l1, PolyLine &l2)
{
    int result = iComparePointOrder(l1.a, l2.a);

    if (result == 0)
    {
        // in case lines share first point
        // we must order the lines by its slope

        auto dx1 = l1.b.x - l1.a.x;
        auto dy1 = l1.b.y - l1.a.y;
        auto dx2 = l2.b.x - l2.a.x;
        auto dy2 = l2.b.y - l2.a.y;

        // by definition of first and last point we are sure that dy > 0

        if (dx1>0 && dx2<0)
            // line 1 in 1st quadrant, line 2 in 2nd quadrant
            // this means line 2 cames first
            return 1;

        if (dx1<0 && dx2>0)
            // line 1 in 2nd quadrant, line 2 in 1st quadrant
            // this means line 1 cames first
            return -1;

        if (dx1 == 0) {
            // first line is vertical
            if (dx2>0)
                // second line in 1st quadrant
                // first line is previous
                return -1;

            if (dx2<0)
                // second line in 2nd quadrant
                // second line is previous
                return 1;
            // this should no happen
            return 0;
        }

        if (dx2 == 0) {
            // second line is vertical
            if (dx1>0)
                // first line in 1st quadrant
                // second line is previous
                return 1;

            if (dx1<0)
                // first line in 2nd quadrant
                // first line is previous
                return -1;

            // this should not happen
            return 0;
        }


        // calculate the slopes
        double m1 = dy1/dx1;
        double m2 = dy2/dx2;
        // line 1 and line 2 in 2nd quadrant
        if (m1 > m2)
            return -1;
        if (m1 < m2)
            return 1;
        
        // in this case we have the same slope in both lines,
        // which means that both lines are coincident.
        return 0;
    }

    return result;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x2c50
movl %eax, 0x2c(%rsp)
cmpl $0x0, 0x2c(%rsp)
jne 0x2c3e
movq 0x38(%rsp), %rax
movss 0x8(%rax), %xmm0
movq 0x38(%rsp), %rax
subss (%rax), %xmm0
movss %xmm0, 0x28(%rsp)
movq 0x38(%rsp), %rax
movss 0xc(%rax), %xmm0
movq 0x38(%rsp), %rax
subss 0x4(%rax), %xmm0
movss %xmm0, 0x24(%rsp)
movq 0x30(%rsp), %rax
movss 0x8(%rax), %xmm0
movq 0x30(%rsp), %rax
subss (%rax), %xmm0
movss %xmm0, 0x20(%rsp)
movq 0x30(%rsp), %rax
movss 0xc(%rax), %xmm0
movq 0x30(%rsp), %rax
subss 0x4(%rax), %xmm0
movss %xmm0, 0x1c(%rsp)
movss 0x28(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x2b17
xorps %xmm0, %xmm0
ucomiss 0x20(%rsp), %xmm0
jbe 0x2b17
movl $0x1, 0x44(%rsp)
jmp 0x2c46
xorps %xmm0, %xmm0
ucomiss 0x28(%rsp), %xmm0
jbe 0x2b3c
movss 0x20(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x2b3c
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0x2c46
movss 0x28(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x2b8b
jp 0x2b8b
movss 0x20(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x2b67
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0x2c46
xorps %xmm0, %xmm0
ucomiss 0x20(%rsp), %xmm0
jbe 0x2b7e
movl $0x1, 0x44(%rsp)
jmp 0x2c46
movl $0x0, 0x44(%rsp)
jmp 0x2c46
movss 0x20(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x2bd4
jp 0x2bd4
movss 0x28(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x2bb6
movl $0x1, 0x44(%rsp)
jmp 0x2c46
xorps %xmm0, %xmm0
ucomiss 0x28(%rsp), %xmm0
jbe 0x2bca
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0x2c46
movl $0x0, 0x44(%rsp)
jmp 0x2c46
movss 0x24(%rsp), %xmm0
divss 0x28(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movss 0x1c(%rsp), %xmm0
divss 0x20(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x10(%rsp), %xmm0
ucomisd 0x8(%rsp), %xmm0
jbe 0x2c18
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0x2c46
movsd 0x10(%rsp), %xmm1
movsd 0x8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x2c34
movl $0x1, 0x44(%rsp)
jmp 0x2c46
movl $0x0, 0x44(%rsp)
jmp 0x2c46
movl 0x2c(%rsp), %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nop