// File: /workspace/github_cpp_makefile_star2_O1/auton1[P]motiflocation/random.cpp
// Function: gamrnd(double, double)
double gamrnd(double ia, double ib)
{
	double b, c, d, u, v, w, x, y, z;
	int accept;
	double ret = 0;

	if (ia == 1.0)
	{
		// gamma is exponetial (Devroye pg 405)
		ret = -ib * log(ran2());
	}
	else if ((ia < 1) && (ia > 0))
	{
		c = 1 / ia;
		d = 1 / (1 - ia);
		accept = 0;
		do 
		{
			u = ran2();
			v = ran2();
			x = pow(u, c);
			y = pow(v, d);
			z = x + y;
			if (z <= 1.0)
				accept = 1;
		} while (accept != 1);

		ret = -ib * log(ran2()) * x / z;
	}
	else if (ia > 1)
	{
		b = ia - 1;
		c = 3.0 * ia - 0.75;
		accept = 0;
		do
		{
			u = ran2();
			v = ran2();
			w = u * (1 - u);
			y = (u - 0.5) * sqrt(c / w);
			x = b + y;
			if (x >= 0.0)
			{
				z = 64.0 * pow(w, 3) * pow(v, 2);
				if (z <= (1 - 2 * y * y / x))
				{ accept = 1; }
				else
				{	
					if (log(z) <= (2 * (b * log(x / b) - y)))
					{ accept = 1; }
				}
			}
		} while (accept != 1);

			ret = ib * x;
	}
	return ret;
}

// Assembly:
subq $0x38, %rsp
ucomisd 0x17ab(%rip), %xmm0     # 0x95d0
jne 0x7e51
jp 0x7e51
xorpd 0x1a1f(%rip), %xmm1     # 0x9850
movapd %xmm1, 0x20(%rsp)
callq 0x7a7b
callq 0x31d0
mulsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rsp)
jmp 0x8098
movsd 0x1777(%rip), %xmm2     # 0x95d0
ucomisd %xmm0, %xmm2
jbe 0x7f2b
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm0
jbe 0x7f2b
movaps %xmm1, 0x20(%rsp)
movapd %xmm2, %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
divsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rsp)
callq 0x7a7b
movsd %xmm0, (%rsp)
callq 0x7a7b
movsd %xmm0, 0x8(%rsp)
movsd (%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
callq 0x34c0
movsd %xmm0, (%rsp)
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
callq 0x34c0
movapd %xmm0, %xmm1
addsd (%rsp), %xmm1
movsd 0x16ee(%rip), %xmm0     # 0x95d0
ucomisd %xmm1, %xmm0
jb 0x7e96
movapd 0x20(%rsp), %xmm0
xorpd 0x195a(%rip), %xmm0     # 0x9850
movapd %xmm0, 0x20(%rsp)
movsd %xmm1, 0x8(%rsp)
callq 0x7a7b
callq 0x31d0
mulsd 0x20(%rsp), %xmm0
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
divsd 0x8(%rsp), %xmm1
movsd %xmm1, (%rsp)
jmp 0x8098
xorpd %xmm2, %xmm2
movsd %xmm2, (%rsp)
ucomisd 0x1694(%rip), %xmm0     # 0x95d0
jbe 0x8098
movaps %xmm1, 0x20(%rsp)
movsd 0x18b1(%rip), %xmm1     # 0x9800
addsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
mulsd 0x167f(%rip), %xmm0     # 0x95e0
addsd 0x18a7(%rip), %xmm0     # 0x9810
movsd %xmm0, 0x30(%rsp)
callq 0x7a7b
movsd %xmm0, 0x8(%rsp)
callq 0x7a7b
movsd 0x8(%rsp), %xmm2
movapd %xmm0, %xmm3
movsd 0x163f(%rip), %xmm1     # 0x95d0
subsd %xmm2, %xmm1
mulsd %xmm2, %xmm1
addsd 0x1877(%rip), %xmm2     # 0x9818
movsd 0x30(%rsp), %xmm0
divsd %xmm1, %xmm0
ucomisd 0x1875(%rip), %xmm0     # 0x9828
movsd %xmm3, 0x10(%rsp)
jb 0x7fc1
sqrtsd %xmm0, %xmm0
jmp 0x7fdc
movsd %xmm2, 0x8(%rsp)
movsd %xmm1, (%rsp)
callq 0x31c0
movsd (%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
mulsd %xmm0, %xmm2
movsd 0x18(%rsp), %xmm0
addsd %xmm2, %xmm0
movsd %xmm0, (%rsp)
ucomisd 0x1831(%rip), %xmm0     # 0x9828
jb 0x7f6f
movapd %xmm1, %xmm0
movsd 0x15d7(%rip), %xmm1     # 0x95e0
movsd %xmm2, 0x8(%rsp)
callq 0x34c0
movsd 0x10(%rsp), %xmm3
movsd 0x8(%rsp), %xmm2
mulsd 0x17f8(%rip), %xmm0     # 0x9820
mulsd %xmm3, %xmm3
mulsd %xmm0, %xmm3
movapd %xmm2, %xmm0
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm0
divsd (%rsp), %xmm0
movsd 0x1587(%rip), %xmm1     # 0x95d0
subsd %xmm0, %xmm1
ucomisd %xmm3, %xmm1
jae 0x808e
movapd %xmm3, %xmm0
callq 0x31d0
movsd %xmm0, 0x10(%rsp)
movsd (%rsp), %xmm0
divsd 0x18(%rsp), %xmm0
callq 0x31d0
mulsd 0x18(%rsp), %xmm0
subsd 0x8(%rsp), %xmm0
addsd %xmm0, %xmm0
ucomisd 0x10(%rsp), %xmm0
jb 0x7f6f
movsd (%rsp), %xmm0
jmp 0x7e41
movsd (%rsp), %xmm0
addq $0x38, %rsp
retq