#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*/ scanf(char *, int *);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(void) {
  srand(time(NULL));
  int iter = 0;
  int sww = 0;
  int stw = 0;
  int i = 0;
  char winstr[4];
  printf("How many times should we run the simulation? ");
  scanf("%d", &iter);
  for (i = 1; i <= iter; i++) {
    int winner = rand() % 3;
    int choice = rand() % 3;
    if (winner == choice) {
      stw++;
      strcpy(winstr, "HOLD");
    } else {
      sww++;
      strcpy(winstr, "SWCH");
    }
    printf("[SIM] %s WON - CUR%%: %f CURSW: %d CURST: %d\r", winstr,
           ((float)sww) / ((float)i), sww, stw);
  }
  printf("\nSwitching won %f percent of the time.\nThere were %d cases in "
         "which switching won.\nThere were %d cases in which holding won.\n",
         ((float)sww) / ((float)iter), sww, stw);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe7e(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1070 <scanf@plt>
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jg     126b <main+0x10b>
mov    $0x0,%al
call   1080 <rand@plt>
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %edx,-0x1c(%rbp)
mov    $0x0,%al
call   1080 <rand@plt>
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %edx,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    121a <main+0xba>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
lea    0xe21(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1233 <main+0xd3>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
lea    -0x18(%rbp),%rdi
lea    0xe08(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x18(%rbp),%rsi
cvtsi2ssl -0xc(%rbp),%xmm0
cvtsi2ssl -0x14(%rbp),%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xde5(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11c0 <main+0x60>
cvtsi2ssl -0xc(%rbp),%xmm0
cvtsi2ssl -0x8(%rbp),%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xddf(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %edi,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
movl   $0x0,(%rsp)
lea    0xe77(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe97(%rip),%rdi
mov    %rsp,%rsi
call   1070 <scanf@plt>
mov    (%rsp),%eax
mov    $0x0,%ebp
test   %eax,%eax
jle    1279 <main+0x119>
lea    0x4(%rsp),%r14
lea    0xe80(%rip),%r15
xor    %r12d,%r12d
xor    %ebp,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%r12d
xor    %eax,%eax
call   1080 <rand@plt>
movslq %eax,%r13
imul   $0x55555556,%r13,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rax
add    %ecx,%eax
lea    (%rax,%rax,2),%eax
sub    %eax,%r13d
xor    %eax,%eax
call   1080 <rand@plt>
cltq
imul   $0x55555556,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,2),%ecx
sub    %ecx,%eax
xor    %ecx,%ecx
xor    %edx,%edx
cmp    %eax,%r13d
setne  %cl
sete   %dl
lea    0xe08(%rip),%rsi
lea    0xdfc(%rip),%rax
cmove  %rax,%rsi
add    %edx,%ebx
add    %ecx,%ebp
mov    %r14,%rdi
call   1030 <strcpy@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %ebp,%xmm0
xorps  %xmm1,%xmm1
cvtsi2ss %r12d,%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    $0x1,%al
call   1040 <printf@plt>
mov    (%rsp),%eax
cmp    %eax,%r12d
jl     11d0 <main+0x70>
xorps  %xmm0,%xmm0
cvtsi2ss %ebp,%xmm0
xorps  %xmm1,%xmm1
cvtsi2ss %eax,%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xdd3(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
mov    $0x1,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [46 x i8] c"How many times should we run the simulation? \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [5 x i8] c"HOLD\00"
@.str.3 = [5 x i8] c"SWCH\00"
@.str.4 = [46 x i8] c"[SIM] %s WON - CUR%%: %f CURSW: %d CURST: %d\0D\00"
@.str.5 = [126 x i8] c"\0ASwitching won %f percent of the time.\0AThere were %d cases in which switching won.\0AThere were %d cases in which holding won.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [4 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = call i32 @time(i32* null)
%10 = call i32 @srand(i32 %9)
store i32 0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
store i32 1, i32* %5
br label %13
13: ; preds = %46, %0
%14 = load i32, i32* %5
%15 = load i32, i32* %2
%16 = icmp sle i32 %14, %15
br i1 %16, label %17, label %49
17: ; preds = %13
%18 = call i32 (...) @rand()
%19 = srem i32 %18, 3
store i32 %19, i32* %7
%20 = call i32 (...) @rand()
%21 = srem i32 %20, 3
store i32 %21, i32* %8
%22 = load i32, i32* %7
%23 = load i32, i32* %8
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %30
25: ; preds = %17
%26 = load i32, i32* %4
%27 = add nsw i32 %26, 1
store i32 %27, i32* %4
%28 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%29 = call i32 @strcpy(i8* %28, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %35
30: ; preds = %17
%31 = load i32, i32* %3
%32 = add nsw i32 %31, 1
store i32 %32, i32* %3
%33 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%34 = call i32 @strcpy(i8* %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %30, %25
%36 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%37 = load i32, i32* %3
%38 = sitofp i32 %37 to float
%39 = load i32, i32* %5
%40 = sitofp i32 %39 to float
%41 = fdiv float %38, %40
%42 = fpext float %41 to double
%43 = load i32, i32* %3
%44 = load i32, i32* %4
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i8* %36, double %42, i32 %43, i32 %44)
br label %46
46: ; preds = %35
%47 = load i32, i32* %5
%48 = add nsw i32 %47, 1
store i32 %48, i32* %5
br label %13
49: ; preds = %13
%50 = load i32, i32* %3
%51 = sitofp i32 %50 to float
%52 = load i32, i32* %2
%53 = sitofp i32 %52 to float
%54 = fdiv float %51, %53
%55 = fpext float %54 to double
%56 = load i32, i32* %3
%57 = load i32, i32* %4
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([126 x i8], [126 x i8]* @.str.5, i64 0, i64 0), double %55, i32 %56, i32 %57)
ret i32 0
}

LLVM IR (-O2):
@.str = [46 x i8] c"How many times should we run the simulation? \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [5 x i8] c"HOLD\00"
@.str.3 = [5 x i8] c"SWCH\00"
@.str.4 = [46 x i8] c"[SIM] %s WON - CUR%%: %f CURSW: %d CURST: %d\0D\00"
@.str.5 = [126 x i8] c"\0ASwitching won %f percent of the time.\0AThere were %d cases in which switching won.\0AThere were %d cases in which holding won.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [4 x i8]
%3 = tail call i32 @time(i32* null)
%4 = tail call i32 @srand(i32 %3)
%5 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%6 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%9 = load i32, i32* %1
%10 = icmp slt i32 %9, 1
br i1 %10, label %35, label %11
11: ; preds = %0, %11
%12 = phi i32 [ %25, %11 ], [ 0, %0 ]
%13 = phi i32 [ %22, %11 ], [ 0, %0 ]
%14 = phi i32 [ %32, %11 ], [ 1, %0 ]
%15 = call i32 (...) @rand()
%16 = srem i32 %15, 3
%17 = call i32 (...) @rand()
%18 = srem i32 %17, 3
%19 = icmp eq i32 %16, %18
%20 = select i1 %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0)
%21 = zext i1 %19 to i32
%22 = add nuw nsw i32 %13, %21
%23 = xor i1 %19, true
%24 = zext i1 %23 to i32
%25 = add nuw nsw i32 %12, %24
%26 = call i32 @strcpy(i8* %6, i8* %20)
%27 = sitofp i32 %25 to float
%28 = sitofp i32 %14 to float
%29 = fdiv float %27, %28
%30 = fpext float %29 to double
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i8* %6, double %30, i32 %25, i32 %22)
%32 = add nuw nsw i32 %14, 1
%33 = load i32, i32* %1
%34 = icmp slt i32 %14, %33
br i1 %34, label %11, label %35
35: ; preds = %11, %0
%36 = phi i32 [ 0, %0 ], [ %22, %11 ]
%37 = phi i32 [ 0, %0 ], [ %25, %11 ]
%38 = phi i32 [ %9, %0 ], [ %33, %11 ]
%39 = sitofp i32 %37 to float
%40 = sitofp i32 %38 to float
%41 = fdiv float %39, %40
%42 = fpext float %41 to double
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([126 x i8], [126 x i8]* @.str.5, i64 0, i64 0), double %42, i32 %37, i32 %36)
ret i32 0
}