#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*/ prefix;
typedef int /*<<< orphan*/ line;
typedef int /*<<< orphan*/ filename;
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int /*<<< orphan*/ stdout;
char *strstr(char *, char *);
void print_hid_descriptor(int bus, int vendor, int product) {
  const char *dirname = "/sys/kernel/debug/hid";
  char prefix[16];
  DIR *dir;
  struct dirent *de;
  char filename[PATH_MAX];
  FILE *file;
  char line[2048];
  snprintf(prefix, sizeof(prefix), "%04X:%04X:%04X.", bus, vendor, product);
  dir = opendir(dirname);
  if (dir == NULL)
    return;
  while ((de = readdir(dir))) {
    if (strstr(de->d_name, prefix) == de->d_name) {
      snprintf(filename, sizeof(filename), "%s/%s/rdesc", dirname, de->d_name);
      file = fopen(filename, "r");
      if (file) {
        printf("  HID descriptor: %s\n\n", de->d_name);
        while (fgets(line, sizeof(line), file)) {
          fputs("    ", stdout);
          fputs(line, stdout);
        }
        fclose(file);
        puts("");
      }
    }
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<print_hid_descriptor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x880,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
lea    0xe35(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x878(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x870(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    -0x8(%rbp),%r8d
mov    -0xc(%rbp),%r9d
mov    $0x10,%esi
lea    0xdf6(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    124e <print_hid_descriptor+0x9e>
movl   $0x1,-0x864(%rbp)
jmp    1365 <print_hid_descriptor+0x1b5>
jmp    1253 <print_hid_descriptor+0xa3>
mov    -0x38(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     1352 <print_hid_descriptor+0x1a2>
mov    -0x40(%rbp),%rax
mov    (%rax),%rdi
lea    -0x30(%rbp),%rsi
call   10d0 <strstr@plt>
mov    -0x40(%rbp),%rcx
cmp    (%rcx),%rax
jne    134d <print_hid_descriptor+0x19d>
mov    -0x870(%rbp),%rdi
mov    -0x878(%rbp),%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rcx
mov    -0x40(%rbp),%rax
mov    (%rax),%r8
lea    0xd7d(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x870(%rbp),%rdi
lea    0xd74(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     1348 <print_hid_descriptor+0x198>
mov    -0x40(%rbp),%rax
mov    (%rax),%rsi
lea    0xd54(%rip),%rdi
call   1060 <printf@plt>
lea    -0x860(%rbp),%rdi
mov    -0x58(%rbp),%rdx
mov    $0x800,%esi
call   10a0 <fgets@plt>
cmp    $0x0,%rax
je     1333 <print_hid_descriptor+0x183>
mov    0x2ccd(%rip),%rax
mov    (%rax),%esi
lea    0xd37(%rip),%rdi
call   1080 <fputs@plt>
lea    -0x860(%rbp),%rdi
mov    0x2cb1(%rip),%rax
mov    (%rax),%esi
call   1080 <fputs@plt>
jmp    12e5 <print_hid_descriptor+0x135>
mov    -0x58(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd07(%rip),%rdi
call   1030 <puts@plt>
jmp    134d <print_hid_descriptor+0x19d>
jmp    1253 <print_hid_descriptor+0xa3>
mov    -0x38(%rbp),%rdi
call   1090 <closedir@plt>
movl   $0x0,-0x864(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_hid_descriptor>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x838,%rsp
mov    %edx,%r9d
mov    %esi,%r8d
mov    %edi,%ecx
mov    %rsp,%r14
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rdx
mov    %rax,-0x40(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rdx
mov    %rdx,-0x38(%rbp)
mov    %rdx,%rsp
lea    0xe0e(%rip),%rdx
lea    -0x60(%rbp),%rdi
mov    $0x10,%esi
xor    %eax,%eax
call   1090 <snprintf@plt>
lea    0xde1(%rip),%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     135c <print_hid_descriptor+0x19c>
mov    %rax,%rbx
mov    %r14,-0x30(%rbp)
mov    %rax,%rdi
call   10d0 <readdir@plt>
test   %rax,%rax
je     1350 <print_hid_descriptor+0x190>
mov    %rax,%r15
lea    -0x860(%rbp),%r13
mov    0x2d82(%rip),%r14
mov    %rbx,-0x48(%rbp)
jmp    128a <print_hid_descriptor+0xca>
nopl   0x0(%rax)
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x48(%rbp),%rbx
mov    %rbx,%rdi
call   10d0 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     1350 <print_hid_descriptor+0x190>
mov    (%r15),%rbx
lea    -0x60(%rbp),%r12
mov    %r12,%rdi
call   1070 <strlen@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %rax,%rdx
call   1040 <strncmp@plt>
test   %eax,%eax
jne    1272 <print_hid_descriptor+0xb2>
mov    -0x38(%rbp),%r12
mov    %r12,%rdi
mov    -0x40(%rbp),%rsi
lea    0xd69(%rip),%rdx
lea    0xd3c(%rip),%rcx
mov    %rbx,%r8
xor    %eax,%eax
call   1090 <snprintf@plt>
mov    %r12,%rdi
lea    0xd5a(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     1272 <print_hid_descriptor+0xb2>
mov    %rax,%r12
mov    (%r15),%rsi
lea    0xd45(%rip),%rdi
call   1080 <printf@plt>
mov    %r13,%rdi
mov    $0x800,%esi
mov    %r12,%rdx
call   10c0 <fgets@plt>
test   %rax,%rax
lea    0xd3d(%rip),%rbx
je     1260 <print_hid_descriptor+0xa0>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r14),%esi
mov    %rbx,%rdi
call   10a0 <fputs@plt>
mov    (%r14),%esi
mov    %r13,%rdi
call   10a0 <fputs@plt>
mov    %r13,%rdi
mov    $0x800,%esi
mov    %r12,%rdx
call   10c0 <fgets@plt>
test   %rax,%rax
jne    1320 <print_hid_descriptor+0x160>
jmp    1260 <print_hid_descriptor+0xa0>
mov    %rbx,%rdi
call   10b0 <closedir@plt>
mov    -0x30(%rbp),%r14
mov    %r14,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [22 x i8] c"/sys/kernel/debug/hid\00"
@PATH_MAX = global i32 0
@.str.1 = [16 x i8] c"%04X:%04X:%04X.\00"
@.str.2 = [12 x i8] c"%s/%s/rdesc\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [23 x i8] c" HID descriptor: %s\0A\0A\00"
@.str.5 = [5 x i8] c" \00"
@stdout = global i32 0
@.str.6 = [1 x i8] zeroinitializer
define void @print_hid_descriptor(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [16 x i8]
%9 = alloca i32*
%10 = alloca %struct.dirent*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32*
%14 = alloca [2048 x i8]
%15 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8** %7
%16 = load i32, i32* @PATH_MAX
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %11
%19 = alloca i8, i64 %17
store i64 %17, i64* %12
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%21 = load i32, i32* %4
%22 = load i32, i32* %5
%23 = load i32, i32* %6
%24 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %20, i32 16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %22, i32 %23)
%25 = load i8*, i8** %7
%26 = call i32* @opendir(i8* %25)
store i32* %26, i32** %9
%27 = load i32*, i32** %9
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %30
29: ; preds = %3
store i32 1, i32* %15
br label %80
30: ; preds = %3
br label %31
31: ; preds = %76, %30
%32 = load i32*, i32** %9
%33 = call %struct.dirent* @readdir(i32* %32)
store %struct.dirent* %33, %struct.dirent** %10
%34 = icmp ne %struct.dirent* %33, null
br i1 %34, label %35, label %77
35: ; preds = %31
%36 = load %struct.dirent*, %struct.dirent** %10
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i32 0, i32 0
%38 = load i8*, i8** %37
%39 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%40 = call i8* @strstr(i8* %38, i8* %39)
%41 = load %struct.dirent*, %struct.dirent** %10
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = icmp eq i8* %40, %43
br i1 %44, label %45, label %76
45: ; preds = %35
%46 = trunc i64 %17 to i32
%47 = load i8*, i8** %7
%48 = load %struct.dirent*, %struct.dirent** %10
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %19, i32 %46, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %47, i8* %50)
%52 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %52, i32** %13
%53 = load i32*, i32** %13
%54 = icmp ne i32* %53, null
br i1 %54, label %55, label %75
55: ; preds = %45
%56 = load %struct.dirent*, %struct.dirent** %10
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %58)
br label %60
60: ; preds = %65, %55
%61 = getelementptr inbounds [2048 x i8], [2048 x i8]* %14, i64 0, i64 0
%62 = load i32*, i32** %13
%63 = call i64 @fgets(i8* %61, i32 2048, i32* %62)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %71
65: ; preds = %60
%66 = load i32, i32* @stdout
%67 = call i32 @fputs(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %66)
%68 = getelementptr inbounds [2048 x i8], [2048 x i8]* %14, i64 0, i64 0
%69 = load i32, i32* @stdout
%70 = call i32 @fputs(i8* %68, i32 %69)
br label %60
71: ; preds = %60
%72 = load i32*, i32** %13
%73 = call i32 @fclose(i32* %72)
%74 = call i32 @puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0))
br label %75
75: ; preds = %71, %45
br label %76
76: ; preds = %75, %35
br label %31
77: ; preds = %31
%78 = load i32*, i32** %9
%79 = call i32 @closedir(i32* %78)
store i32 0, i32* %15
br label %80
80: ; preds = %77, %29
%81 = load i8*, i8** %11
%82 = load i32, i32* %15
switch i32 %82, label %84 [
i32 0, label %83
i32 1, label %83
]
83: ; preds = %80, %80
ret void
84: ; preds = %80
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [22 x i8] c"/sys/kernel/debug/hid\00"
@PATH_MAX = global i32 0
@.str.1 = [16 x i8] c"%04X:%04X:%04X.\00"
@.str.2 = [12 x i8] c"%s/%s/rdesc\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [23 x i8] c" HID descriptor: %s\0A\0A\00"
@.str.5 = [5 x i8] c" \00"
@stdout = global i32 0
define void @print_hid_descriptor(i32 %0, i32 %1, i32 %2) {
%4 = alloca [16 x i8]
%5 = alloca [2048 x i8]
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%12 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %0, i32 %1, i32 %2)
%13 = call i32* @opendir(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %49, label %15
15: ; preds = %3
%16 = call %struct.dirent* @readdir(i32* %13)
%17 = icmp eq %struct.dirent* %16, null
br i1 %17, label %47, label %18
18: ; preds = %15, %44
%19 = phi %struct.dirent* [ %45, %44 ], [ %16, %15 ]
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = call i64 @strlen(i8* %6)
%23 = call i32 @strncmp(i8* %21, i8* %6, i64 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %44
25: ; preds = %18
%26 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %10, i32 %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %21)
%27 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %44, label %29
29: ; preds = %25
%30 = load i8*, i8** %20
%31 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %30)
%32 = call i64 @fgets(i8* %11, i32 2048, i32* %27)
%33 = icmp eq i64 %32, 0
br i1 %33, label %41, label %34
34: ; preds = %29, %34
%35 = load i32, i32* @stdout
%36 = call i32 @fputs(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %35)
%37 = load i32, i32* @stdout
%38 = call i32 @fputs(i8* %11, i32 %37)
%39 = call i64 @fgets(i8* %11, i32 2048, i32* %27)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %34
41: ; preds = %34, %29
%42 = call i32 @fclose(i32* %27)
%43 = call i32 @putchar(i32 10)
br label %44
44: ; preds = %25, %41, %18
%45 = call %struct.dirent* @readdir(i32* %13)
%46 = icmp eq %struct.dirent* %45, null
br i1 %46, label %47, label %18
47: ; preds = %44, %15
%48 = call i32 @closedir(i32* %13)
br label %49
49: ; preds = %3, %47
ret void
}