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

/* Variables and functions */
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ geteuid();
struct passwd *getpwnam(char *);
scalar_t__ initgroups(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(scalar_t__);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  struct passwd *p;
  if (argc < 3) {
    fprintf(stderr, "Usage: setuser user program [ arg ... ]\n");
    exit(1);
  }
  p = getpwnam(argv[1]);
  if (p == NULL) {
    fprintf(stderr, "Can't find user %s\n", argv[1]);
    exit(1);
  }
  if (geteuid() == p->pw_uid) {
    exit(0);
  }
  if (initgroups(argv[1], p->pw_gid)) {
    perror("initgroups");
    exit(1);
  }
  if (setgid(p->pw_gid)) {
    perror("setgid");
    exit(1);
  }
  if (setuid(p->pw_uid)) {
    perror("setuid");
    exit(1);
  }
  execvp(argv[2], argv + 2);
  perror("exec");
  exit(1);
}


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    11d1 <main+0x41>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1216 <main+0x86>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe24(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1030 <geteuid@plt>
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jne    1231 <main+0xa1>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%esi
call   10b0 <initgroups@plt>
cmp    $0x0,%rax
je     1265 <main+0xd5>
lea    0xde7(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
call   1060 <setgid@plt>
cmp    $0x0,%rax
je     1291 <main+0x101>
lea    0xdc6(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <setuid@plt>
cmp    $0x0,%rax
je     12bd <main+0x12d>
lea    0xda1(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
call   1080 <execvp@plt>
lea    0xd7d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jg     11ba <main+0x2a>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
call   1040 <getpwnam@plt>
test   %rax,%rax
jne    11f0 <main+0x60>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xe4a(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <geteuid@plt>
cmp    (%rbx),%rax
jne    1206 <main+0x76>
xor    %edi,%edi
call   1090 <exit@plt>
mov    0x8(%r14),%rdi
mov    0x8(%rbx),%esi
call   10b0 <initgroups@plt>
test   %rax,%rax
jne    124a <main+0xba>
mov    0x8(%rbx),%edi
call   1060 <setgid@plt>
test   %rax,%rax
jne    1253 <main+0xc3>
mov    (%rbx),%rdi
call   10a0 <setuid@plt>
test   %rax,%rax
jne    125c <main+0xcc>
mov    0x10(%r14),%rdi
add    $0x10,%r14
mov    %r14,%rsi
call   1080 <execvp@plt>
lea    0xe0e(%rip),%rdi
jmp    1263 <main+0xd3>
lea    0xdec(%rip),%rdi
jmp    1263 <main+0xd3>
lea    0xdee(%rip),%rdi
jmp    1263 <main+0xd3>
lea    0xdec(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i64, i32 }
@stderr = global i32 0
@.str = [41 x i8] c"Usage: setuser user program [ arg ... ]\0A\00"
@.str.1 = [20 x i8] c"Can't find user %s\0A\00"
@.str.2 = [11 x i8] c"initgroups\00"
@.str.3 = [7 x i8] c"setgid\00"
@.str.4 = [7 x i8] c"setuid\00"
@.str.5 = [5 x i8] c"exec\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 %13
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
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 eq %struct.passwd* %18, null
br i1 %19, label %20, label %27
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %13
%28 = call i64 (...) @geteuid()
%29 = load %struct.passwd*, %struct.passwd** %6
%30 = getelementptr inbounds %struct.passwd, %struct.passwd* %29, i32 0, i32 0
%31 = load i64, i64* %30
%32 = icmp eq i64 %28, %31
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %27
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%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 @initgroups(i8* %38, i32 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %35
%45 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %35
%48 = load %struct.passwd*, %struct.passwd** %6
%49 = getelementptr inbounds %struct.passwd, %struct.passwd* %48, i32 0, i32 1
%50 = load i32, i32* %49
%51 = call i64 @setgid(i32 %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %47
%54 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %47
%57 = load %struct.passwd*, %struct.passwd** %6
%58 = getelementptr inbounds %struct.passwd, %struct.passwd* %57, i32 0, i32 0
%59 = load i64, i64* %58
%60 = call i64 @setuid(i64 %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %56
%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 = %56
%66 = load i8**, i8*** %5
%67 = getelementptr inbounds i8*, i8** %66, i64 2
%68 = load i8*, i8** %67
%69 = load i8**, i8*** %5
%70 = getelementptr inbounds i8*, i8** %69, i64 2
%71 = call i32 @execvp(i8* %68, i8** %70)
%72 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%73 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
%struct.passwd = type { i64, i32 }
@stderr = global i32 0
@.str = [41 x i8] c"Usage: setuser user program [ arg ... ]\0A\00"
@.str.1 = [20 x i8] c"Can't find user %s\0A\00"
@.str.2 = [11 x i8] c"initgroups\00"
@.str.3 = [7 x i8] c"setgid\00"
@.str.4 = [7 x i8] c"setuid\00"
@.str.5 = [5 x i8] c"exec\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 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
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 %18
13: ; preds = %8
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %9
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %8
%19 = tail call i64 (...) @geteuid()
%20 = getelementptr inbounds %struct.passwd, %struct.passwd* %11, i64 0, i32 0
%21 = load i64, i64* %20
%22 = icmp eq i64 %19, %21
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = tail call i32 @exit(i32 0)
unreachable
25: ; preds = %18
%26 = load i8*, i8** %9
%27 = getelementptr inbounds %struct.passwd, %struct.passwd* %11, i64 0, i32 1
%28 = load i32, i32* %27
%29 = tail call i64 @initgroups(i8* %26, i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %25
%32 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %25
%35 = load i32, i32* %27
%36 = tail call i64 @setgid(i32 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %34
%39 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%40 = tail call i32 @exit(i32 1)
unreachable
41: ; preds = %34
%42 = load i64, i64* %20
%43 = tail call i64 @setuid(i64 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %48, label %45
45: ; preds = %41
%46 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%47 = tail call i32 @exit(i32 1)
unreachable
48: ; preds = %41
%49 = getelementptr inbounds i8*, i8** %1, i64 2
%50 = load i8*, i8** %49
%51 = tail call i32 @execvp(i8* %50, i8** %49)
%52 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%53 = tail call i32 @exit(i32 1)
unreachable
}