#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 */
struct dirent {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ ev_bits;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int ABS_MT_POSITION_X;
int ABS_MT_POSITION_Y;
int /*<<< orphan*/ EVIOCGBIT(int /*<<< orphan*/, int);
int /*<<< orphan*/ EV_ABS;
int KEY_MAX;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ dirfd(int /*<<< orphan*/ *);
int ioctl(int, int /*<<< orphan*/, unsigned char *);
int openat(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strncmp(int /*<<< orphan*/, char *, int);
int find_dev() {
  int err;
  int ret = -1;
  DIR *dir = opendir("/dev/input");
  assert(dir);
  struct dirent *de = NULL;
  while ((de = readdir(dir))) {
    if (strncmp(de->d_name, "event", 5))
      continue;
    int fd = openat(dirfd(dir), de->d_name, O_RDONLY);
    assert(fd >= 0);
    unsigned char ev_bits[KEY_MAX / 8 + 1];
    err = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(ev_bits)), ev_bits);
    assert(err >= 0);
    if ((ev_bits[ABS_MT_POSITION_X / 8] & (1 << (ABS_MT_POSITION_X % 8))) &&
        (ev_bits[ABS_MT_POSITION_Y / 8] & (1 << (ABS_MT_POSITION_Y % 8)))) {
      ret = fd;
      break;
    }
    close(fd);
  }
  closedir(dir);
  return ret;
}


x86 assembly (clang, -O0):
<find_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0xffffffff,-0x8(%rbp)
lea    0xe4a(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%edi
call   10b0 <assert@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     138a <find_dev+0x1ea>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     120f <find_dev+0x6f>
jmp    11d2 <find_dev+0x32>
mov    -0x10(%rbp),%rdi
call   10a0 <dirfd@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    0x2dc1(%rip),%rax
mov    (%rax),%edx
call   1060 <openat@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
not    %edi
shr    $0x1f,%edi
call   10b0 <assert@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
add    $0x7,%ecx
test   %eax,%eax
cmovs  %ecx,%eax
sar    $0x3,%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%ecx
mov    %ecx,-0x44(%rbp)
mov    0x2d31(%rip),%rcx
mov    (%rcx),%edi
mov    %eax,%esi
call   1040 <EVIOCGBIT@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %eax,%esi
call   1070 <ioctl@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   10b0 <assert@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x38(%rbp)
mov    0x2cee(%rip),%rax
mov    (%rax),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    -0x38(%rbp),%eax
mov    %edx,%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     135a <find_dev+0x1ba>
mov    0x2cdc(%rip),%rax
mov    (%rax),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x48(%rbp)
mov    0x2cbb(%rip),%rax
mov    (%rax),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    -0x48(%rbp),%eax
mov    %edx,%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     135a <find_dev+0x1ba>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x8(%rbp)
movl   $0x3,-0x34(%rbp)
jmp    1369 <find_dev+0x1c9>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x34(%rbp),%eax
test   %eax,%eax
je     1385 <find_dev+0x1e5>
jmp    1380 <find_dev+0x1e0>
jmp    138a <find_dev+0x1ea>
jmp    11d2 <find_dev+0x32>
mov    -0x10(%rbp),%rdi
call   1090 <closedir@plt>
mov    -0x8(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_dev>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe4b(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,%r15
mov    %r15d,%edi
call   10b0 <assert@plt>
mov    %r15,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     12f5 <find_dev+0x155>
mov    %rax,%rbx
lea    0xe2b(%rip),%r14
jmp    120f <find_dev+0x6f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12d,%edi
call   1080 <close@plt>
mov    %r13,%rsp
mov    %r15,%rdi
call   10c0 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12f5 <find_dev+0x155>
mov    (%rbx),%edi
mov    %r14,%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11fb <find_dev+0x5b>
mov    %r15,%rdi
call   10a0 <dirfd@plt>
mov    (%rbx),%esi
mov    0x2db4(%rip),%rcx
mov    (%rcx),%edx
mov    %eax,%edi
call   1060 <openat@plt>
mov    %eax,%r12d
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   10b0 <assert@plt>
mov    %rsp,%r13
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
lea    0x7(%rax),%esi
test   %eax,%eax
cmovns %eax,%esi
sar    $0x3,%esi
add    $0x1,%esi
lea    0xf(%rsi),%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   1040 <EVIOCGBIT@plt>
mov    %r12d,%edi
mov    %eax,%esi
mov    %rbx,%rdx
call   1070 <ioctl@plt>
not    %eax
shr    $0x1f,%eax
mov    %eax,%edi
call   10b0 <assert@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%eax
lea    0x7(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x3,%ecx
movslq %ecx,%rcx
movzbl (%rcx,%rbx,1),%ecx
and    $0x7,%al
movzbl %al,%eax
bt     %eax,%ecx
jae    11f0 <find_dev+0x50>
mov    0x2d12(%rip),%rax
mov    (%rax),%eax
lea    0x7(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x3,%ecx
movslq %ecx,%rcx
movzbl (%rbx,%rcx,1),%ecx
and    $0x7,%al
movzbl %al,%eax
bt     %eax,%ecx
jae    11f0 <find_dev+0x50>
mov    %r13,%rsp
jmp    12fb <find_dev+0x15b>
mov    $0xffffffff,%r12d
mov    %r15,%rdi
call   1090 <closedir@plt>
mov    %r12d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [11 x i8] c"/dev/input\00"
@.str.1 = [6 x i8] c"event\00"
@O_RDONLY = global i32 0
@KEY_MAX = global i32 0
@EV_ABS = global i32 0
@ABS_MT_POSITION_X = global i32 0
@ABS_MT_POSITION_Y = global i32 0
define i32 @find_dev() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32 -1, i32* %2
%9 = call i32* @opendir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %3
%10 = load i32*, i32** %3
%11 = ptrtoint i32* %10 to i32
%12 = call i32 @assert(i32 %11)
store %struct.dirent* null, %struct.dirent** %4
br label %13
13: ; preds = %82, %23, %0
%14 = load i32*, i32** %3
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %4
%16 = icmp ne %struct.dirent* %15, null
br i1 %16, label %17, label %83
17: ; preds = %13
%18 = load %struct.dirent*, %struct.dirent** %4
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i64 @strncmp(i32 %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %17
br label %13
24: ; preds = %17
%25 = load i32*, i32** %3
%26 = call i32 @dirfd(i32* %25)
%27 = load %struct.dirent*, %struct.dirent** %4
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = load i32, i32* @O_RDONLY
%31 = call i32 @openat(i32 %26, i32 %29, i32 %30)
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = icmp sge i32 %32, 0
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = load i32, i32* @KEY_MAX
%37 = sdiv i32 %36, 8
%38 = add nsw i32 %37, 1
%39 = zext i32 %38 to i64
%40 = call i8* @llvm.stacksave()
store i8* %40, i8** %6
%41 = alloca i8, i64 %39
store i64 %39, i64* %7
%42 = load i32, i32* %5
%43 = load i32, i32* @EV_ABS
%44 = trunc i64 %39 to i32
%45 = call i32 @EVIOCGBIT(i32 %43, i32 %44)
%46 = call i32 @ioctl(i32 %42, i32 %45, i8* %41)
store i32 %46, i32* %1
%47 = load i32, i32* %1
%48 = icmp sge i32 %47, 0
%49 = zext i1 %48 to i32
%50 = call i32 @assert(i32 %49)
%51 = load i32, i32* @ABS_MT_POSITION_X
%52 = sdiv i32 %51, 8
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %41, i64 %53
%55 = load i8, i8* %54
%56 = zext i8 %55 to i32
%57 = load i32, i32* @ABS_MT_POSITION_X
%58 = srem i32 %57, 8
%59 = shl i32 1, %58
%60 = and i32 %56, %59
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %76
62: ; preds = %24
%63 = load i32, i32* @ABS_MT_POSITION_Y
%64 = sdiv i32 %63, 8
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %41, i64 %65
%67 = load i8, i8* %66
%68 = zext i8 %67 to i32
%69 = load i32, i32* @ABS_MT_POSITION_Y
%70 = srem i32 %69, 8
%71 = shl i32 1, %70
%72 = and i32 %68, %71
%73 = icmp ne i32 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %62
%75 = load i32, i32* %5
store i32 %75, i32* %2
store i32 3, i32* %8
br label %79
76: ; preds = %62, %24
%77 = load i32, i32* %5
%78 = call i32 @close(i32 %77)
store i32 0, i32* %8
br label %79
79: ; preds = %76, %74
%80 = load i8*, i8** %6
%81 = load i32, i32* %8
switch i32 %81, label %87 [
i32 0, label %82
i32 3, label %83
]
82: ; preds = %79
br label %13
83: ; preds = %79, %13
%84 = load i32*, i32** %3
%85 = call i32 @closedir(i32* %84)
%86 = load i32, i32* %2
ret i32 %86
87: ; preds = %79
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [11 x i8] c"/dev/input\00"
@.str.1 = [6 x i8] c"event\00"
@O_RDONLY = global i32 0
@KEY_MAX = global i32 0
@EV_ABS = global i32 0
@ABS_MT_POSITION_X = global i32 0
@ABS_MT_POSITION_Y = global i32 0
define i32 @find_dev() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%2 = ptrtoint i32* %1 to i64
%3 = trunc i64 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = call %struct.dirent* @readdir(i32* %1)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %60, label %7
7: ; preds = %0, %13
%8 = phi %struct.dirent* [ %14, %13 ], [ %5, %0 ]
%9 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i64 0, i32 0
%10 = load i32, i32* %9
%11 = call i64 @strncmp(i32 %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %7, %58
%14 = call %struct.dirent* @readdir(i32* %1)
%15 = icmp eq %struct.dirent* %14, null
br i1 %15, label %60, label %7
16: ; preds = %7
%17 = call i32 @dirfd(i32* %1)
%18 = load i32, i32* %9
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @openat(i32 %17, i32 %18, i32 %19)
%21 = xor i32 %20, -1
%22 = lshr i32 %21, 31
%23 = call i32 @assert(i32 %22)
%24 = load i32, i32* @KEY_MAX
%25 = sdiv i32 %24, 8
%26 = add nsw i32 %25, 1
%27 = zext i32 %26 to i64
%28 = call i8* @llvm.stacksave()
%29 = alloca i8, i64 %27
%30 = load i32, i32* @EV_ABS
%31 = call i32 @EVIOCGBIT(i32 %30, i32 %26)
%32 = call i32 @ioctl(i32 %20, i32 %31, i8* %29)
%33 = xor i32 %32, -1
%34 = lshr i32 %33, 31
%35 = call i32 @assert(i32 %34)
%36 = load i32, i32* @ABS_MT_POSITION_X
%37 = sdiv i32 %36, 8
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %29, i64 %38
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
%42 = and i32 %36, 7
%43 = shl nuw nsw i32 1, %42
%44 = and i32 %43, %41
%45 = icmp eq i32 %44, 0
br i1 %45, label %58, label %46
46: ; preds = %16
%47 = load i32, i32* @ABS_MT_POSITION_Y
%48 = sdiv i32 %47, 8
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %29, i64 %49
%51 = load i8, i8* %50
%52 = zext i8 %51 to i32
%53 = and i32 %47, 7
%54 = shl nuw nsw i32 1, %53
%55 = and i32 %54, %52
%56 = icmp eq i32 %55, 0
br i1 %56, label %58, label %57
57: ; preds = %46
br label %60
58: ; preds = %16, %46
%59 = call i32 @close(i32 %20)
br label %13
60: ; preds = %13, %0, %57
%61 = phi i32 [ %20, %57 ], [ -1, %0 ], [ -1, %13 ]
%62 = call i32 @closedir(i32* %1)
ret i32 %61
}