#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 /*<<< orphan*/ exec_and_exit(char *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ is_valid_param(char *);
scalar_t__ is_valid_service(char *);
int /*<<< orphan*/ printf(char *, char *);
int setrlimit(int /*<<< orphan*/, struct rlimit *);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ show_usage();
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ warn(char *);
int main(int argc, char **argv) {
  if (setuid(0) != 0) {
    printf("Unable to setuid(%d)\n", 0);
    exit(2);
  }
  {
    struct rlimit rlim;
    rlim.rlim_cur = RLIM_INFINITY;
    rlim.rlim_max = RLIM_INFINITY;
    if (setrlimit(RLIMIT_MEMLOCK, &rlim) == -1)
      warn("setrlimit()");
  }
  if (argc == 3) {
    char *service = argv[1];
    char *param = argv[2];
    if (is_valid_service(service) && is_valid_param(param)) {
      exec_and_exit(service, param);
    } else {
      if (!is_valid_service(service))
        printf("Service %s is not valid.\n", service);
      else if (!is_valid_param(param))
        printf("Parameter %s is not valid.\n", param);
      show_usage();
    }
  } else {
    show_usage();
  }
  fflush(stdout);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %edi,%edi
call   10c0 <setuid@plt>
cmp    $0x0,%rax
je     11e1 <main+0x41>
lea    0xe32(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <printf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1080 <setrlimit@plt>
cmp    $0xffffffff,%eax
jne    1224 <main+0x84>
lea    0xdf7(%rip),%rdi
call   1030 <warn@plt>
cmpl   $0x3,-0x8(%rbp)
jne    12da <main+0x13a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   10b0 <is_valid_service@plt>
cmp    $0x0,%rax
je     127e <main+0xde>
mov    -0x30(%rbp),%rdi
call   1070 <is_valid_param@plt>
cmp    $0x0,%rax
je     127e <main+0xde>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <exec_and_exit@plt>
jmp    12d5 <main+0x135>
mov    -0x28(%rbp),%rdi
call   10b0 <is_valid_service@plt>
cmp    $0x0,%rax
jne    12a6 <main+0x106>
mov    -0x28(%rbp),%rsi
lea    0xd86(%rip),%rdi
call   1050 <printf@plt>
jmp    12ce <main+0x12e>
mov    -0x30(%rbp),%rdi
call   1070 <is_valid_param@plt>
cmp    $0x0,%rax
jne    12c9 <main+0x129>
mov    -0x30(%rbp),%rsi
lea    0xd78(%rip),%rdi
call   1050 <printf@plt>
jmp    12ce <main+0x12e>
mov    $0x0,%al
call   1090 <show_usage@plt>
jmp    12e1 <main+0x141>
mov    $0x0,%al
call   1090 <show_usage@plt>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %edi,%edi
call   10c0 <setuid@plt>
test   %rax,%rax
jne    1282 <main+0xe2>
mov    0x2e0c(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    %rax,(%rsp)
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1080 <setrlimit@plt>
cmp    $0xffffffff,%eax
je     11ed <main+0x4d>
cmp    $0x3,%ebp
je     11fe <main+0x5e>
jmp    1262 <main+0xc2>
lea    0xe22(%rip),%rdi
call   1030 <warn@plt>
cmp    $0x3,%ebp
jne    1262 <main+0xc2>
mov    0x8(%rbx),%r14
mov    0x10(%rbx),%rbx
mov    %r14,%rdi
call   10b0 <is_valid_service@plt>
test   %rax,%rax
je     122d <main+0x8d>
mov    %rbx,%rdi
call   1070 <is_valid_param@plt>
test   %rax,%rax
je     122d <main+0x8d>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <exec_and_exit@plt>
jmp    1269 <main+0xc9>
mov    %r14,%rdi
call   10b0 <is_valid_service@plt>
test   %rax,%rax
je     1253 <main+0xb3>
mov    %rbx,%rdi
call   1070 <is_valid_param@plt>
test   %rax,%rax
jne    1262 <main+0xc2>
lea    0xdee(%rip),%rdi
mov    %rbx,%rsi
jmp    125d <main+0xbd>
lea    0xdc8(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
call   1090 <show_usage@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd77(%rip),%rdi
xor    %esi,%esi
call   1050 <printf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.rlimit = type { i8*, i8* }
@.str = [22 x i8] c"Unable to setuid(%d)\0A\00"
@RLIM_INFINITY = global i8* null
@RLIMIT_MEMLOCK = global i32 0
@.str.1 = [12 x i8] c"setrlimit()\00"
@.str.2 = [26 x i8] c"Service %s is not valid.\0A\00"
@.str.3 = [28 x i8] c"Parameter %s is not valid.\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.rlimit
%7 = alloca i8*
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i64 @setuid(i32 0)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* null)
%13 = call i32 @exit(i32 2)
unreachable
14: ; preds = %2
%15 = load i8*, i8** @RLIM_INFINITY
%16 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 1
store i8* %15, i8** %16
%17 = load i8*, i8** @RLIM_INFINITY
%18 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 0
store i8* %17, i8** %18
%19 = load i32, i32* @RLIMIT_MEMLOCK
%20 = call i32 @setrlimit(i32 %19, %struct.rlimit* %6)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %14
%23 = call i32 @warn(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %14
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, 3
br i1 %26, label %27, label %63
27: ; preds = %24
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
store i8* %30, i8** %7
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
store i8* %33, i8** %8
%34 = load i8*, i8** %7
%35 = call i64 @is_valid_service(i8* %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %45
37: ; preds = %27
%38 = load i8*, i8** %8
%39 = call i64 @is_valid_param(i8* %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i8*, i8** %7
%43 = load i8*, i8** %8
%44 = call i32 @exec_and_exit(i8* %42, i8* %43)
br label %62
45: ; preds = %37, %27
%46 = load i8*, i8** %7
%47 = call i64 @is_valid_service(i8* %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %45
%50 = load i8*, i8** %7
%51 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %50)
br label %60
52: ; preds = %45
%53 = load i8*, i8** %8
%54 = call i64 @is_valid_param(i8* %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %59, label %56
56: ; preds = %52
%57 = load i8*, i8** %8
%58 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %57)
br label %59
59: ; preds = %56, %52
br label %60
60: ; preds = %59, %49
%61 = call i32 (...) @show_usage()
br label %62
62: ; preds = %60, %41
br label %65
63: ; preds = %24
%64 = call i32 (...) @show_usage()
br label %65
65: ; preds = %63, %62
%66 = load i32, i32* @stdout
%67 = call i32 @fflush(i32 %66)
ret i32 0
}

LLVM IR (-O2):
%struct.rlimit = type { i8*, i8* }
@.str = [22 x i8] c"Unable to setuid(%d)\0A\00"
@RLIM_INFINITY = global i8* null
@RLIMIT_MEMLOCK = global i32 0
@.str.1 = [12 x i8] c"setrlimit()\00"
@.str.2 = [26 x i8] c"Service %s is not valid.\0A\00"
@.str.3 = [28 x i8] c"Parameter %s is not valid.\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.rlimit
%4 = tail call i64 @setuid(i32 0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* null)
%8 = tail call i32 @exit(i32 2)
unreachable
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, -1
br i1 %16, label %17, label %19
17: ; preds = %9
%18 = call i32 @warn(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %9
%20 = icmp eq i32 %0, 3
br i1 %20, label %21, label %45
21: ; preds = %19
%22 = getelementptr inbounds i8*, i8** %1, i64 1
%23 = load i8*, i8** %22
%24 = getelementptr inbounds i8*, i8** %1, i64 2
%25 = load i8*, i8** %24
%26 = call i64 @is_valid_service(i8* %23)
%27 = icmp eq i64 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %21
%29 = call i64 @is_valid_param(i8* %25)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = call i32 @exec_and_exit(i8* %23, i8* %25)
br label %47
33: ; preds = %28, %21
%34 = call i64 @is_valid_service(i8* %23)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %23)
br label %43
38: ; preds = %33
%39 = call i64 @is_valid_param(i8* %25)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %25)
br label %43
43: ; preds = %38, %41, %36
%44 = call i32 (...) @show_usage()
br label %47
45: ; preds = %19
%46 = call i32 (...) @show_usage()
br label %47
47: ; preds = %31, %43, %45
%48 = load i32, i32* @stdout
%49 = call i32 @fflush(i32 %48)
ret i32 0
}