#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 */
typedef int uint8_t;
typedef int int32_t;

/* Variables and functions */
int rand();
uint8_t flipcoins() {
  int32_t coin = rand();
  int32_t shift = 1;
  uint8_t result = 0;
  for (uint8_t i = 0; i < 4 - 1; i++) {
    if (coin & shift)
      result++;
    else
      break;
    shift <<= 1;
  }
  return (result >= 4) ? 4 : result + 1;
}


x86 assembly (clang, -O0):
<flipcoins>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1030 <rand@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x3,-0x10(%rbp)
jge    117a <flipcoins+0x6a>
mov    -0x4(%rbp),%eax
and    -0x8(%rbp),%eax
cmp    $0x0,%eax
je     115e <flipcoins+0x4e>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1163 <flipcoins+0x53>
jmp    117a <flipcoins+0x6a>
mov    -0x8(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1137 <flipcoins+0x27>
cmpl   $0x4,-0xc(%rbp)
jl     1191 <flipcoins+0x81>
mov    $0x4,%eax
mov    %eax,-0x14(%rbp)
jmp    119a <flipcoins+0x8a>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flipcoins>:
push   %rax
xor    %eax,%eax
call   1030 <rand@plt>
mov    $0x1,%ecx
test   $0x1,%al
je     1138 <flipcoins+0x28>
mov    $0x2,%ecx
test   $0x2,%al
je     1138 <flipcoins+0x28>
and    $0x4,%eax
cmp    $0x1,%eax
mov    $0x4,%ecx
sbb    $0x0,%ecx
mov    %ecx,%eax
pop    %rcx
ret

LLVM IR (-O0):
define i32 @flipcoins() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = call i32 (...) @rand()
store i32 %5, i32* %1
store i32 1, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
br label %6
6: ; preds = %21, %0
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 3
br i1 %8, label %9, label %24
9: ; preds = %6
%10 = load i32, i32* %1
%11 = load i32, i32* %2
%12 = and i32 %10, %11
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i32, i32* %3
%16 = add nsw i32 %15, 1
store i32 %16, i32* %3
br label %18
17: ; preds = %9
br label %24
18: ; preds = %14
%19 = load i32, i32* %2
%20 = shl i32 %19, 1
store i32 %20, i32* %2
br label %21
21: ; preds = %18
%22 = load i32, i32* %4
%23 = add nsw i32 %22, 1
store i32 %23, i32* %4
br label %6
24: ; preds = %17, %6
%25 = load i32, i32* %3
%26 = icmp sge i32 %25, 4
br i1 %26, label %27, label %28
27: ; preds = %24
br label %31
28: ; preds = %24
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
br label %31
31: ; preds = %28, %27
%32 = phi i32 [ 4, %27 ], [ %30, %28 ]
ret i32 %32
}

LLVM IR (-O2):
define i32 @flipcoins() {
%1 = tail call i32 (...) @rand()
%2 = and i32 %1, 1
%3 = icmp eq i32 %2, 0
br i1 %3, label %11, label %4
4: ; preds = %0
%5 = and i32 %1, 2
%6 = icmp eq i32 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %4
%8 = and i32 %1, 4
%9 = icmp eq i32 %8, 0
%10 = select i1 %9, i32 3, i32 4
br label %11
11: ; preds = %7, %4, %0
%12 = phi i32 [ 1, %0 ], [ 2, %4 ], [ %10, %7 ]
ret i32 %12
}