#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 rlimit {
  void *rlim_max;
  void *rlim_cur;
};

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_MEMLOCK;
void *RLIM_INFINITY;
int errno;
int execvp(char *, char **);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ getgid();
int /*<<< orphan*/ getuid();
int setgid(int /*<<< orphan*/);
int setrlimit(int /*<<< orphan*/, struct rlimit *);
int setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int);
int main(int argc, char **argv) {
  if (argc <= 1) {
    fprintf(stderr,
            "Usage: %s <program to run> [args...]\n\n"
            "%s reconfigures the memlock limit to the maximum possible value,\n"
            "drops privileges, then runs the program specified.\n",
            argv[0], argv[0]);
    return 1;
  }
  struct rlimit l;
  l.rlim_cur = RLIM_INFINITY;
  l.rlim_max = RLIM_INFINITY;
  int ret = setrlimit(RLIMIT_MEMLOCK, &l);
  if (ret != 0) {
    fprintf(stderr, "Unable to reconfigure rlimit: Error %d (%s)\n", errno,
            strerror(errno));
    return 2;
  }
  if (setgid(getgid()) == -1) {
    fprintf(stderr, "Unable to drop group.\n");
    return 3;
  }
  if (setuid(getuid()) == -1) {
    fprintf(stderr, "Unable to drop user.\n");
    return 4;
  }
  ret = execvp(argv[1], argv + 1);
  fprintf(stderr, "Unable to exec: Error %d (%s)\n", errno, strerror(errno));
  return 5;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     11d1 <main+0x51>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
lea    0xe42(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1325 <main+0x1a5>
mov    0x2df0(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1060 <setrlimit@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1255 <main+0xd5>
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%edx
mov    %rax,%rcx
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1325 <main+0x1a5>
mov    $0x0,%al
call   1050 <getgid@plt>
mov    %eax,%edi
call   1070 <setgid@plt>
cmp    $0xffffffff,%eax
jne    128f <main+0x10f>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x3,-0x4(%rbp)
jmp    1325 <main+0x1a5>
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %eax,%edi
call   1090 <setuid@plt>
cmp    $0xffffffff,%eax
jne    12c9 <main+0x149>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x4,-0x4(%rbp)
jmp    1325 <main+0x1a5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1080 <execvp@plt>
mov    %eax,-0x24(%rbp)
mov    0x2cf0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2cdc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%edx
mov    %rax,%rcx
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x5,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11b2 <main+0x32>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rcx
lea    0xe5f(%rip),%rsi
mov    %rcx,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%eax
jmp    1210 <main+0x90>
mov    0x2e0f(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
mov    %rax,0x8(%rsp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1060 <setrlimit@plt>
test   %eax,%eax
je     1217 <main+0x97>
mov    0x2df4(%rip),%rax
mov    (%rax),%ebx
mov    0x2de3(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10a0 <strerror@plt>
lea    0xe9e(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x2,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
xor    %eax,%eax
call   1050 <getgid@plt>
mov    %eax,%edi
call   1070 <setgid@plt>
cmp    $0xffffffff,%eax
je     1282 <main+0x102>
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %eax,%edi
call   1090 <setuid@plt>
cmp    $0xffffffff,%eax
je     12a3 <main+0x123>
mov    0x8(%rbx),%rdi
add    $0x8,%rbx
mov    %rbx,%rsi
call   1080 <execvp@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%ebx
mov    0x2d73(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10a0 <strerror@plt>
lea    0xe88(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x5,%eax
jmp    1210 <main+0x90>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x3,%eax
jmp    1210 <main+0x90>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x4,%eax
jmp    1210 <main+0x90>

LLVM IR (-O0):
%struct.rlimit = type { i8*, i8* }
@stderr = global i32 0
@.str = [155 x i8] c"Usage: %s <program to run> [args...]\0A\0A%s reconfigures the memlock limit to the maximum possible value,\0Adrops privileges, then runs the program specified.\0A\00"
@RLIM_INFINITY = global i8* null
@RLIMIT_MEMLOCK = global i32 0
@.str.1 = [45 x i8] c"Unable to reconfigure rlimit: Error %d (%s)\0A\00"
@errno = global i32 0
@.str.2 = [23 x i8] c"Unable to drop group.\0A\00"
@.str.3 = [22 x i8] c"Unable to drop user.\0A\00"
@.str.4 = [31 x i8] c"Unable to exec: Error %d (%s)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.rlimit
%7 = alloca i32
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, 1
br i1 %9, label %10, label %19
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 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([155 x i8], [155 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %17)
store i32 1, i32* %3
br label %60
19: ; preds = %2
%20 = load i8*, i8** @RLIM_INFINITY
%21 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 1
store i8* %20, i8** %21
%22 = load i8*, i8** @RLIM_INFINITY
%23 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 0
store i8* %22, i8** %23
%24 = load i32, i32* @RLIMIT_MEMLOCK
%25 = call i32 @setrlimit(i32 %24, %struct.rlimit* %6)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %19
%29 = load i32, i32* @stderr
%30 = load i32, i32* @errno
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %30, i8* %32)
store i32 2, i32* %3
br label %60
34: ; preds = %19
%35 = call i32 (...) @getgid()
%36 = call i32 @setgid(i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 3, i32* %3
br label %60
41: ; preds = %34
%42 = call i32 (...) @getuid()
%43 = call i32 @setuid(i32 %42)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
store i32 4, i32* %3
br label %60
48: ; preds = %41
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 1
%51 = load i8*, i8** %50
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 1
%54 = call i32 @execvp(i8* %51, i8** %53)
store i32 %54, i32* %7
%55 = load i32, i32* @stderr
%56 = load i32, i32* @errno
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %56, i8* %58)
store i32 5, i32* %3
br label %60
60: ; preds = %48, %45, %38, %28, %10
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
%struct.rlimit = type { i8*, i8* }
@stderr = global i32 0
@.str = [155 x i8] c"Usage: %s <program to run> [args...]\0A\0A%s reconfigures the memlock limit to the maximum possible value,\0Adrops privileges, then runs the program specified.\0A\00"
@RLIM_INFINITY = global i8* null
@RLIMIT_MEMLOCK = global i32 0
@.str.1 = [45 x i8] c"Unable to reconfigure rlimit: Error %d (%s)\0A\00"
@errno = global i32 0
@.str.2 = [23 x i8] c"Unable to drop group.\0A\00"
@.str.3 = [22 x i8] c"Unable to drop user.\0A\00"
@.str.4 = [31 x i8] c"Unable to exec: Error %d (%s)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.rlimit
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([155 x i8], [155 x i8]* @.str, i64 0, i64 0), i8* %7, i8* %7)
br label %46
9: ; preds = %2
%10 = bitcast %struct.rlimit* %3 to i8*
%11 = load i8*, i8** @RLIM_INFINITY
%12 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 1
store i8* %11, i8** %12
%13 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 0
store i8* %11, i8** %13
%14 = load i32, i32* @RLIMIT_MEMLOCK
%15 = call i32 @setrlimit(i32 %14, %struct.rlimit* %3)
%16 = icmp eq i32 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %9
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %19, i8* %20)
br label %44
22: ; preds = %9
%23 = call i32 (...) @getgid()
%24 = call i32 @setgid(i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %44
29: ; preds = %22
%30 = call i32 (...) @getuid()
%31 = call i32 @setuid(i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %44
36: ; preds = %29
%37 = getelementptr inbounds i8*, i8** %1, i64 1
%38 = load i8*, i8** %37
%39 = call i32 @execvp(i8* %38, i8** %37)
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %41, i8* %42)
br label %44
44: ; preds = %36, %33, %26, %17
%45 = phi i32 [ 2, %17 ], [ 3, %26 ], [ 4, %33 ], [ 5, %36 ]
br label %46
46: ; preds = %44, %5
%47 = phi i32 [ 1, %5 ], [ %45, %44 ]
ret i32 %47
}