#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*/ *);
int strlen(char *);
scalar_t__ strncmp(char const *, char *, int);
void listDevices() {
  DIR *dirp = opendir("/dev/input/by-id/");
  struct dirent *dp;
  if (dirp == NULL)
    return;
  int len = strlen("event-joystick");
  while ((dp = readdir(dirp)) != NULL) {
    int devlen = strlen(dp->d_name);
    if (devlen >= len) {
      const char *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    $0x20,%rsp
lea    0xea1(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1178 <listDevices+0x28>
jmp    1218 <listDevices+0xc8>
lea    0xe93(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1218 <listDevices+0xc8>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1213 <listDevices+0xc3>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x18(%rbp),%rcx
add    %rcx,%rax
movslq -0x14(%rbp),%rdx
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe2d(%rip),%rsi
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    120e <listDevices+0xbe>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe1f(%rip),%rdi
lea    0xdf7(%rip),%rsi
call   1060 <printf@plt>
jmp    1213 <listDevices+0xc3>
jmp    1187 <listDevices+0x37>
add    $0x20,%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     11fa <listDevices+0xaa>
mov    %rax,%r13
lea    0xe98(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %r13,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11fa <listDevices+0xaa>
mov    %rax,%rbx
movslq %ebp,%r15
neg    %r15
lea    0xe74(%rip),%r14
lea    0xe5b(%rip),%r12
jmp    11c0 <listDevices+0x70>
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11fa <listDevices+0xaa>
mov    (%rbx),%rdi
call   1050 <strlen@plt>
cmp    %ebp,%eax
jl     11b0 <listDevices+0x60>
cltq
mov    (%rbx),%rdi
add    %r15,%rdi
add    %rax,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11b0 <listDevices+0x60>
mov    (%rbx),%rdx
lea    0xe31(%rip),%rdi
mov    %r12,%rsi
call   1060 <printf@plt>
jmp    11b0 <listDevices+0x60>
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 i32
%4 = alloca i32
%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 %46
10: ; preds = %0
%11 = call i32 @strlen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 %11, i32* %3
br label %12
12: ; preds = %45, %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 %46
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 i32 @strlen(i8* %19)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = load i32, i32* %3
%23 = icmp sge i32 %21, %22
br i1 %23, label %24, label %45
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 i32, i32* %4
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i32, i32* %3
%32 = sext i32 %31 to i64
%33 = sub i64 0, %32
%34 = getelementptr inbounds i8, i8* %30, i64 %33
store i8* %34, i8** %5
%35 = load i8*, i8** %5
%36 = load i32, i32* %3
%37 = call i64 @strncmp(i8* %35, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %24
%40 = load %struct.dirent*, %struct.dirent** %2
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i8*, i8** %41
%43 = 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* %42)
br label %44
44: ; preds = %39, %24
br label %45
45: ; preds = %44, %16
br label %12
46: ; 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 %29, label %3
3: ; preds = %0
%4 = tail call i32 @strlen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call %struct.dirent* @readdir(i32* %1)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %29, label %7
7: ; preds = %3
%8 = sext i32 %4 to i64
%9 = sub nsw i64 0, %8
br label %10
10: ; preds = %7, %26
%11 = phi %struct.dirent* [ %5, %7 ], [ %27, %26 ]
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = tail call i32 @strlen(i8* %13)
%15 = icmp slt i32 %14, %4
br i1 %15, label %26, label %16
16: ; preds = %10
%17 = load i8*, i8** %12
%18 = sext i32 %14 to i64
%19 = getelementptr inbounds i8, i8* %17, i64 %9
%20 = getelementptr inbounds i8, i8* %19, i64 %18
%21 = tail call i64 @strncmp(i8* %20, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i8*, i8** %12
%25 = 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* %24)
br label %26
26: ; preds = %16, %23, %10
%27 = tail call %struct.dirent* @readdir(i32* %1)
%28 = icmp eq %struct.dirent* %27, null
br i1 %28, label %29, label %10
29: ; preds = %26, %3, %0
ret void
}