#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 BUFSIZ;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(char *, char const *, char *);
unsigned char *malloc(int);
int open(char const *, int /*<<< orphan*/);
int read(int, unsigned char *, int);
char *strerror(int /*<<< orphan*/);
int sysfs_read(const char *path, unsigned char *buffer) {
  int ret;
  int fd;
  fd = open(path, O_RDONLY);
  if (fd < 0) {
    error("Failed to open %s : %s\n", path, strerror(errno));
    return fd;
  }
  int size = 0;
  unsigned char *buf = malloc(BUFSIZ * sizeof(char));
  if (buffer)
    buf = buffer;
  do {
    ret = read(fd, buf, BUFSIZ);
    if (ret == -1)
      break;
    size += ret;
    if (buffer)
      buf += ret;
  } while (ret > 0);
close:
  close(fd);
  if (ret < 0) {
    error("Failed to read file %s : %s\n", path, strerror(errno));
    size = ret;
  }
  return size;
}


x86 assembly (clang, -O0):
<sysfs_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11c3 <sysfs_read+0x63>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xe4d(%rip),%rdi
call   1060 <error@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a2 <sysfs_read+0x142>
movl   $0x0,-0x24(%rbp)
mov    0x2e07(%rip),%rax
movslq (%rax),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11f6 <sysfs_read+0x96>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11fb <sysfs_read+0x9b>
mov    -0x20(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1222 <sysfs_read+0xc2>
jmp    1256 <sysfs_read+0xf6>
mov    -0x1c(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1247 <sysfs_read+0xe7>
mov    -0x1c(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    124c <sysfs_read+0xec>
cmpl   $0x0,-0x1c(%rbp)
jg     11fb <sysfs_read+0x9b>
jmp    125b <sysfs_read+0xfb>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
cmpl   $0x0,-0x1c(%rbp)
jge    129c <sysfs_read+0x13c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
lea    0xd87(%rip),%rdi
call   1060 <error@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%rbx
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
js     1200 <sysfs_read+0xa0>
mov    %rbx,(%rsp)
mov    0x2e47(%rip),%r14
mov    (%r14),%ebx
mov    %ebx,%edi
call   1050 <malloc@plt>
mov    %rax,%rbp
test   %r12,%r12
cmovne %r12,%rbp
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1040 <read@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11e8 <sysfs_read+0x88>
xor    %r13d,%r13d
nopl   0x0(%rax)
add    %ebx,%r13d
test   %ebx,%ebx
jle    1238 <sysfs_read+0xd8>
test   %r12,%r12
mov    %ebx,%eax
cmove  %r12,%rax
add    %rax,%rbp
mov    (%r14),%edx
mov    %r15d,%edi
mov    %rbp,%rsi
call   1040 <read@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11c0 <sysfs_read+0x60>
mov    %r15d,%edi
call   1030 <close@plt>
lea    0xe21(%rip),%rbp
mov    %ebx,%r15d
mov    (%rsp),%rbx
jmp    1207 <sysfs_read+0xa7>
lea    0xdf9(%rip),%rbp
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1060 <error@plt>
mov    %r15d,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15d,%edi
call   1030 <close@plt>
test   %ebx,%ebx
jns    1226 <sysfs_read+0xc6>
jmp    11f0 <sysfs_read+0x90>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [24 x i8] c"Failed to open %s : %s\0A\00"
@errno = global i32 0
@BUFSIZ = global i32 0
@.str.1 = [29 x i8] c"Failed to read file %s : %s\0A\00"
define i32 @sysfs_read(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %18)
%20 = load i32, i32* %7
store i32 %20, i32* %3
br label %69
21: ; preds = %2
store i32 0, i32* %8
%22 = load i32, i32* @BUFSIZ
%23 = sext i32 %22 to i64
%24 = mul i64 %23, 1
%25 = trunc i64 %24 to i32
%26 = call i8* @malloc(i32 %25)
store i8* %26, i8** %9
%27 = load i8*, i8** %5
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %31
29: ; preds = %21
%30 = load i8*, i8** %5
store i8* %30, i8** %9
br label %31
31: ; preds = %29, %21
br label %32
32: ; preds = %52, %31
%33 = load i32, i32* %7
%34 = load i8*, i8** %9
%35 = load i32, i32* @BUFSIZ
%36 = call i32 @read(i32 %33, i8* %34, i32 %35)
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %40
39: ; preds = %32
br label %55
40: ; preds = %32
%41 = load i32, i32* %6
%42 = load i32, i32* %8
%43 = add nsw i32 %42, %41
store i32 %43, i32* %8
%44 = load i8*, i8** %5
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %51
46: ; preds = %40
%47 = load i32, i32* %6
%48 = load i8*, i8** %9
%49 = sext i32 %47 to i64
%50 = getelementptr inbounds i8, i8* %48, i64 %49
store i8* %50, i8** %9
br label %51
51: ; preds = %46, %40
br label %52
52: ; preds = %51
%53 = load i32, i32* %6
%54 = icmp sgt i32 %53, 0
br i1 %54, label %32, label %55
55: ; preds = %52, %39
br label %56
56: ; preds = %55
%57 = load i32, i32* %7
%58 = call i32 @close(i32 %57)
%59 = load i32, i32* %6
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %67
61: ; preds = %56
%62 = load i8*, i8** %4
%63 = load i32, i32* @errno
%64 = call i8* @strerror(i32 %63)
%65 = call i32 @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %62, i8* %64)
%66 = load i32, i32* %6
store i32 %66, i32* %8
br label %67
67: ; preds = %61, %56
%68 = load i32, i32* %8
store i32 %68, i32* %3
br label %69
69: ; preds = %67, %15
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [24 x i8] c"Failed to open %s : %s\0A\00"
@errno = global i32 0
@BUFSIZ = global i32 0
@.str.1 = [29 x i8] c"Failed to read file %s : %s\0A\00"
define i32 @sysfs_read(i8* %0, i8* %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %32, label %6
6: ; preds = %2
%7 = load i32, i32* @BUFSIZ
%8 = tail call i8* @malloc(i32 %7)
%9 = icmp eq i8* %1, null
%10 = select i1 %9, i8* %8, i8* %1
%11 = tail call i32 @read(i32 %4, i8* %10, i32 %7)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %22, %6
%14 = phi i32 [ %11, %6 ], [ %27, %22 ]
%15 = tail call i32 @close(i32 %4)
br label %32
16: ; preds = %6, %22
%17 = phi i32 [ %27, %22 ], [ %11, %6 ]
%18 = phi i8* [ %25, %22 ], [ %10, %6 ]
%19 = phi i32 [ %20, %22 ], [ 0, %6 ]
%20 = add nsw i32 %17, %19
%21 = icmp sgt i32 %17, 0
br i1 %21, label %22, label %29
22: ; preds = %16
%23 = zext i32 %17 to i64
%24 = select i1 %9, i64 0, i64 %23
%25 = getelementptr i8, i8* %18, i64 %24
%26 = load i32, i32* @BUFSIZ
%27 = tail call i32 @read(i32 %4, i8* %25, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %13, label %16
29: ; preds = %16
%30 = tail call i32 @close(i32 %4)
%31 = icmp slt i32 %17, 0
br i1 %31, label %32, label %38
32: ; preds = %29, %13, %2
%33 = phi i8* [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), %13 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), %29 ]
%34 = phi i32 [ %4, %2 ], [ %14, %13 ], [ %17, %29 ]
%35 = load i32, i32* @errno
%36 = tail call i8* @strerror(i32 %35)
%37 = tail call i32 @error(i8* %33, i8* %0, i8* %36)
br label %38
38: ; preds = %32, %29
%39 = phi i32 [ %20, %29 ], [ %34, %32 ]
ret i32 %39
}