#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 */
struct passwd {
  int /*<<< orphan*/ pw_uid;
  int /*<<< orphan*/ pw_gid;
};

/* Variables and functions */
scalar_t__ chdir(char *);
scalar_t__ chroot(char *);
int execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getopt(int, char **, char *);
struct passwd *getpwnam(char *);
char *optarg;
int optind;
char optopt;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setgid(int /*<<< orphan*/);
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ usage();
int main(int argc, char **argv) {
  int c, i;
  char *prog, *wd = NULL, *jail_dir = NULL, *user = NULL;
  char **params;
  struct passwd *p;
  if (setuid(0)) {
    perror("setuid");
    fprintf(stderr, "Program must be run as root or have setuid permissions.");
    usage();
  }
  while ((c = getopt(argc, argv, "d:j:u:")) != -1) {
    switch (c) {
    case 'd':
      wd = optarg;
      break;
    case 'j':
      jail_dir = optarg;
      break;
    case 'u':
      user = optarg;
      break;
    case '?':
      if (optopt == 'd' || optopt == 'j' || optopt == 'u') {
        fprintf(stderr, "Option -%c requires an argument.\n", optopt);
      }
      break;
    default:
      usage();
    }
  }
  if (optind >= argc) {
    fprintf(stderr, "Missing required argument `program`.");
    usage();
  }
  if (user == NULL) {
    user = "nobody";
  } else {
    if (strcmp(user, "root") == 0) {
      fprintf(stderr, "Running jailed program as root is not permitted.");
      usage();
    }
  }
  if ((p = getpwnam(user)) == NULL) {
    perror("getpwname");
    usage();
  }
  if (jail_dir == NULL) {
    jail_dir = "/var/jail";
  }
  if (chdir(jail_dir) != 0) {
    fprintf(stderr, "chdir: %s: ", jail_dir);
    perror("");
    exit(1);
  }
  if (chroot(jail_dir) != 0) {
    fprintf(stderr, "chroot: %s: ", jail_dir);
    perror("");
    exit(1);
  }
  if (wd != NULL) {
    if (chdir(wd) != 0) {
      fprintf(stderr, "chdir: %s: ", wd);
      perror("");
      exit(1);
    }
  }
  setgid(p->pw_gid);
  setuid(p->pw_uid);
  prog = argv[optind];
  params = &argv[optind];
  return execvp(prog, params);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
xor    %edi,%edi
call   10e0 <setuid@plt>
cmp    $0x0,%eax
je     1228 <main+0x68>
lea    0xdfb(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x0,%al
call   1080 <usage@plt>
jmp    122d <main+0x6d>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe04(%rip),%rdx
call   10b0 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     1337 <main+0x177>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4c(%rbp)
sub    $0x3f,%eax
je     12cc <main+0x10c>
jmp    1260 <main+0xa0>
mov    -0x4c(%rbp),%eax
sub    $0x64,%eax
je     1293 <main+0xd3>
jmp    1271 <main+0xb1>
mov    -0x4c(%rbp),%eax
sub    $0x6a,%eax
je     12a6 <main+0xe6>
jmp    1282 <main+0xc2>
mov    -0x4c(%rbp),%eax
sub    $0x75,%eax
je     12b9 <main+0xf9>
jmp    132b <main+0x16b>
mov    0x2d5e(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    1332 <main+0x172>
mov    0x2d4b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    1332 <main+0x172>
mov    0x2d38(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    1332 <main+0x172>
mov    0x2d0d(%rip),%rax
movsbl (%rax),%eax
cmp    $0x64,%eax
je     1305 <main+0x145>
mov    0x2cfa(%rip),%rax
movsbl (%rax),%eax
cmp    $0x6a,%eax
je     1305 <main+0x145>
mov    0x2ce7(%rip),%rax
movsbl (%rax),%eax
cmp    $0x75,%eax
jne    1326 <main+0x166>
mov    0x2cbc(%rip),%rax
mov    (%rax),%edi
mov    0x2ccb(%rip),%rax
movsbl (%rax),%edx
lea    0xd27(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1332 <main+0x172>
mov    $0x0,%al
call   1080 <usage@plt>
jmp    122d <main+0x6d>
mov    0x2c9a(%rip),%rax
mov    (%rax),%eax
cmp    -0x8(%rbp),%eax
jl     1367 <main+0x1a7>
mov    0x2c78(%rip),%rax
mov    (%rax),%edi
lea    0xd0f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x0,%al
call   1080 <usage@plt>
cmpq   $0x0,-0x38(%rbp)
jne    1382 <main+0x1c2>
lea    0xd14(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    13bf <main+0x1ff>
mov    -0x38(%rbp),%rdi
lea    0xd07(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    13ba <main+0x1fa>
mov    0x2c25(%rip),%rax
mov    (%rax),%edi
lea    0xced(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x0,%al
call   1080 <usage@plt>
jmp    13bf <main+0x1ff>
mov    -0x38(%rbp),%rdi
call   1060 <getpwnam@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    13e9 <main+0x229>
lea    0xced(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x0,%al
call   1080 <usage@plt>
cmpq   $0x0,-0x30(%rbp)
jne    13ff <main+0x23f>
lea    0xcd9(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     1443 <main+0x283>
mov    0x2baf(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xcb8(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    0xc33(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x30(%rbp),%rdi
call   1030 <chroot@plt>
cmp    $0x0,%rax
je     1487 <main+0x2c7>
mov    0x2b6b(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xc80(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    0xbef(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
je     14db <main+0x31b>
mov    -0x28(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     14d6 <main+0x316>
mov    0x2b1c(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xc25(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    0xba0(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    14db <main+0x31b>
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%edi
call   1090 <setgid@plt>
mov    -0x48(%rbp),%rax
mov    (%rax),%edi
call   10e0 <setuid@plt>
mov    -0x10(%rbp),%rax
mov    0x2adb(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2ac5(%rip),%rcx
movslq (%rcx),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   10c0 <execvp@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r12
mov    %edi,%ebp
xor    %edi,%edi
call   10e0 <setuid@plt>
test   %eax,%eax
jne    13bc <main+0x1fc>
lea    0xe56(%rip),%rbx
mov    0x2df0(%rip),%r15
mov    $0x20041,%r14d
lea    0xe49(%rip),%r13
xor    %eax,%eax
mov    %rax,0x8(%rsp)
xor    %eax,%eax
mov    %rax,0x10(%rsp)
xor    %eax,%eax
mov    %rax,(%rsp)
jmp    1230 <main+0x70>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    $0xffffffff,%eax
je     12bf <main+0xff>
xor    %eax,%eax
call   1080 <usage@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %rbx,%rdx
call   10b0 <getopt@plt>
cmp    $0x63,%eax
jg     1270 <main+0xb0>
cmp    $0x3f,%eax
jne    1220 <main+0x60>
movzbl (%r15),%edx
lea    -0x64(%rdx),%eax
cmp    $0x11,%al
ja     1230 <main+0x70>
movzbl %al,%eax
bt     %rax,%r14
jae    1230 <main+0x70>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
mov    %r13,%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1230 <main+0x70>
cmp    $0x75,%eax
je     1281 <main+0xc1>
cmp    $0x6a,%eax
je     1292 <main+0xd2>
cmp    $0x64,%eax
jne    1229 <main+0x69>
jmp    12b0 <main+0xf0>
mov    0x2d70(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
jmp    1230 <main+0x70>
mov    0x2d5f(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
jmp    1230 <main+0x70>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x2d41(%rip),%rax
mov    (%rax),%rax
jmp    120d <main+0x4d>
mov    0x2d12(%rip),%r13
cmp    %ebp,0x0(%r13)
jl     12ea <main+0x12a>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
lea    0xd8c(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %eax,%eax
call   1080 <usage@plt>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     1328 <main+0x168>
lea    0xd99(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    132f <main+0x16f>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
lea    0xd81(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %eax,%eax
call   1080 <usage@plt>
jmp    132f <main+0x16f>
lea    0xd5e(%rip),%rbx
mov    %rbx,%rdi
call   1060 <getpwnam@plt>
mov    %rax,%rbx
test   %rax,%rax
je     13eb <main+0x22b>
mov    0x10(%rsp),%rax
test   %rax,%rax
lea    0xd82(%rip),%rbp
cmovne %rax,%rbp
mov    %rbp,%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jne    1403 <main+0x243>
mov    %rbp,%rdi
call   1030 <chroot@plt>
test   %rax,%rax
jne    1415 <main+0x255>
mov    (%rsp),%rbp
test   %rbp,%rbp
je     138e <main+0x1ce>
mov    %rbp,%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jne    1403 <main+0x243>
mov    0x4(%rbx),%edi
call   1090 <setgid@plt>
mov    (%rbx),%edi
call   10e0 <setuid@plt>
movslq 0x0(%r13),%rax
lea    (%r12,%rax,8),%rsi
mov    (%r12,%rax,8),%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10c0 <execvp@plt>
lea    0xc3d(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2bf9(%rip),%rax
mov    (%rax),%edi
lea    0xc2f(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %eax,%eax
call   1080 <usage@plt>
jmp    11e2 <main+0x22>
lea    0xcd8(%rip),%rdi
call   10a0 <perror@plt>
xor    %eax,%eax
call   1080 <usage@plt>
jmp    1343 <main+0x183>
mov    0x2bbe(%rip),%rax
mov    (%rax),%edi
lea    0xccb(%rip),%rsi
jmp    1425 <main+0x265>
mov    0x2bac(%rip),%rax
mov    (%rax),%edi
lea    0xcc5(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0xc31(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@.str = [7 x i8] c"setuid\00"
@stderr = global i32 0
@.str.1 = [56 x i8] c"Program must be run as root or have setuid permissions.\00"
@.str.2 = [7 x i8] c"d:j:u:\00"
@optarg = global i8* null
@optopt = global i8 0
@.str.3 = [34 x i8] c"Option -%c requires an argument.\0A\00"
@optind = global i32 0
@.str.4 = [37 x i8] c"Missing required argument `program`.\00"
@.str.5 = [7 x i8] c"nobody\00"
@.str.6 = [5 x i8] c"root\00"
@.str.7 = [49 x i8] c"Running jailed program as root is not permitted.\00"
@.str.8 = [10 x i8] c"getpwname\00"
@.str.9 = [10 x i8] c"/var/jail\00"
@.str.10 = [12 x i8] c"chdir: %s: \00"
@.str.11 = [1 x i8] zeroinitializer
@.str.12 = [13 x i8] c"chroot: %s: \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8**
%13 = alloca %struct.passwd*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %9
store i8* null, i8** %10
store i8* null, i8** %11
%14 = call i32 @setuid(i32 0)
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 (...) @usage()
br label %21
21: ; preds = %16, %2
br label %22
22: ; preds = %55, %21
%23 = load i32, i32* %4
%24 = load i8**, i8*** %5
%25 = call i32 @getopt(i32 %23, i8** %24, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 %25, i32* %6
%26 = icmp ne i32 %25, -1
br i1 %26, label %27, label %56
27: ; preds = %22
%28 = load i32, i32* %6
switch i32 %28, label %53 [
i32 100, label %29
i32 106, label %31
i32 117, label %33
i32 63, label %35
]
29: ; preds = %27
%30 = load i8*, i8** @optarg
store i8* %30, i8** %9
br label %55
31: ; preds = %27
%32 = load i8*, i8** @optarg
store i8* %32, i8** %10
br label %55
33: ; preds = %27
%34 = load i8*, i8** @optarg
store i8* %34, i8** %11
br label %55
35: ; preds = %27
%36 = load i8, i8* @optopt
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 100
br i1 %38, label %47, label %39
39: ; preds = %35
%40 = load i8, i8* @optopt
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 106
br i1 %42, label %47, label %43
43: ; preds = %39
%44 = load i8, i8* @optopt
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 117
br i1 %46, label %47, label %52
47: ; preds = %43, %39, %35
%48 = load i32, i32* @stderr
%49 = load i8, i8* @optopt
%50 = sext i8 %49 to i32
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i32 %50)
br label %52
52: ; preds = %47, %43
br label %55
53: ; preds = %27
%54 = call i32 (...) @usage()
br label %55
55: ; preds = %53, %52, %33, %31, %29
br label %22
56: ; preds = %22
%57 = load i32, i32* @optind
%58 = load i32, i32* %4
%59 = icmp sge i32 %57, %58
br i1 %59, label %60, label %64
60: ; preds = %56
%61 = load i32, i32* @stderr
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%63 = call i32 (...) @usage()
br label %64
64: ; preds = %60, %56
%65 = load i8*, i8** %11
%66 = icmp eq i8* %65, null
br i1 %66, label %67, label %68
67: ; preds = %64
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8** %11
br label %77
68: ; preds = %64
%69 = load i8*, i8** %11
%70 = call i64 @strcmp(i8* %69, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%71 = icmp eq i64 %70, 0
br i1 %71, label %72, label %76
72: ; preds = %68
%73 = load i32, i32* @stderr
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.7, i64 0, i64 0))
%75 = call i32 (...) @usage()
br label %76
76: ; preds = %72, %68
br label %77
77: ; preds = %76, %67
%78 = load i8*, i8** %11
%79 = call %struct.passwd* @getpwnam(i8* %78)
store %struct.passwd* %79, %struct.passwd** %13
%80 = icmp eq %struct.passwd* %79, null
br i1 %80, label %81, label %84
81: ; preds = %77
%82 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
%83 = call i32 (...) @usage()
br label %84
84: ; preds = %81, %77
%85 = load i8*, i8** %10
%86 = icmp eq i8* %85, null
br i1 %86, label %87, label %88
87: ; preds = %84
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0), i8** %10
br label %88
88: ; preds = %87, %84
%89 = load i8*, i8** %10
%90 = call i64 @chdir(i8* %89)
%91 = icmp ne i64 %90, 0
br i1 %91, label %92, label %98
92: ; preds = %88
%93 = load i32, i32* @stderr
%94 = load i8*, i8** %10
%95 = call i32 (i32, i8*, ...) @fprintf(i32 %93, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0), i8* %94)
%96 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.11, i64 0, i64 0))
%97 = call i32 @exit(i32 1)
unreachable
98: ; preds = %88
%99 = load i8*, i8** %10
%100 = call i64 @chroot(i8* %99)
%101 = icmp ne i64 %100, 0
br i1 %101, label %102, label %108
102: ; preds = %98
%103 = load i32, i32* @stderr
%104 = load i8*, i8** %10
%105 = call i32 (i32, i8*, ...) @fprintf(i32 %103, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.12, i64 0, i64 0), i8* %104)
%106 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.11, i64 0, i64 0))
%107 = call i32 @exit(i32 1)
unreachable
108: ; preds = %98
%109 = load i8*, i8** %9
%110 = icmp ne i8* %109, null
br i1 %110, label %111, label %122
111: ; preds = %108
%112 = load i8*, i8** %9
%113 = call i64 @chdir(i8* %112)
%114 = icmp ne i64 %113, 0
br i1 %114, label %115, label %121
115: ; preds = %111
%116 = load i32, i32* @stderr
%117 = load i8*, i8** %9
%118 = call i32 (i32, i8*, ...) @fprintf(i32 %116, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0), i8* %117)
%119 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.11, i64 0, i64 0))
%120 = call i32 @exit(i32 1)
unreachable
121: ; preds = %111
br label %122
122: ; preds = %121, %108
%123 = load %struct.passwd*, %struct.passwd** %13
%124 = getelementptr inbounds %struct.passwd, %struct.passwd* %123, i32 0, i32 1
%125 = load i32, i32* %124
%126 = call i32 @setgid(i32 %125)
%127 = load %struct.passwd*, %struct.passwd** %13
%128 = getelementptr inbounds %struct.passwd, %struct.passwd* %127, i32 0, i32 0
%129 = load i32, i32* %128
%130 = call i32 @setuid(i32 %129)
%131 = load i8**, i8*** %5
%132 = load i32, i32* @optind
%133 = sext i32 %132 to i64
%134 = getelementptr inbounds i8*, i8** %131, i64 %133
%135 = load i8*, i8** %134
store i8* %135, i8** %8
%136 = load i8**, i8*** %5
%137 = load i32, i32* @optind
%138 = sext i32 %137 to i64
%139 = getelementptr inbounds i8*, i8** %136, i64 %138
store i8** %139, i8*** %12
%140 = load i8*, i8** %8
%141 = load i8**, i8*** %12
%142 = call i32 @execvp(i8* %140, i8** %141)
ret i32 %142
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@.str = [7 x i8] c"setuid\00"
@stderr = global i32 0
@.str.1 = [56 x i8] c"Program must be run as root or have setuid permissions.\00"
@.str.2 = [7 x i8] c"d:j:u:\00"
@optarg = global i8* null
@optopt = global i8 0
@.str.3 = [34 x i8] c"Option -%c requires an argument.\0A\00"
@optind = global i32 0
@.str.4 = [37 x i8] c"Missing required argument `program`.\00"
@.str.5 = [7 x i8] c"nobody\00"
@.str.6 = [5 x i8] c"root\00"
@.str.7 = [49 x i8] c"Running jailed program as root is not permitted.\00"
@.str.8 = [10 x i8] c"getpwname\00"
@.str.9 = [10 x i8] c"/var/jail\00"
@.str.10 = [12 x i8] c"chdir: %s: \00"
@.str.11 = [1 x i8] zeroinitializer
@.str.12 = [13 x i8] c"chroot: %s: \00"
define i32 @main(i32 %0, i8** %1) {
%3 = tail call i32 @setuid(i32 0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 (...) @usage()
br label %10
10: ; preds = %5, %2
br label %11
11: ; preds = %10, %26
%12 = phi i8* [ null, %10 ], [ %27, %26 ]
%13 = phi i8* [ null, %10 ], [ %16, %26 ]
%14 = phi i8* [ null, %10 ], [ %19, %26 ]
br label %15
15: ; preds = %11, %24
%16 = phi i8* [ %13, %11 ], [ %25, %24 ]
%17 = phi i8* [ %14, %11 ], [ %19, %24 ]
br label %18
18: ; preds = %15, %22
%19 = phi i8* [ %17, %15 ], [ %23, %22 ]
br label %20
20: ; preds = %30, %18
%21 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
switch i32 %21, label %35 [
i32 -1, label %37
i32 100, label %22
i32 106, label %24
i32 117, label %26
i32 63, label %28
]
22: ; preds = %20
%23 = load i8*, i8** @optarg
br label %18
24: ; preds = %20
%25 = load i8*, i8** @optarg
br label %15
26: ; preds = %20
%27 = load i8*, i8** @optarg
br label %11
28: ; preds = %20
%29 = load i8, i8* @optopt
switch i8 %29, label %30 [
i8 117, label %31
i8 106, label %31
i8 100, label %31
]
30: ; preds = %28, %31, %35
br label %20
31: ; preds = %28, %28, %28
%32 = zext i8 %29 to i32
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i32 %32)
br label %30
35: ; preds = %20
%36 = tail call i32 (...) @usage()
br label %30
37: ; preds = %20
%38 = load i32, i32* @optind
%39 = icmp slt i32 %38, %0
br i1 %39, label %44, label %40
40: ; preds = %37
%41 = load i32, i32* @stderr
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%43 = tail call i32 (...) @usage()
br label %44
44: ; preds = %40, %37
%45 = icmp eq i8* %12, null
br i1 %45, label %53, label %46
46: ; preds = %44
%47 = tail call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %46
%50 = load i32, i32* @stderr
%51 = tail call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.7, i64 0, i64 0))
%52 = tail call i32 (...) @usage()
br label %53
53: ; preds = %44, %46, %49
%54 = phi i8* [ %12, %49 ], [ %12, %46 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), %44 ]
%55 = tail call %struct.passwd* @getpwnam(i8* %54)
%56 = icmp eq %struct.passwd* %55, null
br i1 %56, label %57, label %60
57: ; preds = %53
%58 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
%59 = tail call i32 (...) @usage()
br label %60
60: ; preds = %57, %53
%61 = icmp eq i8* %16, null
%62 = select i1 %61, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0), i8* %16
%63 = tail call i64 @chdir(i8* %62)
%64 = icmp eq i64 %63, 0
br i1 %64, label %70, label %65
65: ; preds = %60
%66 = load i32, i32* @stderr
%67 = tail call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0), i8* %62)
%68 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.11, i64 0, i64 0))
%69 = tail call i32 @exit(i32 1)
unreachable
70: ; preds = %60
%71 = tail call i64 @chroot(i8* %62)
%72 = icmp eq i64 %71, 0
br i1 %72, label %78, label %73
73: ; preds = %70
%74 = load i32, i32* @stderr
%75 = tail call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.12, i64 0, i64 0), i8* %62)
%76 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.11, i64 0, i64 0))
%77 = tail call i32 @exit(i32 1)
unreachable
78: ; preds = %70
%79 = icmp eq i8* %19, null
br i1 %79, label %88, label %80
80: ; preds = %78
%81 = tail call i64 @chdir(i8* %19)
%82 = icmp eq i64 %81, 0
br i1 %82, label %88, label %83
83: ; preds = %80
%84 = load i32, i32* @stderr
%85 = tail call i32 (i32, i8*, ...) @fprintf(i32 %84, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0), i8* %19)
%86 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.11, i64 0, i64 0))
%87 = tail call i32 @exit(i32 1)
unreachable
88: ; preds = %80, %78
%89 = getelementptr inbounds %struct.passwd, %struct.passwd* %55, i64 0, i32 1
%90 = load i32, i32* %89
%91 = tail call i32 @setgid(i32 %90)
%92 = getelementptr inbounds %struct.passwd, %struct.passwd* %55, i64 0, i32 0
%93 = load i32, i32* %92
%94 = tail call i32 @setuid(i32 %93)
%95 = load i32, i32* @optind
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds i8*, i8** %1, i64 %96
%98 = load i8*, i8** %97
%99 = tail call i32 @execvp(i8* %98, i8** %97)
ret i32 %99
}