#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 pw_gid;
  int pw_uid;
  int /*<<< orphan*/ pw_name;
};
typedef int gid_t;

/* Variables and functions */
int /*<<< orphan*/ Usage();
scalar_t__ chdir(char *);
scalar_t__ chroot(char *);
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ geteuid();
struct passwd *getpwnam(char *);
int /*<<< orphan*/ initgroups(int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setgid(int);
int /*<<< orphan*/ setgroups(int, int *);
scalar_t__ setuid(int);
int /*<<< orphan*/ stderr;
int strtoul(char *, char **, int /*<<< orphan*/);
int main(int argc, char **argv) {
  char *dir, *end;
  char *user;
  struct passwd *pw;
  int uid, gid = 99;
  gid_t gidset[1];
  gidset[0] = gid;
  if (geteuid() != 0) {
    fprintf(stderr, "jail_uml must be run as root\n");
    exit(1);
  }
  if (argc < 3)
    Usage();
  dir = argv[1];
  user = argv[2];
  pw = getpwnam(user);
  if (pw == NULL) {
    uid = strtoul(argv[2], &end, 0);
    if (*end != '\0')
      Usage();
    setgroups(1, gidset);
  } else {
    initgroups(pw->pw_name, pw->pw_gid);
    uid = pw->pw_uid;
    gid = pw->pw_gid;
  }
  argc -= 3;
  argv += 3;
  if (chdir(dir)) {
    perror("chdir");
    exit(1);
  }
  if (chroot(".")) {
    perror("chroot");
    exit(1);
  }
  if (setgid(gid)) {
    perror("setgid");
    exit(1);
  }
  if (setuid(uid)) {
    perror("setuid");
    exit(1);
  }
  execv(argv[0], argv);
  fprintf(stderr, "execve of %s failed : ", argv[0]);
  perror("");
  exit(1);
}


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)
movl   $0x63,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   1050 <geteuid@plt>
cmp    $0x0,%rax
je     1235 <main+0x55>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
lea    0xddc(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
cmpl   $0x3,-0x8(%rbp)
jge    1246 <main+0x66>
mov    $0x0,%al
call   1070 <Usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <getpwnam@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12b6 <main+0xd6>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   10c0 <strtoul@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12a3 <main+0xc3>
mov    $0x0,%al
call   1070 <Usage@plt>
lea    -0x3c(%rbp),%rsi
mov    $0x1,%edi
call   1060 <setgroups@plt>
jmp    12db <main+0xfb>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x30(%rbp),%rax
mov    (%rax),%esi
call   10f0 <initgroups@plt>
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x3,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x18,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     1319 <main+0x139>
lea    0xd14(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd04(%rip),%rdi
call   1030 <chroot@plt>
cmp    $0x0,%rax
je     1345 <main+0x165>
lea    0xcf0(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x38(%rbp),%edi
call   10a0 <setgid@plt>
cmp    $0x0,%rax
je     136d <main+0x18d>
lea    0xccf(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x34(%rbp),%edi
call   10e0 <setuid@plt>
cmp    $0x0,%rax
je     1395 <main+0x1b5>
lea    0xcae(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1100 <execv@plt>
mov    0x2c34(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xc7f(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
lea    0xc53(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
movl   $0x63,0x4(%rsp)
xor    %eax,%eax
call   1050 <geteuid@plt>
test   %rax,%rax
jne    125e <main+0x7e>
cmp    $0x2,%ebp
jg     1211 <main+0x31>
xor    %eax,%eax
call   1070 <Usage@plt>
mov    0x8(%rbx),%r14
mov    0x10(%rbx),%rdi
call   1080 <getpwnam@plt>
test   %rax,%rax
jne    127f <main+0x9f>
mov    0x10(%rbx),%rdi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   10c0 <strtoul@plt>
mov    %eax,%r15d
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
je     1247 <main+0x67>
xor    %eax,%eax
call   1070 <Usage@plt>
lea    0x4(%rsp),%rsi
mov    $0x1,%edi
call   1060 <setgroups@plt>
mov    $0x63,%r12d
jmp    1294 <main+0xb4>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
lea    0xd92(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    %rax,%rbp
mov    (%rax),%esi
mov    0x8(%rax),%edi
call   10f0 <initgroups@plt>
mov    0x0(%rbp),%r12d
mov    0x4(%rbp),%r15d
mov    %r14,%rdi
call   1040 <chdir@plt>
test   %rax,%rax
jne    12fe <main+0x11e>
lea    0xd7c(%rip),%rdi
call   1030 <chroot@plt>
test   %rax,%rax
jne    1307 <main+0x127>
mov    %r12d,%edi
call   10a0 <setgid@plt>
test   %rax,%rax
jne    1310 <main+0x130>
mov    %r15d,%edi
call   10e0 <setuid@plt>
test   %rax,%rax
jne    1319 <main+0x139>
add    $0x18,%rbx
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1100 <execv@plt>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd4d(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
lea    0xd21(%rip),%rdi
jmp    1320 <main+0x140>
lea    0xd19(%rip),%rdi
jmp    1320 <main+0x140>
lea    0xd18(%rip),%rdi
jmp    1320 <main+0x140>
lea    0xd16(%rip),%rdi
jmp    1320 <main+0x140>
lea    0xd14(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32, i32, i32 }
@stderr = global i32 0
@.str = [30 x i8] c"jail_uml must be run as root\0A\00"
@.str.1 = [6 x i8] c"chdir\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [7 x i8] c"chroot\00"
@.str.4 = [7 x i8] c"setgid\00"
@.str.5 = [7 x i8] c"setuid\00"
@.str.6 = [23 x i8] c"execve of %s failed : \00"
@.str.7 = [1 x i8] zeroinitializer
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.passwd*
%10 = alloca i32
%11 = alloca i32
%12 = alloca [1 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 99, i32* %11
%13 = load i32, i32* %11
%14 = getelementptr inbounds [1 x i32], [1 x i32]* %12, i64 0, i64 0
store i32 %13, i32* %14
%15 = call i64 (...) @geteuid()
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 3
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 (...) @Usage()
br label %26
26: ; preds = %24, %21
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
store i8* %29, i8** %6
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
store i8* %32, i8** %8
%33 = load i8*, i8** %8
%34 = call %struct.passwd* @getpwnam(i8* %33)
store %struct.passwd* %34, %struct.passwd** %9
%35 = load %struct.passwd*, %struct.passwd** %9
%36 = icmp eq %struct.passwd* %35, null
br i1 %36, label %37, label %51
37: ; preds = %26
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 2
%40 = load i8*, i8** %39
%41 = call i32 @strtoul(i8* %40, i8** %7, i32 0)
store i32 %41, i32* %10
%42 = load i8*, i8** %7
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %37
%47 = call i32 (...) @Usage()
br label %48
48: ; preds = %46, %37
%49 = getelementptr inbounds [1 x i32], [1 x i32]* %12, i64 0, i64 0
%50 = call i32 @setgroups(i32 1, i32* %49)
br label %65
51: ; preds = %26
%52 = load %struct.passwd*, %struct.passwd** %9
%53 = getelementptr inbounds %struct.passwd, %struct.passwd* %52, i32 0, i32 2
%54 = load i32, i32* %53
%55 = load %struct.passwd*, %struct.passwd** %9
%56 = getelementptr inbounds %struct.passwd, %struct.passwd* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i32 @initgroups(i32 %54, i32 %57)
%59 = load %struct.passwd*, %struct.passwd** %9
%60 = getelementptr inbounds %struct.passwd, %struct.passwd* %59, i32 0, i32 1
%61 = load i32, i32* %60
store i32 %61, i32* %10
%62 = load %struct.passwd*, %struct.passwd** %9
%63 = getelementptr inbounds %struct.passwd, %struct.passwd* %62, i32 0, i32 0
%64 = load i32, i32* %63
store i32 %64, i32* %11
br label %65
65: ; preds = %51, %48
%66 = load i32, i32* %4
%67 = sub nsw i32 %66, 3
store i32 %67, i32* %4
%68 = load i8**, i8*** %5
%69 = getelementptr inbounds i8*, i8** %68, i64 3
store i8** %69, i8*** %5
%70 = load i8*, i8** %6
%71 = call i64 @chdir(i8* %70)
%72 = icmp ne i64 %71, 0
br i1 %72, label %73, label %76
73: ; preds = %65
%74 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %65
%77 = call i64 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%78 = icmp ne i64 %77, 0
br i1 %78, label %79, label %82
79: ; preds = %76
%80 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%81 = call i32 @exit(i32 1)
unreachable
82: ; preds = %76
%83 = load i32, i32* %11
%84 = call i64 @setgid(i32 %83)
%85 = icmp ne i64 %84, 0
br i1 %85, label %86, label %89
86: ; preds = %82
%87 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%88 = call i32 @exit(i32 1)
unreachable
89: ; preds = %82
%90 = load i32, i32* %10
%91 = call i64 @setuid(i32 %90)
%92 = icmp ne i64 %91, 0
br i1 %92, label %93, label %96
93: ; preds = %89
%94 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%95 = call i32 @exit(i32 1)
unreachable
96: ; preds = %89
%97 = load i8**, i8*** %5
%98 = getelementptr inbounds i8*, i8** %97, i64 0
%99 = load i8*, i8** %98
%100 = load i8**, i8*** %5
%101 = call i32 @execv(i8* %99, i8** %100)
%102 = load i32, i32* @stderr
%103 = load i8**, i8*** %5
%104 = getelementptr inbounds i8*, i8** %103, i64 0
%105 = load i8*, i8** %104
%106 = call i32 (i32, i8*, ...) @fprintf(i32 %102, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i8* %105)
%107 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0))
%108 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32, i32 }
@stderr = global i32 0
@.str = [30 x i8] c"jail_uml must be run as root\0A\00"
@.str.1 = [6 x i8] c"chdir\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [7 x i8] c"chroot\00"
@.str.4 = [7 x i8] c"setgid\00"
@.str.5 = [7 x i8] c"setuid\00"
@.str.6 = [23 x i8] c"execve of %s failed : \00"
@.str.7 = [1 x i8] zeroinitializer
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i8*
%4 = alloca [1 x i32]
%5 = bitcast i8** %3 to i8*
%6 = bitcast [1 x i32]* %4 to i8*
%7 = getelementptr inbounds [1 x i32], [1 x i32]* %4, i64 0, i64 0
store i32 99, i32* %7
%8 = tail call i64 (...) @geteuid()
%9 = icmp eq i64 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = icmp slt i32 %0, 3
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = tail call i32 (...) @Usage()
br label %18
18: ; preds = %16, %14
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = tail call %struct.passwd* @getpwnam(i8* %22)
%24 = icmp eq %struct.passwd* %23, null
br i1 %24, label %25, label %35
25: ; preds = %18
%26 = load i8*, i8** %21
%27 = call i32 @strtoul(i8* %26, i8** %3, i32 0)
%28 = load i8*, i8** %3
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %25
%32 = tail call i32 (...) @Usage()
br label %33
33: ; preds = %31, %25
%34 = call i32 @setgroups(i32 1, i32* %7)
br label %44
35: ; preds = %18
%36 = getelementptr inbounds %struct.passwd, %struct.passwd* %23, i64 0, i32 2
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.passwd, %struct.passwd* %23, i64 0, i32 0
%39 = load i32, i32* %38
%40 = tail call i32 @initgroups(i32 %37, i32 %39)
%41 = getelementptr inbounds %struct.passwd, %struct.passwd* %23, i64 0, i32 1
%42 = load i32, i32* %41
%43 = load i32, i32* %38
br label %44
44: ; preds = %35, %33
%45 = phi i32 [ %27, %33 ], [ %42, %35 ]
%46 = phi i32 [ 99, %33 ], [ %43, %35 ]
%47 = getelementptr inbounds i8*, i8** %1, i64 3
%48 = call i64 @chdir(i8* %20)
%49 = icmp eq i64 %48, 0
br i1 %49, label %53, label %50
50: ; preds = %44
%51 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %44
%54 = call i64 @chroot(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%55 = icmp eq i64 %54, 0
br i1 %55, label %59, label %56
56: ; preds = %53
%57 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %53
%60 = call i64 @setgid(i32 %46)
%61 = icmp eq i64 %60, 0
br i1 %61, label %65, label %62
62: ; preds = %59
%63 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %59
%66 = call i64 @setuid(i32 %45)
%67 = icmp eq i64 %66, 0
br i1 %67, label %71, label %68
68: ; preds = %65
%69 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%70 = call i32 @exit(i32 1)
unreachable
71: ; preds = %65
%72 = load i8*, i8** %47
%73 = call i32 @execv(i8* %72, i8** %47)
%74 = load i32, i32* @stderr
%75 = load i8*, i8** %47
%76 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i8* %75)
%77 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0))
%78 = call i32 @exit(i32 1)
unreachable
}