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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ display();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ geteuid();
struct passwd *getpwnam(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setgroups(int, int /*<<< orphan*/ *);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    fprintf(stderr, "Usage: %s <username> [setgroups]\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 (argc == 3 && strcmp(argv[2], "setgroups") == 0) {
    gid_t gid_list[1];
    gid_list[0] = pwd->pw_gid;
    if (geteuid() == 0 && setgroups(1, gid_list) < 0) {
      perror("setgroups");
      goto fail;
    }
  } else {
    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();
  exit(0);
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   $0x2,-0x8(%rbp)
jge    11fa <main+0x4a>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe19(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    123a <main+0x8a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <display@plt>
cmpl   $0x3,-0x8(%rbp)
jne    12c0 <main+0x110>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xdd3(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12c0 <main+0x110>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <geteuid@plt>
cmp    $0x0,%rax
jne    12bb <main+0x10b>
lea    -0x1c(%rbp),%rsi
mov    $0x1,%edi
call   1060 <setgroups@plt>
cmp    $0x0,%rax
jge    12bb <main+0x10b>
lea    0xd8a(%rip),%rdi
call   10b0 <perror@plt>
jmp    1354 <main+0x1a4>
jmp    12eb <main+0x13b>
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <setgroups@plt>
cmp    $0x0,%rax
jge    12e6 <main+0x136>
lea    0xd5f(%rip),%rdi
call   10b0 <perror@plt>
jmp    1354 <main+0x1a4>
jmp    12eb <main+0x13b>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   10a0 <setgid@plt>
cmp    $0x0,%rax
jge    1312 <main+0x162>
lea    0xd3d(%rip),%rdi
call   10b0 <perror@plt>
jmp    1354 <main+0x1a4>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   10d0 <setuid@plt>
cmp    $0x0,%rax
jge    1338 <main+0x188>
lea    0xd1e(%rip),%rdi
call   10b0 <perror@plt>
jmp    1354 <main+0x1a4>
lea    0xd14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <display@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    0x2c7d(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jle    127f <main+0xcf>
mov    %edi,%ebp
mov    0x8(%rbx),%rdi
call   1080 <getpwnam@plt>
test   %rax,%rax
je     1299 <main+0xe9>
mov    %rax,%r14
lea    0xe50(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <display@plt>
cmp    $0x3,%ebp
jne    122a <main+0x7a>
mov    0x10(%rbx),%rdi
lea    0xe3b(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    122a <main+0x7a>
mov    0x4(%r14),%eax
mov    %eax,0xc(%rsp)
xor    %eax,%eax
call   1050 <geteuid@plt>
test   %rax,%rax
jne    1241 <main+0x91>
lea    0xc(%rsp),%rsi
mov    $0x1,%edi
jmp    122e <main+0x7e>
xor    %edi,%edi
xor    %esi,%esi
call   1060 <setgroups@plt>
test   %rax,%rax
jns    1241 <main+0x91>
lea    0xdfc(%rip),%rdi
jmp    126c <main+0xbc>
mov    0x4(%r14),%edi
call   10a0 <setgid@plt>
test   %rax,%rax
jns    1258 <main+0xa8>
lea    0xdef(%rip),%rdi
jmp    126c <main+0xbc>
mov    (%r14),%edi
call   10d0 <setuid@plt>
test   %rax,%rax
jns    12ad <main+0xfd>
lea    0xde0(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd6e(%rip),%rsi
call   1090 <fprintf@plt>
jmp    1271 <main+0xc1>
mov    0x8(%rbx),%rsi
lea    0xd7e(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1271 <main+0xc1>
lea    0xd9f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <display@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

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

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