void testBernoulli() {
  double p = 1e-16;
  UnitTest_expect_eq_unsigned((0), (Random_bernoulli(p)), __func__, 12);
  UnitTest_expect_eq_unsigned((1), (Random_bernoulli(1 - p)), __func__, 14);
  p = .5;
  const unsigned n = 100;
  unsigned count0 = 0;
  unsigned count1 = 0;
  for (unsigned i = 0; i < n; i++) {
    const unsigned result = Random_bernoulli(p);
    if (result == 1)
      count1++;
    else if (result == 0)
      count0++;
    else
      UnitTest_expect_fail(("p not 1 or 0"), __func__, 27);
    ;
  }
  UnitTest_expect_true((count0 > 0 && count1 > 0), __func__, 29);
  ;
}


x86 assembly (clang, -O0):
<testBernoulli>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  0xec0(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
mov    $0x1,%al
call   1040 <Random_bernoulli@plt>
mov    %eax,%esi
xor    %edi,%edi
lea    0xeac(%rip),%rdx
mov    $0xc,%ecx
mov    $0x0,%al
call   1050 <UnitTest_expect_eq_unsigned@plt>
movsd  0xe88(%rip),%xmm0
subsd  -0x8(%rbp),%xmm0
mov    $0x1,%al
call   1040 <Random_bernoulli@plt>
mov    %eax,%esi
mov    $0x1,%edi
lea    0xe7e(%rip),%rdx
mov    $0xe,%ecx
mov    $0x0,%al
call   1050 <UnitTest_expect_eq_unsigned@plt>
movsd  0xe52(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
movl   $0x64,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x64,-0x18(%rbp)
jae    124a <testBernoulli+0x10a>
movsd  -0x8(%rbp),%xmm0
mov    $0x1,%al
call   1040 <Random_bernoulli@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jne    1200 <testBernoulli+0xc0>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1237 <testBernoulli+0xf7>
cmpl   $0x0,-0x1c(%rbp)
jne    1218 <testBernoulli+0xd8>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1232 <testBernoulli+0xf2>
lea    0xe07(%rip),%rdi
lea    0xdf2(%rip),%rsi
mov    $0x1b,%edx
mov    $0x0,%al
call   1030 <UnitTest_expect_fail@plt>
jmp    1237 <testBernoulli+0xf7>
jmp    123c <testBernoulli+0xfc>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11cf <testBernoulli+0x8f>
xor    %eax,%eax
cmpl   $0x0,-0x10(%rbp)
mov    %al,-0x1d(%rbp)
jbe    1263 <testBernoulli+0x123>
cmpl   $0x0,-0x14(%rbp)
seta   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
lea    0xda6(%rip),%rsi
mov    $0x1d,%edx
mov    $0x0,%al
call   1060 <UnitTest_expect_true@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testBernoulli>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
movsd  0xeb0(%rip),%xmm0
mov    $0x1,%al
call   1040 <Random_bernoulli@plt>
lea    0xeba(%rip),%r14
xor    %edi,%edi
mov    %eax,%esi
mov    %r14,%rdx
mov    $0xc,%ecx
xor    %eax,%eax
call   1050 <UnitTest_expect_eq_unsigned@plt>
movsd  0xe8f(%rip),%xmm0
mov    $0x1,%al
call   1040 <Random_bernoulli@plt>
mov    $0x1,%edi
mov    %eax,%esi
mov    %r14,%rdx
mov    $0xe,%ecx
xor    %eax,%eax
call   1050 <UnitTest_expect_eq_unsigned@plt>
mov    $0x64,%ebp
lea    0xe84(%rip),%r15
xor    %r12d,%r12d
xor    %ebx,%ebx
jmp    11c7 <testBernoulli+0x87>
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x1b,%edx
xor    %eax,%eax
call   1030 <UnitTest_expect_fail@plt>
add    $0xffffffff,%ebp
je     11f5 <testBernoulli+0xb5>
movsd  0xe41(%rip),%xmm0
mov    $0x1,%al
call   1040 <Random_bernoulli@plt>
test   %eax,%eax
je     11f0 <testBernoulli+0xb0>
cmp    $0x1,%eax
jne    11b0 <testBernoulli+0x70>
add    $0x1,%r12d
jmp    11c2 <testBernoulli+0x82>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%ebx
jmp    11c2 <testBernoulli+0x82>
test   %ebx,%ebx
setne  %al
test   %r12d,%r12d
setne  %cl
and    %al,%cl
movzbl %cl,%edi
lea    0xe0c(%rip),%rsi
mov    $0x1d,%edx
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <UnitTest_expect_true@plt>

LLVM IR (-O0):
@__func__.testBernoulli = [14 x i8] c"testBernoulli\00"
@.str = [13 x i8] c"p not 1 or 0\00"
define void @testBernoulli() {
%1 = alloca double
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store double 0x3C9CD2B297D889BC, double* %1
%7 = load double, double* %1
%8 = call i32 (double, ...) bitcast (i32 (...)* @Random_bernoulli to i32 (double, ...)*)(double %7)
%9 = call i32 (i32, i32, i8*, i32, ...) bitcast (i32 (...)* @UnitTest_expect_eq_unsigned to i32 (i32, i32, i8*, i32, ...)*)(i32 0, i32 %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__func__.testBernoulli, i64 0, i64 0), i32 12)
%10 = load double, double* %1
%11 = fsub double 1.000000e+00, %10
%12 = call i32 (double, ...) bitcast (i32 (...)* @Random_bernoulli to i32 (double, ...)*)(double %11)
%13 = call i32 (i32, i32, i8*, i32, ...) bitcast (i32 (...)* @UnitTest_expect_eq_unsigned to i32 (i32, i32, i8*, i32, ...)*)(i32 1, i32 %12, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__func__.testBernoulli, i64 0, i64 0), i32 14)
store double 5.000000e-01, double* %1
store i32 100, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
br label %14
14: ; preds = %35, %0
%15 = load i32, i32* %5
%16 = icmp ult i32 %15, 100
br i1 %16, label %17, label %38
17: ; preds = %14
%18 = load double, double* %1
%19 = call i32 (double, ...) bitcast (i32 (...)* @Random_bernoulli to i32 (double, ...)*)(double %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* %4
%24 = add i32 %23, 1
store i32 %24, i32* %4
br label %34
25: ; preds = %17
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* %3
%30 = add i32 %29, 1
store i32 %30, i32* %3
br label %33
31: ; preds = %25
%32 = call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @UnitTest_expect_fail to i32 (i8*, i8*, i32, ...)*)(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__func__.testBernoulli, i64 0, i64 0), i32 27)
br label %33
33: ; preds = %31, %28
br label %34
34: ; preds = %33, %22
br label %35
35: ; preds = %34
%36 = load i32, i32* %5
%37 = add i32 %36, 1
store i32 %37, i32* %5
br label %14
38: ; preds = %14
%39 = load i32, i32* %3
%40 = icmp ugt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load i32, i32* %4
%43 = icmp ugt i32 %42, 0
br label %44
44: ; preds = %41, %38
%45 = phi i1 [ false, %38 ], [ %43, %41 ]
%46 = zext i1 %45 to i32
%47 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @UnitTest_expect_true to i32 (i32, i8*, i32, ...)*)(i32 %46, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__func__.testBernoulli, i64 0, i64 0), i32 29)
ret void
}

LLVM IR (-O2):
@__func__.testBernoulli = [14 x i8] c"testBernoulli\00"
@.str = [13 x i8] c"p not 1 or 0\00"
define void @testBernoulli() {
%1 = tail call i32 (double, ...) bitcast (i32 (...)* @Random_bernoulli to i32 (double, ...)*)(double 0x3C9CD2B297D889BC)
%2 = tail call i32 (i32, i32, i8*, i32, ...) bitcast (i32 (...)* @UnitTest_expect_eq_unsigned to i32 (i32, i32, i8*, i32, ...)*)(i32 0, i32 %1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__func__.testBernoulli, i64 0, i64 0), i32 12)
%3 = tail call i32 (double, ...) bitcast (i32 (...)* @Random_bernoulli to i32 (double, ...)*)(double 0x3FEFFFFFFFFFFFFF)
%4 = tail call i32 (i32, i32, i8*, i32, ...) bitcast (i32 (...)* @UnitTest_expect_eq_unsigned to i32 (i32, i32, i8*, i32, ...)*)(i32 1, i32 %3, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__func__.testBernoulli, i64 0, i64 0), i32 14)
br label %11
5: ; preds = %22
%6 = icmp ne i32 %23, 0
%7 = icmp ne i32 %24, 0
%8 = select i1 %6, i1 %7, i1 false
%9 = zext i1 %8 to i32
%10 = tail call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @UnitTest_expect_true to i32 (i32, i8*, i32, ...)*)(i32 %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__func__.testBernoulli, i64 0, i64 0), i32 29)
ret void
11: ; preds = %0, %22
%12 = phi i32 [ 0, %0 ], [ %25, %22 ]
%13 = phi i32 [ 0, %0 ], [ %24, %22 ]
%14 = phi i32 [ 0, %0 ], [ %23, %22 ]
%15 = tail call i32 (double, ...) bitcast (i32 (...)* @Random_bernoulli to i32 (double, ...)*)(double 5.000000e-01)
switch i32 %15, label %20 [
i32 1, label %16
i32 0, label %18
]
16: ; preds = %11
%17 = add i32 %13, 1
br label %22
18: ; preds = %11
%19 = add i32 %14, 1
br label %22
20: ; preds = %11
%21 = tail call i32 (i8*, i8*, i32, ...) bitcast (i32 (...)* @UnitTest_expect_fail to i32 (i8*, i8*, i32, ...)*)(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__func__.testBernoulli, i64 0, i64 0), i32 27)
br label %22
22: ; preds = %18, %20, %16
%23 = phi i32 [ %14, %16 ], [ %19, %18 ], [ %14, %20 ]
%24 = phi i32 [ %17, %16 ], [ %13, %18 ], [ %13, %20 ]
%25 = add nuw nsw i32 %12, 1
%26 = icmp eq i32 %25, 100
br i1 %26, label %5, label %11
}