// File: thierry7100[P]CutOptim/src/SvgDoc.cpp
// Function: ComputeFreeRotAngle(Polygon*, int, Polygon*, int, int, double&)
bool ComputeFreeRotAngle(Polygon *FloatPoly, int iVertexFloat, Polygon *FixedPoly, int iVertexFixed, int iRot, double &angle)
{
double sf1, sf2;
double sm1, sm2;

    //  First compute angles of edges ogf Fixed polygon
    if ( iVertexFixed == 0)
    {
        //  Arrival segment, angle is pi + slope, and if angle is greater than pi, substract 2*pi to keep value between -pi and +pi
        sf1 = FixedPoly->getSlope(FixedPoly->nVertices-2) + M_PI;
        if ( sf1 > M_PI ) sf1 -= 2*M_PI;
        sf2 = FixedPoly->getSlope(0);
    }
    else
    {
        //  Arrival segment, angle is pi + slope, and if angle is greater than pi, substract 2*pi to keep value between -pi and +pi
        sf1 = FixedPoly->getSlope(iVertexFixed-1) + M_PI;
        if ( sf1 > M_PI ) sf1 -= 2*M_PI;
        sf2 = FixedPoly->getSlope(iVertexFixed);
    }
    //  Then angles for the floating polygon
    if ( iVertexFloat == 0)
    {
        //  Arrival segment, angle is pi + slope, and if angle is greater than pi, substract 2*pi to keep value between -pi and +pi
        sm1 = FloatPoly->getSlope(FloatPoly->nVertices-2) + M_PI;
        if ( sm1 > M_PI ) sm1 -= 2*M_PI;
        sm2 = FloatPoly->getSlope(0);
    }
    else
    {
        //  Arrival segment, angle is pi + slope, and if angle is greater than pi, substract 2*pi to keep value between -pi and +pi
        sm1 = FloatPoly->getSlope(iVertexFloat-1) + M_PI;
        if ( sm1 > M_PI ) sm1 -= 2*M_PI;
        sm2 = FloatPoly->getSlope(iVertexFloat);
    }
    switch ( iRot )
    {
    case 0:
    //  if iRot is 0, align sm1 on sf1
        angle = sf1 - sm1;
        break;
    case 1:
    //  if iRot is 1, align sm2 on sf1
        angle = sf1 - sm2;
        break;
    case 2:
    //  if iRot is 2, align sm1 on sf2
        angle = sf2 - sm1;
        break;
    case 3:
    //  if iRot is 3, align sm2 on sf2
        angle = sf2 - sm2;
        break;
    default:
        printf("Invalid choice !!\n");
        FloatPoly = 0;
        FloatPoly->nVertices = 0;           //  Segment violation
    }
    sm1 += angle;
    sm2 += angle;
    //  check if sm1 is OK, given angles sm1, smé, sf1 and sf2
    int res1 = CheckOKAngles(sf1, sf2, sm1);
    if ( res1 == 0 ) return false;
    //  Idem with sm2
    int res2 = CheckOKAngles(sf1, sf2, sm2);
    if (res2 == 0 ) return false;
    //  Also check sf1 upon sm1 and sm2
    int res3 = CheckOKAngles(sm1, sm2, sf1);
    if ( res3 == 0 ) return false;
    //  and finally sf2 upon sm1 and sm2
    int res4 = CheckOKAngles(sm1, sm2, sf2);
    if (res4 == 0 ) return false;
    //  All is OK, return true
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl %r8d, -0x28(%rbp)
movq %r9, -0x30(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x6dcc3
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x8(%rax), %esi
subl $0x2, %esi
callq 0x80590
movsd 0x14399(%rip), %xmm1    # 0x82018
addsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x38(%rbp), %xmm0
movsd 0x14383(%rip), %xmm1    # 0x82018
ucomisd %xmm1, %xmm0
jbe 0x6dcb1
movsd -0x38(%rbp), %xmm0
movsd 0x164f8(%rip), %xmm1    # 0x841a0
subsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movq -0x20(%rbp), %rdi
xorl %esi, %esi
callq 0x80590
movsd %xmm0, -0x40(%rbp)
jmp 0x6dd1d
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %esi
subl $0x1, %esi
callq 0x80590
movsd 0x1433e(%rip), %xmm1    # 0x82018
addsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x38(%rbp), %xmm0
movsd 0x14328(%rip), %xmm1    # 0x82018
ucomisd %xmm1, %xmm0
jbe 0x6dd0c
movsd -0x38(%rbp), %xmm0
movsd 0x1649d(%rip), %xmm1    # 0x841a0
subsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x80590
movsd %xmm0, -0x40(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x6dd82
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x8(%rax), %esi
subl $0x2, %esi
callq 0x80590
movsd 0x142da(%rip), %xmm1    # 0x82018
addsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm0
movsd 0x142c4(%rip), %xmm1    # 0x82018
ucomisd %xmm1, %xmm0
jbe 0x6dd70
movsd -0x48(%rbp), %xmm0
movsd 0x16439(%rip), %xmm1    # 0x841a0
subsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x80590
movsd %xmm0, -0x50(%rbp)
jmp 0x6dddc
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
subl $0x1, %esi
callq 0x80590
movsd 0x1427f(%rip), %xmm1    # 0x82018
addsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm0
movsd 0x14269(%rip), %xmm1    # 0x82018
ucomisd %xmm1, %xmm0
jbe 0x6ddcb
movsd -0x48(%rbp), %xmm0
movsd 0x163de(%rip), %xmm1    # 0x841a0
subsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x80590
movsd %xmm0, -0x50(%rbp)
movl -0x28(%rbp), %eax
movq %rax, -0x68(%rbp)
subq $0x3, %rax
ja 0x6de4d
movq -0x68(%rbp), %rax
leaq 0x164a8(%rip), %rcx     # 0x8429c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movsd -0x38(%rbp), %xmm0
subsd -0x48(%rbp), %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, (%rax)
jmp 0x6de6e
movsd -0x38(%rbp), %xmm0
subsd -0x50(%rbp), %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, (%rax)
jmp 0x6de6e
movsd -0x40(%rbp), %xmm0
subsd -0x48(%rbp), %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, (%rax)
jmp 0x6de6e
movsd -0x40(%rbp), %xmm0
subsd -0x50(%rbp), %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, (%rax)
jmp 0x6de6e
leaq 0x17469(%rip), %rdi     # 0x852bd
movb $0x0, %al
callq 0x9080
movq $0x0, -0x10(%rbp)
movq -0x10(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x30(%rbp), %rax
movsd (%rax), %xmm0
addsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x30(%rbp), %rax
movsd (%rax), %xmm0
addsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x38(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
movsd -0x48(%rbp), %xmm2
callq 0x6dad0
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
jne 0x6deba
movb $0x0, -0x1(%rbp)
jmp 0x6df36
movsd -0x38(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
movsd -0x50(%rbp), %xmm2
callq 0x6dad0
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x58(%rbp)
jne 0x6dee2
movb $0x0, -0x1(%rbp)
jmp 0x6df36
movsd -0x48(%rbp), %xmm0
movsd -0x50(%rbp), %xmm1
movsd -0x38(%rbp), %xmm2
callq 0x6dad0
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jne 0x6df0a
movb $0x0, -0x1(%rbp)
jmp 0x6df36
movsd -0x48(%rbp), %xmm0
movsd -0x50(%rbp), %xmm1
movsd -0x40(%rbp), %xmm2
callq 0x6dad0
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x60(%rbp)
cmpl $0x0, -0x60(%rbp)
jne 0x6df32
movb $0x0, -0x1(%rbp)
jmp 0x6df36
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)