#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 */
typedef int /*<<< orphan*/ name;

/* Variables and functions */
int /*<<< orphan*/ EVIOCGNAME(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, char *);
int open(char const *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ strncasecmp(char *, char const *, int /*<<< orphan*/);
int open_powermate(const char *dev, int mode) {
  int fd = open(dev, mode);
  char name[255];
  const char *valid_id_prefix = "Griffin";
  if (fd < 0) {
    fprintf(stderr, "Unable to open \"%s\": %s\n", dev, strerror(errno));
    return -1;
  }
  if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0) {
    fprintf(stderr, "\"%s\": EVIOCGNAME failed: %s\n", dev, strerror(errno));
    close(fd);
    return -1;
  }
  if (!strncasecmp(name, valid_id_prefix, strlen(valid_id_prefix)))
    return fd;
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<open_powermate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
lea    0xe58(%rip),%rax
mov    %rax,-0x128(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1209 <open_powermate+0x89>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x134(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x130(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x134(%rbp),%edi
mov    -0x130(%rbp),%rdx
mov    %rax,%rcx
lea    0xe10(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <open_powermate+0x172>
mov    -0x18(%rbp),%eax
mov    %eax,-0x138(%rbp)
mov    $0xff,%edi
call   1090 <EVIOCGNAME@plt>
mov    -0x138(%rbp),%edi
mov    %eax,%esi
lea    -0x120(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1292 <open_powermate+0x112>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x144(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x140(%rbp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x144(%rbp),%edi
mov    -0x140(%rbp),%rdx
mov    %rax,%rcx
lea    0xda8(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f2 <open_powermate+0x172>
lea    -0x120(%rbp),%rax
mov    %rax,-0x158(%rbp)
mov    -0x128(%rbp),%rax
mov    %rax,-0x150(%rbp)
mov    -0x128(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x158(%rbp),%rdi
mov    -0x150(%rbp),%rsi
mov    %eax,%edx
call   1070 <strncasecmp@plt>
cmp    $0x0,%eax
jne    12e3 <open_powermate+0x163>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12f2 <open_powermate+0x172>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_powermate>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%r14
call   1080 <open@plt>
test   %eax,%eax
js     11d6 <open_powermate+0x56>
mov    %eax,%ebx
mov    $0xff,%edi
call   1090 <EVIOCGNAME@plt>
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %eax,%esi
call   1040 <ioctl@plt>
test   %rax,%rax
js     1203 <open_powermate+0x83>
lea    0xe45(%rip),%rbp
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    %rsp,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1070 <strncasecmp@plt>
test   %eax,%eax
jne    122e <open_powermate+0xae>
jmp    123a <open_powermate+0xba>
mov    0x2e03(%rip),%rax
mov    (%rax),%ebx
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe14(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1060 <fprintf@plt>
jmp    1235 <open_powermate+0xb5>
mov    0x2dd6(%rip),%rax
mov    (%rax),%ebp
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe00(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"Griffin\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Unable to open \22%s\22: %s\0A\00"
@errno = global i32 0
@.str.2 = [29 x i8] c"\22%s\22: EVIOCGNAME failed: %s\0A\00"
define i32 @open_powermate(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [255 x i8]
%8 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = load i32, i32* %5
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %6
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %8
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %4
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %16, i8* %18)
store i32 -1, i32* %3
br label %46
20: ; preds = %2
%21 = load i32, i32* %6
%22 = call i32 @EVIOCGNAME(i32 255)
%23 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%24 = call i64 @ioctl(i32 %21, i32 %22, i8* %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %4
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %28, i8* %30)
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
store i32 -1, i32* %3
br label %46
34: ; preds = %20
%35 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%36 = load i8*, i8** %8
%37 = load i8*, i8** %8
%38 = call i32 @strlen(i8* %37)
%39 = call i32 @strncasecmp(i8* %35, i8* %36, i32 %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %34
%42 = load i32, i32* %6
store i32 %42, i32* %3
br label %46
43: ; preds = %34
%44 = load i32, i32* %6
%45 = call i32 @close(i32 %44)
store i32 -1, i32* %3
br label %46
46: ; preds = %43, %41, %26, %14
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@.str = [8 x i8] c"Griffin\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Unable to open \22%s\22: %s\0A\00"
@errno = global i32 0
@.str.2 = [29 x i8] c"\22%s\22: EVIOCGNAME failed: %s\0A\00"
define i32 @open_powermate(i8* %0, i32 %1) {
%3 = alloca [255 x i8]
%4 = tail call i32 @open(i8* %0, i32 %1)
%5 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%6 = icmp slt i32 %4, 0
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %10)
br label %28
12: ; preds = %2
%13 = tail call i32 @EVIOCGNAME(i32 255)
%14 = call i64 @ioctl(i32 %4, i32 %13, i8* %5)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %19)
%21 = call i32 @close(i32 %4)
br label %28
22: ; preds = %12
%23 = call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @strncasecmp(i8* %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %22
%27 = call i32 @close(i32 %4)
br label %28
28: ; preds = %22, %26, %16, %7
%29 = phi i32 [ -1, %7 ], [ -1, %16 ], [ -1, %26 ], [ %4, %22 ]
ret i32 %29
}