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

/* Variables and functions */
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcat(char *, char *);
char *strdup(int /*<<< orphan*/);
char *getKeyboardDeviceFileName() {
  static const char *command = "grep -E 'Handlers|EV' /proc/bus/input/devices |"
                               "grep -B1 120013 |"
                               "grep -Eo event[0-9]+ |"
                               "tr '\\n' '\\0'";
  FILE *pipe = popen(command, "r");
  if (pipe == NULL) {
    printf("Could not determine keyboard device file");
  }
  char result[20] = "/dev/input/";
  char temp[9];
  fgets(temp, 9, pipe);
  pclose(pipe);
  return strdup(strcat(result, temp));
}


x86 assembly (clang, -O0):
<getKeyboardDeviceFileName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2ee1(%rip),%rdi
lea    0xeed(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1196 <getKeyboardDeviceFileName+0x36>
lea    0xed4(%rip),%rdi
call   1030 <printf@plt>
mov    0xef3(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xef0(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xeee(%rip),%eax
mov    %eax,-0x10(%rbp)
lea    -0x29(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x9,%esi
call   1050 <fgets@plt>
mov    -0x8(%rbp),%rdi
call   1040 <pclose@plt>
lea    -0x20(%rbp),%rdi
lea    -0x29(%rbp),%rsi
call   1070 <strcat@plt>
mov    %eax,%edi
call   1080 <strdup@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getKeyboardDeviceFileName>:
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0xe92(%rip),%rdi
lea    0xeee(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    118e <getKeyboardDeviceFileName+0x2e>
lea    0xedc(%rip),%rdi
call   1030 <printf@plt>
movaps 0xefb(%rip),%xmm0
movaps %xmm0,(%rsp)
movl   $0x0,0x10(%rsp)
lea    0x1f(%rsp),%r14
mov    %r14,%rdi
mov    $0x9,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
mov    %rbx,%rdi
call   1040 <pclose@plt>
mov    %rsp,%rdi
mov    %r14,%rsi
call   1070 <strcat@plt>
mov    %eax,%edi
call   1080 <strdup@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@getKeyboardDeviceFileName.command = internal global i8* getelementptr inbounds ([99 x i8], [99 x i8]* @.str, i32 0, i32 0)
@.str = [99 x i8] c"grep -E 'Handlers|EV' /proc/bus/input/devices |grep -B1 120013 |grep -Eo event[0-9]+ |tr '\\n' '\\0'\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [41 x i8] c"Could not determine keyboard device file\00"
@__const.getKeyboardDeviceFileName.result = [20 x i8] c"/dev/input/\00\00\00\00\00\00\00\00\00"
define i8* @getKeyboardDeviceFileName() {
%1 = alloca i32*
%2 = alloca [20 x i8]
%3 = alloca [9 x i8]
%4 = load i8*, i8** @getKeyboardDeviceFileName.command
%5 = call i32* @popen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %1
%6 = load i32*, i32** %1
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = bitcast [20 x i8]* %2 to i8*
%12 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%13 = load i32*, i32** %1
%14 = call i32 @fgets(i8* %12, i32 9, i32* %13)
%15 = load i32*, i32** %1
%16 = call i32 @pclose(i32* %15)
%17 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%18 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%19 = call i32 @strcat(i8* %17, i8* %18)
%20 = call i8* @strdup(i32 %19)
ret i8* %20
}

LLVM IR (-O2):
@.str = [99 x i8] c"grep -E 'Handlers|EV' /proc/bus/input/devices |grep -B1 120013 |grep -Eo event[0-9]+ |tr '\\n' '\\0'\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [41 x i8] c"Could not determine keyboard device file\00"
@__const.getKeyboardDeviceFileName.result = [20 x i8] c"/dev/input/\00\00\00\00\00\00\00\00\00"
define i8* @getKeyboardDeviceFileName() {
%1 = alloca [20 x i8]
%2 = alloca [9 x i8]
%3 = tail call i32* @popen(i8* getelementptr inbounds ([99 x i8], [99 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 %7
5: ; preds = %0
%6 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %7
7: ; preds = %5, %0
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%9 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 0
%10 = call i32 @fgets(i8* %9, i32 9, i32* %3)
%11 = call i32 @pclose(i32* %3)
%12 = call i32 @strcat(i8* %8, i8* %9)
%13 = call i8* @strdup(i32 %12)
ret i8* %13
}