#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
char *strdup(char *);
char *read_sysfs_str(char *attr_path) {
  int fd, count;
  char buf[256];
  fd = open(attr_path, O_RDONLY);
  free(attr_path);
  if (fd == -1)
    return NULL;
  count = read(fd, buf, 256);
  close(fd);
  if (count == -1)
    return NULL;
  buf[count - 1] = '\0';
  return strdup(buf);
}


x86 assembly (clang, -O0):
<read_sysfs_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0xffffffff,-0x14(%rbp)
jne    1194 <read_sysfs_str+0x44>
movq   $0x0,-0x8(%rbp)
jmp    11ea <read_sysfs_str+0x9a>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmpl   $0xffffffff,-0x18(%rbp)
jne    11ca <read_sysfs_str+0x7a>
movq   $0x0,-0x8(%rbp)
jmp    11ea <read_sysfs_str+0x9a>
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x120(%rbp,%rax,1)
lea    -0x120(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_sysfs_str>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%rbp
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <free@plt>
cmp    $0xffffffff,%ebx
je     11ae <read_sysfs_str+0x5e>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%ebp
je     11ae <read_sysfs_str+0x5e>
movslq %ebp,%rax
movb   $0x0,-0x1(%rax,%r14,1)
mov    %r14,%rdi
call   1070 <strdup@plt>
jmp    11b0 <read_sysfs_str+0x60>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i8* @read_sysfs_str(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i8]
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %4
%10 = load i8*, i8** %3
%11 = call i32 @free(i8* %10)
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %1
store i8* null, i8** %2
br label %31
15: ; preds = %1
%16 = load i32, i32* %4
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%18 = call i32 @read(i32 %16, i8* %17, i32 256)
store i32 %18, i32* %5
%19 = load i32, i32* %4
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %15
store i8* null, i8** %2
br label %31
24: ; preds = %15
%25 = load i32, i32* %5
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %27
store i8 0, i8* %28
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%30 = call i8* @strdup(i8* %29)
store i8* %30, i8** %2
br label %31
31: ; preds = %24, %23, %14
%32 = load i8*, i8** %2
ret i8* %32
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define noalias i8* @read_sysfs_str(i8* nocapture %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = tail call i32 @free(i8* %0)
%7 = icmp eq i32 %5, -1
br i1 %7, label %17, label %8
8: ; preds = %1
%9 = call i32 @read(i32 %5, i8* %3, i32 256)
%10 = tail call i32 @close(i32 %5)
%11 = icmp eq i32 %9, -1
br i1 %11, label %17, label %12
12: ; preds = %8
%13 = add nsw i32 %9, -1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i8* @strdup(i8* %3)
br label %17
17: ; preds = %8, %1, %12
%18 = phi i8* [ %16, %12 ], [ null, %1 ], [ null, %8 ]
ret i8* %18
}