// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rcx, %rbp
movq %rdx, %rbx
movq %rdi, %r15
movslq 0xd000(%rdi), %r14
callq 0x33026
testq %rax, %rax
je 0x32d65
movq %rax, %r12
movq %r14, 0x68(%rsp)
xorl %r14d, %r14d
testq %rax, %rax
je 0x33011
movq 0x118(%rax), %r13
testq %r13, %r13
jne 0x32d24
addq $0x40, %rax
movq %r15, %rdi
movq %rax, %rsi
callq 0x33026
jmp 0x32cfe
movq %rbp, 0x60(%rsp)
movl 0x110(%rax), %eax
movl %eax, 0x2c(%rsp)
movslq %eax, %rbp
shlq $0x4, %rbp
leaq 0x50(%rbp), %rdi
callq 0x85b0
testq %rax, %rax
je 0x32d65
movq %rax, %r14
cmpb $0x1, 0xd9(%r12)
jne 0x32d6d
movupd (%rbx), %xmm1
movupd 0x10(%rbx), %xmm2
subpd %xmm1, %xmm2
jmp 0x32d7f
xorl %r14d, %r14d
jmp 0x33011
movupd 0xd050(%r15), %xmm1
movupd 0xd060(%r15), %xmm2
movb 0x80(%r12), %bl
cmpb $0x2, %bl
movapd %xmm2, 0x30(%rsp)
movapd %xmm1, (%rsp)
jne 0x32e71
movsd 0x88(%r12), %xmm0
movl 0x90(%r12), %esi
movq %r15, %rdi
callq 0x319f9
movsd %xmm0, 0x20(%rsp)
movsd 0x98(%r12), %xmm0
movl 0xa0(%r12), %esi
movaps 0x30(%rsp), %xmm1
movhlps %xmm1, %xmm1            # xmm1 = xmm1[1,1]
movaps %xmm1, %xmm2
movaps %xmm1, 0x40(%rsp)
movaps (%rsp), %xmm1
movhlps %xmm1, %xmm1            # xmm1 = xmm1[1,1]
movaps %xmm1, 0x50(%rsp)
movq %r15, %rdi
callq 0x319f9
movsd %xmm0, 0x18(%rsp)
movsd 0xa8(%r12), %xmm0
movl 0xb0(%r12), %esi
movq %r15, %rdi
movaps (%rsp), %xmm1
movaps 0x30(%rsp), %xmm2
callq 0x319f9
movaps %xmm0, (%rsp)
movsd 0xb8(%r12), %xmm0
movl 0xc0(%r12), %esi
movq %r15, %rdi
movaps 0x50(%rsp), %xmm1
movaps 0x40(%rsp), %xmm2
callq 0x319f9
movapd (%rsp), %xmm4
movsd 0x18(%rsp), %xmm2
movsd 0x20(%rsp), %xmm1
subsd %xmm1, %xmm4
subsd %xmm2, %xmm0
movsd %xmm0, (%r14)
movapd 0x3258(%rip), %xmm3     # 0x360c0
xorpd %xmm4, %xmm3
jmp 0x32f9b
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
movapd %xmm0, 0x40(%rsp)
mulsd %xmm0, %xmm0
movapd %xmm2, %xmm3
mulsd %xmm2, %xmm3
addsd %xmm0, %xmm3
sqrtsd %xmm3, %xmm0
divsd 0x32fd(%rip), %xmm0     # 0x36198
movsd %xmm0, 0x50(%rsp)
movsd 0x88(%r12), %xmm0
movl 0x90(%r12), %esi
movq %r15, %rdi
callq 0x319f9
movsd %xmm0, 0x20(%rsp)
movsd 0x98(%r12), %xmm0
movl 0xa0(%r12), %esi
movaps (%rsp), %xmm1
movhlps %xmm1, %xmm1            # xmm1 = xmm1[1,1]
movaps %xmm1, 0x70(%rsp)
movq %r15, %rdi
movaps 0x40(%rsp), %xmm2
callq 0x319f9
movsd %xmm0, 0x18(%rsp)
movsd 0xb8(%r12), %xmm0
movl 0xc0(%r12), %esi
movq %r15, %rdi
movaps (%rsp), %xmm1
movaps 0x30(%rsp), %xmm2
callq 0x319f9
movaps %xmm0, (%rsp)
movsd 0xc8(%r12), %xmm0
movl 0xd0(%r12), %esi
movq %r15, %rdi
movaps 0x70(%rsp), %xmm1
movaps 0x40(%rsp), %xmm2
callq 0x319f9
movaps %xmm0, 0x30(%rsp)
movsd 0xa8(%r12), %xmm0
movl 0xb0(%r12), %esi
xorps %xmm1, %xmm1
movq %r15, %rdi
movsd 0x50(%rsp), %xmm2
callq 0x319f9
xorpd %xmm3, %xmm3
movsd 0x18(%rsp), %xmm2
movsd %xmm0, (%r14)
movapd (%rsp), %xmm4
unpcklpd 0x30(%rsp), %xmm4       # xmm4 = xmm4[0],mem[0]
movapd %xmm0, %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
divpd %xmm1, %xmm4
movsd 0x20(%rsp), %xmm1
movupd %xmm4, 0x38(%r14)
xorpd %xmm4, %xmm4
movsd %xmm3, 0x8(%r14)
movsd %xmm4, 0x10(%r14)
movsd %xmm0, 0x18(%r14)
movsd %xmm1, 0x20(%r14)
movsd %xmm2, 0x28(%r14)
movq %r12, %rsi
addq $0xe0, %rsi
movq %r14, %rdi
callq 0x31a7c
imulq $0x1a0, 0x68(%rsp), %rax # imm = 0x1A0
leaq (%r15,%rax), %rsi
addq $0x40, %rsi
movq %r14, %rdi
callq 0x31a7c
movb 0xd8(%r12), %al
movb %al, 0x30(%r14)
movq %r14, %rdi
addq $0x50, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x83a0
movl 0x2c(%rsp), %eax
movl %eax, 0x48(%r14)
movq 0x60(%rsp), %rax
movb %bl, (%rax)
movq %r14, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq