#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 randomChance(double);
int /*<<< orphan*/ randomize();
void threeHeads(void) {
  int heads = 0;
  int loop = 0;
  int condition = 1;
  randomize();
  while (condition) {
    switch (randomChance(0.5)) {
    case 1:
      printf("heads\n");
      heads++;
      loop++;
      if (heads == 3)
        condition = 0;
      break;
    case 0:
      printf("tails\n");
      loop++;
      heads = 0;
      break;
    }
  }
  printf("It took %d flips to get heads 3 consecutive times.\n", loop);
}


x86 assembly (clang, -O0):
<threeHeads>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
mov    $0x0,%al
call   1040 <randomize@plt>
cmpl   $0x0,-0xc(%rbp)
je     11e7 <threeHeads+0xb7>
movsd  0xe9a(%rip),%xmm0
call   1050 <randomChance@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
test   %eax,%eax
je     11c4 <threeHeads+0x94>
jmp    117d <threeHeads+0x4d>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
jne    11e2 <threeHeads+0xb2>
jmp    118e <threeHeads+0x5e>
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x3,-0x4(%rbp)
jne    11bf <threeHeads+0x8f>
movl   $0x0,-0xc(%rbp)
jmp    11e2 <threeHeads+0xb2>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
movl   $0x0,-0x4(%rbp)
jmp    1154 <threeHeads+0x24>
mov    -0x8(%rbp),%esi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<threeHeads>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %r12d,%r12d
xor    %eax,%eax
call   1050 <randomize@plt>
lea    0xee3(%rip),%r14
lea    0xee2(%rip),%r15
xor    %ebp,%ebp
jmp    1182 <threeHeads+0x42>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
call   1030 <puts@plt>
add    $0x1,%r12d
xor    %ebp,%ebp
test   %ebx,%ebx
je     11b6 <threeHeads+0x76>
movsd  0xe76(%rip),%xmm0
call   1060 <randomChance@plt>
mov    $0x1,%ebx
test   %eax,%eax
je     1170 <threeHeads+0x30>
cmp    $0x1,%eax
jne    117e <threeHeads+0x3e>
mov    %r15,%rdi
call   1030 <puts@plt>
add    $0x1,%ebp
add    $0x1,%r12d
xor    %ebx,%ebx
cmp    $0x3,%ebp
setne  %bl
jmp    117e <threeHeads+0x3e>
lea    0xe4b(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"heads\0A\00"
@.str.1 = [7 x i8] c"tails\0A\00"
@.str.2 = [52 x i8] c"It took %d flips to get heads 3 consecutive times.\0A\00"
define void @threeHeads() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
store i32 1, i32* %3
%4 = call i32 (...) @randomize()
br label %5
5: ; preds = %24, %0
%6 = load i32, i32* %3
%7 = icmp ne i32 %6, 0
br i1 %7, label %8, label %25
8: ; preds = %5
%9 = call i32 @randomChance(double 5.000000e-01)
switch i32 %9, label %24 [
i32 1, label %10
i32 0, label %20
]
10: ; preds = %8
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* %1
%13 = add nsw i32 %12, 1
store i32 %13, i32* %1
%14 = load i32, i32* %2
%15 = add nsw i32 %14, 1
store i32 %15, i32* %2
%16 = load i32, i32* %1
%17 = icmp eq i32 %16, 3
br i1 %17, label %18, label %19
18: ; preds = %10
store i32 0, i32* %3
br label %19
19: ; preds = %18, %10
br label %24
20: ; preds = %8
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* %2
%23 = add nsw i32 %22, 1
store i32 %23, i32* %2
store i32 0, i32* %1
br label %24
24: ; preds = %8, %20, %19
br label %5
25: ; preds = %5
%26 = load i32, i32* %2
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %26)
ret void
}

LLVM IR (-O2):
@.str.2 = [52 x i8] c"It took %d flips to get heads 3 consecutive times.\0A\00"
@str = [6 x i8] c"tails\00"
@str.3 = [6 x i8] c"heads\00"
define void @threeHeads() {
%1 = tail call i32 (...) @randomize()
br label %2
2: ; preds = %0, %15
%3 = phi i32 [ 0, %0 ], [ %17, %15 ]
%4 = phi i32 [ 0, %0 ], [ %16, %15 ]
%5 = tail call i32 @randomChance(double 5.000000e-01)
switch i32 %5, label %15 [
i32 1, label %6
i32 0, label %12
]
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.3, i64 0, i64 0))
%8 = add nsw i32 %4, 1
%9 = add nsw i32 %3, 1
%10 = icmp ne i32 %8, 3
%11 = zext i1 %10 to i32
br label %15
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
%14 = add nsw i32 %3, 1
br label %15
15: ; preds = %6, %2, %12
%16 = phi i32 [ %4, %2 ], [ 0, %12 ], [ %8, %6 ]
%17 = phi i32 [ %3, %2 ], [ %14, %12 ], [ %9, %6 ]
%18 = phi i32 [ 1, %2 ], [ 1, %12 ], [ %11, %6 ]
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %2
20: ; preds = %15
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %17)
ret void
}