#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 stat {
  int dummy;
};
struct passwd {
  int /*<<< orphan*/ pw_shell;
  int /*<<< orphan*/ pw_dir;
};

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ PER_LINUX32;
scalar_t__ chdir(int /*<<< orphan*/);
int /*<<< orphan*/ check_mounts();
scalar_t__ chroot(char *);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ execl(int /*<<< orphan*/, int /*<<< orphan*/, char *);
int /*<<< orphan*/ execvp(char *, char **);
scalar_t__ geteuid();
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ mount_home(int /*<<< orphan*/);
scalar_t__ personality(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
scalar_t__ stat(char *, struct stat *);
extern int main(int argc, char **argv) {
  struct stat st;
  struct passwd *pwd;
  if (!getuid() || geteuid())
    error("This must be run as non-root, be SETUID, and owned by root.\n");
  if (stat("/usr/lib/fw32", &st))
    error("fw32 root directory does not exist.\n");
  check_mounts();
  pwd = getpwuid(getuid());
  if (!pwd)
    error("Failed to retrieve password entry.\n");
  mount_home(pwd->pw_dir);
  if (chroot("/usr/lib/fw32"))
    error("Failed to enter chroot.\n");
  if (personality(PER_LINUX32))
    error("Failed to enable 32 bit emulation.\n");
  if (chdir(pwd->pw_dir))
    error("Failed to change to home directory.\n");
  if (setuid(getuid()))
    error("Failed to drop root permissions.\n");
  if (argc < 2)
    execl(pwd->pw_shell, pwd->pw_shell, (char *)0);
  else
    execvp(argv[1], argv + 1);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <getuid@plt>
cmp    $0x0,%eax
je     1207 <main+0x37>
mov    $0x0,%al
call   1080 <geteuid@plt>
cmp    $0x0,%rax
je     1213 <main+0x43>
lea    0xdf2(%rip),%rdi
call   10a0 <error@plt>
lea    0xe23(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
je     1239 <main+0x69>
lea    0xe17(%rip),%rdi
call   10a0 <error@plt>
mov    $0x0,%al
call   10e0 <check_mounts@plt>
mov    $0x0,%al
call   1070 <getuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1269 <main+0x99>
lea    0xe0c(%rip),%rdi
call   10a0 <error@plt>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
call   10b0 <mount_home@plt>
lea    0xdc1(%rip),%rdi
call   1040 <chroot@plt>
cmp    $0x0,%rax
je     1297 <main+0xc7>
lea    0xe02(%rip),%rdi
call   10a0 <error@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   1030 <personality@plt>
cmp    $0x0,%rax
je     12bb <main+0xeb>
lea    0xdf7(%rip),%rdi
call   10a0 <error@plt>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
call   1060 <chdir@plt>
cmp    $0x0,%rax
je     12dd <main+0x10d>
lea    0xdf9(%rip),%rdi
call   10a0 <error@plt>
mov    $0x0,%al
call   1070 <getuid@plt>
mov    %eax,%edi
call   10d0 <setuid@plt>
cmp    $0x0,%rax
je     1301 <main+0x131>
lea    0xdfa(%rip),%rdi
call   10a0 <error@plt>
cmpl   $0x2,-0x8(%rbp)
jge    1325 <main+0x155>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   10f0 <execl@plt>
jmp    133a <main+0x16a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   10c0 <execvp@plt>
mov    0x2c97(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
xor    %eax,%eax
call   1070 <getuid@plt>
test   %eax,%eax
je     11f5 <main+0x25>
xor    %eax,%eax
call   1080 <geteuid@plt>
test   %rax,%rax
je     1201 <main+0x31>
lea    0xe04(%rip),%rdi
call   10a0 <error@plt>
lea    0xe35(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1090 <stat@plt>
test   %rax,%rax
je     1223 <main+0x53>
lea    0xe2d(%rip),%rdi
call   10a0 <error@plt>
xor    %eax,%eax
call   10e0 <check_mounts@plt>
xor    %eax,%eax
call   1070 <getuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    124c <main+0x7c>
lea    0xe29(%rip),%rdi
call   10a0 <error@plt>
mov    0x4(%rbp),%edi
call   10b0 <mount_home@plt>
lea    0xde2(%rip),%rdi
call   1040 <chroot@plt>
test   %rax,%rax
je     1271 <main+0xa1>
lea    0xe28(%rip),%rdi
call   10a0 <error@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   1030 <personality@plt>
test   %rax,%rax
je     1290 <main+0xc0>
lea    0xe22(%rip),%rdi
call   10a0 <error@plt>
mov    0x4(%rbp),%edi
call   1060 <chdir@plt>
test   %rax,%rax
je     12a9 <main+0xd9>
lea    0xe2d(%rip),%rdi
call   10a0 <error@plt>
xor    %eax,%eax
call   1070 <getuid@plt>
mov    %eax,%edi
call   10d0 <setuid@plt>
test   %rax,%rax
je     12c8 <main+0xf8>
lea    0xe33(%rip),%rdi
call   10a0 <error@plt>
cmp    $0x1,%r14d
jg     12dc <main+0x10c>
mov    0x0(%rbp),%edi
mov    %edi,%esi
xor    %edx,%edx
call   10f0 <execl@plt>
jmp    12ec <main+0x11c>
mov    0x8(%rbx),%rdi
add    $0x8,%rbx
mov    %rbx,%rsi
call   10c0 <execvp@plt>
mov    0x2ce5(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.passwd = type { i32, i32 }
@.str = [61 x i8] c"This must be run as non-root, be SETUID, and owned by root.\0A\00"
@.str.1 = [14 x i8] c"/usr/lib/fw32\00"
@.str.2 = [37 x i8] c"fw32 root directory does not exist.\0A\00"
@.str.3 = [36 x i8] c"Failed to retrieve password entry.\0A\00"
@.str.4 = [25 x i8] c"Failed to enter chroot.\0A\00"
@PER_LINUX32 = global i32 0
@.str.5 = [36 x i8] c"Failed to enable 32 bit emulation.\0A\00"
@.str.6 = [37 x i8] c"Failed to change to home directory.\0A\00"
@.str.7 = [34 x i8] c"Failed to drop root permissions.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca %struct.passwd*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (...) @getuid()
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i64 (...) @geteuid()
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10, %2
%14 = call i32 @error(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = call i64 @stat(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %6)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %15
%21 = call i32 (...) @check_mounts()
%22 = call i32 (...) @getuid()
%23 = call %struct.passwd* @getpwuid(i32 %22)
store %struct.passwd* %23, %struct.passwd** %7
%24 = load %struct.passwd*, %struct.passwd** %7
%25 = icmp ne %struct.passwd* %24, null
br i1 %25, label %28, label %26
26: ; preds = %20
%27 = call i32 @error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %20
%29 = load %struct.passwd*, %struct.passwd** %7
%30 = getelementptr inbounds %struct.passwd, %struct.passwd* %29, i32 0, i32 1
%31 = load i32, i32* %30
%32 = call i32 @mount_home(i32 %31)
%33 = call i64 @chroot(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %28
%36 = call i32 @error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %37
37: ; preds = %35, %28
%38 = load i32, i32* @PER_LINUX32
%39 = call i64 @personality(i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 @error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
br label %43
43: ; preds = %41, %37
%44 = load %struct.passwd*, %struct.passwd** %7
%45 = getelementptr inbounds %struct.passwd, %struct.passwd* %44, i32 0, i32 1
%46 = load i32, i32* %45
%47 = call i64 @chdir(i32 %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = call i32 @error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0))
br label %51
51: ; preds = %49, %43
%52 = call i32 (...) @getuid()
%53 = call i64 @setuid(i32 %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %51
%56 = call i32 @error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0))
br label %57
57: ; preds = %55, %51
%58 = load i32, i32* %4
%59 = icmp slt i32 %58, 2
br i1 %59, label %60, label %68
60: ; preds = %57
%61 = load %struct.passwd*, %struct.passwd** %7
%62 = getelementptr inbounds %struct.passwd, %struct.passwd* %61, i32 0, i32 0
%63 = load i32, i32* %62
%64 = load %struct.passwd*, %struct.passwd** %7
%65 = getelementptr inbounds %struct.passwd, %struct.passwd* %64, i32 0, i32 0
%66 = load i32, i32* %65
%67 = call i32 @execl(i32 %63, i32 %66, i8* null)
br label %75
68: ; preds = %57
%69 = load i8**, i8*** %5
%70 = getelementptr inbounds i8*, i8** %69, i64 1
%71 = load i8*, i8** %70
%72 = load i8**, i8*** %5
%73 = getelementptr inbounds i8*, i8** %72, i64 1
%74 = call i32 @execvp(i8* %71, i8** %73)
br label %75
75: ; preds = %68, %60
%76 = load i32, i32* @EXIT_SUCCESS
ret i32 %76
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.passwd = type { i32, i32 }
@.str = [61 x i8] c"This must be run as non-root, be SETUID, and owned by root.\0A\00"
@.str.1 = [14 x i8] c"/usr/lib/fw32\00"
@.str.2 = [37 x i8] c"fw32 root directory does not exist.\0A\00"
@.str.3 = [36 x i8] c"Failed to retrieve password entry.\0A\00"
@.str.4 = [25 x i8] c"Failed to enter chroot.\0A\00"
@PER_LINUX32 = global i32 0
@.str.5 = [36 x i8] c"Failed to enable 32 bit emulation.\0A\00"
@.str.6 = [37 x i8] c"Failed to change to home directory.\0A\00"
@.str.7 = [34 x i8] c"Failed to drop root permissions.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i32 (...) @getuid()
%6 = icmp eq i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = tail call i64 (...) @geteuid()
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %7, %2
%11 = tail call i32 @error(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
%13 = call i64 @stat(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %3)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = call i32 @error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = call i32 (...) @check_mounts()
%19 = call i32 (...) @getuid()
%20 = call %struct.passwd* @getpwuid(i32 %19)
%21 = icmp eq %struct.passwd* %20, null
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %22, %17
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %20, i64 0, i32 1
%26 = load i32, i32* %25
%27 = call i32 @mount_home(i32 %26)
%28 = call i64 @chroot(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %24
%31 = call i32 @error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %24
%33 = load i32, i32* @PER_LINUX32
%34 = call i64 @personality(i32 %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %32
%37 = call i32 @error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
br label %38
38: ; preds = %36, %32
%39 = load i32, i32* %25
%40 = call i64 @chdir(i32 %39)
%41 = icmp eq i64 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %38
%43 = call i32 @error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0))
br label %44
44: ; preds = %42, %38
%45 = call i32 (...) @getuid()
%46 = call i64 @setuid(i32 %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %50, label %48
48: ; preds = %44
%49 = call i32 @error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0))
br label %50
50: ; preds = %48, %44
%51 = icmp slt i32 %0, 2
br i1 %51, label %52, label %56
52: ; preds = %50
%53 = getelementptr inbounds %struct.passwd, %struct.passwd* %20, i64 0, i32 0
%54 = load i32, i32* %53
%55 = call i32 @execl(i32 %54, i32 %54, i8* null)
br label %60
56: ; preds = %50
%57 = getelementptr inbounds i8*, i8** %1, i64 1
%58 = load i8*, i8** %57
%59 = call i32 @execvp(i8* %58, i8** %57)
br label %60
60: ; preds = %56, %52
%61 = load i32, i32* @EXIT_SUCCESS
ret i32 %61
}