// File: 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 $0x98, %rsp
movsd %xmm0, 0x90(%rsp)
movsd %xmm1, 0x88(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x90(%rsp), %xmm0
movsd 0x2095(%rip), %xmm1     # 0x11738
ucomisd %xmm1, %xmm0
jne 0xf6f3
jp 0xf6f3
movsd 0x88(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x28(%rsp)
callq 0xf090
callq 0x4290
movaps %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
jmp 0xfa5e
movsd 0x203d(%rip), %xmm0     # 0x11738
ucomisd 0x90(%rsp), %xmm0
jbe 0xf82d
movsd 0x90(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xf82d
movsd 0x2010(%rip), %xmm0     # 0x11738
divsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x1ff9(%rip), %xmm1     # 0x11738
subsd 0x90(%rsp), %xmm1
movsd 0x1fe8(%rip), %xmm0     # 0x11738
divsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movl $0x0, 0x3c(%rsp)
callq 0xf090
movsd %xmm0, 0x68(%rsp)
callq 0xf090
movsd %xmm0, 0x60(%rsp)
movsd 0x68(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
callq 0x45e0
movsd %xmm0, 0x50(%rsp)
movsd 0x60(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
callq 0x45e0
movsd %xmm0, 0x48(%rsp)
movsd 0x50(%rsp), %xmm0
addsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x1f78(%rip), %xmm0     # 0x11738
ucomisd 0x40(%rsp), %xmm0
jb 0xf7d0
movl $0x1, 0x3c(%rsp)
jmp 0xf7d2
cmpl $0x1, 0x3c(%rsp)
jne 0xf762
movsd 0x88(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x20(%rsp)
callq 0xf090
callq 0x4290
movaps %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
mulsd %xmm1, %xmm0
mulsd 0x50(%rsp), %xmm0
divsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
jmp 0xfa5c
movsd 0x90(%rsp), %xmm0
movsd 0x1efa(%rip), %xmm1     # 0x11738
ucomisd %xmm1, %xmm0
jbe 0xfa5a
movsd 0x90(%rsp), %xmm0
movsd 0x1edf(%rip), %xmm1     # 0x11738
subsd %xmm1, %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x90(%rsp), %xmm0
movsd 0x1eb1(%rip), %xmm1     # 0x11728
mulsd %xmm1, %xmm0
movsd 0x20c5(%rip), %xmm1     # 0x11948
addsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
movl $0x0, 0x3c(%rsp)
callq 0xf090
movsd %xmm0, 0x68(%rsp)
callq 0xf090
movsd %xmm0, 0x60(%rsp)
movsd 0x68(%rsp), %xmm0
movsd 0x1e7f(%rip), %xmm1     # 0x11738
subsd 0x68(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x68(%rsp), %xmm0
movsd 0x2049(%rip), %xmm1     # 0x11920
subsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x78(%rsp), %xmm0
divsd 0x58(%rsp), %xmm0
callq 0x4270
movaps %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x80(%rsp), %xmm0
addsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x50(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0xfa38
movsd 0x58(%rsp), %xmm0
movl $0x3, %edi
callq 0x10180
movaps %xmm0, %xmm1
movsd 0x2008(%rip), %xmm0     # 0x11950
mulsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x60(%rsp), %xmm0
movl $0x2, %edi
callq 0x10180
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x40(%rsp), %xmm1
movsd 0x1dad(%rip), %xmm2     # 0x11730
mulsd 0x48(%rsp), %xmm2
mulsd 0x48(%rsp), %xmm2
divsd 0x50(%rsp), %xmm2
movsd 0x1d9b(%rip), %xmm0     # 0x11738
subsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0xf9b4
movl $0x1, 0x3c(%rsp)
jmp 0xfa36
movsd 0x40(%rsp), %xmm0
callq 0x4290
movsd %xmm0, 0x8(%rsp)
movsd 0x80(%rsp), %xmm0
movsd %xmm0, (%rsp)
movsd 0x50(%rsp), %xmm0
divsd 0x80(%rsp), %xmm0
callq 0x4290
movsd (%rsp), %xmm2
movsd 0x8(%rsp), %xmm1
movaps %xmm0, %xmm3
movsd 0x48(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm3, %xmm2
addsd %xmm0, %xmm2
movsd 0x1d0e(%rip), %xmm0     # 0x11730
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0xfa34
movl $0x1, 0x3c(%rsp)
jmp 0xfa36
jmp 0xfa38
jmp 0xfa3a
cmpl $0x1, 0x3c(%rsp)
jne 0xf895
movsd 0x88(%rsp), %xmm0
mulsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
jmp 0xfa5c
jmp 0xfa5e
movsd 0x30(%rsp), %xmm0
addq $0x98, %rsp
retq
nopl (%rax)