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

/* Variables and functions */
int /*<<< orphan*/ EVIOCGBIT(int /*<<< orphan*/, int);
int /*<<< orphan*/ EVIOCGNAME(int);
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*/, ...);
int openat(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strncmp(char *, 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);
    char name[128] = {0};
    err = ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name);
    assert(err >= 0);
    unsigned long ev_bits[8] = {0};
    err = ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits);
    assert(err >= 0);
    if (strncmp(name, "synaptics", 9) == 0 && ev_bits[0] == 0xb) {
      ret = fd;
      break;
    }
    close(fd);
  }
  closedir(dir);
  return ret;
}


x86 assembly (clang, -O0):
<find_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
movl   $0xffffffff,-0x8(%rbp)
lea    0xe27(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%edi
call   10c0 <assert@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   10d0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1351 <find_dev+0x191>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xdf1(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1233 <find_dev+0x73>
jmp    11f5 <find_dev+0x35>
mov    -0x10(%rbp),%rdi
call   10b0 <dirfd@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    0x2d9c(%rip),%rax
mov    (%rax),%edx
call   1060 <openat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   10c0 <assert@plt>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1070 <memset@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0xe8(%rbp)
mov    $0x7f,%edi
call   10e0 <EVIOCGNAME@plt>
mov    -0xe8(%rbp),%edi
mov    %eax,%esi
lea    -0xa0(%rbp),%rdx
mov    $0x0,%al
call   1080 <ioctl@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   10c0 <assert@plt>
lea    -0xe0(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1070 <memset@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0xe4(%rbp)
xor    %edi,%edi
mov    $0x40,%esi
call   1040 <EVIOCGBIT@plt>
mov    -0xe4(%rbp),%edi
mov    %eax,%esi
lea    -0xe0(%rbp),%rdx
mov    $0x0,%al
call   1080 <ioctl@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   10c0 <assert@plt>
lea    -0xa0(%rbp),%rdi
lea    0xcfa(%rip),%rsi
mov    $0x9,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1344 <find_dev+0x184>
cmpq   $0xb,-0xe0(%rbp)
0b
jne    1344 <find_dev+0x184>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    1351 <find_dev+0x191>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
jmp    11f5 <find_dev+0x35>
mov    -0x10(%rbp),%rdi
call   10a0 <closedir@plt>
mov    -0x8(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_dev>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xc8,%rsp
lea    0xe38(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,%rbx
mov    %ebx,%edi
call   10b0 <assert@plt>
mov    %rbx,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     1328 <find_dev+0x178>
mov    %rax,%rbp
lea    0xe19(%rip),%r14
lea    0x40(%rsp),%r15
mov    %rsp,%r12
lea    0xe10(%rip),%r13
jmp    122b <find_dev+0x7b>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
call   1080 <close@plt>
mov    %rbx,%rdi
call   10c0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1328 <find_dev+0x178>
mov    0x0(%rbp),%rdi
mov    %r14,%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1217 <find_dev+0x67>
mov    %rbx,%rdi
call   10a0 <dirfd@plt>
mov    0x0(%rbp),%rsi
mov    0x2d94(%rip),%rcx
mov    (%rcx),%edx
mov    %eax,%edi
call   1060 <openat@plt>
mov    %eax,%ebp
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   10b0 <assert@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
mov    $0x7f,%edi
call   10d0 <EVIOCGNAME@plt>
mov    %ebp,%edi
mov    %eax,%esi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <ioctl@plt>
not    %eax
shr    $0x1f,%eax
mov    %eax,%edi
call   10b0 <assert@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
xor    %edi,%edi
mov    $0x40,%esi
call   1040 <EVIOCGBIT@plt>
mov    %ebp,%edi
mov    %eax,%esi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <ioctl@plt>
not    %eax
shr    $0x1f,%eax
mov    %eax,%edi
call   10b0 <assert@plt>
mov    %r15,%rdi
mov    %r13,%rsi
mov    $0x9,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1210 <find_dev+0x60>
cmpq   $0xb,(%rsp)
jne    1210 <find_dev+0x60>
jmp    132d <find_dev+0x17d>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1090 <closedir@plt>
mov    %ebp,%eax
add    $0xc8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [11 x i8] c"/dev/input\00"
@.str.1 = [6 x i8] c"event\00"
@O_RDONLY = global i32 0
@.str.2 = [10 x i8] c"synaptics\00"
define i32 @find_dev() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca i32
%6 = alloca [128 x i8]
%7 = alloca [8 x i64]
store i32 -1, i32* %2
%8 = call i32* @opendir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = ptrtoint i32* %9 to i32
%11 = call i32 @assert(i32 %10)
store %struct.dirent* null, %struct.dirent** %4
br label %12
12: ; preds = %61, %22, %0
%13 = load i32*, i32** %3
%14 = call %struct.dirent* @readdir(i32* %13)
store %struct.dirent* %14, %struct.dirent** %4
%15 = icmp ne %struct.dirent* %14, null
br i1 %15, label %16, label %64
16: ; preds = %12
%17 = load %struct.dirent*, %struct.dirent** %4
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = call i64 @strncmp(i8* %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %16
br label %12
23: ; preds = %16
%24 = load i32*, i32** %3
%25 = call i32 @dirfd(i32* %24)
%26 = load %struct.dirent*, %struct.dirent** %4
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = load i32, i32* @O_RDONLY
%30 = call i32 @openat(i32 %25, i8* %28, i32 %29)
store i32 %30, i32* %5
%31 = load i32, i32* %5
%32 = icmp sge i32 %31, 0
%33 = zext i1 %32 to i32
%34 = call i32 @assert(i32 %33)
%35 = bitcast [128 x i8]* %6 to i8*
%36 = load i32, i32* %5
%37 = call i32 @EVIOCGNAME(i32 127)
%38 = call i32 (i32, i32, ...) @ioctl(i32 %36, i32 %37, [128 x i8]* %6)
store i32 %38, i32* %1
%39 = load i32, i32* %1
%40 = icmp sge i32 %39, 0
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
%43 = bitcast [8 x i64]* %7 to i8*
%44 = load i32, i32* %5
%45 = call i32 @EVIOCGBIT(i32 0, i32 64)
%46 = getelementptr inbounds [8 x i64], [8 x i64]* %7, i64 0, i64 0
%47 = call i32 (i32, i32, ...) @ioctl(i32 %44, i32 %45, i64* %46)
store i32 %47, i32* %1
%48 = load i32, i32* %1
%49 = icmp sge i32 %48, 0
%50 = zext i1 %49 to i32
%51 = call i32 @assert(i32 %50)
%52 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%53 = call i64 @strncmp(i8* %52, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 9)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %61
55: ; preds = %23
%56 = getelementptr inbounds [8 x i64], [8 x i64]* %7, i64 0, i64 0
%57 = load i64, i64* %56
%58 = icmp eq i64 %57, 11
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = load i32, i32* %5
store i32 %60, i32* %2
br label %64
61: ; preds = %55, %23
%62 = load i32, i32* %5
%63 = call i32 @close(i32 %62)
br label %12
64: ; preds = %59, %12
%65 = load i32*, i32** %3
%66 = call i32 @closedir(i32* %65)
%67 = load i32, i32* %2
ret i32 %67
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [11 x i8] c"/dev/input\00"
@.str.1 = [6 x i8] c"event\00"
@O_RDONLY = global i32 0
@.str.2 = [10 x i8] c"synaptics\00"
define i32 @find_dev() {
%1 = alloca [128 x i8]
%2 = alloca [8 x i64]
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%4 = ptrtoint i32* %3 to i64
%5 = trunc i64 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = call %struct.dirent* @readdir(i32* %3)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %48, label %9
9: ; preds = %0
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%11 = bitcast [8 x i64]* %2 to i8*
%12 = getelementptr inbounds [8 x i64], [8 x i64]* %2, i64 0, i64 0
br label %13
13: ; preds = %9, %19
%14 = phi %struct.dirent* [ %7, %9 ], [ %20, %19 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = call i64 @strncmp(i8* %16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %13, %46
%20 = call %struct.dirent* @readdir(i32* %3)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %48, label %13
22: ; preds = %13
%23 = call i32 @dirfd(i32* %3)
%24 = load i8*, i8** %15
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @openat(i32 %23, i8* %24, i32 %25)
%27 = xor i32 %26, -1
%28 = lshr i32 %27, 31
%29 = call i32 @assert(i32 %28)
%30 = call i32 @EVIOCGNAME(i32 127)
%31 = call i32 (i32, i32, ...) @ioctl(i32 %26, i32 %30, [128 x i8]* %1)
%32 = xor i32 %31, -1
%33 = lshr i32 %32, 31
%34 = call i32 @assert(i32 %33)
%35 = call i32 @EVIOCGBIT(i32 0, i32 64)
%36 = call i32 (i32, i32, ...) @ioctl(i32 %26, i32 %35, i64* %12)
%37 = xor i32 %36, -1
%38 = lshr i32 %37, 31
%39 = call i32 @assert(i32 %38)
%40 = call i64 @strncmp(i8* %10, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 9)
%41 = icmp eq i64 %40, 0
%42 = load i64, i64* %12
%43 = icmp eq i64 %42, 11
%44 = select i1 %41, i1 %43, i1 false
br i1 %44, label %45, label %46
45: ; preds = %22
br label %48
46: ; preds = %22
%47 = call i32 @close(i32 %26)
br label %19
48: ; preds = %19, %0, %45
%49 = phi i32 [ %26, %45 ], [ -1, %0 ], [ -1, %19 ]
%50 = call i32 @closedir(i32* %3)
ret i32 %49
}