// File: auton1[P]motiflocation/random.cpp
// Function: bnlrnd(double, int)
int bnlrnd(double pp, int n)
{
	int j;
	static int nold=(-1);
	double am,em,g,angle,p,bnl,sq,t,y;
	static double pold=(-1.0),pc,plog,pclog,en,oldg;
	p=(pp <= 0.5 ? pp : 1.0-pp);
	am=n*p;		//This is the mean of the deviate to be produced.
	if (n < 25) { 
		bnl = 0.0;
		for (j=1;j<=n;j++)
			if (ran2() < p) ++bnl;
	} else if (am < 1.0) { 
		g=exp(-am);
		t=1.0;
		for (j=0;j<=n;j++) {
			t *= ran2();
			if (t < g) break;
		}
		bnl=(j <= n ? j : n);
	} else {
		if (n != nold) { 
			en=n; 
			oldg=gammln(en+1.0);
			nold=n;
		} if (p != pold) { 
			pc=1.0-p; 
			plog=log(p);
			pclog=log(pc);
			pold=p;
		}
		sq=sqrt(2.0*am*pc); 
		do {
			do {
				angle=PI*ran2();
				y=tan(angle);
				em=sq*y+am;
			} while (em < 0.0 || em >= (en+1.0)); 
			em=floor(em); 
			t=1.2*sq*(1.0+y*y)*exp(oldg-gammln(em+1.0)-gammln(en-em+1.0)+em*plog+(en-em)*pclog);
		} while (ran2() > t); bnl=em; 
	}
	if (p != pp) bnl=n-bnl; 
	return (int)bnl;
}

// Assembly:
subq $0x88, %rsp
movsd %xmm0, 0x80(%rsp)
movl %edi, 0x7c(%rsp)
movsd 0x1ba4(%rip), %xmm0     # 0x11920
ucomisd 0x80(%rsp), %xmm0
jb 0xfd98
movsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
jmp 0xfdaf
movsd 0x1998(%rip), %xmm0     # 0x11738
subsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
cvtsi2sdl 0x7c(%rsp), %xmm0
mulsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
cmpl $0x19, 0x7c(%rsp)
jge 0xfe2b
xorps %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
movl $0x1, 0x78(%rsp)
movl 0x78(%rsp), %eax
cmpl 0x7c(%rsp), %eax
jg 0xfe26
callq 0xf090
movaps %xmm0, %xmm1
movsd 0x50(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xfe17
movsd 0x192d(%rip), %xmm0     # 0x11738
addsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
jmp 0xfe19
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0xfde5
jmp 0x10147
movsd 0x1905(%rip), %xmm0     # 0x11738
ucomisd 0x70(%rsp), %xmm0
jbe 0xfeea
movsd 0x70(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
callq 0x4620
movsd %xmm0, 0x60(%rsp)
movsd 0x18c9(%rip), %xmm0     # 0x11738
movsd %xmm0, 0x38(%rsp)
movl $0x0, 0x78(%rsp)
movl 0x78(%rsp), %eax
cmpl 0x7c(%rsp), %eax
jg 0xfebb
callq 0xf090
mulsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0x38(%rsp), %xmm1
movsd 0x60(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xfeac
jmp 0xfebb
jmp 0xfeae
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0xfe7d
movl 0x78(%rsp), %eax
cmpl 0x7c(%rsp), %eax
jg 0xfecf
movl 0x78(%rsp), %eax
movl %eax, 0x24(%rsp)
jmp 0xfed7
movl 0x7c(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x48(%rsp)
jmp 0x10145
movl 0x7c(%rsp), %eax
cmpl 0x74e4(%rip), %eax      # 0x173d8
je 0xff2b
cvtsi2sdl 0x7c(%rsp), %xmm0
movsd %xmm0, 0x765c(%rip)     # 0x17560
movsd 0x182c(%rip), %xmm0     # 0x11738
addsd 0x764c(%rip), %xmm0     # 0x17560
callq 0xf440
movsd %xmm0, 0x7647(%rip)     # 0x17568
movl 0x7c(%rsp), %eax
movl %eax, 0x74ad(%rip)      # 0x173d8
movsd 0x50(%rsp), %xmm0
ucomisd 0x74a7(%rip), %xmm0     # 0x173e0
jne 0xff3f
jp 0xff3f
jmp 0xff8b
movsd 0x17f1(%rip), %xmm0     # 0x11738
subsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x75f3(%rip)     # 0x17548
movsd 0x50(%rsp), %xmm0
callq 0x4290
movsd %xmm0, 0x75e8(%rip)     # 0x17550
movsd 0x75d8(%rip), %xmm0     # 0x17548
callq 0x4290
movsd %xmm0, 0x75db(%rip)     # 0x17558
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x7455(%rip)     # 0x173e0
movsd 0x179d(%rip), %xmm0     # 0x11730
mulsd 0x70(%rsp), %xmm0
mulsd 0x75a7(%rip), %xmm0     # 0x17548
callq 0x4270
movsd %xmm0, 0x40(%rsp)
jmp 0xffae
callq 0xf090
movaps %xmm0, %xmm1
movsd 0x19a2(%rip), %xmm0     # 0x11960
mulsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x58(%rsp), %xmm0
callq 0x41d0
movsd %xmm0, 0x30(%rsp)
movsd 0x40(%rsp), %xmm0
movsd 0x30(%rsp), %xmm2
movsd 0x70(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
movb $0x1, %al
xorps %xmm0, %xmm0
ucomisd 0x68(%rsp), %xmm0
movb %al, 0x23(%rsp)
ja 0x1002b
movsd 0x68(%rsp), %xmm0
movsd 0x1720(%rip), %xmm1     # 0x11738
addsd 0x7540(%rip), %xmm1     # 0x17560
ucomisd %xmm1, %xmm0
setae %al
movb %al, 0x23(%rsp)
movb 0x23(%rsp), %al
testb $0x1, %al
jne 0xffae
movsd 0x68(%rsp), %xmm0
callq 0x4580
movsd %xmm0, 0x68(%rsp)
movsd 0x1920(%rip), %xmm0     # 0x11970
mulsd 0x40(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
mulsd %xmm2, %xmm1
movsd 0x16ca(%rip), %xmm2     # 0x11738
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x74e4(%rip), %xmm0     # 0x17568
movsd %xmm0, 0x8(%rsp)
movsd 0x16a6(%rip), %xmm0     # 0x11738
addsd 0x68(%rsp), %xmm0
callq 0xf440
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x74a8(%rip), %xmm0     # 0x17560
subsd 0x68(%rsp), %xmm0
movsd 0x1672(%rip), %xmm1     # 0x11738
addsd %xmm1, %xmm0
callq 0xf440
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd 0x68(%rsp), %xmm1
movsd 0x7466(%rip), %xmm2     # 0x17550
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0x7466(%rip), %xmm0     # 0x17560
subsd 0x68(%rsp), %xmm0
movsd 0x7450(%rip), %xmm2     # 0x17558
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
callq 0x4620
movaps %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
callq 0xf090
ucomisd 0x38(%rsp), %xmm0
ja 0xffac
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
jmp 0x10147
movsd 0x50(%rsp), %xmm0
ucomisd 0x80(%rsp), %xmm0
jne 0x1015c
jp 0x1015c
jmp 0x1016e
cvtsi2sdl 0x7c(%rsp), %xmm0
subsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
cvttsd2si 0x48(%rsp), %eax
addq $0x88, %rsp
retq
nopl (%rax)