#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*/ EX_USAGE;
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ roll(int, int);
unsigned long strtoul(char const *, char **, int /*<<< orphan*/);
void roll_em(const char *spec) {
  char *ep;
  int number, sides;
  unsigned long val = 0;
  val = strtoul(spec, &ep, 0);
  if (val < 1)
    errx(EX_USAGE, "value below minimum in '%s'", spec);
  if (val > 10000)
    errx(EX_USAGE, "value above maximum in '%s'", spec);
  if (*ep == '\0') {
    number = 1;
    if (val < 2)
      errx(EX_USAGE, "sides must be >= %d", 2);
    sides = (int)val;
  } else if (*ep == 'd') {
    number = (int)val;
    spec = ep;
    spec++;
    val = strtoul(spec, &ep, 0);
    if (spec[0] == '\0' || *ep != '\0')
      errx(EX_USAGE, "stray characters in '%s'", spec);
    if (val < 2)
      errx(EX_USAGE, "value below minimum in '%s'", spec);
    if (val > 10000)
      errx(EX_USAGE, "value above maximum in '%s'", spec);
    sides = (int)val;
  } else {
    errx(EX_USAGE, "unknown characters in spec '%s'", spec);
  }
  printf("%lu\n", roll(number, sides));
}


x86 assembly (clang, -O0):
<roll_em>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1050 <strtoul@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x1,-0x20(%rbp)
jae    118d <roll_em+0x4d>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe7a(%rip),%rsi
mov    $0x0,%al
call   1030 <errx@plt>
cmpq   $0x2710,-0x20(%rbp)
jbe    11b6 <roll_em+0x76>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe6d(%rip),%rsi
mov    $0x0,%al
call   1030 <errx@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1200 <roll_em+0xc0>
movl   $0x1,-0x14(%rbp)
cmpq   $0x2,-0x20(%rbp)
jae    11f4 <roll_em+0xb4>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1030 <errx@plt>
mov    -0x20(%rbp),%rax
mov    %eax,-0x18(%rbp)
jmp    12f4 <roll_em+0x1b4>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x64,%eax
jne    12d4 <roll_em+0x194>
mov    -0x20(%rbp),%rax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1050 <strtoul@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     125e <roll_em+0x11e>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1279 <roll_em+0x139>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdda(%rip),%rsi
mov    $0x0,%al
call   1030 <errx@plt>
cmpq   $0x2,-0x20(%rbp)
jae    129f <roll_em+0x15f>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd68(%rip),%rsi
mov    $0x0,%al
call   1030 <errx@plt>
cmpq   $0x2710,-0x20(%rbp)
jbe    12c8 <roll_em+0x188>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd5b(%rip),%rsi
mov    $0x0,%al
call   1030 <errx@plt>
mov    -0x20(%rbp),%rax
mov    %eax,-0x18(%rbp)
jmp    12ef <roll_em+0x1af>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd7d(%rip),%rsi
mov    $0x0,%al
call   1030 <errx@plt>
jmp    12f4 <roll_em+0x1b4>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1060 <roll@plt>
mov    %eax,%esi
lea    0xd7d(%rip),%rdi
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<roll_em>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rsp,%rsi
xor    %edx,%edx
call   1050 <strtoul@plt>
mov    %rax,%r12
test   %rax,%rax
je     116f <roll_em+0x2f>
cmp    $0x2711,%r12
jb     1189 <roll_em+0x49>
lea    0xeaf(%rip),%rsi
jmp    1176 <roll_em+0x36>
lea    0xe8a(%rip),%rsi
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <errx@plt>
mov    (%rsp),%rbx
mov    (%rbx),%al
cmp    $0x64,%al
jne    11e8 <roll_em+0xa8>
lea    0x1(%rbx),%r15
mov    %rsp,%rsi
mov    %r15,%rdi
xor    %edx,%edx
call   1050 <strtoul@plt>
mov    %rax,%r14
cmpb   $0x0,0x1(%rbx)
je     11b6 <roll_em+0x76>
mov    (%rsp),%rax
cmpb   $0x0,(%rax)
je     11d0 <roll_em+0x90>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
lea    0xe86(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1030 <errx@plt>
cmp    $0x1,%r14
ja     1215 <roll_em+0xd5>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
jmp    122e <roll_em+0xee>
test   %al,%al
jne    1261 <roll_em+0x121>
mov    $0x1,%ebx
cmp    $0x1,%r12
ja     123e <roll_em+0xfe>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1030 <errx@plt>
jmp    123e <roll_em+0xfe>
cmp    $0x2711,%r14
jb     1238 <roll_em+0xf8>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1030 <errx@plt>
mov    %r12d,%ebx
mov    %r14,%r12
mov    %ebx,%edi
mov    %r12d,%esi
call   1060 <roll@plt>
lea    0xe36(%rip),%rdi
mov    %eax,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <printf@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <errx@plt>

LLVM IR (-O0):
@EX_USAGE = global i32 0
@.str = [28 x i8] c"value below minimum in '%s'\00"
@.str.1 = [28 x i8] c"value above maximum in '%s'\00"
@.str.2 = [20 x i8] c"sides must be >= %d\00"
@.str.3 = [25 x i8] c"stray characters in '%s'\00"
@.str.4 = [32 x i8] c"unknown characters in spec '%s'\00"
@.str.5 = [5 x i8] c"%lu\0A\00"
define void @roll_em(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
store i8* %0, i8** %2
store i64 0, i64* %6
%7 = load i8*, i8** %2
%8 = call i64 @strtoul(i8* %7, i8** %3, i32 0)
store i64 %8, i64* %6
%9 = load i64, i64* %6
%10 = icmp ult i64 %9, 1
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = load i32, i32* @EX_USAGE
%13 = load i8*, i8** %2
%14 = call i32 (i32, i8*, ...) @errx(i32 %12, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %11, %1
%16 = load i64, i64* %6
%17 = icmp ugt i64 %16, 10000
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @EX_USAGE
%20 = load i8*, i8** %2
%21 = call i32 (i32, i8*, ...) @errx(i32 %19, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %18, %15
%23 = load i8*, i8** %3
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %22
store i32 1, i32* %4
%28 = load i64, i64* %6
%29 = icmp ult i64 %28, 2
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* @EX_USAGE
%32 = call i32 (i32, i8*, ...) @errx(i32 %31, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 2)
br label %33
33: ; preds = %30, %27
%34 = load i64, i64* %6
%35 = trunc i64 %34 to i32
store i32 %35, i32* %5
br label %85
36: ; preds = %22
%37 = load i8*, i8** %3
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 100
br i1 %40, label %41, label %80
41: ; preds = %36
%42 = load i64, i64* %6
%43 = trunc i64 %42 to i32
store i32 %43, i32* %4
%44 = load i8*, i8** %3
store i8* %44, i8** %2
%45 = load i8*, i8** %2
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %2
%47 = load i8*, i8** %2
%48 = call i64 @strtoul(i8* %47, i8** %3, i32 0)
store i64 %48, i64* %6
%49 = load i8*, i8** %2
%50 = getelementptr inbounds i8, i8* %49, i64 0
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp eq i32 %52, 0
br i1 %53, label %59, label %54
54: ; preds = %41
%55 = load i8*, i8** %3
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %54, %41
%60 = load i32, i32* @EX_USAGE
%61 = load i8*, i8** %2
%62 = call i32 (i32, i8*, ...) @errx(i32 %60, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %61)
br label %63
63: ; preds = %59, %54
%64 = load i64, i64* %6
%65 = icmp ult i64 %64, 2
br i1 %65, label %66, label %70
66: ; preds = %63
%67 = load i32, i32* @EX_USAGE
%68 = load i8*, i8** %2
%69 = call i32 (i32, i8*, ...) @errx(i32 %67, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %68)
br label %70
70: ; preds = %66, %63
%71 = load i64, i64* %6
%72 = icmp ugt i64 %71, 10000
br i1 %72, label %73, label %77
73: ; preds = %70
%74 = load i32, i32* @EX_USAGE
%75 = load i8*, i8** %2
%76 = call i32 (i32, i8*, ...) @errx(i32 %74, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %75)
br label %77
77: ; preds = %73, %70
%78 = load i64, i64* %6
%79 = trunc i64 %78 to i32
store i32 %79, i32* %5
br label %84
80: ; preds = %36
%81 = load i32, i32* @EX_USAGE
%82 = load i8*, i8** %2
%83 = call i32 (i32, i8*, ...) @errx(i32 %81, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %82)
br label %84
84: ; preds = %80, %77
br label %85
85: ; preds = %84, %33
%86 = load i32, i32* %4
%87 = load i32, i32* %5
%88 = call i32 @roll(i32 %86, i32 %87)
%89 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %88)
ret void
}

LLVM IR (-O2):
@EX_USAGE = global i32 0
@.str = [28 x i8] c"value below minimum in '%s'\00"
@.str.1 = [28 x i8] c"value above maximum in '%s'\00"
@.str.2 = [20 x i8] c"sides must be >= %d\00"
@.str.3 = [25 x i8] c"stray characters in '%s'\00"
@.str.4 = [32 x i8] c"unknown characters in spec '%s'\00"
@.str.5 = [5 x i8] c"%lu\0A\00"
define void @roll_em(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = call i64 @strtoul(i8* %0, i8** %2, i32 0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = icmp ugt i64 %4, 10000
br i1 %7, label %8, label %12
8: ; preds = %6, %1
%9 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), %6 ]
%10 = load i32, i32* @EX_USAGE
%11 = tail call i32 (i32, i8*, ...) @errx(i32 %10, i8* %9, i8* %0)
br label %12
12: ; preds = %8, %6
%13 = load i8*, i8** %2
%14 = load i8, i8* %13
switch i8 %14, label %43 [
i8 0, label %15
i8 100, label %20
]
15: ; preds = %12
%16 = icmp ult i64 %4, 2
br i1 %16, label %17, label %46
17: ; preds = %15
%18 = load i32, i32* @EX_USAGE
%19 = tail call i32 (i32, i8*, ...) @errx(i32 %18, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 2)
br label %46
20: ; preds = %12
%21 = trunc i64 %4 to i32
%22 = getelementptr inbounds i8, i8* %13, i64 1
%23 = call i64 @strtoul(i8* %22, i8** %2, i32 0)
%24 = load i8, i8* %22
%25 = icmp eq i8 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %20
%27 = load i8*, i8** %2
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %26, %20
%31 = load i32, i32* @EX_USAGE
%32 = tail call i32 (i32, i8*, ...) @errx(i32 %31, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %22)
br label %33
33: ; preds = %30, %26
%34 = icmp ult i64 %23, 2
br i1 %34, label %35, label %38
35: ; preds = %33
%36 = load i32, i32* @EX_USAGE
%37 = tail call i32 (i32, i8*, ...) @errx(i32 %36, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %22)
br label %46
38: ; preds = %33
%39 = icmp ugt i64 %23, 10000
br i1 %39, label %40, label %46
40: ; preds = %38
%41 = load i32, i32* @EX_USAGE
%42 = tail call i32 (i32, i8*, ...) @errx(i32 %41, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %46
43: ; preds = %12
%44 = load i32, i32* @EX_USAGE
%45 = tail call i32 (i32, i8*, ...) @errx(i32 %44, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %0)
unreachable
46: ; preds = %38, %40, %35, %15, %17
%47 = phi i32 [ 1, %17 ], [ 1, %15 ], [ %21, %35 ], [ %21, %40 ], [ %21, %38 ]
%48 = phi i64 [ %4, %17 ], [ %4, %15 ], [ %23, %35 ], [ %23, %40 ], [ %23, %38 ]
%49 = trunc i64 %48 to i32
%50 = tail call i32 @roll(i32 %47, i32 %49)
%51 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %50)
ret void
}