#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_gid;
  int /*<<< orphan*/ pw_uid;
};

/* Variables and functions */
int /*<<< orphan*/ execve(char *, char **, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc < 2) {
    fprintf(stderr, "nsu <user> [ <exec> ... ]\n");
    return 1;
  }
  struct passwd *pw = getpwnam(argv[1]);
  if (pw) {
    if (setuid(pw->pw_uid) != 0)
      fprintf(stderr, "setuid fault\n");
    if (setgid(pw->pw_gid) != 0)
      fprintf(stderr, "setgid fault\n");
  } else
    fprintf(stderr, "user not found: %s\n", argv[1]);
  char *prog = argc < 3 ? "/bin/sh" : argv[2];
  printf("execute: %s\n", prog);
  execve(prog, argv + (argc < 3 ? 1 : 2), 0);
  fprintf(stderr, "execve fault\n");
  return 1;
}


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   $0x2,-0x8(%rbp)
jge    11a3 <main+0x43>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c3 <main+0x163>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     121d <main+0xbd>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   1080 <setuid@plt>
cmp    $0x0,%rax
je     11ec <main+0x8c>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1070 <setgid@plt>
cmp    $0x0,%rax
je     1218 <main+0xb8>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    123c <main+0xdc>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe02(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
cmpl   $0x3,-0x8(%rbp)
jge    1256 <main+0xf6>
lea    0xdfe(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    1262 <main+0x102>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xdde(%rip),%rdi
call   1030 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%edx
mov    $0x2,%eax
mov    $0x1,%ecx
cmp    $0x3,%edx
cmovl  %ecx,%eax
cltq
shl    $0x3,%rax
add    %rax,%rsi
xor    %edx,%edx
call   1040 <execve@plt>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
lea    0xdab(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jge    1175 <main+0x15>
lea    0xe90(%rip),%rsi
jmp    1232 <main+0xd2>
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x8(%rsi),%rdi
call   1050 <getpwnam@plt>
test   %rax,%rax
je     11e5 <main+0x85>
mov    %rax,%rbx
mov    0x4(%rax),%edi
call   1080 <setuid@plt>
test   %rax,%rax
je     11af <main+0x4f>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rbx),%edi
call   1070 <setgid@plt>
test   %rax,%rax
je     11d2 <main+0x72>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
cmp    $0x3,%ebp
jae    1205 <main+0xa5>
lea    0xe6d(%rip),%rbx
mov    $0x1,%ebp
jmp    120e <main+0xae>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xe3e(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
cmp    $0x3,%ebp
jb     11d7 <main+0x77>
mov    0x10(%r14),%rbx
mov    $0x2,%ebp
lea    0xe3e(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
lea    (%r14,%rbp,8),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1040 <execve@plt>
lea    0xe2e(%rip),%rsi
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@stderr = global i32 0
@.str = [27 x i8] c"nsu <user> [ <exec> ... ]\0A\00"
@.str.1 = [14 x i8] c"setuid fault\0A\00"
@.str.2 = [14 x i8] c"setgid fault\0A\00"
@.str.3 = [20 x i8] c"user not found: %s\0A\00"
@.str.4 = [8 x i8] c"/bin/sh\00"
@.str.5 = [13 x i8] c"execute: %s\0A\00"
@.str.6 = [14 x i8] c"execve fault\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.passwd*
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %68
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call %struct.passwd* @getpwnam(i8* %16)
store %struct.passwd* %17, %struct.passwd** %6
%18 = load %struct.passwd*, %struct.passwd** %6
%19 = icmp ne %struct.passwd* %18, null
br i1 %19, label %20, label %39
20: ; preds = %13
%21 = load %struct.passwd*, %struct.passwd** %6
%22 = getelementptr inbounds %struct.passwd, %struct.passwd* %21, i32 0, i32 1
%23 = load i32, i32* %22
%24 = call i64 @setuid(i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %26, %20
%30 = load %struct.passwd*, %struct.passwd** %6
%31 = getelementptr inbounds %struct.passwd, %struct.passwd* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i64 @setgid(i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %35, %29
br label %45
39: ; preds = %13
%40 = load i32, i32* @stderr
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %39, %38
%46 = load i32, i32* %4
%47 = icmp slt i32 %46, 3
br i1 %47, label %48, label %49
48: ; preds = %45
br label %53
49: ; preds = %45
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 2
%52 = load i8*, i8** %51
br label %53
53: ; preds = %49, %48
%54 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), %48 ], [ %52, %49 ]
store i8* %54, i8** %7
%55 = load i8*, i8** %7
%56 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %55)
%57 = load i8*, i8** %7
%58 = load i8**, i8*** %5
%59 = load i32, i32* %4
%60 = icmp slt i32 %59, 3
%61 = zext i1 %60 to i64
%62 = select i1 %60, i32 1, i32 2
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8*, i8** %58, i64 %63
%65 = call i32 @execve(i8* %57, i8** %64, i32 0)
%66 = load i32, i32* @stderr
%67 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %68
68: ; preds = %53, %10
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@stderr = global i32 0
@.str = [27 x i8] c"nsu <user> [ <exec> ... ]\0A\00"
@.str.1 = [14 x i8] c"setuid fault\0A\00"
@.str.2 = [14 x i8] c"setgid fault\0A\00"
@.str.3 = [20 x i8] c"user not found: %s\0A\00"
@.str.4 = [8 x i8] c"/bin/sh\00"
@.str.5 = [13 x i8] c"execute: %s\0A\00"
@.str.6 = [14 x i8] c"execve fault\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %40, label %4
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call %struct.passwd* @getpwnam(i8* %6)
%8 = icmp eq %struct.passwd* %7, null
br i1 %8, label %25, label %9
9: ; preds = %4
%10 = getelementptr inbounds %struct.passwd, %struct.passwd* %7, i64 0, i32 1
%11 = load i32, i32* %10
%12 = tail call i64 @setuid(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %14, %9
%18 = getelementptr inbounds %struct.passwd, %struct.passwd* %7, i64 0, i32 0
%19 = load i32, i32* %18
%20 = tail call i64 @setgid(i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %29, label %22
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %29
25: ; preds = %4
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %5
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %17, %22, %25
%30 = icmp ult i32 %0, 3
br i1 %30, label %34, label %31
31: ; preds = %29
%32 = getelementptr inbounds i8*, i8** %1, i64 2
%33 = load i8*, i8** %32
br label %34
34: ; preds = %29, %31
%35 = phi i64 [ 1, %29 ], [ 2, %31 ]
%36 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), %29 ], [ %33, %31 ]
%37 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %36)
%38 = getelementptr inbounds i8*, i8** %1, i64 %35
%39 = tail call i32 @execve(i8* %36, i8** %38, i32 0)
br label %40
40: ; preds = %2, %34
%41 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), %34 ], [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), %2 ]
%42 = load i32, i32* @stderr
%43 = tail call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* %41)
ret i32 1
}