#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 */
scalar_t__ EINVAL;
int EXIT_FAILURE;
int EXIT_SUCCESS;
unsigned long LOOP_GET_STATUS;
unsigned long LOOP_GET_STATUS64;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int ioctl(int, unsigned long, int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ usage(char *);
int main(int argc, char **argv) {
  unsigned long cmd;
  int ret;
  int fd;
  if (argc != 2 && argc != 3)
    usage(argv[0]);
  if (argc == 3) {
    if (strcmp(argv[2], "64") == 0)
      cmd = LOOP_GET_STATUS64;
    else
      usage(argv[0]);
  } else {
    cmd = LOOP_GET_STATUS;
  }
  fd = open(argv[1], O_RDONLY);
  if (fd == -1) {
    perror("open");
    return EXIT_FAILURE;
  }
  ret = ioctl(fd, cmd, NULL);
  if (ret == -1) {
    if (errno == EINVAL) {
      printf("Got EINVAL\n");
    } else {
      perror("ioctl");
      close(fd);
      return EXIT_FAILURE;
    }
  } else {
    printf("Did not get EINVAL\n");
  }
  close(fd);
  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)
cmpl   $0x2,-0x8(%rbp)
je     11a6 <main+0x36>
cmpl   $0x3,-0x8(%rbp)
je     11a6 <main+0x36>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <usage@plt>
cmpl   $0x3,-0x8(%rbp)
jne    11f2 <main+0x82>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe41(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11e1 <main+0x71>
mov    0x2e03(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11ed <main+0x7d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <usage@plt>
jmp    1200 <main+0x90>
mov    0x2def(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1240 <main+0xd0>
lea    0xdd9(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d2 <main+0x162>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12b2 <main+0x142>
mov    0x2d64(%rip),%rax
mov    (%rax),%rax
mov    0x2d4a(%rip),%rcx
cmp    (%rcx),%rax
jne    1288 <main+0x118>
lea    0xd8a(%rip),%rdi
call   1030 <printf@plt>
jmp    12ad <main+0x13d>
lea    0xd85(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d2 <main+0x162>
jmp    12be <main+0x14e>
lea    0xd61(%rip),%rdi
call   1030 <printf@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    0x2ce3(%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   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %edi,%eax
and    $0xfffffffe,%eax
cmp    $0x2,%eax
je     118b <main+0x1b>
mov    (%rbx),%rdi
call   1070 <usage@plt>
cmp    $0x3,%ebp
jne    11af <main+0x3f>
mov    0x10(%rbx),%rdi
lea    0xe65(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11b8 <main+0x48>
mov    (%rbx),%rdi
call   1070 <usage@plt>
jmp    11c2 <main+0x52>
mov    0x2e32(%rip),%rax
jmp    11bf <main+0x4f>
mov    0x2e19(%rip),%rax
mov    (%rax),%r14
mov    0x8(%rbx),%rdi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     122c <main+0xbc>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11f5 <main+0x85>
lea    0xe1b(%rip),%rdi
jmp    1212 <main+0xa2>
mov    0x2dcc(%rip),%rax
mov    (%rax),%rax
mov    0x2db2(%rip),%rcx
cmp    (%rcx),%rax
jne    1241 <main+0xd1>
lea    0xe0f(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdd0(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d81(%rip),%rax
jmp    1225 <main+0xb5>
lea    0xdc0(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2d65(%rip),%rax
jmp    1225 <main+0xb5>

LLVM IR (-O0):
@.str = [3 x i8] c"64\00"
@LOOP_GET_STATUS64 = global i64 0
@LOOP_GET_STATUS = global i64 0
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@.str.2 = [12 x i8] c"Got EINVAL\0A\00"
@.str.3 = [6 x i8] c"ioctl\00"
@.str.4 = [20 x i8] c"Did not get EINVAL\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @usage(i8* %17)
br label %19
19: ; preds = %14, %11, %2
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, 3
br i1 %21, label %22, label %36
22: ; preds = %19
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = load i64, i64* @LOOP_GET_STATUS64
store i64 %29, i64* %6
br label %35
30: ; preds = %22
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 0
%33 = load i8*, i8** %32
%34 = call i32 @usage(i8* %33)
br label %35
35: ; preds = %30, %28
br label %38
36: ; preds = %19
%37 = load i64, i64* @LOOP_GET_STATUS
store i64 %37, i64* %6
br label %38
38: ; preds = %36, %35
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = load i32, i32* @O_RDONLY
%43 = call i32 @open(i8* %41, i32 %42)
store i32 %43, i32* %8
%44 = load i32, i32* %8
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %38
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
store i32 %48, i32* %3
br label %73
49: ; preds = %38
%50 = load i32, i32* %8
%51 = load i64, i64* %6
%52 = call i32 @ioctl(i32 %50, i64 %51, i32* null)
store i32 %52, i32* %7
%53 = load i32, i32* %7
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %67
55: ; preds = %49
%56 = load i64, i64* @errno
%57 = load i64, i64* @EINVAL
%58 = icmp eq i64 %56, %57
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %66
61: ; preds = %55
%62 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%63 = load i32, i32* %8
%64 = call i32 @close(i32 %63)
%65 = load i32, i32* @EXIT_FAILURE
store i32 %65, i32* %3
br label %73
66: ; preds = %59
br label %69
67: ; preds = %49
%68 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %69
69: ; preds = %67, %66
%70 = load i32, i32* %8
%71 = call i32 @close(i32 %70)
%72 = load i32, i32* @EXIT_SUCCESS
store i32 %72, i32* %3
br label %73
73: ; preds = %69, %61, %46
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@.str = [3 x i8] c"64\00"
@LOOP_GET_STATUS64 = global i64 0
@LOOP_GET_STATUS = global i64 0
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@.str.3 = [6 x i8] c"ioctl\00"
@EXIT_SUCCESS = global i32 0
@str = [19 x i8] c"Did not get EINVAL\00"
@str.5 = [11 x i8] c"Got EINVAL\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = and i32 %0, -2
%4 = icmp eq i32 %3, 2
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 @usage(i8* %6)
br label %8
8: ; preds = %5, %2
%9 = icmp eq i32 %0, 3
br i1 %9, label %10, label %20
10: ; preds = %8
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = load i64, i64* @LOOP_GET_STATUS64
br label %22
17: ; preds = %10
%18 = load i8*, i8** %1
%19 = tail call i32 @usage(i8* %18)
br label %22
20: ; preds = %8
%21 = load i64, i64* @LOOP_GET_STATUS
br label %22
22: ; preds = %15, %17, %20
%23 = phi i64 [ %16, %15 ], [ undef, %17 ], [ %21, %20 ]
%24 = getelementptr inbounds i8*, i8** %1, i64 1
%25 = load i8*, i8** %24
%26 = load i32, i32* @O_RDONLY
%27 = tail call i32 @open(i8* %25, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %45
31: ; preds = %22
%32 = tail call i32 @ioctl(i32 %27, i64 %23, i32* null)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %41
34: ; preds = %31
%35 = load i64, i64* @errno
%36 = load i64, i64* @EINVAL
%37 = icmp eq i64 %35, %36
br i1 %37, label %41, label %38
38: ; preds = %34
%39 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%40 = tail call i32 @close(i32 %27)
br label %45
41: ; preds = %31, %34
%42 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @str.5, i64 0, i64 0), %34 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0), %31 ]
%43 = tail call i32 @puts(i8* dereferenceable(1) %42)
%44 = tail call i32 @close(i32 %27)
br label %45
45: ; preds = %41, %38, %29
%46 = phi i32* [ @EXIT_FAILURE, %29 ], [ @EXIT_SUCCESS, %41 ], [ @EXIT_FAILURE, %38 ]
%47 = load i32, i32* %46
ret i32 %47
}