#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ puts(char *);
void analyze(char *name, int (*rand_func)(void)) {
  printf("Testing the low-order bits of the result of rand() from %s\n", name);
  int alternating = 1;
  int ones = 0;
  int previous;
  int i;
  for (i = 0; i < 64; i++) {
    const int low_order_bit = rand_func() & 1;
    printf("%d", low_order_bit);
    if (low_order_bit == 1) {
      ones++;
    }
    if (i > 0 && low_order_bit == previous) {
      alternating = 0;
    }
    previous = low_order_bit;
  }
  putchar('\n');
  if (alternating) {
    puts("Low order bits alternate 0 and 1 (bad)");
  } else {
    printf("%d samples, %d zeros, %d ones\n", 64, 64 - ones, ones);
  }
}


x86 assembly (clang, -O0):
<analyze>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xeb5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x40,-0x20(%rbp)
jge    11cf <analyze+0x9f>
call   *-0x10(%rbp)
and    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xeb8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x1,-0x24(%rbp)
jne    119e <analyze+0x6e>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    11bb <analyze+0x8b>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    11bb <analyze+0x8b>
movl   $0x0,-0x14(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1167 <analyze+0x37>
mov    $0xa,%edi
call   1030 <putchar@plt>
cmpl   $0x0,-0x14(%rbp)
je     11f4 <analyze+0xc4>
lea    0xe55(%rip),%rdi
call   1040 <puts@plt>
jmp    1212 <analyze+0xe2>
mov    $0x40,%edx
sub    -0x18(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe60(%rip),%rdi
mov    $0x40,%esi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<analyze>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,(%rsp)
mov    %rdi,%rsi
lea    0xeb7(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebx
mov    $0x40,%r12d
xor    %r13d,%r13d
xchg   %ax,%ax
mov    %ebx,%r14d
call   *(%rsp)
mov    %eax,%ebp
and    $0x1,%ebp
lea    0xeca(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    %ebp,%r13d
cmp    %r15d,%ebp
mov    $0x0,%eax
cmove  %eax,%ebx
cmp    $0x40,%r12d
cmove  %r14d,%ebx
mov    %ebp,%r15d
add    $0xffffffff,%r12d
jne    1160 <analyze+0x30>
mov    $0xa,%edi
call   1030 <putchar@plt>
test   %ebx,%ebx
je     11c2 <analyze+0x92>
lea    0xe90(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <puts@plt>
mov    $0x40,%edx
sub    %r13d,%edx
lea    0xe95(%rip),%rdi
mov    $0x40,%esi
mov    %r13d,%ecx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>

LLVM IR (-O0):
@.str = [60 x i8] c"Testing the low-order bits of the result of rand() from %s\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [39 x i8] c"Low order bits alternate 0 and 1 (bad)\00"
@.str.3 = [31 x i8] c"%d samples, %d zeros, %d ones\0A\00"
define void @analyze(i8* %0, i32 ()* %1) {
%3 = alloca i8*
%4 = alloca i32 ()*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i32 ()* %1, i32 ()** %4
%10 = load i8*, i8** %3
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i8* %10)
store i32 1, i32* %5
store i32 0, i32* %6
store i32 0, i32* %8
br label %12
12: ; preds = %36, %2
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 64
br i1 %14, label %15, label %39
15: ; preds = %12
%16 = load i32 ()*, i32 ()** %4
%17 = call i32 %16()
%18 = and i32 %17, 1
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %9
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %26
23: ; preds = %15
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 1
store i32 %25, i32* %6
br label %26
26: ; preds = %23, %15
%27 = load i32, i32* %8
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i32, i32* %9
%31 = load i32, i32* %7
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 0, i32* %5
br label %34
34: ; preds = %33, %29, %26
%35 = load i32, i32* %9
store i32 %35, i32* %7
br label %36
36: ; preds = %34
%37 = load i32, i32* %8
%38 = add nsw i32 %37, 1
store i32 %38, i32* %8
br label %12
39: ; preds = %12
%40 = call i32 @putchar(i8 signext 10)
%41 = load i32, i32* %5
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 @puts(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %50
45: ; preds = %39
%46 = load i32, i32* %6
%47 = sub nsw i32 64, %46
%48 = load i32, i32* %6
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 64, i32 %47, i32 %48)
br label %50
50: ; preds = %45, %43
ret void
}

LLVM IR (-O2):
@.str = [60 x i8] c"Testing the low-order bits of the result of rand() from %s\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [39 x i8] c"Low order bits alternate 0 and 1 (bad)\00"
@.str.3 = [31 x i8] c"%d samples, %d zeros, %d ones\0A\00"
define void @analyze(i8* %0, i32 ()* nocapture %1) {
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %4
4: ; preds = %2, %4
%5 = phi i32 [ 1, %2 ], [ %16, %4 ]
%6 = phi i32 [ 0, %2 ], [ %17, %4 ]
%7 = phi i32 [ undef, %2 ], [ %10, %4 ]
%8 = phi i32 [ 0, %2 ], [ %12, %4 ]
%9 = tail call i32 %1()
%10 = and i32 %9, 1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = add nuw nsw i32 %10, %8
%13 = icmp ne i32 %6, 0
%14 = icmp eq i32 %10, %7
%15 = select i1 %13, i1 %14, i1 false
%16 = select i1 %15, i32 0, i32 %5
%17 = add nuw nsw i32 %6, 1
%18 = icmp eq i32 %17, 64
br i1 %18, label %19, label %4
19: ; preds = %4
%20 = tail call i32 @putchar(i8 signext 10)
%21 = icmp eq i32 %16, 0
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %27
24: ; preds = %19
%25 = sub nsw i32 64, %12
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 64, i32 %25, i32 %12)
br label %27
27: ; preds = %24, %22
ret void
}