#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__ ENOTTY;
int /*<<< orphan*/ FIGETBSZ;
int /*<<< orphan*/ FIOCLEX;
int /*<<< orphan*/ FIONBIO;
int /*<<< orphan*/ FS_IOC_GETVERSION;
int /*<<< orphan*/ FS_IOC_SETVERSION;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int ioctl(int, int /*<<< orphan*/, ...);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int main(int argc, char **argv) {
  int fd;
  int rc;
  int val = 0;
  fd = open(argv[1], O_RDONLY, 0);
  if (fd == -1) {
    perror("test_ioctl:open");
    exit(1);
  }
  rc = ioctl(fd, FIGETBSZ, &val);
  if (rc < 0) {
    perror("test_ioctl:FIGETBSZ");
    exit(1);
  }
  rc = ioctl(fd, FIOCLEX);
  if (rc < 0) {
    perror("test_ioctl:FIOCLEX");
    exit(1);
  }
  rc = ioctl(fd, FIONBIO, &val);
  if (rc < 0) {
    perror("test_ioctl:FIONBIO");
    exit(1);
  }
  rc = ioctl(fd, FS_IOC_GETVERSION, &val);
  if (rc < 0 && errno != ENOTTY) {
    perror("test_ioctl:FS_IOC_GETVERSION");
    exit(1);
  }
  val = 0;
  rc = ioctl(fd, FS_IOC_SETVERSION, &val);
  if (rc < 0 && errno != ENOTTY) {
    perror("test_ioctl:FS_IOC_SETVERSION");
    exit(1);
  }
  close(fd);
  exit(0);
}


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)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a8 <main+0x58>
lea    0xe67(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
mov    $0x0,%al
call   1030 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11e2 <main+0x92>
lea    0xe3d(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1218 <main+0xc8>
lea    0xe1b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
mov    $0x0,%al
call   1030 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1252 <main+0x102>
lea    0xdf4(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
mov    $0x0,%al
call   1030 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12a6 <main+0x156>
mov    0x2d3b(%rip),%rax
mov    (%rax),%rax
mov    0x2d59(%rip),%rcx
cmp    (%rcx),%rax
je     12a6 <main+0x156>
lea    0xdb3(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d11(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
mov    $0x0,%al
call   1030 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1301 <main+0x1b1>
mov    0x2ce0(%rip),%rax
mov    (%rax),%rax
mov    0x2cfe(%rip),%rcx
cmp    (%rcx),%rax
je     1301 <main+0x1b1>
lea    0xd75(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
mov    0x8(%rsi),%rdi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
jne    117f <main+0x2f>
lea    0xe83(%rip),%rdi
jmp    11e6 <main+0x96>
mov    %eax,%ebx
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <ioctl@plt>
test   %eax,%eax
jns    11a5 <main+0x55>
lea    0xe6d(%rip),%rdi
jmp    11e6 <main+0x96>
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <ioctl@plt>
test   %eax,%eax
jns    11c4 <main+0x74>
lea    0xe62(%rip),%rdi
jmp    11e6 <main+0x96>
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <ioctl@plt>
test   %eax,%eax
jns    11f5 <main+0xa5>
lea    0xe51(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <ioctl@plt>
test   %eax,%eax
jns    1226 <main+0xd6>
mov    0x2da1(%rip),%rax
mov    (%rax),%rax
mov    0x2dbf(%rip),%rcx
cmp    (%rcx),%rax
jne    126d <main+0x11d>
movl   $0x0,0xc(%rsp)
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <ioctl@plt>
test   %eax,%eax
jns    125f <main+0x10f>
mov    0x2d68(%rip),%rax
mov    (%rax),%rax
mov    0x2d86(%rip),%rcx
cmp    (%rcx),%rax
jne    1279 <main+0x129>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xdd6(%rip),%rdi
jmp    11e6 <main+0x96>
lea    0xde7(%rip),%rdi
jmp    11e6 <main+0x96>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [16 x i8] c"test_ioctl:open\00"
@FIGETBSZ = global i32 0
@.str.1 = [20 x i8] c"test_ioctl:FIGETBSZ\00"
@FIOCLEX = global i32 0
@.str.2 = [19 x i8] c"test_ioctl:FIOCLEX\00"
@FIONBIO = global i32 0
@.str.3 = [19 x i8] c"test_ioctl:FIONBIO\00"
@FS_IOC_GETVERSION = global i32 0
@errno = global i64 0
@ENOTTY = global i64 0
@.str.4 = [29 x i8] c"test_ioctl:FS_IOC_GETVERSION\00"
@FS_IOC_SETVERSION = global i32 0
@.str.5 = [29 x i8] c"test_ioctl:FS_IOC_SETVERSION\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12, i32 0)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i32, i32* %6
%21 = load i32, i32* @FIGETBSZ
%22 = call i32 (i32, i32, ...) @ioctl(i32 %20, i32 %21, i32* %8)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %19
%29 = load i32, i32* %6
%30 = load i32, i32* @FIOCLEX
%31 = call i32 (i32, i32, ...) @ioctl(i32 %29, i32 %30)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %28
%38 = load i32, i32* %6
%39 = load i32, i32* @FIONBIO
%40 = call i32 (i32, i32, ...) @ioctl(i32 %38, i32 %39, i32* %8)
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %37
%44 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %37
%47 = load i32, i32* %6
%48 = load i32, i32* @FS_IOC_GETVERSION
%49 = call i32 (i32, i32, ...) @ioctl(i32 %47, i32 %48, i32* %8)
store i32 %49, i32* %7
%50 = load i32, i32* %7
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %59
52: ; preds = %46
%53 = load i64, i64* @errno
%54 = load i64, i64* @ENOTTY
%55 = icmp ne i64 %53, %54
br i1 %55, label %56, label %59
56: ; preds = %52
%57 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %52, %46
store i32 0, i32* %8
%60 = load i32, i32* %6
%61 = load i32, i32* @FS_IOC_SETVERSION
%62 = call i32 (i32, i32, ...) @ioctl(i32 %60, i32 %61, i32* %8)
store i32 %62, i32* %7
%63 = load i32, i32* %7
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %72
65: ; preds = %59
%66 = load i64, i64* @errno
%67 = load i64, i64* @ENOTTY
%68 = icmp ne i64 %66, %67
br i1 %68, label %69, label %72
69: ; preds = %65
%70 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
%71 = call i32 @exit(i32 1)
unreachable
72: ; preds = %65, %59
%73 = load i32, i32* %6
%74 = call i32 @close(i32 %73)
%75 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [16 x i8] c"test_ioctl:open\00"
@FIGETBSZ = global i32 0
@.str.1 = [20 x i8] c"test_ioctl:FIGETBSZ\00"
@FIOCLEX = global i32 0
@.str.2 = [19 x i8] c"test_ioctl:FIOCLEX\00"
@FIONBIO = global i32 0
@.str.3 = [19 x i8] c"test_ioctl:FIONBIO\00"
@FS_IOC_GETVERSION = global i32 0
@errno = global i64 0
@ENOTTY = global i64 0
@.str.4 = [29 x i8] c"test_ioctl:FS_IOC_GETVERSION\00"
@FS_IOC_SETVERSION = global i32 0
@.str.5 = [29 x i8] c"test_ioctl:FS_IOC_SETVERSION\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i32, i32* @FIGETBSZ
%15 = call i32 (i32, i32, ...) @ioctl(i32 %8, i32 %14, i32* %3)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = load i32, i32* @FIOCLEX
%22 = call i32 (i32, i32, ...) @ioctl(i32 %8, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %20
%28 = load i32, i32* @FIONBIO
%29 = call i32 (i32, i32, ...) @ioctl(i32 %8, i32 %28, i32* %3)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %27
%35 = load i32, i32* @FS_IOC_GETVERSION
%36 = call i32 (i32, i32, ...) @ioctl(i32 %8, i32 %35, i32* %3)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %45
38: ; preds = %34
%39 = load i64, i64* @errno
%40 = load i64, i64* @ENOTTY
%41 = icmp eq i64 %39, %40
br i1 %41, label %45, label %42
42: ; preds = %38
%43 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %38, %34
store i32 0, i32* %3
%46 = load i32, i32* @FS_IOC_SETVERSION
%47 = call i32 (i32, i32, ...) @ioctl(i32 %8, i32 %46, i32* %3)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %56
49: ; preds = %45
%50 = load i64, i64* @errno
%51 = load i64, i64* @ENOTTY
%52 = icmp eq i64 %50, %51
br i1 %52, label %56, label %53
53: ; preds = %49
%54 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %49, %45
%57 = call i32 @close(i32 %8)
%58 = call i32 @exit(i32 0)
unreachable
}