#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 /*<<< orphan*/ O_RDWR;
void *atol(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ ivshmem_print_opts();
int /*<<< orphan*/ ivshmem_send(int, int, long);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
char *strdup(char *);
int main(int argc, char **argv) {
  int rv, fd;
  int *count;
  short rv_short;
  int i, x = 0;
  void *map_region;
  char *file;
  int my_ioctl;
  long ioctl_arg;
  if (argc < 3) {
    fprintf(stderr, "USAGE: guestlock <file> <ioctl> <ioctl_arg>\n");
    ivshmem_print_opts();
    exit(-1);
  }
  file = strdup(argv[1]);
  my_ioctl = atol(argv[2]);
  if (argc == 4) {
    ioctl_arg = atol(argv[3]);
  } else {
    ioctl_arg = -1;
  }
  if ((fd = open(file, O_RDWR)) < 0) {
    fprintf(stderr, "ERROR: cannot open file\n");
    exit(-1);
  }
  ivshmem_send(fd, my_ioctl, ioctl_arg);
  close(fd);
  printf("exiting\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11dd <main+0x4d>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x0,%al
call   1030 <ivshmem_print_opts@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atol@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x4,-0x8(%rbp)
jne    121e <main+0x8e>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1080 <atol@plt>
mov    %rax,-0x50(%rbp)
jmp    1226 <main+0x96>
movq   $0xffffffffffffffff,-0x50(%rbp)
ff
mov    -0x40(%rbp),%rdi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1263 <main+0xd3>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x44(%rbp),%esi
mov    -0x50(%rbp),%rdx
call   1070 <ivshmem_send@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
lea    0xdc5(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x2,%edi
jle    1214 <main+0x84>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x8(%rsi),%rdi
call   10b0 <strdup@plt>
mov    %rax,%r15
mov    0x10(%rbx),%rdi
call   1080 <atol@plt>
mov    %rax,%r14
mov    $0xffffffffffffffff,%r12
cmp    $0x4,%ebp
jne    11d2 <main+0x42>
mov    0x18(%rbx),%rdi
call   1080 <atol@plt>
mov    %rax,%r12
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     123a <main+0xaa>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14d,%esi
mov    %r12,%rdx
call   1070 <ivshmem_send@plt>
mov    %ebx,%edi
call   1050 <close@plt>
lea    0xe42(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xddc(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
call   1040 <ivshmem_print_opts@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xde3(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [45 x i8] c"USAGE: guestlock <file> <ioctl> <ioctl_arg>\0A\00"
@O_RDWR = global i32 0
@.str.1 = [25 x i8] c"ERROR: cannot open file\0A\00"
@.str.2 = [9 x i8] c"exiting\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i16
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %11
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 3
br i1 %17, label %18, label %23
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%21 = call i32 (...) @ivshmem_print_opts()
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i8* @strdup(i8* %26)
store i8* %27, i8** %13
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i8* @atol(i8* %30)
%32 = ptrtoint i8* %31 to i32
store i32 %32, i32* %14
%33 = load i32, i32* %4
%34 = icmp eq i32 %33, 4
br i1 %34, label %35, label %41
35: ; preds = %23
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 3
%38 = load i8*, i8** %37
%39 = call i8* @atol(i8* %38)
%40 = ptrtoint i8* %39 to i64
store i64 %40, i64* %15
br label %42
41: ; preds = %23
store i64 -1, i64* %15
br label %42
42: ; preds = %41, %35
%43 = load i8*, i8** %13
%44 = load i32, i32* @O_RDWR
%45 = call i32 @open(i8* %43, i32 %44)
store i32 %45, i32* %7
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %42
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%50 = call i32 @exit(i32 -1)
unreachable
51: ; preds = %42
%52 = load i32, i32* %7
%53 = load i32, i32* %14
%54 = load i64, i64* %15
%55 = call i32 @ivshmem_send(i32 %52, i32 %53, i64 %54)
%56 = load i32, i32* %7
%57 = call i32 @close(i32 %56)
%58 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [45 x i8] c"USAGE: guestlock <file> <ioctl> <ioctl_arg>\0A\00"
@O_RDWR = global i32 0
@.str.1 = [25 x i8] c"ERROR: cannot open file\0A\00"
@str = [8 x i8] c"exiting\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 (...) @ivshmem_print_opts()
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i8* @strdup(i8* %11)
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i8* @atol(i8* %14)
%16 = icmp eq i32 %0, 4
br i1 %16, label %17, label %22
17: ; preds = %9
%18 = getelementptr inbounds i8*, i8** %1, i64 3
%19 = load i8*, i8** %18
%20 = tail call i8* @atol(i8* %19)
%21 = ptrtoint i8* %20 to i64
br label %22
22: ; preds = %9, %17
%23 = phi i64 [ %21, %17 ], [ -1, %9 ]
%24 = load i32, i32* @O_RDWR
%25 = tail call i32 @open(i8* %12, i32 %24)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%30 = tail call i32 @exit(i32 -1)
unreachable
31: ; preds = %22
%32 = ptrtoint i8* %15 to i64
%33 = trunc i64 %32 to i32
%34 = tail call i32 @ivshmem_send(i32 %25, i32 %33, i64 %23)
%35 = tail call i32 @close(i32 %25)
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
ret i32 0
}