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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
scalar_t__ strstr(char *, char *);
void activate_usb_autosuspend(void) {
  DIR *dir;
  struct dirent *dirent;
  FILE *file;
  char filename[PATH_MAX];
  int len;
  char linkto[PATH_MAX];
  dir = opendir("/sys/bus/usb/devices");
  if (!dir)
    return;
  while ((dirent = readdir(dir))) {
    if (dirent->d_name[0] == '.')
      continue;
    sprintf(filename, "/sys/bus/usb/devices/%s/driver", dirent->d_name);
    len = readlink(filename, linkto, sizeof(link) - 1);
    if (strstr(linkto, "usbhid"))
      continue;
    sprintf(filename, "/sys/bus/usb/devices/%s/power/level", dirent->d_name);
    file = fopen(filename, "w");
    if (!file)
      continue;
    fprintf(file, "auto\n");
    fclose(file);
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<activate_usb_autosuspend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x2e51(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x20(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x50(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x28(%rbp)
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
lea    0xe17(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1209 <activate_usb_autosuspend+0x79>
movl   $0x1,-0x3c(%rbp)
jmp    12f1 <activate_usb_autosuspend+0x161>
jmp    120e <activate_usb_autosuspend+0x7e>
mov    -0x8(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     12e1 <activate_usb_autosuspend+0x151>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    123d <activate_usb_autosuspend+0xad>
jmp    120e <activate_usb_autosuspend+0x7e>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdc6(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    $0x3,%edx
call   1030 <readlink@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,-0x2c(%rbp)
lea    0xdc0(%rip),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%rax
je     1288 <activate_usb_autosuspend+0xf8>
jmp    120e <activate_usb_autosuspend+0x7e>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xda1(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x50(%rbp),%rdi
lea    0xdb5(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12c3 <activate_usb_autosuspend+0x133>
jmp    120e <activate_usb_autosuspend+0x7e>
mov    -0x18(%rbp),%rdi
lea    0xd93(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
jmp    120e <activate_usb_autosuspend+0x7e>
mov    -0x8(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<activate_usb_autosuspend>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    %rsp,%r14
sub    %rax,%r14
mov    %r14,%rsp
lea    0xe38(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1298 <activate_usb_autosuspend+0x108>
mov    %rax,%r15
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1290 <activate_usb_autosuspend+0x100>
mov    %rax,%r12
jmp    1204 <activate_usb_autosuspend+0x74>
nop
mov    %r15,%rdi
call   1070 <readdir@plt>
mov    %rax,%r12
test   %rax,%rax
je     1290 <activate_usb_autosuspend+0x100>
mov    (%r12),%rdx
cmpb   $0x2e,(%rdx)
je     11f0 <activate_usb_autosuspend+0x60>
mov    %r13,%rdi
lea    0xdfe(%rip),%rsi
call   1090 <sprintf@plt>
mov    %r13,%rdi
mov    %r14,%rsi
mov    $0x3,%edx
call   1030 <readlink@plt>
mov    %r14,%rdi
lea    0xdfe(%rip),%rsi
call   10b0 <strstr@plt>
test   %rax,%rax
jne    11f0 <activate_usb_autosuspend+0x60>
mov    (%r12),%rdx
mov    %r13,%rdi
lea    0xded(%rip),%rsi
call   1090 <sprintf@plt>
mov    %r13,%rdi
lea    0xe02(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f0 <activate_usb_autosuspend+0x60>
mov    %rax,%rbx
mov    $0x5,%esi
mov    $0x1,%edx
lea    0xde6(%rip),%rdi
mov    %rax,%rcx
call   10a0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11f0 <activate_usb_autosuspend+0x60>
mov    %r15,%rdi
call   1060 <closedir@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [21 x i8] c"/sys/bus/usb/devices\00"
@.str.1 = [31 x i8] c"/sys/bus/usb/devices/%s/driver\00"
@.str.2 = [7 x i8] c"usbhid\00"
@.str.3 = [36 x i8] c"/sys/bus/usb/devices/%s/power/level\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [6 x i8] c"auto\0A\00"
define void @activate_usb_autosuspend() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %4
%12 = alloca i8, i64 %10
store i64 %10, i64* %5
%13 = load i32, i32* @PATH_MAX
%14 = zext i32 %13 to i64
%15 = alloca i8, i64 %14
store i64 %14, i64* %7
%16 = call i32* @opendir(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %1
%17 = load i32*, i32** %1
%18 = icmp ne i32* %17, null
br i1 %18, label %20, label %19
19: ; preds = %0
store i32 1, i32* %8
br label %60
20: ; preds = %0
br label %21
21: ; preds = %52, %51, %42, %33, %20
%22 = load i32*, i32** %1
%23 = call %struct.dirent* @readdir(i32* %22)
store %struct.dirent* %23, %struct.dirent** %2
%24 = icmp ne %struct.dirent* %23, null
br i1 %24, label %25, label %57
25: ; preds = %21
%26 = load %struct.dirent*, %struct.dirent** %2
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = getelementptr inbounds i8, i8* %28, i64 0
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 46
br i1 %32, label %33, label %34
33: ; preds = %25
br label %21
34: ; preds = %25
%35 = load %struct.dirent*, %struct.dirent** %2
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %37)
%39 = call i32 @readlink(i8* %12, i8* %15, i32 3)
store i32 %39, i32* %6
%40 = call i64 @strstr(i8* %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %34
br label %21
43: ; preds = %34
%44 = load %struct.dirent*, %struct.dirent** %2
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 0
%46 = load i8*, i8** %45
%47 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %48, i32** %3
%49 = load i32*, i32** %3
%50 = icmp ne i32* %49, null
br i1 %50, label %52, label %51
51: ; preds = %43
br label %21
52: ; preds = %43
%53 = load i32*, i32** %3
%54 = call i32 @fprintf(i32* %53, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%55 = load i32*, i32** %3
%56 = call i32 @fclose(i32* %55)
br label %21
57: ; preds = %21
%58 = load i32*, i32** %1
%59 = call i32 @closedir(i32* %58)
store i32 0, i32* %8
br label %60
60: ; preds = %57, %19
%61 = load i8*, i8** %4
%62 = load i32, i32* %8
switch i32 %62, label %64 [
i32 0, label %63
i32 1, label %63
]
63: ; preds = %60, %60
ret void
64: ; preds = %60
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [21 x i8] c"/sys/bus/usb/devices\00"
@.str.1 = [31 x i8] c"/sys/bus/usb/devices/%s/driver\00"
@.str.2 = [7 x i8] c"usbhid\00"
@.str.3 = [36 x i8] c"/sys/bus/usb/devices/%s/power/level\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [6 x i8] c"auto\0A\00"
define void @activate_usb_autosuspend() {
%1 = load i32, i32* @PATH_MAX
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = alloca i8, i64 %2
%5 = tail call i32* @opendir(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %34, label %7
7: ; preds = %0
%8 = call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %32, label %10
10: ; preds = %7, %29
%11 = phi %struct.dirent* [ %30, %29 ], [ %8, %7 ]
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 46
br i1 %15, label %29, label %16
16: ; preds = %10
%17 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%18 = call i32 @readlink(i8* %3, i8* %4, i32 3)
%19 = call i64 @strstr(i8* %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %16
%22 = load i8*, i8** %12
%23 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %22)
%24 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %29, label %26
26: ; preds = %21
%27 = call i64 @fwrite(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i64 5, i64 1, i32* %24)
%28 = call i32 @fclose(i32* %24)
br label %29
29: ; preds = %26, %10, %16, %21
%30 = call %struct.dirent* @readdir(i32* %5)
%31 = icmp eq %struct.dirent* %30, null
br i1 %31, label %32, label %10
32: ; preds = %29, %7
%33 = call i32 @closedir(i32* %5)
br label %34
34: ; preds = %0, %32
ret void
}