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

/* Variables and functions */
int /*<<< orphan*/ EV_KEY;
int KEY_MAX;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
scalar_t__ supports_event_type(int, int /*<<< orphan*/);
scalar_t__ supports_specific_key(int, int);
int detect_keyboard(char *out) {
  int i;
  int fd;
  int key;
  int num_supported_keys;
  char device[256];
  for (i = 0; i < 8; i++) {
    sprintf(device, "/dev/input/event%d", i);
    if ((fd = open(device, O_RDONLY)) < 0) {
      continue;
    }
    if (supports_event_type(fd, EV_KEY)) {
      num_supported_keys = 0;
      for (key = 0; key <= KEY_MAX; key++) {
        if (supports_specific_key(fd, key)) {
          num_supported_keys += 1;
        }
      }
      if (num_supported_keys > 20) {
        printf("Found keyboard at: %s\n", device);
        strncpy(out, device, 256 - 1);
        close(fd);
        return 0;
      }
    }
    close(fd);
  }
  return -1;
}


x86 assembly (clang, -O0):
<detect_keyboard>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x8,-0x14(%rbp)
jge    1299 <detect_keyboard+0x129>
lea    -0x120(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe5f(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11cc <detect_keyboard+0x5c>
jmp    128b <detect_keyboard+0x11b>
mov    -0x18(%rbp),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
call   1070 <supports_event_type@plt>
cmp    $0x0,%rax
je     1283 <detect_keyboard+0x113>
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2dd1(%rip),%rcx
cmp    (%rcx),%eax
jg     1238 <detect_keyboard+0xc8>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1060 <supports_specific_key@plt>
cmp    $0x0,%rax
je     1225 <detect_keyboard+0xb5>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    122a <detect_keyboard+0xba>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f5 <detect_keyboard+0x85>
cmpl   $0x14,-0x20(%rbp)
jle    127e <detect_keyboard+0x10e>
lea    -0x120(%rbp),%rsi
lea    0xdc3(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a0 <detect_keyboard+0x130>
jmp    1283 <detect_keyboard+0x113>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1186 <detect_keyboard+0x16>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detect_keyboard>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rdi,0x8(%rsp)
xor    %r13d,%r13d
lea    0xe70(%rip),%r12
lea    0x10(%rsp),%rbp
mov    0x2e4c(%rip),%r14
mov    0x2e2d(%rip),%r15
jmp    11c5 <detect_keyboard+0x55>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x1,%r13d
cmp    $0x8,%r13d
je     1270 <detect_keyboard+0x100>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r13d,%edx
call   1090 <sprintf@plt>
mov    (%r14),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11b7 <detect_keyboard+0x47>
mov    %eax,%ebx
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <supports_event_type@plt>
test   %rax,%rax
je     11b0 <detect_keyboard+0x40>
cmpl   $0x0,(%r15)
js     11b0 <detect_keyboard+0x40>
xor    %r14d,%r14d
mov    $0xffffffff,%ebp
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebp
mov    %ebx,%edi
mov    %ebp,%esi
call   1060 <supports_specific_key@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%r14d
cmp    (%r15),%ebp
jl     1210 <detect_keyboard+0xa0>
cmp    $0x15,%r14d
lea    0x10(%rsp),%rbp
mov    0x2daf(%rip),%r14
jb     11b0 <detect_keyboard+0x40>
lea    0xdcd(%rip),%rdi
lea    0x10(%rsp),%rbp
mov    %rbp,%rsi
call   1040 <printf@plt>
mov    0x8(%rsp),%rdi
mov    %rbp,%rsi
mov    $0xff,%edx
call   1030 <strncpy@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    1275 <detect_keyboard+0x105>
mov    $0xffffffff,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"/dev/input/event%d\00"
@O_RDONLY = global i32 0
@EV_KEY = global i32 0
@KEY_MAX = global i32 0
@.str.1 = [23 x i8] c"Found keyboard at: %s\0A\00"
define i32 @detect_keyboard(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [256 x i8]
store i8* %0, i8** %3
store i32 0, i32* %4
br label %9
9: ; preds = %58, %1
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 8
br i1 %11, label %12, label %61
12: ; preds = %9
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%14 = load i32, i32* %4
%15 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %5
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %12
br label %58
21: ; preds = %12
%22 = load i32, i32* %5
%23 = load i32, i32* @EV_KEY
%24 = call i64 @supports_event_type(i32 %22, i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %55
26: ; preds = %21
store i32 0, i32* %7
store i32 0, i32* %6
br label %27
27: ; preds = %40, %26
%28 = load i32, i32* %6
%29 = load i32, i32* @KEY_MAX
%30 = icmp sle i32 %28, %29
br i1 %30, label %31, label %43
31: ; preds = %27
%32 = load i32, i32* %5
%33 = load i32, i32* %6
%34 = call i64 @supports_specific_key(i32 %32, i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
store i32 %38, i32* %7
br label %39
39: ; preds = %36, %31
br label %40
40: ; preds = %39
%41 = load i32, i32* %6
%42 = add nsw i32 %41, 1
store i32 %42, i32* %6
br label %27
43: ; preds = %27
%44 = load i32, i32* %7
%45 = icmp sgt i32 %44, 20
br i1 %45, label %46, label %54
46: ; preds = %43
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%48 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %47)
%49 = load i8*, i8** %3
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%51 = call i32 @strncpy(i8* %49, i8* %50, i32 255)
%52 = load i32, i32* %5
%53 = call i32 @close(i32 %52)
store i32 0, i32* %2
br label %62
54: ; preds = %43
br label %55
55: ; preds = %54, %21
%56 = load i32, i32* %5
%57 = call i32 @close(i32 %56)
br label %58
58: ; preds = %55, %20
%59 = load i32, i32* %4
%60 = add nsw i32 %59, 1
store i32 %60, i32* %4
br label %9
61: ; preds = %9
store i32 -1, i32* %2
br label %62
62: ; preds = %61, %46
%63 = load i32, i32* %2
ret i32 %63
}

LLVM IR (-O2):
@.str = [19 x i8] c"/dev/input/event%d\00"
@O_RDONLY = global i32 0
@EV_KEY = global i32 0
@KEY_MAX = global i32 0
@.str.1 = [23 x i8] c"Found keyboard at: %s\0A\00"
define i32 @detect_keyboard(i8* %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %1, %35
%5 = phi i32 [ 0, %1 ], [ %36, %35 ]
%6 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %3, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %35, label %10
10: ; preds = %4
%11 = load i32, i32* @EV_KEY
%12 = tail call i64 @supports_event_type(i32 %8, i32 %11)
%13 = icmp eq i64 %12, 0
%14 = load i32, i32* @KEY_MAX
%15 = icmp slt i32 %14, 0
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %33, label %17
17: ; preds = %10, %17
%18 = phi i32 [ %23, %17 ], [ 0, %10 ]
%19 = phi i32 [ %24, %17 ], [ 0, %10 ]
%20 = tail call i64 @supports_specific_key(i32 %8, i32 %19)
%21 = icmp ne i64 %20, 0
%22 = zext i1 %21 to i32
%23 = add nuw nsw i32 %18, %22
%24 = add nuw nsw i32 %19, 1
%25 = load i32, i32* @KEY_MAX
%26 = icmp slt i32 %19, %25
br i1 %26, label %17, label %27
27: ; preds = %17
%28 = icmp ugt i32 %23, 20
br i1 %28, label %29, label %33
29: ; preds = %27
%30 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%31 = call i32 @strncpy(i8* %0, i8* %3, i32 255)
%32 = call i32 @close(i32 %8)
br label %38
33: ; preds = %27, %10
%34 = tail call i32 @close(i32 %8)
br label %35
35: ; preds = %4, %33
%36 = add nuw nsw i32 %5, 1
%37 = icmp eq i32 %36, 8
br i1 %37, label %38, label %4
38: ; preds = %35, %29
%39 = phi i32 [ 0, %29 ], [ -1, %35 ]
ret i32 %39
}