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

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
char *basename(char *);
int /*<<< orphan*/ do_bind(char **, int, int);
int /*<<< orphan*/ do_unbind(char **, int, int);
int /*<<< orphan*/ getegid();
int /*<<< orphan*/ geteuid();
int /*<<< orphan*/ help();
scalar_t__ load_vfctl(char **);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ setgid(int /*<<< orphan*/);
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ xfree(char *);
int main(int argc, char *argv[]) {
  setuid(geteuid());
  setgid(getegid());
  if (argc > 1) {
    char *base = basename(argv[0]);
    char *vfctl = NULL;
    if (load_vfctl(&vfctl) != 0) {
      puts("Failed loading vfioctl.lst");
      return EXIT_FAILURE;
    }
    if (strcmp(base, "vfio-bind") == 0)
      do_bind(argv, 1, argc);
    else if (strcmp(base, "vfio-unbind") == 0)
      do_unbind(argv, 1, argc);
    else {
      if (strcmp(argv[1], "bind") == 0)
        do_bind(argv, 2, argc);
      else if (strcmp(argv[1], "unbind") == 0)
        do_unbind(argv, 2, argc);
      else
        help();
    }
    xfree(vfctl);
  } else {
    help();
  }
  return EXIT_SUCCESS;
}


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)
mov    $0x0,%al
call   1060 <geteuid@plt>
mov    %eax,%edi
call   10e0 <setuid@plt>
mov    $0x0,%al
call   10a0 <getegid@plt>
mov    %eax,%edi
call   10b0 <setgid@plt>
cmpl   $0x1,-0x8(%rbp)
jle    1330 <main+0x170>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <basename@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
call   1070 <load_vfctl@plt>
cmp    $0x0,%rax
je     1244 <main+0x84>
lea    0xdd2(%rip),%rdi
call   1030 <puts@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1343 <main+0x183>
mov    -0x18(%rbp),%rdi
lea    0xdcc(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1274 <main+0xb4>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    $0x1,%esi
call   10d0 <do_bind@plt>
jmp    1322 <main+0x162>
mov    -0x18(%rbp),%rdi
lea    0xda6(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    12a4 <main+0xe4>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    $0x1,%esi
call   1050 <do_unbind@plt>
jmp    131d <main+0x15d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd6d(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    12d8 <main+0x118>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    $0x2,%esi
call   10d0 <do_bind@plt>
jmp    1318 <main+0x158>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd43(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    130c <main+0x14c>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    $0x2,%esi
call   1050 <do_unbind@plt>
jmp    1313 <main+0x153>
mov    $0x0,%al
call   10c0 <help@plt>
jmp    1318 <main+0x158>
jmp    131d <main+0x15d>
jmp    1322 <main+0x162>
mov    -0x20(%rbp),%rdi
call   1040 <xfree@plt>
jmp    1337 <main+0x177>
mov    $0x0,%al
call   10c0 <help@plt>
mov    0x2c9a(%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
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
xor    %eax,%eax
call   1060 <geteuid@plt>
mov    %eax,%edi
call   10e0 <setuid@plt>
xor    %eax,%eax
call   10a0 <getegid@plt>
mov    %eax,%edi
call   10b0 <setgid@plt>
cmp    $0x2,%ebx
jl     122a <main+0x6a>
mov    (%r14),%rdi
call   1090 <basename@plt>
mov    %rax,%r15
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
call   1070 <load_vfctl@plt>
test   %rax,%rax
je     1236 <main+0x76>
lea    0xde7(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dbb(%rip),%rax
jmp    12d6 <main+0x116>
xor    %eax,%eax
call   10c0 <help@plt>
jmp    12cf <main+0x10f>
lea    0xdde(%rip),%rsi
mov    %r15,%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1291 <main+0xd1>
lea    0xdd4(%rip),%rsi
mov    %r15,%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
je     129b <main+0xdb>
mov    0x8(%r14),%rdi
lea    0xdb7(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     12a5 <main+0xe5>
mov    0x8(%r14),%rdi
lea    0xdac(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     12b6 <main+0xf6>
xor    %eax,%eax
call   10c0 <help@plt>
jmp    12c5 <main+0x105>
mov    %r14,%rdi
mov    $0x1,%esi
jmp    12ad <main+0xed>
mov    %r14,%rdi
mov    $0x1,%esi
jmp    12be <main+0xfe>
mov    %r14,%rdi
mov    $0x2,%esi
mov    %ebx,%edx
call   10d0 <do_bind@plt>
jmp    12c5 <main+0x105>
mov    %r14,%rdi
mov    $0x2,%esi
mov    %ebx,%edx
call   1050 <do_unbind@plt>
mov    0x8(%rsp),%rdi
call   1040 <xfree@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [27 x i8] c"Failed loading vfioctl.lst\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"vfio-bind\00"
@.str.2 = [12 x i8] c"vfio-unbind\00"
@.str.3 = [5 x i8] c"bind\00"
@.str.4 = [7 x i8] c"unbind\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (...) @geteuid()
%9 = call i32 @setuid(i32 %8)
%10 = call i32 (...) @getegid()
%11 = call i32 @setgid(i32 %10)
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %68
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i8* @basename(i8* %17)
store i8* %18, i8** %6
store i8* null, i8** %7
%19 = call i64 @load_vfctl(i8** %7)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = call i32 @puts(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
store i32 %23, i32* %3
br label %72
24: ; preds = %14
%25 = load i8*, i8** %6
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i8**, i8*** %5
%30 = load i32, i32* %4
%31 = call i32 @do_bind(i8** %29, i32 1, i32 %30)
br label %65
32: ; preds = %24
%33 = load i8*, i8** %6
%34 = call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = load i8**, i8*** %5
%38 = load i32, i32* %4
%39 = call i32 @do_unbind(i8** %37, i32 1, i32 %38)
br label %64
40: ; preds = %32
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = call i64 @strcmp(i8* %43, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %40
%47 = load i8**, i8*** %5
%48 = load i32, i32* %4
%49 = call i32 @do_bind(i8** %47, i32 2, i32 %48)
br label %63
50: ; preds = %40
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 1
%53 = load i8*, i8** %52
%54 = call i64 @strcmp(i8* %53, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %60
56: ; preds = %50
%57 = load i8**, i8*** %5
%58 = load i32, i32* %4
%59 = call i32 @do_unbind(i8** %57, i32 2, i32 %58)
br label %62
60: ; preds = %50
%61 = call i32 (...) @help()
br label %62
62: ; preds = %60, %56
br label %63
63: ; preds = %62, %46
br label %64
64: ; preds = %63, %36
br label %65
65: ; preds = %64, %28
%66 = load i8*, i8** %7
%67 = call i32 @xfree(i8* %66)
br label %70
68: ; preds = %2
%69 = call i32 (...) @help()
br label %70
70: ; preds = %68, %65
%71 = load i32, i32* @EXIT_SUCCESS
store i32 %71, i32* %3
br label %72
72: ; preds = %70, %21
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@.str = [27 x i8] c"Failed loading vfioctl.lst\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"vfio-bind\00"
@.str.2 = [12 x i8] c"vfio-unbind\00"
@.str.3 = [5 x i8] c"bind\00"
@.str.4 = [7 x i8] c"unbind\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i8*
%4 = tail call i32 (...) @geteuid()
%5 = tail call i32 @setuid(i32 %4)
%6 = tail call i32 (...) @getegid()
%7 = tail call i32 @setgid(i32 %6)
%8 = icmp sgt i32 %0, 1
br i1 %8, label %9, label %46
9: ; preds = %2
%10 = load i8*, i8** %1
%11 = tail call i8* @basename(i8* %10)
%12 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%13 = call i64 @load_vfctl(i8** %3)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %43
15: ; preds = %9
%16 = call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @do_bind(i8** %1, i32 1, i32 %0)
br label %40
20: ; preds = %15
%21 = call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @do_unbind(i8** %1, i32 1, i32 %0)
br label %40
25: ; preds = %20
%26 = getelementptr inbounds i8*, i8** %1, i64 1
%27 = load i8*, i8** %26
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @do_bind(i8** %1, i32 2, i32 %0)
br label %40
32: ; preds = %25
%33 = load i8*, i8** %26
%34 = call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @do_unbind(i8** %1, i32 2, i32 %0)
br label %40
38: ; preds = %32
%39 = call i32 (...) @help()
br label %40
40: ; preds = %18, %30, %38, %36, %23
%41 = load i8*, i8** %3
%42 = call i32 @xfree(i8* %41)
br label %48
43: ; preds = %9
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
br label %50
46: ; preds = %2
%47 = tail call i32 (...) @help()
br label %48
48: ; preds = %40, %46
%49 = load i32, i32* @EXIT_SUCCESS
br label %50
50: ; preds = %43, %48
%51 = phi i32 [ %45, %43 ], [ %49, %48 ]
ret i32 %51
}