#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 rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
void fig06_09() {
  size_t face;
  unsigned int roll;
  unsigned int frequency[7] = {0};
  srand(time(NULL));
  for (roll = 1; roll <= 6000000; ++roll) {
    face = 1 + rand() % 6;
    ++frequency[face];
  }
  printf("%s%17s\n", "Face", "Frequency");
  for (face = 1; face < 7; ++face) {
    printf("%4d%17d\n", face, frequency[face]);
  }
}


x86 assembly (clang, -O0):
<fig06_09>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1040 <memset@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
movl   $0x1,-0xc(%rbp)
cmpl   $0x5b8d80,-0xc(%rbp)
ja     11c2 <fig06_09+0x72>
mov    $0x0,%al
call   1070 <rand@plt>
mov    $0x6,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
movslq %edx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%ecx
add    $0x1,%ecx
mov    %ecx,-0x30(%rbp,%rax,4)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    117f <fig06_09+0x2f>
lea    0xe37(%rip),%rdi
lea    0xe38(%rip),%rsi
lea    0xe36(%rip),%rdx
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x1,-0x8(%rbp)
cmpq   $0x7,-0x8(%rbp)
jae    121c <fig06_09+0xcc>
mov    -0x8(%rbp),%rsi
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%edx
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    11e6 <fig06_09+0x96>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fig06_09>:
push   %rbx
sub    $0x20,%rsp
xorps  %xmm0,%xmm0
movups %xmm0,0xc(%rsp)
movaps %xmm0,(%rsp)
xor    %edi,%edi
call   1050 <time@plt>
mov    %eax,%edi
call   1040 <srand@plt>
mov    $0x5b8d80,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1060 <rand@plt>
cltq
imul   $0x2aaaaaab,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,2),%ecx
sub    %ecx,%eax
cltq
addl   $0x1,0x4(%rsp,%rax,4)
add    $0xffffffff,%ebx
jne    1170 <fig06_09+0x30>
lea    0xe59(%rip),%rdi
lea    0xe5a(%rip),%rsi
lea    0xe58(%rip),%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x4(%rsp),%edx
lea    0xe50(%rip),%rbx
mov    $0x1,%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x8(%rsp),%edx
mov    $0x2,%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0xc(%rsp),%edx
mov    $0x3,%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x10(%rsp),%edx
mov    $0x4,%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x14(%rsp),%edx
mov    $0x5,%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x18(%rsp),%edx
mov    $0x6,%esi
mov    %rbx,%rdi
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
jmp    1030 <printf@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"%s%17s\0A\00"
@.str.1 = [5 x i8] c"Face\00"
@.str.2 = [10 x i8] c"Frequency\00"
@.str.3 = [9 x i8] c"%4d%17d\0A\00"
define void @fig06_09() {
%1 = alloca i64
%2 = alloca i32
%3 = alloca [7 x i32]
%4 = bitcast [7 x i32]* %3 to i8*
%5 = call i32 @time(i32* null)
%6 = call i32 @srand(i32 %5)
store i32 1, i32* %2
br label %7
7: ; preds = %19, %0
%8 = load i32, i32* %2
%9 = icmp ule i32 %8, 6000000
br i1 %9, label %10, label %22
10: ; preds = %7
%11 = call i32 (...) @rand()
%12 = srem i32 %11, 6
%13 = add nsw i32 1, %12
%14 = sext i32 %13 to i64
store i64 %14, i64* %1
%15 = load i64, i64* %1
%16 = getelementptr inbounds [7 x i32], [7 x i32]* %3, i64 0, i64 %15
%17 = load i32, i32* %16
%18 = add i32 %17, 1
store i32 %18, i32* %16
br label %19
19: ; preds = %10
%20 = load i32, i32* %2
%21 = add i32 %20, 1
store i32 %21, i32* %2
br label %7
22: ; preds = %7
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i64 1, i64* %1
br label %24
24: ; preds = %33, %22
%25 = load i64, i64* %1
%26 = icmp ult i64 %25, 7
br i1 %26, label %27, label %36
27: ; preds = %24
%28 = load i64, i64* %1
%29 = load i64, i64* %1
%30 = getelementptr inbounds [7 x i32], [7 x i32]* %3, i64 0, i64 %29
%31 = load i32, i32* %30
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i64 %28, i32 %31)
br label %33
33: ; preds = %27
%34 = load i64, i64* %1
%35 = add i64 %34, 1
store i64 %35, i64* %1
br label %24
36: ; preds = %24
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"%s%17s\0A\00"
@.str.1 = [5 x i8] c"Face\00"
@.str.2 = [10 x i8] c"Frequency\00"
@.str.3 = [9 x i8] c"%4d%17d\0A\00"
define void @fig06_09() {
%1 = alloca [7 x i32]
%2 = bitcast [7 x i32]* %1 to i8*
%3 = tail call i32 @time(i32* null)
%4 = tail call i32 @srand(i32 %3)
br label %5
5: ; preds = %0, %5
%6 = phi i32 [ 1, %0 ], [ %14, %5 ]
%7 = tail call i32 (...) @rand()
%8 = srem i32 %7, 6
%9 = add nsw i32 %8, 1
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [7 x i32], [7 x i32]* %1, i64 0, i64 %10
%12 = load i32, i32* %11
%13 = add i32 %12, 1
store i32 %13, i32* %11
%14 = add nuw nsw i32 %6, 1
%15 = icmp eq i32 %14, 6000001
br i1 %15, label %16, label %5
16: ; preds = %5
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%18 = getelementptr inbounds [7 x i32], [7 x i32]* %1, i64 0, i64 1
%19 = load i32, i32* %18
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i64 1, i32 %19)
%21 = getelementptr inbounds [7 x i32], [7 x i32]* %1, i64 0, i64 2
%22 = load i32, i32* %21
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i64 2, i32 %22)
%24 = getelementptr inbounds [7 x i32], [7 x i32]* %1, i64 0, i64 3
%25 = load i32, i32* %24
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i64 3, i32 %25)
%27 = getelementptr inbounds [7 x i32], [7 x i32]* %1, i64 0, i64 4
%28 = load i32, i32* %27
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i64 4, i32 %28)
%30 = getelementptr inbounds [7 x i32], [7 x i32]* %1, i64 0, i64 5
%31 = load i32, i32* %30
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i64 5, i32 %31)
%33 = getelementptr inbounds [7 x i32], [7 x i32]* %1, i64 0, i64 6
%34 = load i32, i32* %33
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i64 6, i32 %34)
ret void
}