#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 uint32_t;

/* Variables and functions */
int /*<<< orphan*/ EX_IOERR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ emit_help();
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ exit(int);
int getopt(int, char **, char *);
int open(char *, int /*<<< orphan*/);
scalar_t__ optind;
int /*<<< orphan*/ puts(char *);
int read(int, int *, int);
int main(int argc, char *argv[]) {
  int ch;
  uint32_t result;
  while ((ch = getopt(argc, argv, "h?")) != -1) {
    switch (ch) {
    case 'h':
    case '?':
    default:
      emit_help();
    }
  }
  argc -= optind;
  argv += optind;
  if (argc == 0)
    emit_help();
  if ((ch = open("/dev/urandom", O_RDONLY)) == -1)
    err(EX_IOERR, "open /dev/urandom failed");
  if (read(ch, &result, sizeof(uint32_t)) != sizeof(uint32_t))
    err(EX_IOERR, "read from /dev/urandom failed");
  puts(argv[result % argc]);
  exit(result ? 0 : 1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe6c(%rip),%rdx
call   1080 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     11db <main+0x6b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x3f,%eax
je     11ca <main+0x5a>
jmp    11b9 <main+0x49>
mov    -0x1c(%rbp),%eax
sub    $0x68,%eax
jne    11cf <main+0x5f>
jmp    11ca <main+0x5a>
jmp    11cf <main+0x5f>
mov    $0x0,%al
call   1030 <emit_help@plt>
jmp    1186 <main+0x16>
mov    0x2df6(%rip),%rax
mov    (%rax),%rcx
movslq -0x8(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x8(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%rax
shl    $0x3,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1216 <main+0xa6>
mov    $0x0,%al
call   1030 <emit_help@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
lea    0xddd(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    124c <main+0xdc>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xdc9(%rip),%rsi
call   1060 <err@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cltq
cmp    $0x4,%rax
je     127e <main+0x10e>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
lea    0xdb0(%rip),%rsi
call   1060 <err@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
cltd
idivl  -0x8(%rbp)
mov    -0x28(%rbp),%rax
movslq %edx,%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <puts@plt>
mov    -0x18(%rbp),%ecx
mov    $0x1,%edi
xor    %eax,%eax
cmp    $0x0,%ecx
cmovne %eax,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r15d
lea    0xe7c(%rip),%rdx
call   1080 <getopt@plt>
cmp    $0xffffffff,%eax
je     11ba <main+0x4a>
lea    0xe6b(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1030 <emit_help@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1080 <getopt@plt>
cmp    $0xffffffff,%eax
jne    11a0 <main+0x30>
mov    0x2e17(%rip),%rax
mov    (%rax),%rbp
sub    %ebp,%r15d
jne    11d0 <main+0x60>
xor    %eax,%eax
call   1030 <emit_help@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
lea    0xe23(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    1202 <main+0x92>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
call   1060 <err@plt>
lea    0x4(%rsp),%rsi
mov    %r14d,%edi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x4,%eax
je     122e <main+0xbe>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
call   1060 <err@plt>
lea    (%rbx,%rbp,8),%rcx
mov    0x4(%rsp),%ebx
mov    %ebx,%eax
cltd
idiv   %r15d
movslq %edx,%rax
mov    (%rcx,%rax,8),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
test   %ebx,%ebx
sete   %dil
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"h?\00"
@optind = global i64 0
@.str.1 = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@EX_IOERR = global i32 0
@.str.2 = [25 x i8] c"open /dev/urandom failed\00"
@.str.3 = [30 x i8] c"read from /dev/urandom failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
br label %8
8: ; preds = %18, %2
%9 = load i32, i32* %4
%10 = load i8**, i8*** %5
%11 = call i32 @getopt(i32 %9, i8** %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 %11, i32* %6
%12 = icmp ne i32 %11, -1
br i1 %12, label %13, label %19
13: ; preds = %8
%14 = load i32, i32* %6
switch i32 %14, label %16 [
i32 104, label %15
i32 63, label %15
]
15: ; preds = %13, %13
br label %16
16: ; preds = %13, %15
%17 = call i32 (...) @emit_help()
br label %18
18: ; preds = %16
br label %8
19: ; preds = %8
%20 = load i64, i64* @optind
%21 = load i32, i32* %4
%22 = sext i32 %21 to i64
%23 = sub nsw i64 %22, %20
%24 = trunc i64 %23 to i32
store i32 %24, i32* %4
%25 = load i64, i64* @optind
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 %25
store i8** %27, i8*** %5
%28 = load i32, i32* %4
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %19
%31 = call i32 (...) @emit_help()
br label %32
32: ; preds = %30, %19
%33 = load i32, i32* @O_RDONLY
%34 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %33)
store i32 %34, i32* %6
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i32, i32* @EX_IOERR
%38 = call i32 @err(i32 %37, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %36, %32
%40 = load i32, i32* %6
%41 = call i32 @read(i32 %40, i32* %7, i32 4)
%42 = sext i32 %41 to i64
%43 = icmp ne i64 %42, 4
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = load i32, i32* @EX_IOERR
%46 = call i32 @err(i32 %45, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %44, %39
%48 = load i8**, i8*** %5
%49 = load i32, i32* %7
%50 = load i32, i32* %4
%51 = srem i32 %49, %50
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8*, i8** %48, i64 %52
%54 = load i8*, i8** %53
%55 = call i32 @puts(i8* %54)
%56 = load i32, i32* %7
%57 = icmp ne i32 %56, 0
%58 = zext i1 %57 to i64
%59 = select i1 %57, i32 0, i32 1
%60 = call i32 @exit(i32 %59)
unreachable
}

LLVM IR (-O2):
@.str = [3 x i8] c"h?\00"
@optind = global i64 0
@.str.1 = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@EX_IOERR = global i32 0
@.str.2 = [25 x i8] c"open /dev/urandom failed\00"
@.str.3 = [30 x i8] c"read from /dev/urandom failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32 %5, -1
br i1 %6, label %11, label %7
7: ; preds = %2, %7
%8 = tail call i32 (...) @emit_help()
%9 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %7
11: ; preds = %7, %2
%12 = load i64, i64* @optind
%13 = trunc i64 %12 to i32
%14 = sub i32 %0, %13
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = tail call i32 (...) @emit_help()
br label %18
18: ; preds = %16, %11
%19 = load i32, i32* @O_RDONLY
%20 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* @EX_IOERR
%24 = tail call i32 @err(i32 %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %22, %18
%26 = call i32 @read(i32 %20, i32* %3, i32 4)
%27 = icmp eq i32 %26, 4
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = load i32, i32* @EX_IOERR
%30 = tail call i32 @err(i32 %29, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %28, %25
%32 = getelementptr inbounds i8*, i8** %1, i64 %12
%33 = load i32, i32* %3
%34 = srem i32 %33, %14
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8*, i8** %32, i64 %35
%37 = load i8*, i8** %36
%38 = tail call i32 @puts(i8* dereferenceable(1) %37)
%39 = icmp eq i32 %33, 0
%40 = zext i1 %39 to i32
%41 = tail call i32 @exit(i32 %40)
unreachable
}