#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
char *index(char *, char);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
char *realloc(char *, int);
char *get_sysfs_str(char *sysfs_attr) {
  int fd;
  char *string;
  ssize_t count;
  char *indx;
  if ((fd = open(sysfs_attr, O_RDONLY)) < 0)
    return NULL;
  if ((string = malloc(4096)) == NULL) {
    close(fd);
    return NULL;
  }
  if ((count = read(fd, string, 4096)) <= 0) {
    close(fd);
    free(string);
    return NULL;
  } else
    string = realloc(string, count + 1);
  if ((string == NULL) || (*string == 0)) {
    close(fd);
    return NULL;
  }
  indx = index(string, '\n');
  if (indx < (string + count))
    *indx = 0;
  close(fd);
  return string;
}


x86 assembly (clang, -O0):
<get_sysfs_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11a7 <get_sysfs_str+0x37>
movq   $0x0,-0x8(%rbp)
jmp    1293 <get_sysfs_str+0x123>
mov    $0x1000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11d4 <get_sysfs_str+0x64>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1293 <get_sysfs_str+0x123>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jg     120f <get_sysfs_str+0x9f>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1293 <get_sysfs_str+0x123>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
add    $0x1,%esi
call   1080 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     123d <get_sysfs_str+0xcd>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1252 <get_sysfs_str+0xe2>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1293 <get_sysfs_str+0x123>
mov    -0x20(%rbp),%rdi
mov    $0xa,%esi
call   1040 <index@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
movslq -0x24(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    1283 <get_sysfs_str+0x113>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_sysfs_str>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     120c <get_sysfs_str+0x9c>
mov    %eax,%ebp
mov    $0x1000,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11f4 <get_sysfs_str+0x84>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    11fd <get_sysfs_str+0x8d>
mov    %eax,%r14d
lea    0x1(%r14),%esi
mov    %rbx,%rdi
call   1080 <realloc@plt>
test   %rax,%rax
je     11f4 <get_sysfs_str+0x84>
mov    %rax,%rbx
cmpb   $0x0,(%rax)
je     11f4 <get_sysfs_str+0x84>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1040 <index@plt>
mov    %r14d,%ecx
mov    %rbx,%rdx
add    %rcx,%rdx
cmp    %rdx,%rax
jae    11eb <get_sysfs_str+0x7b>
movb   $0x0,(%rax)
mov    %ebp,%edi
call   1050 <close@plt>
jmp    120e <get_sysfs_str+0x9e>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    120c <get_sysfs_str+0x9c>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i8* @get_sysfs_str(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i8* null, i8** %2
br label %60
13: ; preds = %1
%14 = call i8* @malloc(i32 4096)
store i8* %14, i8** %5
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* %4
%18 = call i32 @close(i32 %17)
store i8* null, i8** %2
br label %60
19: ; preds = %13
%20 = load i32, i32* %4
%21 = load i8*, i8** %5
%22 = call i32 @read(i32 %20, i8* %21, i32 4096)
store i32 %22, i32* %6
%23 = icmp sle i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i32, i32* %4
%26 = call i32 @close(i32 %25)
%27 = load i8*, i8** %5
%28 = call i32 @free(i8* %27)
store i8* null, i8** %2
br label %60
29: ; preds = %19
%30 = load i8*, i8** %5
%31 = load i32, i32* %6
%32 = add nsw i32 %31, 1
%33 = call i8* @realloc(i8* %30, i32 %32)
store i8* %33, i8** %5
br label %34
34: ; preds = %29
%35 = load i8*, i8** %5
%36 = icmp eq i8* %35, null
br i1 %36, label %42, label %37
37: ; preds = %34
%38 = load i8*, i8** %5
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %37, %34
%43 = load i32, i32* %4
%44 = call i32 @close(i32 %43)
store i8* null, i8** %2
br label %60
45: ; preds = %37
%46 = load i8*, i8** %5
%47 = call i8* @index(i8* %46, i8 signext 10)
store i8* %47, i8** %7
%48 = load i8*, i8** %7
%49 = load i8*, i8** %5
%50 = load i32, i32* %6
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
%53 = icmp ult i8* %48, %52
br i1 %53, label %54, label %56
54: ; preds = %45
%55 = load i8*, i8** %7
store i8 0, i8* %55
br label %56
56: ; preds = %54, %45
%57 = load i32, i32* %4
%58 = call i32 @close(i32 %57)
%59 = load i8*, i8** %5
store i8* %59, i8** %2
br label %60
60: ; preds = %56, %42, %24, %16, %12
%61 = load i8*, i8** %2
ret i8* %61
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i8* @get_sysfs_str(i8* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %33, label %5
5: ; preds = %1
%6 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @close(i32 %3)
br label %33
10: ; preds = %5
%11 = tail call i32 @read(i32 %3, i8* %6, i32 4096)
%12 = icmp slt i32 %11, 1
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i32 @close(i32 %3)
%15 = tail call i32 @free(i8* %6)
br label %33
16: ; preds = %10
%17 = add nuw nsw i32 %11, 1
%18 = tail call i8* @realloc(i8* %6, i32 %17)
%19 = icmp eq i8* %18, null
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = load i8, i8* %18
%22 = icmp eq i8 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20, %16
%24 = tail call i32 @close(i32 %3)
br label %33
25: ; preds = %20
%26 = tail call i8* @index(i8* %18, i8 signext 10)
%27 = zext i32 %11 to i64
%28 = getelementptr inbounds i8, i8* %18, i64 %27
%29 = icmp ult i8* %26, %28
br i1 %29, label %30, label %31
30: ; preds = %25
store i8 0, i8* %26
br label %31
31: ; preds = %30, %25
%32 = tail call i32 @close(i32 %3)
br label %33
33: ; preds = %1, %31, %23, %13, %8
%34 = phi i8* [ null, %8 ], [ null, %13 ], [ null, %23 ], [ %18, %31 ], [ null, %1 ]
ret i8* %34
}