#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 */
scalar_t__ errno;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct passwd *getpwnam(char *);
scalar_t__ initgroups(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(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) {
    printf("Usage: %s account child\n", argv[0]);
    return 111;
  }
  char *account = argv[1];
  errno = 0;
  struct passwd *pwnam = getpwnam(account);
  if (pwnam == NULL) {
    if (errno == 0) {
      fprintf(stderr, "unknown user: %s\n", account);
    } else {
      perror("getpwnam failed");
    }
    return 111;
  }
  if (setgid(pwnam->pw_gid) != 0) {
    perror("setgid failed");
    return 111;
  }
  if (initgroups(account, pwnam->pw_gid) != 0) {
    perror("initgroups failed");
    return 111;
  }
  if (setuid(pwnam->pw_uid) != 0) {
    perror("setuid failed");
    return 111;
  }
  execvp(argv[2], &(argv[2]));
  perror("exec failed");
  return 111;
}


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   $0x2,-0x8(%rbp)
jg     11bf <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe52(%rip),%rdi
call   1030 <printf@plt>
movl   $0x6f,-0x4(%rbp)
jmp    12ed <main+0x16d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e06(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1238 <main+0xb8>
mov    0x2de0(%rip),%rax
cmpq   $0x0,(%rax)
jne    1220 <main+0xa0>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe03(%rip),%rsi
call   1050 <fprintf@plt>
jmp    122c <main+0xac>
lea    0xe04(%rip),%rdi
call   1070 <perror@plt>
movl   $0x6f,-0x4(%rbp)
jmp    12ed <main+0x16d>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
call   1060 <setgid@plt>
cmp    $0x0,%rax
je     1266 <main+0xe6>
lea    0xde6(%rip),%rdi
call   1070 <perror@plt>
movl   $0x6f,-0x4(%rbp)
jmp    12ed <main+0x16d>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
call   10a0 <initgroups@plt>
cmp    $0x0,%rax
je     1298 <main+0x118>
lea    0xdc2(%rip),%rdi
call   1070 <perror@plt>
movl   $0x6f,-0x4(%rbp)
jmp    12ed <main+0x16d>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1090 <setuid@plt>
cmp    $0x0,%rax
je     12c5 <main+0x145>
lea    0xda7(%rip),%rdi
call   1070 <perror@plt>
movl   $0x6f,-0x4(%rbp)
jmp    12ed <main+0x16d>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
call   1080 <execvp@plt>
lea    0xd88(%rip),%rdi
call   1070 <perror@plt>
movl   $0x6f,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jg     11a1 <main+0x21>
mov    (%rbx),%rsi
lea    0xe66(%rip),%rdi
call   1030 <printf@plt>
jmp    11e0 <main+0x60>
mov    0x8(%rbx),%r14
mov    0x2e2c(%rip),%r12
movq   $0x0,(%r12)
mov    %r14,%rdi
call   1040 <getpwnam@plt>
test   %rax,%rax
jne    11f1 <main+0x71>
cmpq   $0x0,(%r12)
jne    1238 <main+0xb8>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    $0x6f,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %rax,%r15
mov    0x4(%rax),%edi
call   1060 <setgid@plt>
test   %rax,%rax
jne    1241 <main+0xc1>
mov    0x4(%r15),%esi
mov    %r14,%rdi
call   10a0 <initgroups@plt>
test   %rax,%rax
jne    124a <main+0xca>
mov    (%r15),%edi
call   1090 <setuid@plt>
test   %rax,%rax
jne    1253 <main+0xd3>
mov    0x10(%rbx),%rdi
add    $0x10,%rbx
mov    %rbx,%rsi
call   1080 <execvp@plt>
lea    0xe33(%rip),%rdi
jmp    125a <main+0xda>
lea    0xdec(%rip),%rdi
jmp    125a <main+0xda>
lea    0xdf3(%rip),%rdi
jmp    125a <main+0xda>
lea    0xdf8(%rip),%rdi
jmp    125a <main+0xda>
lea    0xe01(%rip),%rdi
call   1070 <perror@plt>
jmp    11e0 <main+0x60>

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@.str = [25 x i8] c"Usage: %s account child\0A\00"
@errno = global i64 0
@stderr = global i32 0
@.str.1 = [18 x i8] c"unknown user: %s\0A\00"
@.str.2 = [16 x i8] c"getpwnam failed\00"
@.str.3 = [14 x i8] c"setgid failed\00"
@.str.4 = [18 x i8] c"initgroups failed\00"
@.str.5 = [14 x i8] c"setuid failed\00"
@.str.6 = [12 x i8] c"exec failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca %struct.passwd*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp sle i32 %8, 2
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %13)
store i32 111, i32* %3
br label %66
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
store i64 0, i64* @errno
%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 eq %struct.passwd* %21, null
br i1 %22, label %23, label %33
23: ; preds = %15
%24 = load i64, i64* @errno
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %6
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %28)
br label %32
30: ; preds = %23
%31 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
store i32 111, i32* %3
br label %66
33: ; preds = %15
%34 = load %struct.passwd*, %struct.passwd** %7
%35 = getelementptr inbounds %struct.passwd, %struct.passwd* %34, i32 0, i32 1
%36 = load i32, i32* %35
%37 = call i64 @setgid(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 111, i32* %3
br label %66
41: ; preds = %33
%42 = load i8*, i8** %6
%43 = load %struct.passwd*, %struct.passwd** %7
%44 = getelementptr inbounds %struct.passwd, %struct.passwd* %43, i32 0, i32 1
%45 = load i32, i32* %44
%46 = call i64 @initgroups(i8* %42, i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %41
%49 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
store i32 111, i32* %3
br label %66
50: ; preds = %41
%51 = load %struct.passwd*, %struct.passwd** %7
%52 = getelementptr inbounds %struct.passwd, %struct.passwd* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i64 @setuid(i32 %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %50
%57 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
store i32 111, i32* %3
br label %66
58: ; preds = %50
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 2
%61 = load i8*, i8** %60
%62 = load i8**, i8*** %5
%63 = getelementptr inbounds i8*, i8** %62, i64 2
%64 = call i32 @execvp(i8* %61, i8** %63)
%65 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
store i32 111, i32* %3
br label %66
66: ; preds = %58, %56, %48, %39, %32, %10
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@.str = [25 x i8] c"Usage: %s account child\0A\00"
@errno = global i64 0
@stderr = global i32 0
@.str.1 = [18 x i8] c"unknown user: %s\0A\00"
@.str.2 = [16 x i8] c"getpwnam failed\00"
@.str.3 = [14 x i8] c"setgid failed\00"
@.str.4 = [18 x i8] c"initgroups failed\00"
@.str.5 = [14 x i8] c"setuid failed\00"
@.str.6 = [12 x i8] c"exec failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %45
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
store i64 0, i64* @errno
%10 = tail call %struct.passwd* @getpwnam(i8* %9)
%11 = icmp eq %struct.passwd* %10, null
br i1 %11, label %12, label %20
12: ; preds = %7
%13 = load i64, i64* @errno
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %9)
br label %45
18: ; preds = %12
%19 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %45
20: ; preds = %7
%21 = getelementptr inbounds %struct.passwd, %struct.passwd* %10, i64 0, i32 1
%22 = load i32, i32* %21
%23 = tail call i64 @setgid(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %20
%26 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %45
27: ; preds = %20
%28 = load i32, i32* %21
%29 = tail call i64 @initgroups(i8* %9, i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %27
%32 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %45
33: ; preds = %27
%34 = getelementptr inbounds %struct.passwd, %struct.passwd* %10, i64 0, i32 0
%35 = load i32, i32* %34
%36 = tail call i64 @setuid(i32 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %33
%39 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %45
40: ; preds = %33
%41 = getelementptr inbounds i8*, i8** %1, i64 2
%42 = load i8*, i8** %41
%43 = tail call i32 @execvp(i8* %42, i8** %41)
%44 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
br label %45
45: ; preds = %25, %31, %38, %40, %18, %15, %4
ret i32 111
}