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

/* Variables and functions */
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, long, int /*<<< orphan*/ *);
int inv_sysfs_read(char *filename, long num_bytes, char *data) {
  FILE *fp;
  int count;
  if (!filename)
    return -1;
  fp = fopen(filename, "r");
  if (!fp)
    return -errno;
  count = fread(data, 1, num_bytes, fp);
  fclose(fp);
  return count;
}


x86 assembly (clang, -O0):
<inv_sysfs_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115b <inv_sysfs_read+0x2b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b5 <inv_sysfs_read+0x85>
mov    -0x10(%rbp),%rdi
lea    0xe9a(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118d <inv_sysfs_read+0x5d>
xor    %eax,%eax
mov    0x2e5d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11b5 <inv_sysfs_read+0x85>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inv_sysfs_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1177 <inv_sysfs_read+0x47>
mov    %rdx,%r15
mov    %rsi,%r14
lea    0xeb7(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     117e <inv_sysfs_read+0x4e>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
jmp    1189 <inv_sysfs_read+0x59>
mov    $0xffffffff,%eax
jmp    1189 <inv_sysfs_read+0x59>
mov    0x2e5b(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
define i32 @inv_sysfs_read(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %3
store i32 -1, i32* %4
br label %29
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %8
%16 = load i32*, i32** %8
%17 = icmp ne i32* %16, null
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = load i32, i32* @errno
%20 = sub nsw i32 0, %19
store i32 %20, i32* %4
br label %29
21: ; preds = %13
%22 = load i8*, i8** %7
%23 = load i64, i64* %6
%24 = load i32*, i32** %8
%25 = call i32 @fread(i8* %22, i32 1, i64 %23, i32* %24)
store i32 %25, i32* %9
%26 = load i32*, i32** %8
%27 = call i32 @fclose(i32* %26)
%28 = load i32, i32* %9
store i32 %28, i32* %4
br label %29
29: ; preds = %21, %18, %12
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
define i32 @inv_sysfs_read(i8* %0, i64 %1, i8* nocapture %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %14, label %5
5: ; preds = %3
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = load i32, i32* @errno
%10 = sub nsw i32 0, %9
br label %14
11: ; preds = %5
%12 = tail call i32 @fread(i8* %2, i32 1, i64 %1, i32* %6)
%13 = tail call i32 @fclose(i32* %6)
br label %14
14: ; preds = %3, %11, %8
%15 = phi i32 [ %12, %11 ], [ %10, %8 ], [ -1, %3 ]
ret i32 %15
}