#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*/ __NR_setgid;
int /*<<< orphan*/ __NR_setgroups;
int /*<<< orphan*/ __NR_setuid;
int /*<<< orphan*/ display();
scalar_t__ errno;
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 *, ...);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
scalar_t__ syscall(int /*<<< orphan*/, int, ...);
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();
  errno = 0;
  if (argc == 3 && strcmp(argv[2], "setgroups") == 0) {
    gid_t gid_list[1];
    gid_list[0] = pwd->pw_gid;
    if (geteuid() == 0 && syscall(__NR_setgroups, 1, gid_list) < 0) {
      perror("setgroups");
      goto fail;
    }
  } else {
    if (syscall(__NR_setgroups, 0, NULL) < 0) {
      perror("setgroups");
      goto fail;
    }
  }
  errno = 0;
  if (syscall(__NR_setgid, pwd->pw_gid) < 0) {
    perror("setgid");
    goto fail;
  }
  errno = 0;
  if (syscall(__NR_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    11da <main+0x4a>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    121a <main+0x8a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <display@plt>
mov    0x2d9a(%rip),%rax
movq   $0x0,(%rax)
cmpl   $0x3,-0x8(%rbp)
jne    12b9 <main+0x129>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xde5(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12b9 <main+0x129>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <geteuid@plt>
cmp    $0x0,%rax
jne    12b4 <main+0x124>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
lea    -0x1c(%rbp),%rdx
mov    $0x1,%esi
mov    $0x0,%al
call   1090 <syscall@plt>
cmp    $0x0,%rax
jge    12b4 <main+0x124>
lea    0xd91(%rip),%rdi
call   10a0 <perror@plt>
jmp    138a <main+0x1fa>
jmp    12ef <main+0x15f>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %eax,%eax
mov    %eax,%edx
mov    $0x0,%al
call   1090 <syscall@plt>
cmp    $0x0,%rax
jge    12ea <main+0x15a>
lea    0xd5b(%rip),%rdi
call   10a0 <perror@plt>
jmp    138a <main+0x1fa>
jmp    12ef <main+0x15f>
mov    0x2cda(%rip),%rax
movq   $0x0,(%rax)
mov    0x2cac(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
mov    $0x0,%al
call   1090 <syscall@plt>
cmp    $0x0,%rax
jge    132f <main+0x19f>
lea    0xd20(%rip),%rdi
call   10a0 <perror@plt>
jmp    138a <main+0x1fa>
mov    0x2c9a(%rip),%rax
movq   $0x0,(%rax)
mov    0x2c84(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <syscall@plt>
cmp    $0x0,%rax
jge    136e <main+0x1de>
lea    0xce8(%rip),%rdi
call   10a0 <perror@plt>
jmp    138a <main+0x1fa>
lea    0xcde(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <display@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x2c2f(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x1,%edi
jle    12a4 <main+0x114>
mov    %edi,%ebp
mov    0x8(%rbx),%rdi
call   1070 <getpwnam@plt>
test   %rax,%rax
je     12be <main+0x12e>
mov    %rax,%r14
lea    0xe71(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <display@plt>
mov    0x2dfa(%rip),%r15
movq   $0x0,(%r15)
cmp    $0x3,%ebp
jne    1220 <main+0x90>
mov    0x10(%rbx),%rdi
lea    0xe4e(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    1220 <main+0x90>
mov    0x4(%r14),%eax
mov    %eax,0x4(%rsp)
xor    %eax,%eax
call   1050 <geteuid@plt>
test   %rax,%rax
jne    1242 <main+0xb2>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0x4(%rsp),%rdx
mov    $0x1,%esi
jmp    122d <main+0x9d>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %eax,%eax
call   1090 <syscall@plt>
test   %rax,%rax
jns    1242 <main+0xb2>
lea    0xdfb(%rip),%rdi
jmp    1291 <main+0x101>
movq   $0x0,(%r15)
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
mov    0x4(%r14),%esi
xor    %eax,%eax
call   1090 <syscall@plt>
test   %rax,%rax
jns    126b <main+0xdb>
lea    0xddc(%rip),%rdi
jmp    1291 <main+0x101>
movq   $0x0,(%r15)
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%esi
xor    %eax,%eax
call   1090 <syscall@plt>
test   %rax,%rax
jns    12d2 <main+0x142>
lea    0xdbb(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd49(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1296 <main+0x106>
mov    0x8(%rbx),%rsi
lea    0xd59(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1296 <main+0x106>
lea    0xd7a(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <display@plt>
xor    %edi,%edi
call   10b0 <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"
@errno = global i64 0
@.str.3 = [10 x i8] c"setgroups\00"
@__NR_setgroups = global i32 0
@__NR_setgid = global i32 0
@.str.4 = [7 x i8] c"setgid\00"
@__NR_setuid = global i32 0
@.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()
store i64 0, i64* @errno
%35 = load i32, i32* %4
%36 = icmp eq i32 %35, 3
br i1 %36, label %37, label %58
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 %58
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 %57
50: ; preds = %43
%51 = load i32, i32* @__NR_setgroups
%52 = getelementptr inbounds [1 x i32], [1 x i32]* %7, i64 0, i64 0
%53 = call i64 (i32, i32, ...) @syscall(i32 %51, i32 1, i32* %52)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %50
%56 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %87
57: ; preds = %50, %43
br label %65
58: ; preds = %37, %32
%59 = load i32, i32* @__NR_setgroups
%60 = call i64 (i32, i32, ...) @syscall(i32 %59, i32 0, i8* null)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %87
64: ; preds = %58
br label %65
65: ; preds = %64, %57
store i64 0, i64* @errno
%66 = load i32, i32* @__NR_setgid
%67 = load %struct.passwd*, %struct.passwd** %6
%68 = getelementptr inbounds %struct.passwd, %struct.passwd* %67, i32 0, i32 1
%69 = load i32, i32* %68
%70 = call i64 (i32, i32, ...) @syscall(i32 %66, i32 %69)
%71 = icmp slt i64 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %65
%73 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %87
74: ; preds = %65
store i64 0, i64* @errno
%75 = load i32, i32* @__NR_setuid
%76 = load %struct.passwd*, %struct.passwd** %6
%77 = getelementptr inbounds %struct.passwd, %struct.passwd* %76, i32 0, i32 0
%78 = load i32, i32* %77
%79 = call i64 (i32, i32, ...) @syscall(i32 %75, i32 %78)
%80 = icmp slt i64 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %74
%82 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %87
83: ; preds = %74
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%85 = call i32 (...) @display()
%86 = call i32 @exit(i32 0)
unreachable
87: ; preds = %81, %72, %62, %55
%88 = load i32, i32* @EXIT_FAILURE
%89 = call i32 @exit(i32 %88)
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"
@errno = global i64 0
@.str.3 = [10 x i8] c"setgroups\00"
@__NR_setgroups = global i32 0
@__NR_setgid = global i32 0
@.str.4 = [7 x i8] c"setgid\00"
@__NR_setuid = global i32 0
@.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()
store i64 0, i64* @errno
%24 = icmp eq i32 %0, 3
br i1 %24, label %25, label %44
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 %44
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 %41
37: ; preds = %30
%38 = load i32, i32* @__NR_setgroups
%39 = call i64 (i32, i32, ...) @syscall(i32 %38, i32 1, i32* %34)
%40 = icmp slt i64 %39, 0
br i1 %40, label %42, label %41
41: ; preds = %37, %30
br label %50
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %70
44: ; preds = %25, %21
%45 = load i32, i32* @__NR_setgroups
%46 = tail call i64 (i32, i32, ...) @syscall(i32 %45, i32 0, i8* null)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %70
50: ; preds = %41, %44
store i64 0, i64* @errno
%51 = load i32, i32* @__NR_setgid
%52 = getelementptr inbounds %struct.passwd, %struct.passwd* %14, i64 0, i32 1
%53 = load i32, i32* %52
%54 = call i64 (i32, i32, ...) @syscall(i32 %51, i32 %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %50
%57 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %70
58: ; preds = %50
store i64 0, i64* @errno
%59 = load i32, i32* @__NR_setuid
%60 = getelementptr inbounds %struct.passwd, %struct.passwd* %14, i64 0, i32 0
%61 = load i32, i32* %60
%62 = call i64 (i32, i32, ...) @syscall(i32 %59, i32 %61)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %58
%65 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %70
66: ; preds = %58
%67 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%68 = call i32 (...) @display()
%69 = call i32 @exit(i32 0)
unreachable
70: ; preds = %42, %64, %56, %48
%71 = load i32, i32* @EXIT_FAILURE
%72 = call i32 @exit(i32 %71)
unreachable
}