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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ authenticate(char *, int /*<<< orphan*/ *,
                                int /*<<< orphan*/ *);
int execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fatal(char *);
int get_groups_for_user(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int getopt(int, char **, char *);
int /*<<< orphan*/ helpAndLeave(char *, int /*<<< orphan*/);
char *optarg;
scalar_t__ opterr;
int optind;
int /*<<< orphan*/ pexit(char *);
int setgroups(int, int /*<<< orphan*/ *);
int setregid(int /*<<< orphan*/, int /*<<< orphan*/);
int setreuid(int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (argc < 2)
    helpAndLeave(argv[0], EXIT_FAILURE);
  int opt;
  int num_groups;
  uid_t uid;
  gid_t gid;
  gid_t groups[(1024)];
  char *file;
  char *username = ("root");
  opterr = 0;
  while ((opt = getopt(argc, argv, "+u:")) != -1) {
    switch (opt) {
    case 'u':
      username = optarg;
      break;
    case '?':
      helpAndLeave(argv[0], EXIT_FAILURE);
      break;
    }
  }
  if (optind == argc)
    helpAndLeave(argv[0], EXIT_FAILURE);
  if (!authenticate(username, &uid, &gid))
    fatal("Authentication failure");
  if ((num_groups = get_groups_for_user(uid, (1024), groups)) == -1)
    pexit("get_groups_for_user");
  if (setgroups(num_groups, groups) == -1)
    pexit("setgroups");
  if (setreuid(uid, uid) == -1)
    pexit("setreuid");
  if (setregid(gid, gid) == -1)
    pexit("setregid");
  file = argv[optind];
  if (execvp(file, &argv[optind]) == -1)
    pexit("execvp");
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11e8 <main+0x38>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
call   1080 <helpAndLeave@plt>
lea    0xe11(%rip),%rax
mov    %rax,-0x1030(%rbp)
mov    0x2dcb(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xdf3(%rip),%rdx
call   1090 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     127e <main+0xce>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1034(%rbp)
sub    $0x3f,%eax
je     1264 <main+0xb4>
jmp    123a <main+0x8a>
mov    -0x1034(%rbp),%eax
sub    $0x75,%eax
jne    1279 <main+0xc9>
jmp    124e <main+0x9e>
mov    0x2da3(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x1030(%rbp)
jmp    1279 <main+0xc9>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
call   1080 <helpAndLeave@plt>
jmp    1204 <main+0x54>
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
cmp    -0x8(%rbp),%eax
jne    12a5 <main+0xf5>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
call   1080 <helpAndLeave@plt>
mov    -0x1030(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <authenticate@plt>
cmp    $0x0,%eax
jne    12ce <main+0x11e>
lea    0xd40(%rip),%rdi
call   10d0 <fatal@plt>
mov    -0x1c(%rbp),%edi
lea    -0x1020(%rbp),%rdx
mov    $0x400,%esi
call   1040 <get_groups_for_user@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    12fa <main+0x14a>
lea    0xd2b(%rip),%rdi
call   10c0 <pexit@plt>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
call   1050 <setgroups@plt>
cmp    $0xffffffff,%eax
jne    131e <main+0x16e>
lea    0xd1b(%rip),%rdi
call   10c0 <pexit@plt>
mov    -0x1c(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1060 <setreuid@plt>
cmp    $0xffffffff,%eax
jne    133e <main+0x18e>
lea    0xd05(%rip),%rdi
call   10c0 <pexit@plt>
mov    -0x20(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1070 <setregid@plt>
cmp    $0xffffffff,%eax
jne    135e <main+0x1ae>
lea    0xcee(%rip),%rdi
call   10c0 <pexit@plt>
mov    -0x10(%rbp),%rax
mov    0x2c77(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x1028(%rbp)
mov    -0x1028(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2c57(%rip),%rax
movslq (%rax),%rax
shl    $0x3,%rax
add    %rax,%rsi
call   10a0 <execvp@plt>
cmp    $0xffffffff,%eax
jne    13ad <main+0x1fd>
lea    0xca8(%rip),%rdi
call   10c0 <pexit@plt>
mov    0x2c0c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%r15
mov    %edi,%ebp
cmp    $0x1,%edi
jg     11dc <main+0x2c>
mov    (%r15),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
call   1080 <helpAndLeave@plt>
mov    0x2de5(%rip),%rax
movq   $0x0,(%rax)
lea    0xe0f(%rip),%r14
lea    0xe0d(%rip),%rbx
mov    0x2dd1(%rip),%r13
mov    0x2df2(%rip),%r12
jmp    1210 <main+0x60>
mov    (%r12),%r14
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1090 <getopt@plt>
cmp    $0x3f,%eax
je     1230 <main+0x80>
cmp    $0x75,%eax
je     1208 <main+0x58>
cmp    $0xffffffff,%eax
jne    1210 <main+0x60>
jmp    123e <main+0x8e>
xchg   %ax,%ax
mov    (%r15),%rdi
mov    0x0(%r13),%esi
call   1080 <helpAndLeave@plt>
jmp    1210 <main+0x60>
mov    0x2d9b(%rip),%r12
cmp    %ebp,(%r12)
jne    1257 <main+0xa7>
mov    (%r15),%rdi
mov    0x0(%r13),%esi
call   1080 <helpAndLeave@plt>
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r14,%rdi
call   1030 <authenticate@plt>
test   %eax,%eax
jne    1279 <main+0xc9>
lea    0xd95(%rip),%rdi
call   10d0 <fatal@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rdx
mov    $0x400,%esi
call   1040 <get_groups_for_user@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    129f <main+0xef>
lea    0xd86(%rip),%rdi
call   10c0 <pexit@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1050 <setgroups@plt>
cmp    $0xffffffff,%eax
jne    12bc <main+0x10c>
lea    0xd7d(%rip),%rdi
call   10c0 <pexit@plt>
mov    0x8(%rsp),%edi
mov    %edi,%esi
call   1060 <setreuid@plt>
cmp    $0xffffffff,%eax
jne    12d8 <main+0x128>
lea    0xd6b(%rip),%rdi
call   10c0 <pexit@plt>
mov    0xc(%rsp),%edi
mov    %edi,%esi
call   1070 <setregid@plt>
cmp    $0xffffffff,%eax
jne    12f4 <main+0x144>
lea    0xd58(%rip),%rdi
call   10c0 <pexit@plt>
movslq (%r12),%rax
lea    (%r15,%rax,8),%rsi
mov    (%r15,%rax,8),%rdi
call   10a0 <execvp@plt>
cmp    $0xffffffff,%eax
jne    1316 <main+0x166>
lea    0xd3f(%rip),%rdi
call   10c0 <pexit@plt>
mov    0x2ca3(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [5 x i8] c"root\00"
@opterr = global i64 0
@.str.1 = [4 x i8] c"+u:\00"
@optarg = global i8* null
@optind = global i32 0
@.str.2 = [23 x i8] c"Authentication failure\00"
@.str.3 = [20 x i8] c"get_groups_for_user\00"
@.str.4 = [10 x i8] c"setgroups\00"
@.str.5 = [9 x i8] c"setreuid\00"
@.str.6 = [9 x i8] c"setregid\00"
@.str.7 = [7 x i8] c"execvp\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1024 x i32]
%11 = alloca i8*
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @helpAndLeave(i8* %18, i32 %19)
br label %21
21: ; preds = %15, %2
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %12
store i64 0, i64* @opterr
br label %22
22: ; preds = %37, %21
%23 = load i32, i32* %4
%24 = load i8**, i8*** %5
%25 = call i32 @getopt(i32 %23, i8** %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32 %25, i32* %6
%26 = icmp ne i32 %25, -1
br i1 %26, label %27, label %38
27: ; preds = %22
%28 = load i32, i32* %6
switch i32 %28, label %37 [
i32 117, label %29
i32 63, label %31
]
29: ; preds = %27
%30 = load i8*, i8** @optarg
store i8* %30, i8** %12
br label %37
31: ; preds = %27
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 0
%34 = load i8*, i8** %33
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @helpAndLeave(i8* %34, i32 %35)
br label %37
37: ; preds = %27, %31, %29
br label %22
38: ; preds = %22
%39 = load i32, i32* @optind
%40 = load i32, i32* %4
%41 = icmp eq i32 %39, %40
br i1 %41, label %42, label %48
42: ; preds = %38
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 0
%45 = load i8*, i8** %44
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @helpAndLeave(i8* %45, i32 %46)
br label %48
48: ; preds = %42, %38
%49 = load i8*, i8** %12
%50 = call i32 @authenticate(i8* %49, i32* %8, i32* %9)
%51 = icmp ne i32 %50, 0
br i1 %51, label %54, label %52
52: ; preds = %48
%53 = call i32 @fatal(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %54
54: ; preds = %52, %48
%55 = load i32, i32* %8
%56 = getelementptr inbounds [1024 x i32], [1024 x i32]* %10, i64 0, i64 0
%57 = call i32 @get_groups_for_user(i32 %55, i32 1024, i32* %56)
store i32 %57, i32* %7
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %61
59: ; preds = %54
%60 = call i32 @pexit(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %61
61: ; preds = %59, %54
%62 = load i32, i32* %7
%63 = getelementptr inbounds [1024 x i32], [1024 x i32]* %10, i64 0, i64 0
%64 = call i32 @setgroups(i32 %62, i32* %63)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %68
66: ; preds = %61
%67 = call i32 @pexit(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %68
68: ; preds = %66, %61
%69 = load i32, i32* %8
%70 = load i32, i32* %8
%71 = call i32 @setreuid(i32 %69, i32 %70)
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %75
73: ; preds = %68
%74 = call i32 @pexit(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %75
75: ; preds = %73, %68
%76 = load i32, i32* %9
%77 = load i32, i32* %9
%78 = call i32 @setregid(i32 %76, i32 %77)
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %82
80: ; preds = %75
%81 = call i32 @pexit(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %82
82: ; preds = %80, %75
%83 = load i8**, i8*** %5
%84 = load i32, i32* @optind
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i8*, i8** %83, i64 %85
%87 = load i8*, i8** %86
store i8* %87, i8** %11
%88 = load i8*, i8** %11
%89 = load i8**, i8*** %5
%90 = load i32, i32* @optind
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds i8*, i8** %89, i64 %91
%93 = call i32 @execvp(i8* %88, i8** %92)
%94 = icmp eq i32 %93, -1
br i1 %94, label %95, label %97
95: ; preds = %82
%96 = call i32 @pexit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %97
97: ; preds = %95, %82
%98 = load i32, i32* @EXIT_SUCCESS
%99 = call i32 @exit(i32 %98)
unreachable
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [5 x i8] c"root\00"
@opterr = global i64 0
@.str.1 = [4 x i8] c"+u:\00"
@optarg = global i8* null
@optind = global i32 0
@.str.2 = [23 x i8] c"Authentication failure\00"
@.str.3 = [20 x i8] c"get_groups_for_user\00"
@.str.4 = [10 x i8] c"setgroups\00"
@.str.5 = [9 x i8] c"setreuid\00"
@.str.6 = [9 x i8] c"setregid\00"
@.str.7 = [7 x i8] c"execvp\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i32]
%6 = icmp slt i32 %0, 2
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @helpAndLeave(i8* %8, i32 %9)
br label %11
11: ; preds = %7, %2
%12 = bitcast i32* %3 to i8*
%13 = bitcast i32* %4 to i8*
%14 = bitcast [1024 x i32]* %5 to i8*
store i64 0, i64* @opterr
br label %15
15: ; preds = %20, %11
%16 = phi i8* [ %21, %20 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %11 ]
br label %17
17: ; preds = %19, %15
%18 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
switch i32 %18, label %19 [
i32 -1, label %26
i32 117, label %20
i32 63, label %22
]
19: ; preds = %17, %22
br label %17
20: ; preds = %17
%21 = load i8*, i8** @optarg
br label %15
22: ; preds = %17
%23 = load i8*, i8** %1
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @helpAndLeave(i8* %23, i32 %24)
br label %19
26: ; preds = %17
%27 = load i32, i32* @optind
%28 = icmp eq i32 %27, %0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i8*, i8** %1
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @helpAndLeave(i8* %30, i32 %31)
br label %33
33: ; preds = %29, %26
%34 = call i32 @authenticate(i8* %16, i32* %3, i32* %4)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @fatal(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %33
%39 = load i32, i32* %3
%40 = getelementptr inbounds [1024 x i32], [1024 x i32]* %5, i64 0, i64 0
%41 = call i32 @get_groups_for_user(i32 %39, i32 1024, i32* %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = call i32 @pexit(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %43, %38
%46 = call i32 @setgroups(i32 %41, i32* %40)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @pexit(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %50
50: ; preds = %48, %45
%51 = load i32, i32* %3
%52 = call i32 @setreuid(i32 %51, i32 %51)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = call i32 @pexit(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %56
56: ; preds = %54, %50
%57 = load i32, i32* %4
%58 = call i32 @setregid(i32 %57, i32 %57)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %56
%61 = call i32 @pexit(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %62
62: ; preds = %60, %56
%63 = load i32, i32* @optind
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8*, i8** %1, i64 %64
%66 = load i8*, i8** %65
%67 = call i32 @execvp(i8* %66, i8** %65)
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %71
69: ; preds = %62
%70 = call i32 @pexit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %71
71: ; preds = %69, %62
%72 = load i32, i32* @EXIT_SUCCESS
%73 = call i32 @exit(i32 %72)
unreachable
}