#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 */
typedef int /*<<< orphan*/ path;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int PATH_MAX;
int /*<<< orphan*/ close(int);
scalar_t__ isspace(char);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *);
char *sysfs_attr(const char *dev, const char *attr) {
  int fd, len = 0;
  char path[PATH_MAX];
  static char buf[129];
  memset(buf, 0, sizeof(buf));
  snprintf(path, sizeof(path) - 1, "/sys/bus/usb/devices/%s/%s", dev, attr);
  fd = open(path, O_RDONLY);
  if (fd >= 0) {
    len = read(fd, buf, sizeof(buf) - 1);
    close(fd);
  }
  while (--len > 0 && isspace(buf[len]))
    buf[len] = 0;
  return (len >= 0) ? buf : NULL;
}


x86 assembly (clang, -O0):
<sysfs_attr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x38(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
lea    0x2ec2(%rip),%rdi
xor    %esi,%esi
mov    $0x81,%edx
call   1040 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
sub    $0x1,%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xe29(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x30(%rbp),%rdi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     121a <sysfs_attr+0xba>
mov    -0x14(%rbp),%edi
lea    0x2e6b(%rip),%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
jmp    121f <sysfs_attr+0xbf>
mov    -0x18(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x18(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x39(%rbp)
jle    1254 <sysfs_attr+0xf4>
movslq -0x18(%rbp),%rcx
lea    0x2e2f(%rip),%rax
movsbl (%rax,%rcx,1),%edi
call   1060 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    1264 <sysfs_attr+0x104>
jmp    1278 <sysfs_attr+0x118>
movslq -0x18(%rbp),%rcx
lea    0x2e01(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
jmp    121f <sysfs_attr+0xbf>
mov    -0x18(%rbp),%edx
xor    %eax,%eax
lea    0x2dec(%rip),%rcx
cmp    $0x0,%edx
cmovge %rcx,%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_attr>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r12
mov    0x2e75(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%rbx
mov    %r13,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0x2ed7(%rip),%rdi
xor    %r14d,%r14d
xor    %esi,%esi
mov    $0x81,%edx
call   1040 <memset@plt>
lea    -0x1(%r13),%esi
lea    0xe4d(%rip),%rdx
mov    %rbx,%rdi
mov    %r12,%rcx
mov    %r15,%r8
call   1030 <snprintf@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1245 <sysfs_attr+0xe5>
mov    %eax,%r15d
lea    0x2e90(%rip),%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %eax,%r12d
mov    %r15d,%edi
call   1050 <close@plt>
cmp    $0x2,%r12d
jl     123d <sysfs_attr+0xdd>
mov    %r12d,%r12d
lea    0x2e66(%rip),%r15
nopw   0x0(%rax,%rax,1)
lea    -0x1(%r12),%rbx
mov    %ebx,%r13d
movsbl 0x0(%r13,%r15,1),%edi
call   1060 <isspace@plt>
test   %rax,%rax
je     123d <sysfs_attr+0xdd>
movb   $0x0,0x0(%r13,%r15,1)
lea    0x1(%rbx),%rax
mov    %rbx,%r12
cmp    $0x2,%rax
ja     1210 <sysfs_attr+0xb0>
jmp    1245 <sysfs_attr+0xe5>
test   %r12d,%r12d
jg     1245 <sysfs_attr+0xe5>
xor    %r14d,%r14d
mov    %r14,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@sysfs_attr.buf = internal global [129 x i8] zeroinitializer
@.str = [27 x i8] c"/sys/bus/usb/devices/%s/%s\00"
@O_RDONLY = global i32 0
define i8* @sysfs_attr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %6
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %7
%12 = alloca i8, i64 %10
store i64 %10, i64* %8
%13 = call i32 @memset(i8* getelementptr inbounds ([129 x i8], [129 x i8]* @sysfs_attr.buf, i64 0, i64 0), i32 0, i32 129)
%14 = sub i64 %10, 1
%15 = trunc i64 %14 to i32
%16 = load i8*, i8** %3
%17 = load i8*, i8** %4
%18 = call i32 @snprintf(i8* %12, i32 %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %17)
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* %12, i32 %19)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp sge i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %2
%24 = load i32, i32* %5
%25 = call i32 @read(i32 %24, i8* getelementptr inbounds ([129 x i8], [129 x i8]* @sysfs_attr.buf, i64 0, i64 0), i32 128)
store i32 %25, i32* %6
%26 = load i32, i32* %5
%27 = call i32 @close(i32 %26)
br label %28
28: ; preds = %23, %2
br label %29
29: ; preds = %42, %28
%30 = load i32, i32* %6
%31 = add nsw i32 %30, -1
store i32 %31, i32* %6
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %29
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [129 x i8], [129 x i8]* @sysfs_attr.buf, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = call i64 @isspace(i8 signext %37)
%39 = icmp ne i64 %38, 0
br label %40
40: ; preds = %33, %29
%41 = phi i1 [ false, %29 ], [ %39, %33 ]
br i1 %41, label %42, label %46
42: ; preds = %40
%43 = load i32, i32* %6
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [129 x i8], [129 x i8]* @sysfs_attr.buf, i64 0, i64 %44
store i8 0, i8* %45
br label %29
46: ; preds = %40
%47 = load i32, i32* %6
%48 = icmp sge i32 %47, 0
%49 = zext i1 %48 to i64
%50 = select i1 %48, i8* getelementptr inbounds ([129 x i8], [129 x i8]* @sysfs_attr.buf, i64 0, i64 0), i8* null
%51 = load i8*, i8** %7
ret i8* %50
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@sysfs_attr.buf = internal global [129 x i8] zeroinitializer
@.str = [27 x i8] c"/sys/bus/usb/devices/%s/%s\00"
@O_RDONLY = global i32 0
define i8* @sysfs_attr(i8* %0, i8* %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = tail call i32 @memset(i8* getelementptr inbounds ([129 x i8], [129 x i8]* @sysfs_attr.buf, i64 0, i64 0), i32 0, i32 129)
%7 = add i32 %3, -1
%8 = call i32 @snprintf(i8* %5, i32 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %5, i32 %9)
%11 = icmp sgt i32 %10, -1
br i1 %11, label %12, label %34
12: ; preds = %2
%13 = call i32 @read(i32 %10, i8* getelementptr inbounds ([129 x i8], [129 x i8]* @sysfs_attr.buf, i64 0, i64 0), i32 128)
%14 = call i32 @close(i32 %10)
%15 = icmp sgt i32 %13, 1
br i1 %15, label %16, label %30
16: ; preds = %12
%17 = zext i32 %13 to i64
br label %18
18: ; preds = %16, %26
%19 = phi i64 [ %17, %16 ], [ %20, %26 ]
%20 = add nsw i64 %19, -1
%21 = and i64 %20, 4294967295
%22 = getelementptr inbounds [129 x i8], [129 x i8]* @sysfs_attr.buf, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = call i64 @isspace(i8 signext %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %18
store i8 0, i8* %22
%27 = icmp ugt i64 %19, 2
br i1 %27, label %18, label %33
28: ; preds = %18
%29 = trunc i64 %19 to i32
br label %30
30: ; preds = %28, %12
%31 = phi i32 [ %13, %12 ], [ %29, %28 ]
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %26, %30
br label %34
34: ; preds = %2, %30, %33
%35 = phi i8* [ getelementptr inbounds ([129 x i8], [129 x i8]* @sysfs_attr.buf, i64 0, i64 0), %33 ], [ null, %30 ], [ null, %2 ]
ret i8* %35
}