#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 unsigned int uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ exit(int);
unsigned int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
void rollDie(uint32_t sides, FILE *file) {
  uint32_t randVal;
  do {
    uint32_t numBytes = 0u;
    randVal = 0u;
    while (1u << (8u * numBytes) <= sides) {
      numBytes++;
      int c = getc(file);
      if (c == EOF) {
        printf("Ran out of random data\n");
        exit(1);
      }
      randVal = (randVal << 8u) | getc(file);
    }
  } while (randVal >= sides);
  printf(" %u", randVal + 1u);
}


x86 assembly (clang, -O0):
<rollDie>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x18(%rbp),%ecx
shl    $0x3,%ecx
mov    $0x1,%eax
shl    %cl,%eax
cmp    -0x4(%rbp),%eax
ja     11c3 <rollDie+0x93>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <getc@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e66(%rip),%rcx
cmp    (%rcx),%eax
jne    11a2 <rollDie+0x72>
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
mov    -0x14(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <getc@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    114d <rollDie+0x1d>
jmp    11c8 <rollDie+0x98>
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    113f <rollDie+0xf>
mov    -0x14(%rbp),%esi
add    $0x1,%esi
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rollDie>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %edi,%r14d
mov    0x2e93(%rip),%r12
jmp    1165 <rollDie+0x25>
nopw   0x0(%rax,%rax,1)
cmp    %r14d,%ebp
jb     11ab <rollDie+0x6b>
xor    %ebp,%ebp
test   %r14d,%r14d
je     1160 <rollDie+0x20>
mov    $0x8,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1060 <getc@plt>
cmp    (%r12),%eax
je     11c6 <rollDie+0x86>
shl    $0x8,%ebp
mov    %r15,%rdi
call   1060 <getc@plt>
mov    %r14d,%edx
mov    %ebx,%ecx
shr    %cl,%edx
or     %eax,%ebp
add    $0x8,%ebx
test   %edx,%edx
jne    1180 <rollDie+0x40>
jmp    1160 <rollDie+0x20>
add    $0x1,%ebp
lea    0xe4b(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>
lea    0xe37(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@EOF = global i32 0
@.str = [24 x i8] c"Ran out of random data\0A\00"
@.str.1 = [4 x i8] c" %u\00"
define void @rollDie(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
br label %8
8: ; preds = %33, %2
store i32 0, i32* %6
store i32 0, i32* %5
br label %9
9: ; preds = %26, %8
%10 = load i32, i32* %6
%11 = mul i32 8, %10
%12 = shl i32 1, %11
%13 = load i32, i32* %3
%14 = icmp ule i32 %12, %13
br i1 %14, label %15, label %32
15: ; preds = %9
%16 = load i32, i32* %6
%17 = add i32 %16, 1
store i32 %17, i32* %6
%18 = load i32*, i32** %4
%19 = call i32 @getc(i32* %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = load i32, i32* @EOF
%22 = icmp eq i32 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %15
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %15
%27 = load i32, i32* %5
%28 = shl i32 %27, 8
%29 = load i32*, i32** %4
%30 = call i32 @getc(i32* %29)
%31 = or i32 %28, %30
store i32 %31, i32* %5
br label %9
32: ; preds = %9
br label %33
33: ; preds = %32
%34 = load i32, i32* %5
%35 = load i32, i32* %3
%36 = icmp uge i32 %34, %35
br i1 %36, label %8, label %37
37: ; preds = %33
%38 = load i32, i32* %5
%39 = add i32 %38, 1
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %39)
ret void
}

LLVM IR (-O2):
@EOF = global i32 0
@.str.1 = [4 x i8] c" %u\00"
@str = [23 x i8] c"Ran out of random data\00"
define void @rollDie(i32 %0, i32* nocapture %1) {
%3 = icmp eq i32 %0, 0
br label %4
4: ; preds = %22, %2
br i1 %3, label %22, label %5
5: ; preds = %4, %14
%6 = phi i32 [ %18, %14 ], [ 0, %4 ]
%7 = phi i32 [ %15, %14 ], [ 0, %4 ]
%8 = tail call i32 @getc(i32* %1)
%9 = load i32, i32* @EOF
%10 = icmp eq i32 %8, %9
br i1 %10, label %11, label %14
11: ; preds = %5
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %5
%15 = add i32 %7, 1
%16 = shl i32 %6, 8
%17 = tail call i32 @getc(i32* %1)
%18 = or i32 %17, %16
%19 = shl i32 %15, 3
%20 = lshr i32 %0, %19
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %5
22: ; preds = %14, %4
%23 = phi i32 [ 0, %4 ], [ %18, %14 ]
%24 = icmp ult i32 %23, %0
br i1 %24, label %25, label %4
25: ; preds = %22
%26 = add nuw i32 %23, 1
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %26)
ret void
}