#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 */
int EXIT_FAILURE;
int /*<<< orphan*/ execvp(char *, char **);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ setgroups(int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ setregid(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ setreuid(int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  struct passwd *pw = getpwnam("nobody");
  if (pw == NULL) {
    perror("getpwnam");
    return EXIT_FAILURE;
  }
  if (setgroups(0, NULL) < 0) {
    perror("setgroups");
    return EXIT_FAILURE;
  }
  if (setregid(pw->pw_gid, pw->pw_gid) < 0) {
    perror("setgroups");
    return EXIT_FAILURE;
  }
  if (setreuid(pw->pw_uid, pw->pw_uid) < 0) {
    perror("setreuid");
    return EXIT_FAILURE;
  }
  execvp(argv[1], argv + 1);
  perror("execvp");
  return 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)
lea    0xe83(%rip),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ae <main+0x4e>
lea    0xe6f(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127f <main+0x11f>
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <setgroups@plt>
cmp    $0x0,%rax
jge    11e0 <main+0x80>
lea    0xe46(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127f <main+0x11f>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
call   1060 <setregid@plt>
cmp    $0x0,%rax
jge    121a <main+0xba>
lea    0xe0c(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127f <main+0x11f>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   1050 <setreuid@plt>
cmp    $0x0,%rax
jge    1252 <main+0xf2>
lea    0xdde(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127f <main+0x11f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1080 <execvp@plt>
lea    0xdb5(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
lea    0xe91(%rip),%rdi
call   1040 <getpwnam@plt>
test   %rax,%rax
jne    1182 <main+0x22>
lea    0xe87(%rip),%r15
jmp    11d7 <main+0x77>
mov    %rax,%rbx
xor    %edi,%edi
xor    %esi,%esi
call   1030 <setgroups@plt>
lea    0xe7b(%rip),%r15
test   %rax,%rax
js     11d7 <main+0x77>
mov    0x4(%rbx),%edi
mov    %edi,%esi
call   1060 <setregid@plt>
test   %rax,%rax
js     11d7 <main+0x77>
mov    (%rbx),%edi
mov    %edi,%esi
call   1050 <setreuid@plt>
test   %rax,%rax
jns    11c0 <main+0x60>
lea    0xe5c(%rip),%r15
jmp    11d7 <main+0x77>
mov    0x8(%r14),%rdi
add    $0x8,%r14
mov    %r14,%rsi
call   1080 <execvp@plt>
lea    0xe4c(%rip),%r15
mov    %r15,%rdi
call   1070 <perror@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@.str = [7 x i8] c"nobody\00"
@.str.1 = [9 x i8] c"getpwnam\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [10 x i8] c"setgroups\00"
@.str.3 = [9 x i8] c"setreuid\00"
@.str.4 = [7 x i8] c"execvp\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 = call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store %struct.passwd* %7, %struct.passwd** %6
%8 = load %struct.passwd*, %struct.passwd** %6
%9 = icmp eq %struct.passwd* %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
store i32 %12, i32* %3
br label %52
13: ; preds = %2
%14 = call i64 @setgroups(i32 0, i32* null)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
store i32 %18, i32* %3
br label %52
19: ; preds = %13
%20 = load %struct.passwd*, %struct.passwd** %6
%21 = getelementptr inbounds %struct.passwd, %struct.passwd* %20, i32 0, i32 1
%22 = load i32, i32* %21
%23 = load %struct.passwd*, %struct.passwd** %6
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %23, i32 0, i32 1
%25 = load i32, i32* %24
%26 = call i64 @setregid(i32 %22, i32 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %19
%29 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
store i32 %30, i32* %3
br label %52
31: ; preds = %19
%32 = load %struct.passwd*, %struct.passwd** %6
%33 = getelementptr inbounds %struct.passwd, %struct.passwd* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = load %struct.passwd*, %struct.passwd** %6
%36 = getelementptr inbounds %struct.passwd, %struct.passwd* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i64 @setreuid(i32 %34, i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %31
%41 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
store i32 %42, i32* %3
br label %52
43: ; preds = %31
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = call i32 @execvp(i8* %46, i8** %48)
%50 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
store i32 %51, i32* %3
br label %52
52: ; preds = %43, %40, %28, %16, %10
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@.str = [7 x i8] c"nobody\00"
@.str.1 = [9 x i8] c"getpwnam\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [10 x i8] c"setgroups\00"
@.str.3 = [9 x i8] c"setreuid\00"
@.str.4 = [7 x i8] c"execvp\00"
define i32 @main(i32 %0, i8** %1) {
%3 = tail call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq %struct.passwd* %3, null
br i1 %4, label %22, label %5
5: ; preds = %2
%6 = tail call i64 @setgroups(i32 0, i32* null)
%7 = icmp slt i64 %6, 0
br i1 %7, label %22, label %8
8: ; preds = %5
%9 = getelementptr inbounds %struct.passwd, %struct.passwd* %3, i64 0, i32 1
%10 = load i32, i32* %9
%11 = tail call i64 @setregid(i32 %10, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %22, label %13
13: ; preds = %8
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i64 @setreuid(i32 %15, i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %13
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = tail call i32 @execvp(i8* %20, i8** %19)
br label %22
22: ; preds = %13, %8, %5, %2, %18
%23 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), %18 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), %2 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), %5 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), %8 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), %13 ]
%24 = tail call i32 @perror(i8* %23)
%25 = load i32, i32* @EXIT_FAILURE
ret i32 %25
}