#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*/ EXIT_FAILURE;
int RAND_MAX;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned int const);
int CalcMaxAcceptedRand(const unsigned int numberofsides) {
  int MaxRand = (RAND_MAX - 1);
  int MaxRandPlusOne = RAND_MAX;
  int MaxAcceptedRand = 0;
  if (MaxRandPlusOne < numberofsides) {
    printf("The RNG's range is too small to simulate a die with %d sides.\n",
           numberofsides);
    perror("Too many sides per die");
    exit(EXIT_FAILURE);
  } else {
    MaxAcceptedRand = (MaxRand - (MaxRandPlusOne % numberofsides));
  }
  return MaxAcceptedRand;
}


x86 assembly (clang, -O0):
<CalcMaxAcceptedRand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%eax
sub    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    0x2e97(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    1192 <CalcMaxAcceptedRand+0x62>
mov    -0x4(%rbp),%esi
lea    0xe8d(%rip),%rdi
call   1030 <printf@plt>
lea    0xec0(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
xor    %edx,%edx
divl   -0x4(%rbp)
mov    -0x14(%rbp),%eax
sub    %edx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CalcMaxAcceptedRand>:
push   %rax
mov    %edi,%esi
mov    0x2eae(%rip),%rax
mov    (%rax),%ecx
cmp    %edi,%ecx
jb     114e <CalcMaxAcceptedRand+0x1e>
mov    %ecx,%eax
xor    %edx,%edx
div    %esi
not    %edx
add    %edx,%ecx
mov    %ecx,%eax
pop    %rcx
ret
lea    0xeab(%rip),%rdi
call   1030 <printf@plt>
lea    0xede(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@RAND_MAX = global i32 0
@.str = [63 x i8] c"The RNG's range is too small to simulate a die with %d sides.\0A\00"
@.str.1 = [23 x i8] c"Too many sides per die\00"
@EXIT_FAILURE = global i32 0
define i32 @CalcMaxAcceptedRand(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* @RAND_MAX
%7 = sub nsw i32 %6, 1
store i32 %7, i32* %3
%8 = load i32, i32* @RAND_MAX
store i32 %8, i32* %4
store i32 0, i32* %5
%9 = load i32, i32* %4
%10 = load i32, i32* %2
%11 = icmp ult i32 %9, %10
br i1 %11, label %12, label %18
12: ; preds = %1
%13 = load i32, i32* %2
%14 = call i32 @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %1
%19 = load i32, i32* %3
%20 = load i32, i32* %4
%21 = load i32, i32* %2
%22 = urem i32 %20, %21
%23 = sub i32 %19, %22
store i32 %23, i32* %5
br label %24
24: ; preds = %18
%25 = load i32, i32* %5
ret i32 %25
}

LLVM IR (-O2):
@RAND_MAX = global i32 0
@.str = [63 x i8] c"The RNG's range is too small to simulate a die with %d sides.\0A\00"
@.str.1 = [23 x i8] c"Too many sides per die\00"
@EXIT_FAILURE = global i32 0
define i32 @CalcMaxAcceptedRand(i32 %0) {
%2 = load i32, i32* @RAND_MAX
%3 = icmp ult i32 %2, %0
br i1 %3, label %4, label %9
4: ; preds = %1
%5 = tail call i32 @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %1
%10 = urem i32 %2, %0
%11 = xor i32 %10, -1
%12 = add i32 %2, %11
ret i32 %12
}