#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*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int PATH_MAX;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
size_t read(int, char *, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
scalar_t__ strstr(char *, char *);
int find_external_mice() {
  struct dirent *input_dir_entry;
  DIR *input_dir;
  input_dir = opendir("/sys/class/input");
  if (!input_dir)
    return 0;
  while ((input_dir_entry = readdir(input_dir)) != NULL) {
    if (strstr(input_dir_entry->d_name, "mouse")) {
      char buf[4096];
      char devNamePath[PATH_MAX];
      int fd;
      size_t count;
      snprintf(devNamePath, PATH_MAX, "/sys/class/input/%s/device/name",
               input_dir_entry->d_name);
      fd = open(devNamePath, O_RDONLY);
      if (fd < 0)
        continue;
      count = read(fd, buf, 4096);
      if (count < 0) {
        close(fd);
        continue;
      }
      buf[count] = 0;
      close(fd);
      if (!strstr(buf, "Synaptics"))
        return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<find_external_mice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
lea    0xe7e(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a2 <find_external_mice+0x32>
movl   $0x0,-0x4(%rbp)
jmp    134d <find_external_mice+0x1dd>
jmp    11a7 <find_external_mice+0x37>
mov    -0x18(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1346 <find_external_mice+0x1d6>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe45(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     1341 <find_external_mice+0x1d1>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x1028(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x1050(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x1030(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
lea    0xdf0(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x1050(%rbp),%rdi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1034(%rbp)
cmpl   $0x0,-0x1034(%rbp)
jge    1263 <find_external_mice+0xf3>
movl   $0x2,-0x1044(%rbp)
jmp    1300 <find_external_mice+0x190>
mov    -0x1034(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %rax,-0x1040(%rbp)
cmpq   $0x0,-0x1040(%rbp)
jae    12a9 <find_external_mice+0x139>
mov    -0x1034(%rbp),%edi
call   1050 <close@plt>
movl   $0x2,-0x1044(%rbp)
jmp    1300 <find_external_mice+0x190>
mov    -0x1040(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
mov    -0x1034(%rbp),%edi
call   1050 <close@plt>
lea    -0x1020(%rbp),%rdi
lea    0xd66(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
jne    12f6 <find_external_mice+0x186>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x1044(%rbp)
jmp    1300 <find_external_mice+0x190>
movl   $0x0,-0x1044(%rbp)
mov    -0x1028(%rbp),%rax
mov    %rax,%rsp
mov    -0x1044(%rbp),%eax
mov    %eax,-0x1054(%rbp)
test   %eax,%eax
je     133c <find_external_mice+0x1cc>
jmp    1323 <find_external_mice+0x1b3>
mov    -0x1054(%rbp),%eax
sub    $0x1,%eax
je     134d <find_external_mice+0x1dd>
jmp    1337 <find_external_mice+0x1c7>
jmp    11a7 <find_external_mice+0x37>
jmp    1341 <find_external_mice+0x1d1>
jmp    11a7 <find_external_mice+0x37>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_external_mice>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
lea    0xe75(%rip),%rdi
call   1030 <opendir@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1279 <find_external_mice+0x109>
mov    %rax,%r12
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1279 <find_external_mice+0x109>
mov    %rax,%r14
mov    0x2e37(%rip),%r13
jmp    11d7 <find_external_mice+0x67>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rsp
mov    %r12,%rdi
call   1070 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     1277 <find_external_mice+0x107>
mov    (%r14),%rdi
lea    0xe30(%rip),%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     11c3 <find_external_mice+0x53>
mov    %rsp,%r15
mov    0x0(%r13),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x0(%r13),%esi
mov    (%r14),%rcx
mov    %rbx,%rdi
lea    0xe03(%rip),%rdx
call   1040 <snprintf@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11c0 <find_external_mice+0x50>
mov    %eax,%ebx
mov    %eax,%edi
lea    -0x1030(%rbp),%r14
mov    %r14,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
movb   $0x0,-0x1030(%rbp,%rax,1)
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r14,%rdi
lea    0xdd8(%rip),%rsi
call   1090 <strstr@plt>
test   %rax,%rax
mov    %r15,%rsp
jne    11c3 <find_external_mice+0x53>
mov    $0x1,%ebx
jmp    1279 <find_external_mice+0x109>
xor    %ebx,%ebx
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [17 x i8] c"/sys/class/input\00"
@.str.1 = [6 x i8] c"mouse\00"
@PATH_MAX = global i32 0
@.str.2 = [32 x i8] c"/sys/class/input/%s/device/name\00"
@O_RDONLY = global i32 0
@.str.3 = [10 x i8] c"Synaptics\00"
define i32 @find_external_mice() {
%1 = alloca i32
%2 = alloca %struct.dirent*
%3 = alloca i32*
%4 = alloca [4096 x i8]
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32
%10 = call i32* @opendir(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %3
%11 = load i32*, i32** %3
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %0
store i32 0, i32* %1
br label %65
14: ; preds = %0
br label %15
15: ; preds = %63, %59, %14
%16 = load i32*, i32** %3
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %2
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %19, label %64
19: ; preds = %15
%20 = load %struct.dirent*, %struct.dirent** %2
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i64 @strstr(i8* %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %63
25: ; preds = %19
%26 = load i32, i32* @PATH_MAX
%27 = zext i32 %26 to i64
%28 = call i8* @llvm.stacksave()
store i8* %28, i8** %5
%29 = alloca i8, i64 %27
store i64 %27, i64* %6
%30 = load i32, i32* @PATH_MAX
%31 = load %struct.dirent*, %struct.dirent** %2
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 0
%33 = load i8*, i8** %32
%34 = call i32 @snprintf(i8* %29, i32 %30, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = load i32, i32* @O_RDONLY
%36 = call i32 @open(i8* %29, i32 %35)
store i32 %36, i32* %7
%37 = load i32, i32* %7
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %25
store i32 2, i32* %9
br label %59
40: ; preds = %25
%41 = load i32, i32* %7
%42 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%43 = call i64 @read(i32 %41, i8* %42, i32 4096)
store i64 %43, i64* %8
%44 = load i64, i64* %8
%45 = icmp ult i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = load i32, i32* %7
%48 = call i32 @close(i32 %47)
store i32 2, i32* %9
br label %59
49: ; preds = %40
%50 = load i64, i64* %8
%51 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %50
store i8 0, i8* %51
%52 = load i32, i32* %7
%53 = call i32 @close(i32 %52)
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%55 = call i64 @strstr(i8* %54, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%56 = icmp ne i64 %55, 0
br i1 %56, label %58, label %57
57: ; preds = %49
store i32 1, i32* %1
store i32 1, i32* %9
br label %59
58: ; preds = %49
store i32 0, i32* %9
br label %59
59: ; preds = %58, %57, %46, %39
%60 = load i8*, i8** %5
%61 = load i32, i32* %9
switch i32 %61, label %67 [
i32 0, label %62
i32 2, label %15
i32 1, label %65
]
62: ; preds = %59
br label %63
63: ; preds = %62, %19
br label %15
64: ; preds = %15
store i32 0, i32* %1
br label %65
65: ; preds = %64, %59, %13
%66 = load i32, i32* %1
ret i32 %66
67: ; preds = %59
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [17 x i8] c"/sys/class/input\00"
@.str.1 = [6 x i8] c"mouse\00"
@PATH_MAX = global i32 0
@.str.2 = [32 x i8] c"/sys/class/input/%s/device/name\00"
@O_RDONLY = global i32 0
@.str.3 = [10 x i8] c"Synaptics\00"
define i32 @find_external_mice() {
%1 = alloca [4096 x i8]
%2 = tail call i32* @opendir(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %36, label %4
4: ; preds = %0
%5 = call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %36, label %7
7: ; preds = %4
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
br label %9
9: ; preds = %7, %33
%10 = phi %struct.dirent* [ %5, %7 ], [ %34, %33 ]
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%12 = load i8*, i8** %11
%13 = call i64 @strstr(i8* %12, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %33, label %15
15: ; preds = %9
%16 = load i32, i32* @PATH_MAX
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
%19 = alloca i8, i64 %17
%20 = load i32, i32* @PATH_MAX
%21 = load i8*, i8** %11
%22 = call i32 @snprintf(i8* %19, i32 %20, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %19, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %15
%27 = call i64 @read(i32 %24, i8* %8, i32 4096)
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 %27
store i8 0, i8* %28
%29 = call i32 @close(i32 %24)
%30 = call i64 @strstr(i8* %8, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %36, label %33
32: ; preds = %15
br label %33
33: ; preds = %26, %9, %32
%34 = call %struct.dirent* @readdir(i32* %2)
%35 = icmp eq %struct.dirent* %34, null
br i1 %35, label %36, label %9
36: ; preds = %33, %26, %4, %0
%37 = phi i32 [ 0, %0 ], [ 0, %4 ], [ 0, %33 ], [ 1, %26 ]
ret i32 %37
}