// File: thierry7100[P]CutOptim/include/nanosvg.h
// Function: nsvg__createGradient(NSVGparser*, char const*, double const*, char*)
static NSVGgradient* nsvg__createGradient(NSVGparser* p, const char* id, const double* localBounds, char* paintType)
{
	NSVGattrib* attr = nsvg__getAttr(p);
	NSVGgradientData* data = NULL;
	NSVGgradientData* ref = NULL;
	NSVGgradientStop* stops = NULL;
	NSVGgradient* grad;
	double ox, oy, sw, sh, sl;
	int nstops = 0;

	data = nsvg__findGradientData(p, id);
	if (data == NULL) return NULL;

	// TODO: use ref to fill in all unset values too.
	ref = data;
	while (ref != NULL) {
		if (stops == NULL && ref->stops != NULL) {
			stops = ref->stops;
			nstops = ref->nstops;
			break;
		}
		ref = nsvg__findGradientData(p, ref->ref);
	}
	if (stops == NULL) return NULL;

	grad = (NSVGgradient*)malloc(sizeof(NSVGgradient) + sizeof(NSVGgradientStop)*(nstops-1));
	if (grad == NULL) return NULL;

	// The shape width and height.
	if (data->units == NSVG_OBJECT_SPACE) {
		ox = localBounds[0];
		oy = localBounds[1];
		sw = localBounds[2] - localBounds[0];
		sh = localBounds[3] - localBounds[1];
	} else {
		ox = nsvg__actualOrigX(p);
		oy = nsvg__actualOrigY(p);
		sw = nsvg__actualWidth(p);
		sh = nsvg__actualHeight(p);
	}
	sl = sqrt(sw*sw + sh*sh) / sqrt(2.0);

	if (data->type == NSVG_PAINT_LINEAR_GRADIENT) {
		double x1, y1, x2, y2, dx, dy;
		x1 = nsvg__convertToPixels(p, data->linear.x1, ox, sw);
		y1 = nsvg__convertToPixels(p, data->linear.y1, oy, sh);
		x2 = nsvg__convertToPixels(p, data->linear.x2, ox, sw);
		y2 = nsvg__convertToPixels(p, data->linear.y2, oy, sh);
		// Calculate transform aligned to the line
		dx = x2 - x1;
		dy = y2 - y1;
		grad->xform[0] = dy; grad->xform[1] = -dx;
		grad->xform[2] = dx; grad->xform[3] = dy;
		grad->xform[4] = x1; grad->xform[5] = y1;
	} else {
		double cx, cy, fx, fy, r;
		cx = nsvg__convertToPixels(p, data->radial.cx, ox, sw);
		cy = nsvg__convertToPixels(p, data->radial.cy, oy, sh);
		fx = nsvg__convertToPixels(p, data->radial.fx, ox, sw);
		fy = nsvg__convertToPixels(p, data->radial.fy, oy, sh);
		r = nsvg__convertToPixels(p, data->radial.r, 0, sl);
		// Calculate transform aligned to the circle
		grad->xform[0] = r; grad->xform[1] = 0;
		grad->xform[2] = 0; grad->xform[3] = r;
		grad->xform[4] = cx; grad->xform[5] = cy;
		grad->fx = fx / r;
		grad->fy = fy / r;
	}

	nsvg__xformMultiply(grad->xform, data->xform);
	nsvg__xformMultiply(grad->xform, attr->xform);

	grad->spread = data->spread;
	memcpy(grad->stops, stops, nstops*sizeof(NSVGgradientStop));
	grad->nstops = nstops;

	*paintType = data->type;

	return grad;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rdi
callq 0x7a330
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x7c(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x7f980
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x7f39c
movq $0x0, -0x8(%rbp)
jmp 0x7f965
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x7f3f5
cmpq $0x0, -0x48(%rbp)
jne 0x7f3de
movq -0x40(%rbp), %rax
cmpq $0x0, 0x118(%rax)
je 0x7f3de
movq -0x40(%rbp), %rax
movq 0x118(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
movl 0x110(%rax), %eax
movl %eax, -0x7c(%rbp)
jmp 0x7f3f5
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
addq $0x40, %rsi
callq 0x7f980
movq %rax, -0x40(%rbp)
jmp 0x7f3a4
cmpq $0x0, -0x48(%rbp)
jne 0x7f409
movq $0x0, -0x8(%rbp)
jmp 0x7f965
movl -0x7c(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rdi
shlq $0x4, %rdi
addq $0x60, %rdi
callq 0x9730
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x7f437
movq $0x0, -0x8(%rbp)
jmp 0x7f965
movq -0x38(%rbp), %rax
movsbl 0xd9(%rax), %eax
cmpl $0x1, %eax
jne 0x7f491
movq -0x20(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x58(%rbp)
movq -0x20(%rbp), %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x60(%rbp)
movq -0x20(%rbp), %rax
movsd 0x10(%rax), %xmm0
movq -0x20(%rbp), %rax
subsd (%rax), %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x20(%rbp), %rax
movsd 0x18(%rax), %xmm0
movq -0x20(%rbp), %rax
subsd 0x8(%rax), %xmm0
movsd %xmm0, -0x70(%rbp)
jmp 0x7f4c9
movq -0x10(%rbp), %rdi
callq 0x7f9f0
movsd %xmm0, -0x58(%rbp)
movq -0x10(%rbp), %rdi
callq 0x7fa10
movsd %xmm0, -0x60(%rbp)
movq -0x10(%rbp), %rdi
callq 0x7b2c0
movsd %xmm0, -0x68(%rbp)
movq -0x10(%rbp), %rdi
callq 0x7b2e0
movsd %xmm0, -0x70(%rbp)
movsd -0x68(%rbp), %xmm0
movsd -0x68(%rbp), %xmm2
movsd -0x70(%rbp), %xmm1
mulsd -0x70(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
callq 0x9460
movsd %xmm0, -0x170(%rbp)
movsd 0x4ba6(%rip), %xmm0     # 0x840a0
callq 0x9460
movaps %xmm0, %xmm1
movsd -0x170(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0x38(%rbp), %rax
movsbl 0x80(%rax), %eax
cmpl $0x2, %eax
jne 0x7f6fc
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x88(%rax), %rcx
movq %rcx, -0xc0(%rbp)
movq 0x90(%rax), %rax
movq %rax, -0xb8(%rbp)
movsd -0x58(%rbp), %xmm1
movsd -0x68(%rbp), %xmm2
movsd -0xc0(%rbp), %xmm0
movl -0xb8(%rbp), %esi
callq 0x7b120
movsd %xmm0, -0x88(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x98(%rax), %rcx
movq %rcx, -0xd0(%rbp)
movq 0xa0(%rax), %rax
movq %rax, -0xc8(%rbp)
movsd -0x60(%rbp), %xmm1
movsd -0x70(%rbp), %xmm2
movsd -0xd0(%rbp), %xmm0
movl -0xc8(%rbp), %esi
callq 0x7b120
movsd %xmm0, -0x90(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0xa8(%rax), %rcx
movq %rcx, -0xe0(%rbp)
movq 0xb0(%rax), %rax
movq %rax, -0xd8(%rbp)
movsd -0x58(%rbp), %xmm1
movsd -0x68(%rbp), %xmm2
movsd -0xe0(%rbp), %xmm0
movl -0xd8(%rbp), %esi
callq 0x7b120
movsd %xmm0, -0x98(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0xb8(%rax), %rcx
movq %rcx, -0xf0(%rbp)
movq 0xc0(%rax), %rax
movq %rax, -0xe8(%rbp)
movsd -0x60(%rbp), %xmm1
movsd -0x70(%rbp), %xmm2
movsd -0xf0(%rbp), %xmm0
movl -0xe8(%rbp), %esi
callq 0x7b120
movsd %xmm0, -0xa0(%rbp)
movsd -0x98(%rbp), %xmm0
subsd -0x88(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0xa0(%rbp), %xmm0
subsd -0x90(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd -0xb0(%rbp), %xmm0
movq -0x50(%rbp), %rax
movsd %xmm0, (%rax)
movsd -0xa8(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq -0x50(%rbp), %rax
movsd %xmm0, 0x8(%rax)
movsd -0xa8(%rbp), %xmm0
movq -0x50(%rbp), %rax
movsd %xmm0, 0x10(%rax)
movsd -0xb0(%rbp), %xmm0
movq -0x50(%rbp), %rax
movsd %xmm0, 0x18(%rax)
movsd -0x88(%rbp), %xmm0
movq -0x50(%rbp), %rax
movsd %xmm0, 0x20(%rax)
movsd -0x90(%rbp), %xmm0
movq -0x50(%rbp), %rax
movsd %xmm0, 0x28(%rax)
jmp 0x7f8f4
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x88(%rax), %rcx
movq %rcx, -0x128(%rbp)
movq 0x90(%rax), %rax
movq %rax, -0x120(%rbp)
movsd -0x58(%rbp), %xmm1
movsd -0x68(%rbp), %xmm2
movsd -0x128(%rbp), %xmm0
movl -0x120(%rbp), %esi
callq 0x7b120
movsd %xmm0, -0xf8(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x98(%rax), %rcx
movq %rcx, -0x138(%rbp)
movq 0xa0(%rax), %rax
movq %rax, -0x130(%rbp)
movsd -0x60(%rbp), %xmm1
movsd -0x70(%rbp), %xmm2
movsd -0x138(%rbp), %xmm0
movl -0x130(%rbp), %esi
callq 0x7b120
movsd %xmm0, -0x100(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0xb8(%rax), %rcx
movq %rcx, -0x148(%rbp)
movq 0xc0(%rax), %rax
movq %rax, -0x140(%rbp)
movsd -0x58(%rbp), %xmm1
movsd -0x68(%rbp), %xmm2
movsd -0x148(%rbp), %xmm0
movl -0x140(%rbp), %esi
callq 0x7b120
movsd %xmm0, -0x108(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0xc8(%rax), %rcx
movq %rcx, -0x158(%rbp)
movq 0xd0(%rax), %rax
movq %rax, -0x150(%rbp)
movsd -0x60(%rbp), %xmm1
movsd -0x70(%rbp), %xmm2
movsd -0x158(%rbp), %xmm0
movl -0x150(%rbp), %esi
callq 0x7b120
movsd %xmm0, -0x110(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0xa8(%rax), %rcx
movq %rcx, -0x168(%rbp)
movq 0xb0(%rax), %rax
movq %rax, -0x160(%rbp)
movsd -0x78(%rbp), %xmm2
movsd -0x168(%rbp), %xmm0
movl -0x160(%rbp), %esi
xorps %xmm1, %xmm1
callq 0x7b120
movsd %xmm0, -0x118(%rbp)
movsd -0x118(%rbp), %xmm0
movq -0x50(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x50(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rax)
movq -0x50(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rax)
movsd -0x118(%rbp), %xmm0
movq -0x50(%rbp), %rax
movsd %xmm0, 0x18(%rax)
movsd -0xf8(%rbp), %xmm0
movq -0x50(%rbp), %rax
movsd %xmm0, 0x20(%rax)
movsd -0x100(%rbp), %xmm0
movq -0x50(%rbp), %rax
movsd %xmm0, 0x28(%rax)
movsd -0x108(%rbp), %xmm0
divsd -0x118(%rbp), %xmm0
movq -0x50(%rbp), %rax
movsd %xmm0, 0x38(%rax)
movsd -0x110(%rbp), %xmm0
divsd -0x118(%rbp), %xmm0
movq -0x50(%rbp), %rax
movsd %xmm0, 0x40(%rax)
movq -0x50(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq $0xe0, %rsi
callq 0x7b410
movq -0x50(%rbp), %rdi
movq -0x30(%rbp), %rsi
addq $0x40, %rsi
callq 0x7b410
movq -0x38(%rbp), %rax
movb 0xd8(%rax), %cl
movq -0x50(%rbp), %rax
movb %cl, 0x30(%rax)
movq -0x50(%rbp), %rdi
addq $0x50, %rdi
movq -0x48(%rbp), %rsi
movslq -0x7c(%rbp), %rdx
shlq $0x4, %rdx
callq 0x9490
movl -0x7c(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x48(%rax)
movq -0x38(%rbp), %rax
movb 0x80(%rax), %cl
movq -0x28(%rbp), %rax
movb %cl, (%rax)
movq -0x50(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
nopw %cs:(%rax,%rax)