#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*/ ENOTSUP;
int /*<<< orphan*/ EVIOCGBIT(int /*<<< orphan*/, int);
int /*<<< orphan*/ EV_FF;
int FF_PERIODIC;
int FF_RUMBLE;
int O_CLOEXEC;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, unsigned long *);
int open(char const *, int);
int /*<<< orphan*/ perror(char *);
int ffmemless_evdev_file_open(const char *file_name) {
  int result, fp;
  unsigned long features[4];
  fp = open(file_name, O_RDWR | O_CLOEXEC);
  if (fp == -1) {
    perror("test file open");
    return fp;
  }
  if (ioctl(fp, EVIOCGBIT(EV_FF, sizeof(unsigned long) * 4), features) < 0) {
    perror("Ioctl query failed");
    close(fp);
    return -1;
  }
  result = ((features[((FF_RUMBLE) / (sizeof(long) * 8))] >>
             ((FF_RUMBLE) % (sizeof(long) * 8))) &
            1);
  result = result && ((features[((FF_PERIODIC) / (sizeof(long) * 8))] >>
                       ((FF_PERIODIC) % (sizeof(long) * 8))) &
                      1);
  if (result) {
    return fp;
  } else {
    close(fp);
    errno = ENOTSUP;
    return -1;
  }
}


x86 assembly (clang, -O0):
<ffmemless_evdev_file_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    119b <ffmemless_evdev_file_open+0x4b>
lea    0xe75(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1299 <ffmemless_evdev_file_open+0x149>
mov    -0x18(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    $0x20,%esi
call   1030 <EVIOCGBIT@plt>
mov    -0x44(%rbp),%edi
mov    %eax,%esi
lea    -0x40(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11ec <ffmemless_evdev_file_open+0x9c>
lea    0xe3c(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1299 <ffmemless_evdev_file_open+0x149>
mov    0x2dcd(%rip),%rax
movslq (%rax),%rax
shr    $0x6,%rax
mov    -0x40(%rbp,%rax,8),%rax
mov    0x2dba(%rip),%rcx
movslq (%rcx),%rcx
and    $0x3f,%rcx
shr    %cl,%rax
and    $0x1,%rax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x45(%rbp)
je     1258 <ffmemless_evdev_file_open+0x108>
mov    0x2d9b(%rip),%rax
movslq (%rax),%rax
shr    $0x6,%rax
mov    -0x40(%rbp,%rax,8),%rax
mov    0x2d88(%rip),%rcx
movslq (%rcx),%rcx
and    $0x3f,%rcx
shr    %cl,%rax
and    $0x1,%rax
cmp    $0x0,%rax
setne  %al
mov    %al,-0x45(%rbp)
mov    -0x45(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1278 <ffmemless_evdev_file_open+0x128>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1299 <ffmemless_evdev_file_open+0x149>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    0x2d59(%rip),%rax
mov    (%rax),%ecx
mov    0x2d28(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ffmemless_evdev_file_open>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e8c(%rip),%rax
mov    0x2e45(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11f7 <ffmemless_evdev_file_open+0xa7>
mov    %eax,%ebx
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    $0x20,%esi
call   1030 <EVIOCGBIT@plt>
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %eax,%esi
call   1040 <ioctl@plt>
test   %rax,%rax
js     1205 <ffmemless_evdev_file_open+0xb5>
mov    0x2e1e(%rip),%rax
movslq (%rax),%rax
mov    %rax,%rcx
shr    $0x6,%rcx
mov    (%rsp,%rcx,8),%rcx
bt     %rax,%rcx
jae    11d1 <ffmemless_evdev_file_open+0x81>
mov    0x2e0b(%rip),%rax
movslq (%rax),%rax
mov    %rax,%rcx
shr    $0x6,%rcx
mov    (%rsp,%rcx,8),%rcx
bt     %rax,%rcx
jb     11ef <ffmemless_evdev_file_open+0x9f>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    0x2dd0(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe02(%rip),%rdi
call   1070 <perror@plt>
jmp    11ea <ffmemless_evdev_file_open+0x9a>
lea    0xe03(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11ea <ffmemless_evdev_file_open+0x9a>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CLOEXEC = global i32 0
@.str = [15 x i8] c"test file open\00"
@EV_FF = global i32 0
@.str.1 = [19 x i8] c"Ioctl query failed\00"
@FF_RUMBLE = global i32 0
@FF_PERIODIC = global i32 0
@ENOTSUP = global i32 0
@errno = global i32 0
define i32 @ffmemless_evdev_file_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [4 x i64]
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_CLOEXEC
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %7, i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* %5
store i32 %16, i32* %2
br label %65
17: ; preds = %1
%18 = load i32, i32* %5
%19 = load i32, i32* @EV_FF
%20 = call i32 @EVIOCGBIT(i32 %19, i32 32)
%21 = getelementptr inbounds [4 x i64], [4 x i64]* %6, i64 0, i64 0
%22 = call i64 @ioctl(i32 %18, i32 %20, i64* %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* %5
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %2
br label %65
28: ; preds = %17
%29 = load i32, i32* @FF_RUMBLE
%30 = sext i32 %29 to i64
%31 = udiv i64 %30, 64
%32 = getelementptr inbounds [4 x i64], [4 x i64]* %6, i64 0, i64 %31
%33 = load i64, i64* %32
%34 = load i32, i32* @FF_RUMBLE
%35 = sext i32 %34 to i64
%36 = urem i64 %35, 64
%37 = lshr i64 %33, %36
%38 = and i64 %37, 1
%39 = trunc i64 %38 to i32
store i32 %39, i32* %4
%40 = load i32, i32* %4
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %54
42: ; preds = %28
%43 = load i32, i32* @FF_PERIODIC
%44 = sext i32 %43 to i64
%45 = udiv i64 %44, 64
%46 = getelementptr inbounds [4 x i64], [4 x i64]* %6, i64 0, i64 %45
%47 = load i64, i64* %46
%48 = load i32, i32* @FF_PERIODIC
%49 = sext i32 %48 to i64
%50 = urem i64 %49, 64
%51 = lshr i64 %47, %50
%52 = and i64 %51, 1
%53 = icmp ne i64 %52, 0
br label %54
54: ; preds = %42, %28
%55 = phi i1 [ false, %28 ], [ %53, %42 ]
%56 = zext i1 %55 to i32
store i32 %56, i32* %4
%57 = load i32, i32* %4
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %54
%60 = load i32, i32* %5
store i32 %60, i32* %2
br label %65
61: ; preds = %54
%62 = load i32, i32* %5
%63 = call i32 @close(i32 %62)
%64 = load i32, i32* @ENOTSUP
store i32 %64, i32* @errno
store i32 -1, i32* %2
br label %65
65: ; preds = %61, %59, %24, %14
%66 = load i32, i32* %2
ret i32 %66
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CLOEXEC = global i32 0
@.str = [15 x i8] c"test file open\00"
@EV_FF = global i32 0
@.str.1 = [19 x i8] c"Ioctl query failed\00"
@FF_RUMBLE = global i32 0
@FF_PERIODIC = global i32 0
@ENOTSUP = global i32 0
@errno = global i32 0
define i32 @ffmemless_evdev_file_open(i8* nocapture %0) {
%2 = alloca [4 x i64]
%3 = bitcast [4 x i64]* %2 to i8*
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CLOEXEC
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %43
11: ; preds = %1
%12 = load i32, i32* @EV_FF
%13 = tail call i32 @EVIOCGBIT(i32 %12, i32 32)
%14 = getelementptr inbounds [4 x i64], [4 x i64]* %2, i64 0, i64 0
%15 = call i64 @ioctl(i32 %7, i32 %13, i64* %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @close(i32 %7)
br label %43
20: ; preds = %11
%21 = load i32, i32* @FF_RUMBLE
%22 = sext i32 %21 to i64
%23 = lshr i64 %22, 6
%24 = getelementptr inbounds [4 x i64], [4 x i64]* %2, i64 0, i64 %23
%25 = load i64, i64* %24
%26 = and i64 %22, 63
%27 = shl nuw i64 1, %26
%28 = and i64 %27, %25
%29 = icmp eq i64 %28, 0
br i1 %29, label %40, label %30
30: ; preds = %20
%31 = load i32, i32* @FF_PERIODIC
%32 = sext i32 %31 to i64
%33 = lshr i64 %32, 6
%34 = getelementptr inbounds [4 x i64], [4 x i64]* %2, i64 0, i64 %33
%35 = load i64, i64* %34
%36 = and i64 %32, 63
%37 = shl nuw i64 1, %36
%38 = and i64 %37, %35
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %20, %30
%41 = call i32 @close(i32 %7)
%42 = load i32, i32* @ENOTSUP
store i32 %42, i32* @errno
br label %43
43: ; preds = %30, %40, %17, %9
%44 = phi i32 [ -1, %9 ], [ -1, %17 ], [ -1, %40 ], [ %7, %30 ]
ret i32 %44
}