#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*/ linkto;
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*/ memset(char *, int /*<<< orphan*/, int);
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);
    memset(linkto, 0, sizeof(linkto));
    len = readlink(filename, linkto, sizeof(link) - 1);
    if (strstr(linkto, "usbhid"))
      continue;
    sprintf(filename, "/sys/bus/usb/devices/%s/power/control", 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    $0x60,%rsp
mov    0x2e41(%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,-0x58(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x28(%rbp)
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
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    0xe03(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    121d <activate_usb_autosuspend+0x7d>
movl   $0x1,-0x3c(%rbp)
jmp    1316 <activate_usb_autosuspend+0x176>
jmp    1222 <activate_usb_autosuspend+0x82>
mov    -0x8(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1306 <activate_usb_autosuspend+0x166>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1251 <activate_usb_autosuspend+0xb1>
jmp    1222 <activate_usb_autosuspend+0x82>
mov    -0x58(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdb2(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x58(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    $0x3,%edx
call   1030 <readlink@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,-0x2c(%rbp)
lea    0xd9b(%rip),%rsi
call   10c0 <strstr@plt>
cmp    $0x0,%rax
je     12ad <activate_usb_autosuspend+0x10d>
jmp    1222 <activate_usb_autosuspend+0x82>
mov    -0x58(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd7c(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x58(%rbp),%rdi
lea    0xd92(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12e8 <activate_usb_autosuspend+0x148>
jmp    1222 <activate_usb_autosuspend+0x82>
mov    -0x18(%rbp),%rdi
lea    0xd70(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1222 <activate_usb_autosuspend+0x82>
mov    -0x8(%rbp),%rdi
call   1070 <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    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
mov    %rax,-0x30(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %rsp,%r14
sub    %rax,%r14
mov    %r14,%rsp
lea    0xe24(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     12ca <activate_usb_autosuspend+0x12a>
mov    %rax,%r12
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     12c2 <activate_usb_autosuspend+0x122>
mov    %rax,%r13
jmp    1224 <activate_usb_autosuspend+0x84>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
call   1080 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12c2 <activate_usb_autosuspend+0x122>
mov    0x0(%r13),%rdx
cmpb   $0x2e,(%rdx)
je     1210 <activate_usb_autosuspend+0x70>
mov    %r15,%rdi
lea    0xdde(%rip),%rsi
call   10a0 <sprintf@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    -0x30(%rbp),%rdx
call   1060 <memset@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x3,%edx
call   1030 <readlink@plt>
mov    %r14,%rdi
lea    0xdd0(%rip),%rsi
call   10c0 <strstr@plt>
test   %rax,%rax
jne    1210 <activate_usb_autosuspend+0x70>
mov    0x0(%r13),%rdx
mov    %r15,%rdi
lea    0xdbf(%rip),%rsi
call   10a0 <sprintf@plt>
mov    %r15,%rdi
lea    0xdd6(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1210 <activate_usb_autosuspend+0x70>
mov    %rax,%rbx
mov    $0x5,%esi
mov    $0x1,%edx
lea    0xdb6(%rip),%rdi
mov    %rax,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1210 <activate_usb_autosuspend+0x70>
mov    %r12,%rdi
call   1070 <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 = [38 x i8] c"/sys/bus/usb/devices/%s/power/control\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 %62
20: ; preds = %0
br label %21
21: ; preds = %54, %53, %44, %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 %59
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 = trunc i64 %14 to i32
%40 = call i32 @memset(i8* %15, i32 0, i32 %39)
%41 = call i32 @readlink(i8* %12, i8* %15, i32 3)
store i32 %41, i32* %6
%42 = call i64 @strstr(i8* %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %34
br label %21
45: ; preds = %34
%46 = load %struct.dirent*, %struct.dirent** %2
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %48)
%50 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %50, i32** %3
%51 = load i32*, i32** %3
%52 = icmp ne i32* %51, null
br i1 %52, label %54, label %53
53: ; preds = %45
br label %21
54: ; preds = %45
%55 = load i32*, i32** %3
%56 = call i32 @fprintf(i32* %55, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%57 = load i32*, i32** %3
%58 = call i32 @fclose(i32* %57)
br label %21
59: ; preds = %21
%60 = load i32*, i32** %1
%61 = call i32 @closedir(i32* %60)
store i32 0, i32* %8
br label %62
62: ; preds = %59, %19
%63 = load i8*, i8** %4
%64 = load i32, i32* %8
switch i32 %64, label %66 [
i32 0, label %65
i32 1, label %65
]
65: ; preds = %62, %62
ret void
66: ; preds = %62
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 = [38 x i8] c"/sys/bus/usb/devices/%s/power/control\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 %35, label %7
7: ; preds = %0
%8 = call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %33, label %10
10: ; preds = %7, %30
%11 = phi %struct.dirent* [ %31, %30 ], [ %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 %30, 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 @memset(i8* %4, i32 0, i32 %1)
%19 = call i32 @readlink(i8* %3, i8* %4, i32 3)
%20 = call i64 @strstr(i8* %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %16
%23 = load i8*, i8** %12
%24 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %23)
%25 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%26 = icmp eq i32* %25, null
br i1 %26, label %30, label %27
27: ; preds = %22
%28 = call i64 @fwrite(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i64 5, i64 1, i32* %25)
%29 = call i32 @fclose(i32* %25)
br label %30
30: ; preds = %27, %10, %16, %22
%31 = call %struct.dirent* @readdir(i32* %5)
%32 = icmp eq %struct.dirent* %31, null
br i1 %32, label %33, label %10
33: ; preds = %30, %7
%34 = call i32 @closedir(i32* %5)
br label %35
35: ; preds = %0, %33
ret void
}