// File: thierry7100[P]CutOptim/src/SvgDoc.cpp
// Function: SplitBezierCurves(NSVGpath*, double)
static void SplitBezierCurves(NSVGpath *path, double flat_factor)
{
NSVGPathElt *Elt = path->PathElts;
float x, y;
float xmin, xmax, ymin, ymax;

    NSVGpath *dPath = (NSVGpath *) malloc(sizeof(NSVGpath));
    path->PolyLine = dPath;
    dPath->StartX = path->StartX;          //  Copy elements
    dPath->StartY = path->StartY;
    dPath->nElts = path->nElts;
    dPath->next = 0;
    dPath->PolyLine = dPath;
    dPath->ClosePolygon = NULL;
    dPath->hasBezier = 0;
    dPath->closed = path->closed;
    //  Copy all elements of path
    NSVGPathElt *dElt, *oldElt = 0;
    while ( Elt )
    {
        dElt = (NSVGPathElt *) malloc(sizeof(NSVGPathElt));
        *dElt = *Elt;
        dElt->next = NULL;
        if ( oldElt )
            oldElt->next = dElt;
        else
            path->PolyLine->PathElts = dElt;
        oldElt = dElt;
        Elt = Elt->next;
    }
    //  Now walk through all path elements to transform bezier curves into line segments
    x = path->StartX;
    y = path->StartY;
    xmin = x;
    xmax = x;
    ymin = y;
    ymax = y;
    Elt = path->PolyLine->PathElts;
    while ( Elt != NULL )
    {
        if ( Elt->Type == NSVG_PATH_BEZIER_C )
        {
            //  Break bezier curves into line segments
            //  For this, split bezier curve into two parts, until parts are nearly line segments
            double d_bezier = Distance_bezier_segment(x, y, Elt);
            if ( d_bezier <=  flat_factor )
            {
                Elt->Type = NSVG_PATH_LINE;     //  nearly a line segment, transform type
            }
            else
            {
                //  split bezier into two parts, then resume processing at the same point.
                NSVGPathElt *Elt2 = (NSVGPathElt *) malloc(sizeof(NSVGPathElt));    //  Create a new element
                //  First create the linked list
                Elt2->next = Elt->next;
                Elt->next = Elt2;
                Elt2->Type = NSVG_PATH_BEZIER_C;
                //  The end point of the second half is the previous end point.
                Elt2->EndX = Elt->EndX;
                Elt2->EndY = Elt->EndY;
                //  Now compute new bezier control points.
                //  First is halfway between start and first control point
                float m1x, m1y;
                m1x = (x + Elt->BzCtrl1X) / 2;
                m1y = (y + Elt->BzCtrl1Y) / 2;
                //  Temp point, halfway between the 2 two control points
                float m2x, m2y;
                m2x = (Elt->BzCtrl2X + Elt->BzCtrl1X) / 2;
                m2y = (Elt->BzCtrl2Y + Elt->BzCtrl1Y) / 2;
                // Then a point halfway between the second control point and the end point
                float m3x, m3y;
                m3x = (Elt->BzCtrl2X + Elt->EndX) / 2;
                m3y = (Elt->BzCtrl2Y + Elt->EndY) / 2;
                // Then compute a new point in the middle of m1, m2
                float m4x, m4y;
                m4x = (m1x + m2x)/2;
                m4y = (m1y + m2y)/2;
                // Then compute a new point in the middle of m2, m3
                float m5x, m5y;
                m5x = (m2x + m3x)/2;
                m5y = (m2y + m3y)/2;
                // And at last compute a new point in the middle of m4, m5
                float m6x, m6y;
                m6x = (m4x + m5x)/2;
                m6y = (m4y + m5y)/2;
                //  The new control points for the first half are m1 and m4, and it goes up to m6
                Elt->EndX = m6x;
                Elt->EndY = m6y;
                Elt->BzCtrl1X = m1x;
                Elt->BzCtrl1Y = m1y;
                Elt->BzCtrl2X = m4x;
                Elt->BzCtrl2Y = m4y;
                //  The new control points for the second half are m5 and m3
                Elt2->BzCtrl1X = m5x;
                Elt2->BzCtrl1Y = m5y;
                Elt2->BzCtrl2X = m3x;
                Elt2->BzCtrl2Y = m3y;
                path->PolyLine->nElts++;
                continue;           //  Restart procrssing of the segment, which is smaller now.
            }

        }
        //  Change start point for next, which is end of this one
        x = Elt->EndX;
        y = Elt->EndY;
        xmin = fmin(xmin, x);
        xmax = fmax(xmax, x);
        ymin = fmin(ymin, y);
        ymax = fmax(ymax, y);
        //  Then next element
        Elt = Elt->next;
    }
    path->PolyLine->bounds[0] = xmin;
    path->PolyLine->bounds[1] = ymin;
    path->PolyLine->bounds[2] = xmax;
    path->PolyLine->bounds[3] = ymax;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x18(%rbp)
movl $0xf8, %edi
callq 0x9730
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x8(%rbp), %rax
movsd (%rax), %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x8(%rbp), %rax
movsd 0x8(%rax), %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, 0x8(%rax)
movq -0x8(%rbp), %rax
movl 0x18(%rax), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x48(%rax)
movq -0x38(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x60(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x20(%rax)
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq $0x0, -0x48(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x6a49d
movl $0x40, %edi
callq 0x9730
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rsi
movq -0x40(%rbp), %rdi
movl $0x40, %edx
callq 0x9490
movq -0x40(%rbp), %rax
movq $0x0, 0x38(%rax)
cmpq $0x0, -0x48(%rbp)
je 0x6a477
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x38(%rax)
jmp 0x6a487
movq -0x40(%rbp), %rcx
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x6a42f
movq -0x8(%rbp), %rax
movsd (%rax), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movsd 0x8(%rax), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x20(%rbp)
movss -0x1c(%rbp), %xmm0
movss %xmm0, -0x24(%rbp)
movss -0x1c(%rbp), %xmm0
movss %xmm0, -0x28(%rbp)
movss -0x20(%rbp), %xmm0
movss %xmm0, -0x2c(%rbp)
movss -0x20(%rbp), %xmm0
movss %xmm0, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x6a890
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x6a80b
movss -0x1c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movss -0x20(%rbp), %xmm1
cvtss2sd %xmm1, %xmm1
movq -0x18(%rbp), %rdi
callq 0x7fa80
movsd %xmm0, -0x50(%rbp)
movsd -0x50(%rbp), %xmm1
movsd -0x10(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x6a54f
movq -0x18(%rbp), %rax
movl $0x0, (%rax)
jmp 0x6a809
movl $0x40, %edi
callq 0x9730
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x58(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x58(%rbp), %rax
movl $0x1, (%rax)
movq -0x18(%rbp), %rax
movsd 0x8(%rax), %xmm0
movq -0x58(%rbp), %rax
movsd %xmm0, 0x8(%rax)
movq -0x18(%rbp), %rax
movsd 0x10(%rax), %xmm0
movq -0x58(%rbp), %rax
movsd %xmm0, 0x10(%rax)
movss -0x1c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x18(%rbp), %rax
addsd 0x18(%rax), %xmm0
movsd 0x19adf(%rip), %xmm1    # 0x840a0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x5c(%rbp)
movss -0x20(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x18(%rbp), %rax
addsd 0x20(%rax), %xmm0
movsd 0x19ab8(%rip), %xmm1    # 0x840a0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x60(%rbp)
movq -0x18(%rbp), %rax
movsd 0x28(%rax), %xmm0
movq -0x18(%rbp), %rax
addsd 0x18(%rax), %xmm0
movsd 0x19a91(%rip), %xmm1    # 0x840a0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x64(%rbp)
movq -0x18(%rbp), %rax
movsd 0x30(%rax), %xmm0
movq -0x18(%rbp), %rax
addsd 0x20(%rax), %xmm0
movsd 0x19a6a(%rip), %xmm1    # 0x840a0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x68(%rbp)
movq -0x18(%rbp), %rax
movsd 0x28(%rax), %xmm0
movq -0x18(%rbp), %rax
addsd 0x8(%rax), %xmm0
movsd 0x19a43(%rip), %xmm1    # 0x840a0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x6c(%rbp)
movq -0x18(%rbp), %rax
movsd 0x30(%rax), %xmm0
movq -0x18(%rbp), %rax
addsd 0x10(%rax), %xmm0
movsd 0x19a1c(%rip), %xmm1    # 0x840a0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x70(%rbp)
movss -0x5c(%rbp), %xmm0
addss -0x64(%rbp), %xmm0
movss 0x19bf5(%rip), %xmm1    # 0x84298
divss %xmm1, %xmm0
movss %xmm0, -0x74(%rbp)
movss -0x60(%rbp), %xmm0
addss -0x68(%rbp), %xmm0
movss 0x19bda(%rip), %xmm1    # 0x84298
divss %xmm1, %xmm0
movss %xmm0, -0x78(%rbp)
movss -0x64(%rbp), %xmm0
addss -0x6c(%rbp), %xmm0
movss 0x19bbf(%rip), %xmm1    # 0x84298
divss %xmm1, %xmm0
movss %xmm0, -0x7c(%rbp)
movss -0x68(%rbp), %xmm0
addss -0x70(%rbp), %xmm0
movss 0x19ba4(%rip), %xmm1    # 0x84298
divss %xmm1, %xmm0
movss %xmm0, -0x80(%rbp)
movss -0x74(%rbp), %xmm0
addss -0x7c(%rbp), %xmm0
movss 0x19b89(%rip), %xmm1    # 0x84298
divss %xmm1, %xmm0
movss %xmm0, -0x84(%rbp)
movss -0x78(%rbp), %xmm0
addss -0x80(%rbp), %xmm0
movss 0x19b6b(%rip), %xmm1    # 0x84298
divss %xmm1, %xmm0
movss %xmm0, -0x88(%rbp)
movss -0x84(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x18(%rbp), %rax
movsd %xmm0, 0x8(%rax)
movss -0x88(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x18(%rbp), %rax
movsd %xmm0, 0x10(%rax)
movss -0x5c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x18(%rbp), %rax
movsd %xmm0, 0x18(%rax)
movss -0x60(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x18(%rbp), %rax
movsd %xmm0, 0x20(%rax)
movss -0x74(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x18(%rbp), %rax
movsd %xmm0, 0x28(%rax)
movss -0x78(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x18(%rbp), %rax
movsd %xmm0, 0x30(%rax)
movss -0x7c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x58(%rbp), %rax
movsd %xmm0, 0x18(%rax)
movss -0x80(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x58(%rbp), %rax
movsd %xmm0, 0x20(%rax)
movss -0x6c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x58(%rbp), %rax
movsd %xmm0, 0x28(%rax)
movss -0x70(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x58(%rbp), %rax
movsd %xmm0, 0x30(%rax)
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movl 0x18(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x18(%rax)
jmp 0x6a4f8
jmp 0x6a80b
movq -0x18(%rbp), %rax
movsd 0x8(%rax), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movsd 0x10(%rax), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x20(%rbp)
movss -0x24(%rbp), %xmm0
movss -0x1c(%rbp), %xmm1
callq 0x7fbf0
movss %xmm0, -0x24(%rbp)
movss -0x28(%rbp), %xmm0
movss -0x1c(%rbp), %xmm1
callq 0x7fc30
movss %xmm0, -0x28(%rbp)
movss -0x2c(%rbp), %xmm0
movss -0x20(%rbp), %xmm1
callq 0x7fbf0
movss %xmm0, -0x2c(%rbp)
movss -0x30(%rbp), %xmm0
movss -0x20(%rbp), %xmm1
callq 0x7fc30
movss %xmm0, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x6a4f8
movss -0x24(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movsd %xmm0, 0x28(%rax)
movss -0x2c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movsd %xmm0, 0x30(%rax)
movss -0x28(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movsd %xmm0, 0x38(%rax)
movss -0x30(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movsd %xmm0, 0x40(%rax)
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)