// File: thierry7100[P]CutOptim/src/SvgDoc.cpp
// Function: SvgDoc::PlacementNotPossible(Polygon*, std::__cxx11::list<NSVGpath*, std::allocator<NSVGpath*>>, Rectangle*, Point const*, int)
int SvgDoc::PlacementNotPossible(Polygon *CurPoly, std::list<NSVGpath *> FixedPath, Rectangle *OverAll, const Point *RefPoint, int Cached)
{
int PlacedPolygon = 0;
int FixedBit = 0;
int FixedVertex = 0;
int CCW_B;

    //  If bounding box is outside the sheet, return false
    Rectangle BBox = CurPoly->GetBoundingBox();
    if ( BBox.A.x < 0 ) return FAIL_OUTSIDE_SHEET | FIXED_BIT;
    if ( BBox.A.y < 0 ) return FAIL_OUTSIDE_SHEET | FIXED_BIT;
    if ( BBox.B.x > SheetSizeX ) return FAIL_OUTSIDE_SHEET | FIXED_BIT;
    if ( BBox.B.y > SheetSizeY ) return FAIL_OUTSIDE_SHEET | FIXED_BIT;
    //  If BBox outside OverAll return true
    if ( BBox.A.x >= OverAll->B.x ) return 0;
    if ( BBox.A.y >= OverAll->B.y ) return 0;
    if ( BBox.B.x <= OverAll->A.x ) return 0;
    if ( BBox.B.y <= OverAll->A.y ) return 0;

    PlacedPolygon = 0;
    //  Now checks if some vertex of CurPoly lies into a polygon from the list
    FixedVertex = 0;
    for (std::list<NSVGpath *>::iterator it=FixedPath.begin(); it != FixedPath.end(); ++it, PlacedPolygon++)
    {
        NSVGpath *path = *it;
        if ( path->isFixed )
        {
            FixedBit = FIXED_BIT;
            FixedVertex += path->LargePolygon->nVertices;
        }
        else
            FixedBit = 0;
        if ( Cached != 0 && path->isFixed && Cached > FixedVertex) continue;     //  No need to reprocess fixed polygons which are already computed
        //  Check if start point of real path is inside polygon. This test is mandatory to avoid placing two polygons at the same place !
        if ( path->PlacedPolygon->isInPoly(RefPoint) > 0)
            return FAIL_VERTEX_IN_POLYGON | FixedBit;
        //  Then check for all vertices of polygon
        int idx_common_vertex_B = -1;
        int idx_common_vertex_CurPoly = -1;
        int SegmentIndex = -1;
        //  For each vertex of CurPoly, so stop at CurPoly->nVertices - 1 to avoid double check of vertex 0
        for ( int i = 0; i < CurPoly->nVertices - 1; i++ )
        {
            nbPointInPoly++;
            int ResInPoly = path->PlacedPolygon->isInPoly(CurPoly->GetVertex(i), &SegmentIndex);
            if ( ResInPoly > 0 )        //  Vertex in polygon, bad placement
                return FAIL_VERTEX_IN_POLYGON | FixedBit;
            //  If point is on an edge of PlacedPolygon, check angles to verify that this is possible
            if ( ResInPoly == 0 && SegmentIndex >= 0 )
            {
                //  If impossible, the edge will be inside the Placed Polygon.
                if ( !CheckAnglesOnEdge(CurPoly, i, path->PlacedPolygon, SegmentIndex) )
                    return FAIL_EDGE_INTERSECT | FixedBit;
            }
            //  If the 2 polygons share more than 2 vertices with the same order (clockwise or counter clockwise), placement is NOT possible
            if ( ResInPoly < 0  )
            {
                //  Compute difference between indices of common vertices of polygon B ( the biggest one )
                if ( idx_common_vertex_B >= 0 && idx_common_vertex_B + ResInPoly != 0)  //  Check there is already a common vertex and this is NOT the same
                {
                    int diffB = -ResInPoly - idx_common_vertex_B;
                    if (diffB < 0) diffB += path->PlacedPolygon->nVertices - 1;
                    // diffB gives the distance (in number of vertices) between the 2 vertices of the biggest polygon (not CutPoly)
                    //  If this distance is less than half of number of vertices, this is CCW
                    CCW_B = (2*diffB > path->PlacedPolygon->nVertices);
                    // If at least 2 common vertex, one should be CCW and other one CW
                    //  Compute if CurPoly vertex are CW or CCW
                    int CCW_CurPoly = 2*(i-idx_common_vertex_CurPoly) > CurPoly->nVertices;
                    //  SO exclusive OR of CCW_B and CCW_CurPoly should be true
                    if ((CCW_B ^ CCW_CurPoly) == 0 )
                    {
                        if ( debug_level > 2 )
                        {
                            OutDebug << "Placement impossible , polygon share at least 2 vertices with polygon " << PlacedPolygon << ", vertices are " <<  idx_common_vertex_B << " and " << -ResInPoly - 1 << "\n";
                        }
                        return FAIL_VERTEX_IN_POLYGON | FixedBit;
                    }
                }
                idx_common_vertex_B = -ResInPoly - 1;       //  Store common vertex for following loops
                idx_common_vertex_CurPoly = i;
            }
        }
    }
    //  Then checks if some vertex of placed polygons lies in CurPoly

    PlacedPolygon = 0;
    FixedVertex = 0;
    for (std::list<NSVGpath *>::iterator it=FixedPath.begin(); it != FixedPath.end(); ++it, PlacedPolygon++)
    {
        NSVGpath *path = *it;
        if ( path->isFixed )
        {
            FixedBit = FIXED_BIT;
            FixedVertex += path->LargePolygon->nVertices;
        }
        else
            FixedBit = 0;
        if ( Cached != 0 && path->isFixed && Cached > FixedVertex) continue;       //  No need to reprocess fixed polygons already computed
        int SegmentIndex = -1;
        for ( int i = 0; i < path->PlacedPolygon->nVertices; i++ )
        {
            nbPointInPoly++;
            int ResInPoly = CurPoly->isInPoly(path->PlacedPolygon->GetVertex(i), &SegmentIndex);
            if ( ResInPoly > 0 )        //  Vertex in polygon, bad placement
                return FAIL_VERTEX_IN_POLYGON | FixedBit;
        }
    }
    //  Last, also checks if polygon overlap with segment crossing, without point inclusion.
    //  Check if one segment of CurPoly intersect with one segment of one of the fixed polygons
    for (std::list<NSVGpath *>::iterator it=FixedPath.begin(); it != FixedPath.end(); ++it, PlacedPolygon++)
    {
        NSVGpath *path = *it;
        if ( path->isFixed )
        {
            FixedBit = FIXED_BIT;
            FixedVertex += path->LargePolygon->nVertices;
        }
        else
            FixedBit = 0;
        if ( Cached != 0 && path->isFixed && Cached > FixedVertex) continue;       //  No need to reprocess fixed polygons already computed
        Polygon *fPoly = path->PlacedPolygon;
        nbIntersectPoly++;
        if ( CurPoly->Intersect(fPoly) )
            return FAIL_EDGE_INTERSECT | FixedBit;
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdx, -0xf8(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xf0(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x61320
xorps %xmm0, %xmm0
ucomisd -0x68(%rbp), %xmm0
jbe 0x726fd
movl $0x11, -0x4(%rbp)
jmp 0x72db6
xorps %xmm0, %xmm0
ucomisd -0x60(%rbp), %xmm0
jbe 0x72713
movl $0x11, -0x4(%rbp)
jmp 0x72db6
movq -0xf0(%rbp), %rax
movsd -0x58(%rbp), %xmm0
ucomisd 0x10(%rax), %xmm0
jbe 0x72732
movl $0x11, -0x4(%rbp)
jmp 0x72db6
movq -0xf0(%rbp), %rax
movsd -0x50(%rbp), %xmm0
ucomisd 0x18(%rax), %xmm0
jbe 0x72751
movl $0x11, -0x4(%rbp)
jmp 0x72db6
movsd -0x68(%rbp), %xmm0
movq -0x28(%rbp), %rax
ucomisd 0x10(%rax), %xmm0
jb 0x7276d
movl $0x0, -0x4(%rbp)
jmp 0x72db6
movsd -0x60(%rbp), %xmm0
movq -0x28(%rbp), %rax
ucomisd 0x18(%rax), %xmm0
jb 0x72789
movl $0x0, -0x4(%rbp)
jmp 0x72db6
movsd -0x58(%rbp), %xmm1
movq -0x28(%rbp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jb 0x727a8
movl $0x0, -0x4(%rbp)
jmp 0x72db6
movsd -0x50(%rbp), %xmm1
movq -0x28(%rbp), %rax
movsd 0x8(%rax), %xmm0
ucomisd %xmm1, %xmm0
jb 0x727c8
movl $0x0, -0x4(%rbp)
jmp 0x72db6
movq -0xf8(%rbp), %rdi
movl $0x0, -0x38(%rbp)
movl $0x0, -0x40(%rbp)
callq 0x803f0
movq %rax, -0x70(%rbp)
movq -0xf8(%rbp), %rdi
callq 0x80450
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x80420
testb $0x1, %al
jne 0x7280c
jmp 0x72b11
leaq -0x70(%rbp), %rdi
callq 0x80480
movq (%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpl $0x0, 0x80(%rax)
je 0x72843
movl $0x10, -0x3c(%rbp)
movq -0x80(%rbp), %rax
movq 0x68(%rax), %rax
movl 0x8(%rax), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
jmp 0x7284a
movl $0x0, -0x3c(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x7286a
movq -0x80(%rbp), %rax
cmpl $0x0, 0x80(%rax)
je 0x7286a
movl -0x34(%rbp), %eax
cmpl -0x40(%rbp), %eax
jle 0x7286a
jmp 0x72afa
movq -0x80(%rbp), %rax
movq 0x78(%rax), %rdi
movq -0x30(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x60a10
cmpl $0x0, %eax
jle 0x72892
movl -0x3c(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x4(%rbp)
jmp 0x72db6
movl $0xffffffff, -0x84(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x88(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x8c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
movq -0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x72af8
movq -0xf0(%rbp), %rax
movq 0x288(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x288(%rax)
movq -0x80(%rbp), %rax
movq 0x78(%rax), %rax
movq %rax, -0x100(%rbp)
movq -0x18(%rbp), %rdi
movl -0x90(%rbp), %esi
callq 0x7ff40
movq -0x100(%rbp), %rdi
movq %rax, %rsi
leaq -0x8c(%rbp), %rdx
callq 0x60a10
movl %eax, -0x94(%rbp)
cmpl $0x0, -0x94(%rbp)
jle 0x7293c
movl -0x3c(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x4(%rbp)
jmp 0x72db6
cmpl $0x0, -0x94(%rbp)
jne 0x7297f
cmpl $0x0, -0x8c(%rbp)
jl 0x7297f
movq -0x18(%rbp), %rdi
movl -0x90(%rbp), %esi
movq -0x80(%rbp), %rax
movq 0x78(%rax), %rdx
movl -0x8c(%rbp), %ecx
callq 0x72ee0
testb $0x1, %al
jne 0x7297d
movl -0x3c(%rbp), %eax
orl $0x3, %eax
movl %eax, -0x4(%rbp)
jmp 0x72db6
jmp 0x7297f
cmpl $0x0, -0x94(%rbp)
jge 0x72ae2
cmpl $0x0, -0x84(%rbp)
jl 0x72ac5
movl -0x84(%rbp), %eax
addl -0x94(%rbp), %eax
cmpl $0x0, %eax
je 0x72ac5
xorl %eax, %eax
subl -0x94(%rbp), %eax
subl -0x84(%rbp), %eax
movl %eax, -0x98(%rbp)
cmpl $0x0, -0x98(%rbp)
jge 0x729e5
movq -0x80(%rbp), %rax
movq 0x78(%rax), %rax
movl 0x8(%rax), %eax
subl $0x1, %eax
addl -0x98(%rbp), %eax
movl %eax, -0x98(%rbp)
movl -0x98(%rbp), %eax
shll %eax
movq -0x80(%rbp), %rcx
movq 0x78(%rcx), %rcx
cmpl 0x8(%rcx), %eax
setg %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x44(%rbp)
movl -0x90(%rbp), %eax
subl -0x88(%rbp), %eax
shll %eax
movq -0x18(%rbp), %rcx
cmpl 0x8(%rcx), %eax
setg %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x9c(%rbp)
movl -0x44(%rbp), %eax
xorl -0x9c(%rbp), %eax
cmpl $0x0, %eax
jne 0x72ac3
movq -0xf0(%rbp), %rax
cmpl $0x2, 0x60(%rax)
jle 0x72ab5
movq -0xf0(%rbp), %rdi
addq $0x68, %rdi
leaq 0x1307a(%rip), %rsi     # 0x85ad1
callq 0x9530
movq %rax, %rdi
movl -0x38(%rbp), %esi
callq 0x9930
movq %rax, %rdi
leaq 0x130a7(%rip), %rsi     # 0x85b18
callq 0x9530
movq %rax, %rdi
movl -0x84(%rbp), %esi
callq 0x9930
movq %rax, %rdi
leaq 0x127e1(%rip), %rsi     # 0x8526f
callq 0x9530
movq %rax, %rdi
xorl %esi, %esi
subl -0x94(%rbp), %esi
subl $0x1, %esi
callq 0x9930
movq %rax, %rdi
leaq 0x1280b(%rip), %rsi     # 0x852bb
callq 0x9530
movl -0x3c(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x4(%rbp)
jmp 0x72db6
jmp 0x72ac5
xorl %eax, %eax
subl -0x94(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x84(%rbp)
movl -0x90(%rbp), %eax
movl %eax, -0x88(%rbp)
jmp 0x72ae4
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x728ba
jmp 0x72afa
leaq -0x70(%rbp), %rdi
callq 0x80570
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x727e6
movq -0xf8(%rbp), %rdi
movl $0x0, -0x38(%rbp)
movl $0x0, -0x40(%rbp)
callq 0x803f0
movq %rax, -0xa8(%rbp)
movq -0xf8(%rbp), %rdi
callq 0x80450
movq %rax, -0xb0(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x80420
testb $0x1, %al
jne 0x72b61
jmp 0x72c9b
leaq -0xa8(%rbp), %rdi
callq 0x80480
movq (%rax), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
cmpl $0x0, 0x80(%rax)
je 0x72ba4
movl $0x10, -0x3c(%rbp)
movq -0xb8(%rbp), %rax
movq 0x68(%rax), %rax
movl 0x8(%rax), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
jmp 0x72bab
movl $0x0, -0x3c(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x72bce
movq -0xb8(%rbp), %rax
cmpl $0x0, 0x80(%rax)
je 0x72bce
movl -0x34(%rbp), %eax
cmpl -0x40(%rbp), %eax
jle 0x72bce
jmp 0x72c81
movl $0xffffffff, -0xbc(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0xc0(%rbp)
movl -0xc0(%rbp), %eax
movq -0xb8(%rbp), %rcx
movq 0x78(%rcx), %rcx
cmpl 0x8(%rcx), %eax
jge 0x72c7f
movq -0xf0(%rbp), %rax
movq 0x288(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x288(%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0xb8(%rbp), %rax
movq 0x78(%rax), %rdi
movl -0xc0(%rbp), %esi
callq 0x7ff40
movq -0x108(%rbp), %rdi
movq %rax, %rsi
leaq -0xbc(%rbp), %rdx
callq 0x60a10
movl %eax, -0xc4(%rbp)
cmpl $0x0, -0xc4(%rbp)
jle 0x72c69
movl -0x3c(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x4(%rbp)
jmp 0x72db6
jmp 0x72c6b
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x72be2
jmp 0x72c81
leaq -0xa8(%rbp), %rdi
callq 0x80570
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x72b32
movq -0xf8(%rbp), %rdi
callq 0x803f0
movq %rax, -0xd0(%rbp)
movq -0xf8(%rbp), %rdi
callq 0x80450
movq %rax, -0xd8(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x80420
testb $0x1, %al
jne 0x72cdd
jmp 0x72daf
leaq -0xd0(%rbp), %rdi
callq 0x80480
movq (%rax), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
cmpl $0x0, 0x80(%rax)
je 0x72d20
movl $0x10, -0x3c(%rbp)
movq -0xe0(%rbp), %rax
movq 0x68(%rax), %rax
movl 0x8(%rax), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
jmp 0x72d27
movl $0x0, -0x3c(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x72d47
movq -0xe0(%rbp), %rax
cmpl $0x0, 0x80(%rax)
je 0x72d47
movl -0x34(%rbp), %eax
cmpl -0x40(%rbp), %eax
jle 0x72d47
jmp 0x72d95
movq -0xf0(%rbp), %rax
movq -0xe0(%rbp), %rcx
movq 0x78(%rcx), %rcx
movq %rcx, -0xe8(%rbp)
movq 0x290(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x290(%rax)
movq -0x18(%rbp), %rdi
movq -0xe8(%rbp), %rsi
callq 0x62a60
testb $0x1, %al
jne 0x72d88
jmp 0x72d93
movl -0x3c(%rbp), %eax
orl $0x3, %eax
movl %eax, -0x4(%rbp)
jmp 0x72db6
jmp 0x72d95
leaq -0xd0(%rbp), %rdi
callq 0x80570
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x72cae
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)