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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int matchEventNum(char *);
char *strstr(char *, char *);
int detect_dev_keyboard() {
  int result = -1;
  FILE *fp_proc = fopen("/proc/bus/input/devices", "r");
  if (fp_proc == NULL) {
    exit(EXIT_FAILURE);
  }
  char *line = NULL;
  size_t len = 0;
  ssize_t read;
  int good_section = 0;
  while ((read = getline(&line, &len, fp_proc)) != -1) {
    char *has_N = strstr(line, "N:");
    char *has_keyboard = strstr(line, "keyboard");
    char *has_Keyboard = strstr(line, "Keyboard");
    int keyboard = has_keyboard || has_Keyboard;
    char *has_ACPI = strstr(line, "ACPI");
    char *has_Virtual = strstr(line, "Virtual");
    char *has_H = strstr(line, "H:");
    if (has_N) {
      good_section = 0;
    }
    if (has_N && keyboard && !has_ACPI && !has_Virtual) {
      good_section = 1;
    }
    if (has_H && good_section) {
      result = matchEventNum(line);
    }
  }
  fclose(fp_proc);
  free(line);
  return result;
}


x86 assembly (clang, -O0):
<detect_dev_keyboard>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0xffffffff,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
lea    0xe8b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11af <detect_dev_keyboard+0x3f>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <getline@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     12eb <detect_dev_keyboard+0x17b>
mov    -0x18(%rbp),%rdi
lea    0xe2c(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe1b(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe10(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x40(%rbp)
mov    $0x1,%al
cmpq   $0x0,-0x38(%rbp)
mov    %al,-0x61(%rbp)
jne    123a <detect_dev_keyboard+0xca>
cmpq   $0x0,-0x40(%rbp)
setne  %al
mov    %al,-0x61(%rbp)
mov    -0x61(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xddf(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xdd0(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xdc4(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1293 <detect_dev_keyboard+0x123>
movl   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12c5 <detect_dev_keyboard+0x155>
cmpl   $0x0,-0x44(%rbp)
je     12c5 <detect_dev_keyboard+0x155>
cmpq   $0x0,-0x50(%rbp)
jne    12c5 <detect_dev_keyboard+0x155>
cmpq   $0x0,-0x58(%rbp)
jne    12c5 <detect_dev_keyboard+0x155>
movl   $0x1,-0x28(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     12e6 <detect_dev_keyboard+0x176>
cmpl   $0x0,-0x28(%rbp)
je     12e6 <detect_dev_keyboard+0x176>
mov    -0x18(%rbp),%rdi
call   1040 <matchEventNum@plt>
mov    %eax,-0x4(%rbp)
jmp    11c6 <detect_dev_keyboard+0x56>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detect_dev_keyboard>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xe7b(%rip),%rdi
lea    0xe8c(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     12e3 <detect_dev_keyboard+0x173>
mov    %rax,%rbp
movq   $0x0,0x10(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x10(%rsp),%rdi
lea    0x20(%rsp),%rsi
mov    %rax,%rdx
call   1070 <getline@plt>
cmp    $0xffffffff,%eax
je     12b6 <detect_dev_keyboard+0x146>
xor    %ebx,%ebx
movl   $0xffffffff,0xc(%rsp)
ff
mov    %rbp,0x18(%rsp)
jmp    1200 <detect_dev_keyboard+0x90>
nopl   0x0(%rax,%rax,1)
mov    0x18(%rsp),%rbp
lea    0x10(%rsp),%rdi
lea    0x20(%rsp),%rsi
mov    %rbp,%rdx
call   1070 <getline@plt>
cmp    $0xffffffff,%eax
je     12be <detect_dev_keyboard+0x14e>
mov    0x10(%rsp),%r12
mov    %r12,%rdi
lea    0xe0b(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,%rbp
mov    %r12,%rdi
lea    0xe0e(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,%r13
mov    %r12,%rdi
lea    0xe01(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,%r14
mov    %r12,%rdi
lea    0xdf7(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,%r15
xor    %eax,%eax
test   %rbp,%rbp
je     127d <detect_dev_keyboard+0x10d>
mov    %r12,%rdi
lea    0xdbf(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,%rbp
mov    %r12,%rdi
lea    0xdb6(%rip),%rsi
call   1090 <strstr@plt>
or     %rbp,%rax
setne  %al
xor    %ebx,%ebx
mov    %ebx,%ecx
or     %r14,%r13
mov    $0x1,%ebx
cmovne %ecx,%ebx
test   %al,%al
cmove  %ecx,%ebx
test   %r15,%r15
je     11e0 <detect_dev_keyboard+0x70>
test   %ebx,%ebx
mov    0x18(%rsp),%rbp
je     11e5 <detect_dev_keyboard+0x75>
mov    %r12,%rdi
call   1040 <matchEventNum@plt>
mov    %eax,0xc(%rsp)
jmp    11e5 <detect_dev_keyboard+0x75>
movl   $0xffffffff,0xc(%rsp)
ff
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    0xc(%rsp),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"/proc/bus/input/devices\00"
@.str.1 = [2 x i8] c"r\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [3 x i8] c"N:\00"
@.str.3 = [9 x i8] c"keyboard\00"
@.str.4 = [9 x i8] c"Keyboard\00"
@.str.5 = [5 x i8] c"ACPI\00"
@.str.6 = [8 x i8] c"Virtual\00"
@.str.7 = [3 x i8] c"H:\00"
define i32 @detect_dev_keyboard() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
store i32 -1, i32* %1
%14 = call i32* @fopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %2
%15 = load i32*, i32** %2
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %0
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %0
store i8* null, i8** %3
store i64 0, i64* %4
store i32 0, i32* %6
br label %21
21: ; preds = %71, %20
%22 = load i32*, i32** %2
%23 = call i32 @getline(i8** %3, i64* %4, i32* %22)
store i32 %23, i32* %5
%24 = icmp ne i32 %23, -1
br i1 %24, label %25, label %72
25: ; preds = %21
%26 = load i8*, i8** %3
%27 = call i8* @strstr(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i8* %27, i8** %7
%28 = load i8*, i8** %3
%29 = call i8* @strstr(i8* %28, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
store i8* %29, i8** %8
%30 = load i8*, i8** %3
%31 = call i8* @strstr(i8* %30, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
store i8* %31, i8** %9
%32 = load i8*, i8** %8
%33 = icmp ne i8* %32, null
br i1 %33, label %37, label %34
34: ; preds = %25
%35 = load i8*, i8** %9
%36 = icmp ne i8* %35, null
br label %37
37: ; preds = %34, %25
%38 = phi i1 [ true, %25 ], [ %36, %34 ]
%39 = zext i1 %38 to i32
store i32 %39, i32* %10
%40 = load i8*, i8** %3
%41 = call i8* @strstr(i8* %40, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
store i8* %41, i8** %11
%42 = load i8*, i8** %3
%43 = call i8* @strstr(i8* %42, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
store i8* %43, i8** %12
%44 = load i8*, i8** %3
%45 = call i8* @strstr(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
store i8* %45, i8** %13
%46 = load i8*, i8** %7
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %49
48: ; preds = %37
store i32 0, i32* %6
br label %49
49: ; preds = %48, %37
%50 = load i8*, i8** %7
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %62
52: ; preds = %49
%53 = load i32, i32* %10
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %62
55: ; preds = %52
%56 = load i8*, i8** %11
%57 = icmp ne i8* %56, null
br i1 %57, label %62, label %58
58: ; preds = %55
%59 = load i8*, i8** %12
%60 = icmp ne i8* %59, null
br i1 %60, label %62, label %61
61: ; preds = %58
store i32 1, i32* %6
br label %62
62: ; preds = %61, %58, %55, %52, %49
%63 = load i8*, i8** %13
%64 = icmp ne i8* %63, null
br i1 %64, label %65, label %71
65: ; preds = %62
%66 = load i32, i32* %6
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %65
%69 = load i8*, i8** %3
%70 = call i32 @matchEventNum(i8* %69)
store i32 %70, i32* %1
br label %71
71: ; preds = %68, %65, %62
br label %21
72: ; preds = %21
%73 = load i32*, i32** %2
%74 = call i32 @fclose(i32* %73)
%75 = load i8*, i8** %3
%76 = call i32 @free(i8* %75)
%77 = load i32, i32* %1
ret i32 %77
}

LLVM IR (-O2):
@.str = [24 x i8] c"/proc/bus/input/devices\00"
@.str.1 = [2 x i8] c"r\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [3 x i8] c"N:\00"
@.str.3 = [9 x i8] c"keyboard\00"
@.str.4 = [9 x i8] c"Keyboard\00"
@.str.5 = [5 x i8] c"ACPI\00"
@.str.6 = [8 x i8] c"Virtual\00"
@.str.7 = [3 x i8] c"H:\00"
define i32 @detect_dev_keyboard() {
%1 = alloca i8*
%2 = alloca i64
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%10 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%11 = call i32 @getline(i8** %1, i64* %2, i32* %3)
%12 = icmp eq i32 %11, -1
br i1 %12, label %46, label %13
13: ; preds = %8, %42
%14 = phi i32 [ %43, %42 ], [ -1, %8 ]
%15 = phi i32 [ %36, %42 ], [ 0, %8 ]
%16 = load i8*, i8** %1
%17 = call i8* @strstr(i8* dereferenceable(1) %16, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%18 = call i8* @strstr(i8* dereferenceable(1) %16, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%19 = call i8* @strstr(i8* dereferenceable(1) %16, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%20 = call i8* @strstr(i8* dereferenceable(1) %16, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
%21 = icmp eq i8* %17, null
br i1 %21, label %28, label %22
22: ; preds = %13
%23 = call i8* @strstr(i8* dereferenceable(1) %16, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp ne i8* %23, null
%25 = call i8* @strstr(i8* dereferenceable(1) %16, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%26 = icmp ne i8* %25, null
%27 = select i1 %24, i1 true, i1 %26
br label %28
28: ; preds = %22, %13
%29 = phi i1 [ %27, %22 ], [ false, %13 ]
%30 = phi i32 [ 0, %22 ], [ %15, %13 ]
%31 = xor i1 %29, true
%32 = icmp ne i8* %18, null
%33 = select i1 %31, i1 true, i1 %32
%34 = icmp ne i8* %19, null
%35 = select i1 %33, i1 true, i1 %34
%36 = select i1 %35, i32 %30, i32 1
%37 = icmp ne i8* %20, null
%38 = icmp ne i32 %36, 0
%39 = select i1 %37, i1 %38, i1 false
br i1 %39, label %40, label %42
40: ; preds = %28
%41 = call i32 @matchEventNum(i8* %16)
br label %42
42: ; preds = %40, %28
%43 = phi i32 [ %41, %40 ], [ %14, %28 ]
%44 = call i32 @getline(i8** %1, i64* %2, i32* %3)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %13
46: ; preds = %42, %8
%47 = phi i32 [ -1, %8 ], [ %43, %42 ]
%48 = call i32 @fclose(i32* %3)
%49 = load i8*, i8** %1
%50 = call i32 @free(i8* %49)
ret i32 %47
}