#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 {
  scalar_t__ pw_uid;
  scalar_t__ pw_gid;
};

/* Variables and functions */
int EXIT_FAILURE;
int /*<<< orphan*/ error_exit(char *);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct passwd *getpwnam(char *);
scalar_t__ setgid(scalar_t__);
scalar_t__ setuid(scalar_t__);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc < 3) {
    fprintf(stderr, "usage: nonroot username executable [args...] \n");
    return EXIT_FAILURE;
  }
  char *username = argv[1];
  struct passwd *user = getpwnam(username);
  if (!user) {
    error_exit("Invalid user");
  }
  if (user->pw_uid == 0 || user->pw_gid == 0) {
    error_exit("UID or GID is zero. Specify a non-root user.");
  }
  if (setgid(user->pw_gid)) {
    error_exit("Failed to switch user");
  }
  if (setuid(user->pw_uid)) {
    error_exit("Failed to switch user");
  }
  char **new_argv = argv + 2;
  execvp(new_argv[0], new_argv);
  error_exit("Failed to execute the command");
}


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)
cmpl   $0x3,-0x8(%rbp)
jge    11a6 <main+0x46>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    126c <main+0x10c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d6 <main+0x76>
lea    0xe5e(%rip),%rdi
call   1030 <error_exit@plt>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     11f3 <main+0x93>
mov    -0x20(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11ff <main+0x9f>
lea    0xe42(%rip),%rdi
call   1030 <error_exit@plt>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <setgid@plt>
cmp    $0x0,%rax
je     1222 <main+0xc2>
lea    0xe4c(%rip),%rdi
call   1030 <error_exit@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1080 <setuid@plt>
cmp    $0x0,%rax
je     1244 <main+0xe4>
lea    0xe2a(%rip),%rdi
call   1030 <error_exit@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <execvp@plt>
lea    0xe18(%rip),%rdi
call   1030 <error_exit@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jg     118c <main+0x2c>
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
lea    0xe87(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
jmp    1220 <main+0xc0>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
call   1040 <getpwnam@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11a8 <main+0x48>
cmpq   $0x0,(%rbx)
jne    11ba <main+0x5a>
jmp    11c3 <main+0x63>
lea    0xe80(%rip),%rdi
call   1030 <error_exit@plt>
cmpq   $0x0,(%rbx)
je     11c3 <main+0x63>
mov    0x8(%rbx),%rdi
test   %rdi,%rdi
jne    11d3 <main+0x73>
lea    0xe72(%rip),%rdi
call   1030 <error_exit@plt>
mov    0x8(%rbx),%rdi
call   1060 <setgid@plt>
test   %rax,%rax
je     11e9 <main+0x89>
lea    0xe85(%rip),%rdi
call   1030 <error_exit@plt>
mov    (%rbx),%rdi
call   1080 <setuid@plt>
test   %rax,%rax
je     1202 <main+0xa2>
lea    0xe6c(%rip),%rdi
call   1030 <error_exit@plt>
mov    0x10(%r14),%rdi
add    $0x10,%r14
mov    %r14,%rsi
call   1070 <execvp@plt>
lea    0xe66(%rip),%rdi
call   1030 <error_exit@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.passwd = type { i64, i64 }
@stderr = global i32 0
@.str = [47 x i8] c"usage: nonroot username executable [args...] \0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [13 x i8] c"Invalid user\00"
@.str.2 = [45 x i8] c"UID or GID is zero. Specify a non-root user.\00"
@.str.3 = [22 x i8] c"Failed to switch user\00"
@.str.4 = [30 x i8] c"Failed to execute the command\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca %struct.passwd*
%8 = alloca i8**
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 3
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
store i32 %14, i32* %3
br label %62
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
store i8* %18, i8** %6
%19 = load i8*, i8** %6
%20 = call %struct.passwd* @getpwnam(i8* %19)
store %struct.passwd* %20, %struct.passwd** %7
%21 = load %struct.passwd*, %struct.passwd** %7
%22 = icmp ne %struct.passwd* %21, null
br i1 %22, label %25, label %23
23: ; preds = %15
%24 = call i32 @error_exit(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %15
%26 = load %struct.passwd*, %struct.passwd** %7
%27 = getelementptr inbounds %struct.passwd, %struct.passwd* %26, i32 0, i32 0
%28 = load i64, i64* %27
%29 = icmp eq i64 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %25
%31 = load %struct.passwd*, %struct.passwd** %7
%32 = getelementptr inbounds %struct.passwd, %struct.passwd* %31, i32 0, i32 1
%33 = load i64, i64* %32
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %30, %25
%36 = call i32 @error_exit(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %30
%38 = load %struct.passwd*, %struct.passwd** %7
%39 = getelementptr inbounds %struct.passwd, %struct.passwd* %38, i32 0, i32 1
%40 = load i64, i64* %39
%41 = call i64 @setgid(i64 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = call i32 @error_exit(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %43, %37
%46 = load %struct.passwd*, %struct.passwd** %7
%47 = getelementptr inbounds %struct.passwd, %struct.passwd* %46, i32 0, i32 0
%48 = load i64, i64* %47
%49 = call i64 @setuid(i64 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %45
%52 = call i32 @error_exit(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %53
53: ; preds = %51, %45
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 2
store i8** %55, i8*** %8
%56 = load i8**, i8*** %8
%57 = getelementptr inbounds i8*, i8** %56, i64 0
%58 = load i8*, i8** %57
%59 = load i8**, i8*** %8
%60 = call i32 @execvp(i8* %58, i8** %59)
%61 = call i32 @error_exit(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %62
62: ; preds = %53, %11
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
%struct.passwd = type { i64, i64 }
@stderr = global i32 0
@.str = [47 x i8] c"usage: nonroot username executable [args...] \0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [13 x i8] c"Invalid user\00"
@.str.2 = [45 x i8] c"UID or GID is zero. Specify a non-root user.\00"
@.str.3 = [22 x i8] c"Failed to switch user\00"
@.str.4 = [30 x i8] c"Failed to execute the command\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
br label %44
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call %struct.passwd* @getpwnam(i8* %10)
%12 = icmp eq %struct.passwd* %11, null
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @error_exit(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %8
%16 = getelementptr inbounds %struct.passwd, %struct.passwd* %11, i64 0, i32 0
%17 = load i64, i64* %16
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = getelementptr inbounds %struct.passwd, %struct.passwd* %11, i64 0, i32 1
%21 = load i64, i64* %20
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %19, %15
%24 = tail call i32 @error_exit(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %11, i64 0, i32 1
%26 = load i64, i64* %25
br label %27
27: ; preds = %23, %19
%28 = phi i64 [ %26, %23 ], [ %21, %19 ]
%29 = tail call i64 @setgid(i64 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %27
%32 = tail call i32 @error_exit(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %31, %27
%34 = load i64, i64* %16
%35 = tail call i64 @setuid(i64 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %33
%38 = tail call i32 @error_exit(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %33
%40 = getelementptr inbounds i8*, i8** %1, i64 2
%41 = load i8*, i8** %40
%42 = tail call i32 @execvp(i8* %41, i8** %40)
%43 = tail call i32 @error_exit(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %44
44: ; preds = %39, %4
%45 = phi i32 [ %7, %4 ], [ 0, %39 ]
ret i32 %45
}