#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 /*<<< orphan*/ random;
typedef int id_t;
typedef int /*<<< orphan*/ cwd;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int PATH_MAX;
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ failQuit(char *);
int /*<<< orphan*/ *getcwd(char *, int);
int open(char *, int /*<<< orphan*/);
int read(int, unsigned short *, int);
scalar_t__ setgid(int);
scalar_t__ setuid(int);
int /*<<< orphan*/ usageQuit();
int main(int argc, char **argv) {
  char cwd[PATH_MAX];
  id_t id;
  int fd;
  unsigned short random;
  if (argc == 1)
    usageQuit();
  fd = open("/dev/urandom", O_RDONLY);
  if (fd == -1)
    failQuit("Unable to open /dev/urandom");
  if (read(fd, &random, sizeof(random)) != sizeof(random))
    failQuit("Unable to read from /dev/urandom");
  close(fd);
  if (getcwd(cwd, sizeof(cwd)) == NULL)
    failQuit("getcwd() failed");
  id = 31337 + random;
  if (setgid(id) != 0 || setuid(id) != 0) {
    failQuit("Unable to setuid(), aborting.");
  }
  if (chdir(cwd) != 0)
    if (chdir("/") != 0)
      failQuit("Unable to chdir(\"/\")");
  execvp(argv[1], argv + 1);
  failQuit("Not able to execute.");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11f7 <main+0x57>
mov    $0x0,%al
call   1090 <usageQuit@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    0xe29(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1225 <main+0x85>
lea    0xde0(%rip),%rdi
call   1070 <failQuit@plt>
mov    -0x28(%rbp),%edi
lea    -0x2a(%rbp),%rsi
mov    $0x2,%edx
call   1060 <read@plt>
cltq
cmp    $0x2,%rax
je     124e <main+0xae>
lea    0xdd3(%rip),%rdi
call   1070 <failQuit@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %eax,%esi
call   1040 <getcwd@plt>
cmp    $0x0,%rax
jne    127b <main+0xdb>
lea    0xdc7(%rip),%rdi
call   1070 <failQuit@plt>
movzwl -0x2a(%rbp),%eax
add    $0x7a69,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1080 <setgid@plt>
cmp    $0x0,%rax
jne    12ab <main+0x10b>
mov    -0x24(%rbp),%edi
call   10c0 <setuid@plt>
cmp    $0x0,%rax
je     12b7 <main+0x117>
lea    0xd9b(%rip),%rdi
call   1070 <failQuit@plt>
mov    -0x38(%rbp),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
je     12f1 <main+0x151>
lea    0xd9a(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
je     12ec <main+0x14c>
lea    0xd86(%rip),%rdi
call   1070 <failQuit@plt>
jmp    12f1 <main+0x151>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   10b0 <execvp@plt>
lea    0xd75(%rip),%rdi
call   1070 <failQuit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    0x2e37(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%r12
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
cmp    $0x1,%edi
jne    11d9 <main+0x39>
xor    %eax,%eax
call   1090 <usageQuit@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
lea    0xe47(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1201 <main+0x61>
lea    0xe04(%rip),%rdi
call   1070 <failQuit@plt>
lea    -0x22(%rbp),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1060 <read@plt>
cmp    $0x2,%eax
je     1222 <main+0x82>
lea    0xdff(%rip),%rdi
call   1070 <failQuit@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r12,%rdi
mov    %r15d,%esi
call   1040 <getcwd@plt>
test   %rax,%rax
jne    1245 <main+0xa5>
lea    0xdfd(%rip),%rdi
call   1070 <failQuit@plt>
movzwl -0x22(%rbp),%ebx
add    $0x7a69,%ebx
mov    %ebx,%edi
call   1080 <setgid@plt>
test   %rax,%rax
jne    1267 <main+0xc7>
mov    %ebx,%edi
call   10c0 <setuid@plt>
test   %rax,%rax
je     1273 <main+0xd3>
lea    0xddf(%rip),%rdi
call   1070 <failQuit@plt>
mov    %r12,%rdi
call   1030 <chdir@plt>
test   %rax,%rax
je     129d <main+0xfd>
lea    0xde4(%rip),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
je     129d <main+0xfd>
lea    0xdd5(%rip),%rdi
call   1070 <failQuit@plt>
mov    0x8(%r14),%rdi
add    $0x8,%r14
mov    %r14,%rsi
call   10b0 <execvp@plt>
lea    0xdce(%rip),%rdi
call   1070 <failQuit@plt>
xor    %eax,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [28 x i8] c"Unable to open /dev/urandom\00"
@.str.2 = [33 x i8] c"Unable to read from /dev/urandom\00"
@.str.3 = [16 x i8] c"getcwd() failed\00"
@.str.4 = [30 x i8] c"Unable to setuid(), aborting.\00"
@.str.5 = [2 x i8] c"/\00"
@.str.6 = [21 x i8] c"Unable to chdir(\22/\22)\00"
@.str.7 = [21 x i8] c"Not able to execute.\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i16
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 (...) @usageQuit()
br label %19
19: ; preds = %17, %2
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %20)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @failQuit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %19
%27 = load i32, i32* %9
%28 = call i32 @read(i32 %27, i16* %10, i32 2)
%29 = sext i32 %28 to i64
%30 = icmp ne i64 %29, 2
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @failQuit(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %26
%34 = load i32, i32* %9
%35 = call i32 @close(i32 %34)
%36 = trunc i64 %12 to i32
%37 = call i32* @getcwd(i8* %14, i32 %36)
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 @failQuit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %33
%42 = load i16, i16* %10
%43 = zext i16 %42 to i32
%44 = add nsw i32 31337, %43
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = call i64 @setgid(i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %52, label %48
48: ; preds = %41
%49 = load i32, i32* %8
%50 = call i64 @setuid(i32 %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48, %41
%53 = call i32 @failQuit(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %54
54: ; preds = %52, %48
%55 = call i64 @chdir(i8* %14)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %63
57: ; preds = %54
%58 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %57
%61 = call i32 @failQuit(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
br label %62
62: ; preds = %60, %57
br label %63
63: ; preds = %62, %54
%64 = load i8**, i8*** %5
%65 = getelementptr inbounds i8*, i8** %64, i64 1
%66 = load i8*, i8** %65
%67 = load i8**, i8*** %5
%68 = getelementptr inbounds i8*, i8** %67, i64 1
%69 = call i32 @execvp(i8* %66, i8** %68)
%70 = call i32 @failQuit(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%71 = load i8*, i8** %6
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [28 x i8] c"Unable to open /dev/urandom\00"
@.str.2 = [33 x i8] c"Unable to read from /dev/urandom\00"
@.str.3 = [16 x i8] c"getcwd() failed\00"
@.str.4 = [30 x i8] c"Unable to setuid(), aborting.\00"
@.str.5 = [2 x i8] c"/\00"
@.str.6 = [21 x i8] c"Unable to chdir(\22/\22)\00"
@.str.7 = [21 x i8] c"Not able to execute.\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i16
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast i16* %3 to i8*
%8 = icmp eq i32 %0, 1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 (...) @usageQuit()
br label %11
11: ; preds = %9, %2
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @failQuit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = call i32 @read(i32 %13, i16* %3, i32 2)
%19 = icmp eq i32 %18, 2
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = tail call i32 @failQuit(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
%23 = tail call i32 @close(i32 %13)
%24 = call i32* @getcwd(i8* %6, i32 %4)
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @failQuit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
%29 = load i16, i16* %3
%30 = zext i16 %29 to i32
%31 = add nuw nsw i32 %30, 31337
%32 = call i64 @setgid(i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = call i64 @setuid(i32 %31)
%36 = icmp eq i64 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %34, %28
%38 = call i32 @failQuit(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
%40 = call i64 @chdir(i8* %6)
%41 = icmp eq i64 %40, 0
br i1 %41, label %47, label %42
42: ; preds = %39
%43 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%44 = icmp eq i64 %43, 0
br i1 %44, label %47, label %45
45: ; preds = %42
%46 = call i32 @failQuit(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
br label %47
47: ; preds = %42, %45, %39
%48 = getelementptr inbounds i8*, i8** %1, i64 1
%49 = load i8*, i8** %48
%50 = call i32 @execvp(i8* %49, i8** %48)
%51 = call i32 @failQuit(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
ret i32 0
}