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

/* Variables and functions */
int NAME_MAX;
int PATH_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t snprintf(char *, int, char *, char const *, char const *);
char *strchrnul(char *, char);
char *strdup(char *);
char *sysfs_read_attr(const char *syspath, const char *attr) {
  char path[PATH_MAX];
  char buf[NAME_MAX];
  char *buf_end = NULL;
  char *line = NULL;
  FILE *fp = NULL;
  ssize_t slen = 0;
  slen = snprintf(path, sizeof(path), "%s/%s", syspath, attr);
  if (slen <= 0 || slen == (ssize_t)sizeof(path)) {
    return NULL;
  } else {
    path[slen] = '\0';
  }
  if ((fp = fopen(path, "r")) != NULL) {
    line = fgets(buf, sizeof(buf), fp);
    fclose(fp);
  }
  if (line == NULL) {
    return NULL;
  }
  buf_end = strchrnul(line, '\n');
  *buf_end = '\0';
  if (*line == '\0') {
    return NULL;
  }
  return strdup(line);
}


x86 assembly (clang, -O0):
<sysfs_read_attr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x78(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x70(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    0x2e26(%rip),%rcx
mov    (%rcx),%ecx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x60(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xe03(%rip),%rdx
call   1050 <snprintf@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jbe    121f <sysfs_read_attr+0xbf>
mov    -0x78(%rbp),%rax
cmp    %rax,-0x50(%rbp)
jne    1233 <sysfs_read_attr+0xd3>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    12ed <sysfs_read_attr+0x18d>
mov    -0x70(%rbp),%rax
mov    -0x50(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x70(%rbp),%rdi
lea    0xdbc(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
je     127d <sysfs_read_attr+0x11d>
mov    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%rax
mov    %eax,%esi
mov    -0x48(%rbp),%rdx
call   1060 <fgets@plt>
mov    %rax,-0x40(%rbp)
mov    -0x48(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x40(%rbp)
jne    129c <sysfs_read_attr+0x13c>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    12ed <sysfs_read_attr+0x18d>
mov    -0x40(%rbp),%rdi
mov    $0xa,%esi
call   1030 <strchrnul@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x40(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    12d9 <sysfs_read_attr+0x179>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    12ed <sysfs_read_attr+0x18d>
mov    -0x40(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_read_attr>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r8
mov    %rdi,%rcx
mov    0x2e78(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e3c(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r15
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    0xe51(%rip),%rdx
mov    %rbx,%rdi
mov    %r12d,%esi
call   1050 <snprintf@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     1224 <sysfs_read_attr+0xc4>
cmp    %r12,%rcx
je     1224 <sysfs_read_attr+0xc4>
movb   $0x0,(%rbx,%rcx,1)
lea    0xe32(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1222 <sysfs_read_attr+0xc2>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %r14,%r14
je     1222 <sysfs_read_attr+0xc2>
mov    %r14,%rdi
mov    $0xa,%esi
call   1030 <strchrnul@plt>
movb   $0x0,(%rax)
cmpb   $0x0,(%r14)
je     1222 <sysfs_read_attr+0xc2>
mov    %r14,%rdi
call   1080 <strdup@plt>
jmp    1224 <sysfs_read_attr+0xc4>
xor    %eax,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@NAME_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [2 x i8] c"r\00"
define i8* @sysfs_read_attr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca i64
%13 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%14 = load i32, i32* @PATH_MAX
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %6
%17 = alloca i8, i64 %15
store i64 %15, i64* %7
%18 = load i32, i32* @NAME_MAX
%19 = zext i32 %18 to i64
%20 = alloca i8, i64 %19
store i64 %19, i64* %8
store i8* null, i8** %9
store i8* null, i8** %10
store i32* null, i32** %11
store i64 0, i64* %12
%21 = trunc i64 %15 to i32
%22 = load i8*, i8** %4
%23 = load i8*, i8** %5
%24 = call i64 @snprintf(i8* %17, i32 %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %22, i8* %23)
store i64 %24, i64* %12
%25 = load i64, i64* %12
%26 = icmp ule i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %2
%28 = load i64, i64* %12
%29 = icmp eq i64 %28, %15
br i1 %29, label %30, label %31
30: ; preds = %27, %2
store i8* null, i8** %3
store i32 1, i32* %13
br label %59
31: ; preds = %27
%32 = load i64, i64* %12
%33 = getelementptr inbounds i8, i8* %17, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %31
%35 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %35, i32** %11
%36 = icmp ne i32* %35, null
br i1 %36, label %37, label %43
37: ; preds = %34
%38 = trunc i64 %19 to i32
%39 = load i32*, i32** %11
%40 = call i8* @fgets(i8* %20, i32 %38, i32* %39)
store i8* %40, i8** %10
%41 = load i32*, i32** %11
%42 = call i32 @fclose(i32* %41)
br label %43
43: ; preds = %37, %34
%44 = load i8*, i8** %10
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %47
46: ; preds = %43
store i8* null, i8** %3
store i32 1, i32* %13
br label %59
47: ; preds = %43
%48 = load i8*, i8** %10
%49 = call i8* @strchrnul(i8* %48, i8 signext 10)
store i8* %49, i8** %9
%50 = load i8*, i8** %9
store i8 0, i8* %50
%51 = load i8*, i8** %10
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %47
store i8* null, i8** %3
store i32 1, i32* %13
br label %59
56: ; preds = %47
%57 = load i8*, i8** %10
%58 = call i8* @strdup(i8* %57)
store i8* %58, i8** %3
store i32 1, i32* %13
br label %59
59: ; preds = %56, %55, %46, %30
%60 = load i8*, i8** %6
%61 = load i8*, i8** %3
ret i8* %61
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@NAME_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [2 x i8] c"r\00"
define noalias i8* @sysfs_read_attr(i8* %0, i8* %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = load i32, i32* @NAME_MAX
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = call i64 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%10 = icmp eq i64 %9, 0
%11 = icmp eq i64 %9, %4
%12 = or i1 %10, %11
br i1 %12, label %27, label %13
13: ; preds = %2
%14 = getelementptr inbounds i8, i8* %5, i64 %9
store i8 0, i8* %14
%15 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %27, label %17
17: ; preds = %13
%18 = call i8* @fgets(i8* %8, i32 %6, i32* %15)
%19 = call i32 @fclose(i32* %15)
%20 = icmp eq i8* %18, null
br i1 %20, label %27, label %21
21: ; preds = %17
%22 = call i8* @strchrnul(i8* %18, i8 signext 10)
store i8 0, i8* %22
%23 = load i8, i8* %18
%24 = icmp eq i8 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %21
%26 = call i8* @strdup(i8* %18)
br label %27
27: ; preds = %13, %21, %17, %2, %25
%28 = phi i8* [ %26, %25 ], [ null, %2 ], [ null, %17 ], [ null, %21 ], [ null, %13 ]
ret i8* %28
}