#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 */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ display();
int /*<<< orphan*/ execv(char *, char *const *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setgroups(int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc < 3) {
    fprintf(stderr, "Usage: %s <username> <exec>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  struct passwd *pwd = getpwnam(argv[1]);
  if (pwd == NULL) {
    printf("'%s' not found\n", argv[1]);
    exit(EXIT_FAILURE);
  }
  printf("Before: ");
  display();
  if (setgroups(0, NULL) < 0) {
    perror("setgroups");
    goto fail;
  }
  if (setgid(pwd->pw_gid) < 0) {
    perror("setgid");
    goto fail;
  }
  if (setuid(pwd->pw_uid) < 0) {
    perror("setuid");
    goto fail;
  }
  printf("After: ");
  display();
  printf("Executing: %s...\n", argv[2]);
  execv(argv[2], (char *const *)&argv[2]);
  perror("execv failed");
fail:
  exit(EXIT_FAILURE);
}


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)
cmpl   $0x3,-0x8(%rbp)
jge    11ea <main+0x4a>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe29(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    122a <main+0x8a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <display@plt>
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <setgroups@plt>
cmp    $0x0,%rax
jge    1265 <main+0xc5>
lea    0xddb(%rip),%rdi
call   1090 <perror@plt>
jmp    12fe <main+0x15e>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   1080 <setgid@plt>
cmp    $0x0,%rax
jge    128c <main+0xec>
lea    0xdbe(%rip),%rdi
call   1090 <perror@plt>
jmp    12fe <main+0x15e>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   10b0 <setuid@plt>
cmp    $0x0,%rax
jge    12b2 <main+0x112>
lea    0xd9f(%rip),%rdi
call   1090 <perror@plt>
jmp    12fe <main+0x15e>
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <display@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
call   10c0 <execv@plt>
lea    0xd6f(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jg     11c9 <main+0x29>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe41(%rip),%rsi
call   1070 <fprintf@plt>
jmp    128d <main+0xed>
mov    0x8(%rbx),%rdi
call   1060 <getpwnam@plt>
test   %rax,%rax
jne    11ee <main+0x4e>
mov    0x8(%rbx),%rsi
lea    0xe3b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    128d <main+0xed>
mov    %rax,%r14
lea    0xe35(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <display@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1050 <setgroups@plt>
test   %rax,%rax
jns    121d <main+0x7d>
lea    0xe1b(%rip),%rdi
jmp    1288 <main+0xe8>
mov    0x4(%r14),%edi
call   1080 <setgid@plt>
test   %rax,%rax
jns    1234 <main+0x94>
lea    0xe0e(%rip),%rdi
jmp    1288 <main+0xe8>
mov    (%r14),%edi
call   10b0 <setuid@plt>
test   %rax,%rax
jns    124a <main+0xaa>
lea    0xdff(%rip),%rdi
jmp    1288 <main+0xe8>
lea    0xdfd(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <display@plt>
lea    0x10(%rbx),%r14
mov    0x10(%rbx),%rsi
lea    0xde8(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x10(%rbx),%rdi
mov    %r14,%rsi
call   10c0 <execv@plt>
lea    0xde0(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

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

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@stderr = global i32 0
@.str = [29 x i8] c"Usage: %s <username> <exec>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"'%s' not found\0A\00"
@.str.2 = [9 x i8] c"Before: \00"
@.str.3 = [10 x i8] c"setgroups\00"
@.str.4 = [7 x i8] c"setgid\00"
@.str.5 = [7 x i8] c"setuid\00"
@.str.6 = [8 x i8] c"After: \00"
@.str.7 = [18 x i8] c"Executing: %s...\0A\00"
@.str.8 = [13 x i8] c"execv failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %10
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call %struct.passwd* @getpwnam(i8* %12)
%14 = icmp eq %struct.passwd* %13, null
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = load i8*, i8** %11
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %10
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%22 = tail call i32 (...) @display()
%23 = tail call i64 @setgroups(i32 0, i32* null)
%24 = icmp slt i64 %23, 0
br i1 %24, label %43, label %25
25: ; preds = %20
%26 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i64 0, i32 1
%27 = load i32, i32* %26
%28 = tail call i64 @setgid(i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %43, label %30
30: ; preds = %25
%31 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i64 0, i32 0
%32 = load i32, i32* %31
%33 = tail call i64 @setuid(i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %43, label %35
35: ; preds = %30
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%37 = tail call i32 (...) @display()
%38 = getelementptr inbounds i8*, i8** %1, i64 2
%39 = load i8*, i8** %38
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i8* %39)
%41 = load i8*, i8** %38
%42 = tail call i32 @execv(i8* %41, i8** %38)
br label %43
43: ; preds = %30, %25, %20, %35
%44 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), %35 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), %20 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), %25 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), %30 ]
%45 = tail call i32 @perror(i8* %44)
%46 = load i32, i32* @EXIT_FAILURE
%47 = tail call i32 @exit(i32 %46)
unreachable
}