#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*/ rpm_path;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exec_and_exit(int, char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ is_valid_action(char *);
int /*<<< orphan*/ pkg_exists(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ show_usage();
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ validate_pkg(char *);
int main(int argc, char **argv) {
  if (setuid(0) != 0) {
    printf("Unable to setuid(%d)\n", 0);
    exit(EXIT_FAILURE);
  }
  if (argc < 2) {
    printf("Too few arguments provided.\n");
    show_usage();
  }
  char *second_arg;
  char *action = argv[1];
  int program_to_exec = 2;
  if (!is_valid_action(action)) {
    printf("Action %s is not valid.\n", action);
    show_usage();
  }
  second_arg = (argc == 3) ? argv[2] : NULL;
  if (strcmp(action, "install") == 0) {
    if (second_arg == NULL) {
      printf("You need to provide a package name.\n");
      show_usage();
    }
    validate_pkg(second_arg);
    char rpm_path[512];
    snprintf(rpm_path, sizeof(rpm_path), "%s/%s", "/tmp/rpm", second_arg);
    if (!pkg_exists(rpm_path)) {
      printf("Package %s does not exist.\n", rpm_path);
      exit(EXIT_FAILURE);
    }
    second_arg = rpm_path;
    program_to_exec = 1;
  } else if (strcmp(action, "clean") == 0) {
    second_arg = "all";
  }
  exec_and_exit(program_to_exec, action, second_arg);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %edi,%edi
call   10c0 <setuid@plt>
cmp    $0x0,%rax
je     11e8 <main+0x48>
lea    0xe2f(%rip),%rdi
xor    %esi,%esi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpl   $0x2,-0x8(%rbp)
jge    1207 <main+0x67>
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10a0 <show_usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
movl   $0x2,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <is_valid_action@plt>
cmp    $0x0,%eax
jne    1245 <main+0xa5>
mov    -0x20(%rbp),%rsi
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10a0 <show_usage@plt>
cmpl   $0x3,-0x8(%rbp)
jne    1263 <main+0xc3>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x238(%rbp)
jmp    1271 <main+0xd1>
xor    %eax,%eax
mov    %rax,-0x238(%rbp)
jmp    1271 <main+0xd1>
mov    -0x238(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xdc5(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1331 <main+0x191>
cmpq   $0x0,-0x18(%rbp)
jne    12b6 <main+0x116>
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10a0 <show_usage@plt>
mov    -0x18(%rbp),%rdi
call   1030 <validate_pkg@plt>
lea    -0x230(%rbp),%rdi
mov    -0x18(%rbp),%r8
mov    $0x200,%esi
lea    0xda3(%rip),%rdx
lea    0xda2(%rip),%rcx
call   1070 <snprintf@plt>
lea    -0x230(%rbp),%rdi
call   1090 <pkg_exists@plt>
cmp    $0x0,%eax
jne    131a <main+0x17a>
lea    -0x230(%rbp),%rsi
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x230(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    135b <main+0x1bb>
mov    -0x20(%rbp),%rdi
lea    0xd68(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1356 <main+0x1b6>
lea    0xcfe(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    135b <main+0x1bb>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <exec_and_exit@plt>
xor    %eax,%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
xor    %edi,%edi
call   10d0 <setuid@plt>
test   %rax,%rax
jne    12dd <main+0x12d>
cmp    $0x1,%ebp
jg     11e8 <main+0x38>
lea    0xeb0(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10b0 <show_usage@plt>
mov    0x8(%r14),%rbx
mov    %rbx,%rdi
call   1070 <is_valid_action@plt>
test   %eax,%eax
je     121c <main+0x6c>
cmp    $0x3,%ebp
jne    1239 <main+0x89>
mov    0x10(%r14),%r14
lea    0xe27(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1250 <main+0xa0>
test   %r14,%r14
jne    128a <main+0xda>
jmp    1274 <main+0xc4>
lea    0xdf3(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   10b0 <show_usage@plt>
cmp    $0x3,%ebp
je     11fd <main+0x4d>
lea    0xdef(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1274 <main+0xc4>
xor    %r14d,%r14d
lea    0xe0b(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
lea    0xdca(%rip),%rbp
cmovne %r14,%rbp
mov    $0x2,%edi
jmp    12c4 <main+0x114>
lea    0xded(%rip),%rdi
call   1040 <puts@plt>
xor    %r14d,%r14d
xor    %eax,%eax
call   10b0 <show_usage@plt>
mov    %r14,%rdi
call   1030 <validate_pkg@plt>
lea    0xd9e(%rip),%rdx
lea    0xd9d(%rip),%rcx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x200,%esi
mov    %r14,%r8
call   1080 <snprintf@plt>
mov    %rbp,%rdi
call   10a0 <pkg_exists@plt>
test   %eax,%eax
je     12ef <main+0x13f>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1050 <exec_and_exit@plt>
xor    %eax,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd1c(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1300 <main+0x150>
lea    0xd50(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"Unable to setuid(%d)\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [29 x i8] c"Too few arguments provided.\0A\00"
@.str.2 = [25 x i8] c"Action %s is not valid.\0A\00"
@.str.3 = [8 x i8] c"install\00"
@.str.4 = [37 x i8] c"You need to provide a package name.\0A\00"
@.str.5 = [6 x i8] c"%s/%s\00"
@.str.6 = [9 x i8] c"/tmp/rpm\00"
@.str.7 = [28 x i8] c"Package %s does not exist.\0A\00"
@.str.8 = [6 x i8] c"clean\00"
@.str.9 = [4 x i8] c"all\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [512 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i64 @setuid(i32 0)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 0)
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 2
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 (...) @show_usage()
br label %22
22: ; preds = %19, %16
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
store i8* %25, i8** %7
store i32 2, i32* %8
%26 = load i8*, i8** %7
%27 = call i32 @is_valid_action(i8* %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %22
%30 = load i8*, i8** %7
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %30)
%32 = call i32 (...) @show_usage()
br label %33
33: ; preds = %29, %22
%34 = load i32, i32* %4
%35 = icmp eq i32 %34, 3
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 2
%39 = load i8*, i8** %38
br label %41
40: ; preds = %33
br label %41
41: ; preds = %40, %36
%42 = phi i8* [ %39, %36 ], [ null, %40 ]
store i8* %42, i8** %6
%43 = load i8*, i8** %7
%44 = call i64 @strcmp(i8* %43, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %68
46: ; preds = %41
%47 = load i8*, i8** %6
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%51 = call i32 (...) @show_usage()
br label %52
52: ; preds = %49, %46
%53 = load i8*, i8** %6
%54 = call i32 @validate_pkg(i8* %53)
%55 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%56 = load i8*, i8** %6
%57 = call i32 @snprintf(i8* %55, i32 512, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %56)
%58 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%59 = call i32 @pkg_exists(i8* %58)
%60 = icmp ne i32 %59, 0
br i1 %60, label %66, label %61
61: ; preds = %52
%62 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i8* %62)
%64 = load i32, i32* @EXIT_FAILURE
%65 = call i32 @exit(i32 %64)
unreachable
66: ; preds = %52
%67 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
store i8* %67, i8** %6
store i32 1, i32* %8
br label %74
68: ; preds = %41
%69 = load i8*, i8** %7
%70 = call i64 @strcmp(i8* %69, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%71 = icmp eq i64 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %68
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0), i8** %6
br label %73
73: ; preds = %72, %68
br label %74
74: ; preds = %73, %66
%75 = load i32, i32* %8
%76 = load i8*, i8** %7
%77 = load i8*, i8** %6
%78 = call i32 @exec_and_exit(i32 %75, i8* %76, i8* %77)
ret i32 0
}

LLVM IR (-O2):
@.str = [22 x i8] c"Unable to setuid(%d)\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [25 x i8] c"Action %s is not valid.\0A\00"
@.str.3 = [8 x i8] c"install\00"
@.str.5 = [6 x i8] c"%s/%s\00"
@.str.6 = [9 x i8] c"/tmp/rpm\00"
@.str.7 = [28 x i8] c"Package %s does not exist.\0A\00"
@.str.8 = [6 x i8] c"clean\00"
@.str.9 = [4 x i8] c"all\00"
@str = [36 x i8] c"You need to provide a package name.\00"
@str.10 = [28 x i8] c"Too few arguments provided.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = tail call i64 @setuid(i32 0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 0)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = icmp slt i32 %0, 2
br i1 %11, label %12, label %15
12: ; preds = %10
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.10, i64 0, i64 0))
%14 = tail call i32 (...) @show_usage()
br label %15
15: ; preds = %12, %10
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = tail call i32 @is_valid_action(i8* %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%22 = tail call i32 (...) @show_usage()
br label %23
23: ; preds = %20, %15
%24 = icmp eq i32 %0, 3
br i1 %24, label %25, label %30
25: ; preds = %23
%26 = getelementptr inbounds i8*, i8** %1, i64 2
%27 = load i8*, i8** %26
%28 = tail call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %33, label %50
30: ; preds = %23
%31 = tail call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %50
33: ; preds = %25
%34 = icmp eq i8* %27, null
br i1 %34, label %35, label %38
35: ; preds = %30, %33
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%37 = tail call i32 (...) @show_usage()
br label %38
38: ; preds = %35, %33
%39 = phi i8* [ null, %35 ], [ %27, %33 ]
%40 = tail call i32 @validate_pkg(i8* %39)
%41 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%42 = call i32 @snprintf(i8* %41, i32 512, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %39)
%43 = call i32 @pkg_exists(i8* %41)
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %38
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i8* %41)
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %38
br label %55
50: ; preds = %30, %25
%51 = phi i8* [ null, %30 ], [ %27, %25 ]
%52 = tail call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%53 = icmp eq i64 %52, 0
%54 = select i1 %53, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0), i8* %51
br label %55
55: ; preds = %50, %49
%56 = phi i8* [ %41, %49 ], [ %54, %50 ]
%57 = phi i32 [ 1, %49 ], [ 2, %50 ]
%58 = call i32 @exec_and_exit(i32 %57, i8* %17, i8* %56)
ret i32 0
}