#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 dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
size_t strlen(char *);
scalar_t__ strncmp(char const *const, char *, size_t);
void listDevices(void) {
  DIR *dirp = opendir("/dev/input/by-id/");
  struct dirent *dp;
  if (dirp == NULL)
    return;
  size_t len = strlen("event-joystick");
  while ((dp = readdir(dirp)) != NULL) {
    size_t devlen = strlen(dp->d_name);
    if (devlen >= len) {
      const char *const start = dp->d_name + devlen - len;
      if (strncmp(start, "event-joystick", len) == 0)
        printf("%s%s\n", "/dev/input/by-id/", dp->d_name);
    }
  }
}


x86 assembly (clang, -O0):
<listDevices>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xea1(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1178 <listDevices+0x28>
jmp    1217 <listDevices+0xc7>
lea    0xe93(%rip),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1217 <listDevices+0xc7>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     1212 <listDevices+0xc2>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
add    -0x20(%rbp),%rax
xor    %ecx,%ecx
sub    -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe2e(%rip),%rsi
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    120d <listDevices+0xbd>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe20(%rip),%rdi
lea    0xdf8(%rip),%rsi
call   1060 <printf@plt>
jmp    1212 <listDevices+0xc2>
jmp    1188 <listDevices+0x38>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listDevices>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe9e(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11eb <listDevices+0x9b>
mov    %rax,%r13
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11eb <listDevices+0x9b>
mov    %rax,%rbx
lea    0xe8c(%rip),%r14
lea    0xe94(%rip),%r15
lea    0xe6c(%rip),%r12
jmp    11b0 <listDevices+0x60>
cs nopw 0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11eb <listDevices+0x9b>
mov    (%rbx),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
cmp    $0xe,%rax
jb     11a0 <listDevices+0x50>
lea    (%rax,%rbp,1),%rdi
add    $0xfffffffffffffff2,%rdi
mov    $0xe,%edx
mov    %r14,%rsi
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11a0 <listDevices+0x50>
mov    (%rbx),%rdx
mov    %r15,%rdi
mov    %r12,%rsi
call   1060 <printf@plt>
jmp    11a0 <listDevices+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [18 x i8] c"/dev/input/by-id/\00"
@.str.1 = [15 x i8] c"event-joystick\00"
@.str.2 = [6 x i8] c"%s%s\0A\00"
define void @listDevices() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
%6 = call i32* @opendir(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %1
%7 = load i32*, i32** %1
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %10
9: ; preds = %0
br label %44
10: ; preds = %0
%11 = call i64 @strlen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i64 %11, i64* %3
br label %12
12: ; preds = %43, %10
%13 = load i32*, i32** %1
%14 = call %struct.dirent* @readdir(i32* %13)
store %struct.dirent* %14, %struct.dirent** %2
%15 = icmp ne %struct.dirent* %14, null
br i1 %15, label %16, label %44
16: ; preds = %12
%17 = load %struct.dirent*, %struct.dirent** %2
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = call i64 @strlen(i8* %19)
store i64 %20, i64* %4
%21 = load i64, i64* %4
%22 = load i64, i64* %3
%23 = icmp uge i64 %21, %22
br i1 %23, label %24, label %43
24: ; preds = %16
%25 = load %struct.dirent*, %struct.dirent** %2
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = load i64, i64* %4
%29 = getelementptr inbounds i8, i8* %27, i64 %28
%30 = load i64, i64* %3
%31 = sub i64 0, %30
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8* %32, i8** %5
%33 = load i8*, i8** %5
%34 = load i64, i64* %3
%35 = call i64 @strncmp(i8* %33, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i64 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %24
%38 = load %struct.dirent*, %struct.dirent** %2
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 0
%40 = load i8*, i8** %39
%41 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %37, %24
br label %43
43: ; preds = %42, %16
br label %12
44: ; preds = %9, %12
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [18 x i8] c"/dev/input/by-id/\00"
@.str.1 = [15 x i8] c"event-joystick\00"
@.str.2 = [6 x i8] c"%s%s\0A\00"
define void @listDevices() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %23, label %3
3: ; preds = %0
%4 = tail call %struct.dirent* @readdir(i32* %1)
%5 = icmp eq %struct.dirent* %4, null
br i1 %5, label %23, label %6
6: ; preds = %3, %20
%7 = phi %struct.dirent* [ %21, %20 ], [ %4, %3 ]
%8 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i64 0, i32 0
%9 = load i8*, i8** %8
%10 = tail call i64 @strlen(i8* dereferenceable(1) %9)
%11 = icmp ugt i64 %10, 13
br i1 %11, label %12, label %20
12: ; preds = %6
%13 = getelementptr inbounds i8, i8* %9, i64 -14
%14 = getelementptr inbounds i8, i8* %13, i64 %10
%15 = tail call i64 @strncmp(i8* %14, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i64 14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i8*, i8** %8
%19 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %12, %17, %6
%21 = tail call %struct.dirent* @readdir(i32* %1)
%22 = icmp eq %struct.dirent* %21, null
br i1 %22, label %23, label %6
23: ; preds = %20, %3, %0
ret void
}