#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*/ EVIOCGBIT(int /*<<< orphan*/, int);
int /*<<< orphan*/ EV_FF;
int FF_PERIODIC;
int FF_RUMBLE;
int O_CLOEXEC;
int O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, unsigned long *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int ffmemless_evdev_file_search(void) {
  int result, i = 0;
  int fp = 1;
  char device_file_name[24];
  unsigned long features[4];
  while (fp && i < 256) {
    sprintf(device_file_name, "/dev/input/event%d", i);
    fp = open(device_file_name, O_RDWR | O_CLOEXEC);
    if (fp == -1) {
      perror("test file open");
      break;
    }
    if (ioctl(fp, EVIOCGBIT(EV_FF, sizeof(unsigned long) * 4), features) < 0) {
      perror("Ioctl query failed");
      close(fp);
      i++;
      continue;
    }
    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;
    close(fp);
    i++;
  }
  return -1;
}


x86 assembly (clang, -O0):
<ffmemless_evdev_file_search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x10(%rbp)
mov    %al,-0x51(%rbp)
je     1192 <ffmemless_evdev_file_search+0x32>
cmpl   $0x100,-0xc(%rbp)
setl   %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    11a2 <ffmemless_evdev_file_search+0x42>
jmp    12e3 <ffmemless_evdev_file_search+0x183>
lea    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe50(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    0x2def(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11ee <ffmemless_evdev_file_search+0x8e>
lea    0xe2f(%rip),%rdi
call   1070 <perror@plt>
jmp    12e3 <ffmemless_evdev_file_search+0x183>
mov    -0x10(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    $0x20,%esi
call   1030 <EVIOCGBIT@plt>
mov    -0x58(%rbp),%edi
mov    %eax,%esi
lea    -0x50(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1241 <ffmemless_evdev_file_search+0xe1>
lea    0xdfc(%rip),%rdi
call   1070 <perror@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1176 <ffmemless_evdev_file_search+0x16>
mov    0x2d80(%rip),%rax
movslq (%rax),%rax
shr    $0x6,%rax
mov    -0x50(%rbp,%rax,8),%rax
mov    0x2d6d(%rip),%rcx
movslq (%rcx),%rcx
and    $0x3f,%rcx
shr    %cl,%rax
and    $0x1,%rax
mov    %eax,-0x8(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x8(%rbp)
mov    %al,-0x59(%rbp)
je     12ad <ffmemless_evdev_file_search+0x14d>
mov    0x2d4e(%rip),%rax
movslq (%rax),%rax
shr    $0x6,%rax
mov    -0x50(%rbp,%rax,8),%rax
mov    0x2d3b(%rip),%rcx
movslq (%rcx),%rcx
and    $0x3f,%rcx
shr    %cl,%rax
and    $0x1,%rax
cmp    $0x0,%rax
setne  %al
mov    %al,-0x59(%rbp)
mov    -0x59(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     12cd <ffmemless_evdev_file_search+0x16d>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ea <ffmemless_evdev_file_search+0x18a>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1176 <ffmemless_evdev_file_search+0x16>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ffmemless_evdev_file_search>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
xor    %ebx,%ebx
lea    0x20(%rsp),%r15
mov    0x2e3c(%rip),%r14
mov    0x2e55(%rip),%r13
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
lea    0xe66(%rip),%rsi
mov    %ebx,%edx
call   1080 <sprintf@plt>
mov    (%r14),%esi
mov    0x2e3d(%rip),%rax
or     (%rax),%esi
mov    %r15,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1246 <ffmemless_evdev_file_search+0xe6>
mov    %eax,%ebp
mov    0x0(%r13),%edi
mov    $0x20,%esi
call   1030 <EVIOCGBIT@plt>
mov    %ebp,%edi
mov    %eax,%esi
mov    %r12,%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
js     1238 <ffmemless_evdev_file_search+0xd8>
mov    0x2de2(%rip),%rax
movslq (%rax),%rax
mov    %rax,%rcx
shr    $0x6,%rcx
mov    (%rsp,%rcx,8),%rcx
bt     %rax,%rcx
jae    1215 <ffmemless_evdev_file_search+0xb5>
mov    0x2dcf(%rip),%rax
movslq (%rax),%rax
mov    %rax,%rcx
shr    $0x6,%rcx
mov    (%rsp,%rcx,8),%rcx
bt     %rax,%rcx
jb     1257 <ffmemless_evdev_file_search+0xf7>
mov    %ebp,%edi
call   1050 <close@plt>
test   %ebp,%ebp
mov    $0xffffffff,%ebp
je     1257 <ffmemless_evdev_file_search+0xf7>
lea    0x1(%rbx),%eax
cmp    $0xff,%ebx
mov    %eax,%ebx
jb     1190 <ffmemless_evdev_file_search+0x30>
jmp    1257 <ffmemless_evdev_file_search+0xf7>
lea    0xde3(%rip),%rdi
call   1070 <perror@plt>
jmp    1215 <ffmemless_evdev_file_search+0xb5>
lea    0xdc6(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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