#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 *);
uint32_t 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);
  return randVal;
}


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     11c1 <rollDie+0x91>
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    11a0 <rollDie+0x70>
lea    0xe6f(%rip),%rdi
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    11c6 <rollDie+0x96>
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    113f <rollDie+0xf>
mov    -0x14(%rbp),%eax
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    0x2ea3(%rip),%r12
jmp    1155 <rollDie+0x25>
nopw   0x0(%rax,%rax,1)
cmp    %r14d,%ebp
jb     119b <rollDie+0x6b>
xor    %ebp,%ebp
test   %r14d,%r14d
je     1150 <rollDie+0x20>
mov    $0x8,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1050 <getc@plt>
cmp    (%r12),%eax
je     11a6 <rollDie+0x76>
shl    $0x8,%ebp
mov    %r15,%rdi
call   1050 <getc@plt>
mov    %r14d,%edx
mov    %ebx,%ecx
shr    %cl,%edx
or     %eax,%ebp
add    $0x8,%ebx
test   %edx,%edx
jne    1170 <rollDie+0x40>
jmp    1150 <rollDie+0x20>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe53(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@EOF = global i32 0
@.str = [24 x i8] c"Ran out of random data\0A\00"
define i32 @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 @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
ret i32 %38
}

LLVM IR (-O2):
@EOF = global i32 0
@str = [23 x i8] c"Ran out of random data\00"
define i32 @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
ret i32 %23
}