#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 stat {
  int st_rdev;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
scalar_t__ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ linux_evdev_init_mouse(char const *, int);
int open(char const *, int);
int stat(char const *, struct stat *);
int open_if_mouse(const char *fname) {
  struct stat statbuf;
  int fd;
  int devmajor, devminor;
  if (stat(fname, &statbuf) == -1)
    return 0;
  if (S_ISCHR(statbuf.st_mode) == 0)
    return 0;
  devmajor = (statbuf.st_rdev & 0xFF00) >> 8;
  devminor = (statbuf.st_rdev & 0x00FF);
  if ((devmajor != 13) || (devminor < 64) || (devminor > 96))
    return 0;
  if ((fd = open(fname, O_RDONLY | O_NONBLOCK)) == -1)
    return 0;
  if (linux_evdev_init_mouse(fname, fd))
    return 1;
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<open_if_mouse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
jne    117e <open_if_mouse+0x2e>
movl   $0x0,-0x4(%rbp)
jmp    1243 <open_if_mouse+0xf3>
mov    -0x14(%rbp),%edi
call   1050 <S_ISCHR@plt>
cmp    $0x0,%rax
jne    119c <open_if_mouse+0x4c>
movl   $0x0,-0x4(%rbp)
jmp    1243 <open_if_mouse+0xf3>
mov    -0x18(%rbp),%eax
and    $0xff00,%eax
sar    $0x8,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0xd,-0x20(%rbp)
jne    11d3 <open_if_mouse+0x83>
cmpl   $0x40,-0x24(%rbp)
jl     11d3 <open_if_mouse+0x83>
cmpl   $0x60,-0x24(%rbp)
jle    11df <open_if_mouse+0x8f>
movl   $0x0,-0x4(%rbp)
jmp    1243 <open_if_mouse+0xf3>
mov    -0x10(%rbp),%rdi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1212 <open_if_mouse+0xc2>
movl   $0x0,-0x4(%rbp)
jmp    1243 <open_if_mouse+0xf3>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1070 <linux_evdev_init_mouse@plt>
cmp    $0x0,%rax
je     1234 <open_if_mouse+0xe4>
movl   $0x1,-0x4(%rbp)
jmp    1243 <open_if_mouse+0xf3>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_if_mouse>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1040 <stat@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
je     11d9 <open_if_mouse+0x89>
mov    0xc(%rsp),%edi
call   1050 <S_ISCHR@plt>
test   %rax,%rax
je     11d9 <open_if_mouse+0x89>
mov    0x8(%rsp),%eax
mov    %eax,%ecx
and    $0xff00,%ecx
cmp    $0xd00,%ecx
jne    11d9 <open_if_mouse+0x89>
movzbl %al,%eax
add    $0xffffff9f,%eax
cmp    $0xffffffdf,%eax
jb     11d9 <open_if_mouse+0x89>
mov    0x2e40(%rip),%rax
mov    0x2e41(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11d9 <open_if_mouse+0x89>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    %eax,%esi
call   1070 <linux_evdev_init_mouse@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    11d9 <open_if_mouse+0x89>
mov    %r14d,%edi
call   1030 <close@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
define i32 @open_if_mouse(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @stat(i8* %8, %struct.stat* %4)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 0, i32* %2
br label %52
12: ; preds = %1
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%14 = load i32, i32* %13
%15 = call i64 @S_ISCHR(i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
store i32 0, i32* %2
br label %52
18: ; preds = %12
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%20 = load i32, i32* %19
%21 = and i32 %20, 65280
%22 = ashr i32 %21, 8
store i32 %22, i32* %6
%23 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%24 = load i32, i32* %23
%25 = and i32 %24, 255
store i32 %25, i32* %7
%26 = load i32, i32* %6
%27 = icmp ne i32 %26, 13
br i1 %27, label %34, label %28
28: ; preds = %18
%29 = load i32, i32* %7
%30 = icmp slt i32 %29, 64
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = load i32, i32* %7
%33 = icmp sgt i32 %32, 96
br i1 %33, label %34, label %35
34: ; preds = %31, %28, %18
store i32 0, i32* %2
br label %52
35: ; preds = %31
%36 = load i8*, i8** %3
%37 = load i32, i32* @O_RDONLY
%38 = load i32, i32* @O_NONBLOCK
%39 = or i32 %37, %38
%40 = call i32 @open(i8* %36, i32 %39)
store i32 %40, i32* %5
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %43
42: ; preds = %35
store i32 0, i32* %2
br label %52
43: ; preds = %35
%44 = load i8*, i8** %3
%45 = load i32, i32* %5
%46 = call i64 @linux_evdev_init_mouse(i8* %44, i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %43
store i32 1, i32* %2
br label %52
49: ; preds = %43
%50 = load i32, i32* %5
%51 = call i32 @close(i32 %50)
store i32 0, i32* %2
br label %52
52: ; preds = %49, %48, %42, %34, %17, %11
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
define i32 @open_if_mouse(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %31, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%8 = load i32, i32* %7
%9 = tail call i64 @S_ISCHR(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %31, label %11
11: ; preds = %6
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = and i32 %13, 255
%15 = and i32 %13, 65280
%16 = icmp ne i32 %15, 3328
%17 = add nsw i32 %14, -97
%18 = icmp ult i32 %17, -33
%19 = or i1 %16, %18
br i1 %19, label %31, label %20
20: ; preds = %11
%21 = load i32, i32* @O_RDONLY
%22 = load i32, i32* @O_NONBLOCK
%23 = or i32 %22, %21
%24 = tail call i32 @open(i8* %0, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %31, label %26
26: ; preds = %20
%27 = tail call i64 @linux_evdev_init_mouse(i8* %0, i32 %24)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = tail call i32 @close(i32 %24)
br label %31
31: ; preds = %26, %20, %11, %6, %1, %29
%32 = phi i32 [ 0, %29 ], [ 0, %1 ], [ 0, %6 ], [ 0, %11 ], [ 0, %20 ], [ 1, %26 ]
ret i32 %32
}