// File: /workspace/github_cpp_makefile_star2_O2/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 0x18a7(%rip), %xmm0     # 0x85a8
jne 0x6d2d
jp 0x6d2d
xorpd 0x1ab3(%rip), %xmm1     # 0x87c0
movapd %xmm1, 0x20(%rsp)
callq 0x69af
callq 0x31f0
mulsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rsp)
jmp 0x6f74
movsd 0x1873(%rip), %xmm2     # 0x85a8
ucomisd %xmm0, %xmm2
jbe 0x6e07
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm0
jbe 0x6e07
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 0x69af
movsd %xmm0, (%rsp)
callq 0x69af
movsd %xmm0, 0x8(%rsp)
movsd (%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
callq 0x3450
movsd %xmm0, (%rsp)
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
callq 0x3450
movapd %xmm0, %xmm1
addsd (%rsp), %xmm1
movsd 0x17ea(%rip), %xmm0     # 0x85a8
ucomisd %xmm1, %xmm0
jb 0x6d72
movapd 0x20(%rsp), %xmm0
xorpd 0x19ee(%rip), %xmm0     # 0x87c0
movapd %xmm0, 0x20(%rsp)
movsd %xmm1, 0x8(%rsp)
callq 0x69af
callq 0x31f0
mulsd 0x20(%rsp), %xmm0
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
divsd 0x8(%rsp), %xmm1
movsd %xmm1, (%rsp)
jmp 0x6f74
xorpd %xmm2, %xmm2
movsd %xmm2, (%rsp)
ucomisd 0x1790(%rip), %xmm0     # 0x85a8
jbe 0x6f74
movaps %xmm1, 0x20(%rsp)
movsd 0x1945(%rip), %xmm1     # 0x8770
addsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
mulsd 0x177b(%rip), %xmm0     # 0x85b8
addsd 0x193b(%rip), %xmm0     # 0x8780
movsd %xmm0, 0x30(%rsp)
callq 0x69af
movsd %xmm0, 0x8(%rsp)
callq 0x69af
movsd 0x8(%rsp), %xmm2
movapd %xmm0, %xmm3
movsd 0x173b(%rip), %xmm1     # 0x85a8
subsd %xmm2, %xmm1
mulsd %xmm2, %xmm1
addsd 0x190b(%rip), %xmm2     # 0x8788
movsd 0x30(%rsp), %xmm0
divsd %xmm1, %xmm0
ucomisd 0x1909(%rip), %xmm0     # 0x8798
movsd %xmm3, 0x10(%rsp)
jb 0x6e9d
sqrtsd %xmm0, %xmm0
jmp 0x6eb8
movsd %xmm2, 0x8(%rsp)
movsd %xmm1, (%rsp)
callq 0x31e0
movsd (%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
mulsd %xmm0, %xmm2
movsd 0x18(%rsp), %xmm0
addsd %xmm2, %xmm0
movsd %xmm0, (%rsp)
ucomisd 0x18c5(%rip), %xmm0     # 0x8798
jb 0x6e4b
movapd %xmm1, %xmm0
movsd 0x16d3(%rip), %xmm1     # 0x85b8
movsd %xmm2, 0x8(%rsp)
callq 0x3450
movsd 0x10(%rsp), %xmm3
movsd 0x8(%rsp), %xmm2
mulsd 0x188c(%rip), %xmm0     # 0x8790
mulsd %xmm3, %xmm3
mulsd %xmm0, %xmm3
movapd %xmm2, %xmm0
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm0
divsd (%rsp), %xmm0
movsd 0x1683(%rip), %xmm1     # 0x85a8
subsd %xmm0, %xmm1
ucomisd %xmm3, %xmm1
jae 0x6f6a
movapd %xmm3, %xmm0
callq 0x31f0
movsd %xmm0, 0x10(%rsp)
movsd (%rsp), %xmm0
divsd 0x18(%rsp), %xmm0
callq 0x31f0
mulsd 0x18(%rsp), %xmm0
subsd 0x8(%rsp), %xmm0
addsd %xmm0, %xmm0
ucomisd 0x10(%rsp), %xmm0
jb 0x6e4b
movsd (%rsp), %xmm0
jmp 0x6d1d
movsd (%rsp), %xmm0
addq $0x38, %rsp
retq