#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 < 16 - 1; i++) {
    if (coin & shift)
      result++;
    else
      break;
    shift <<= 1;
  }
  return (result >= 16) ? 16 : 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   $0xf,-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   $0x10,-0xc(%rbp)
jl     1191 <flipcoins+0x81>
mov    $0x10,%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    %eax,%ecx
mov    $0x1,%eax
test   $0x1,%cl
je     1143 <flipcoins+0x33>
mov    $0x1,%eax
mov    $0x1,%edx
xchg   %ax,%ax
lea    -0x1(%rax),%esi
add    $0x1,%eax
cmp    $0xd,%esi
ja     1143 <flipcoins+0x33>
add    %edx,%edx
mov    %edx,%esi
and    %ecx,%esi
jne    1130 <flipcoins+0x20>
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, 15
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, 16
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 [ 16, %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 %15, label %4
4: ; preds = %0, %4
%5 = phi i32 [ %7, %4 ], [ 0, %0 ]
%6 = phi i32 [ %8, %4 ], [ 1, %0 ]
%7 = add nuw nsw i32 %5, 1
%8 = shl i32 %6, 1
%9 = icmp ugt i32 %5, 13
%10 = and i32 %8, %1
%11 = icmp eq i32 %10, 0
%12 = select i1 %9, i1 true, i1 %11
br i1 %12, label %13, label %4
13: ; preds = %4
%14 = add nuw i32 %5, 2
br label %15
15: ; preds = %13, %0
%16 = phi i32 [ 1, %0 ], [ %14, %13 ]
ret i32 %16
}