#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 atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int sysfs_read(char *file) {
  FILE *f;
  int ret = -1;
  f = fopen(file, "r");
  if (f != NULL) {
    char tmp[64];
    if (-1 != fread(tmp, 64, 1, f)) {
      ret = atoi(tmp);
    } else {
      perror("fread");
    }
  } else {
    perror("fopen");
  }
  fclose(f);
  return ret;
}


x86 assembly (clang, -O0):
<sysfs_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ca <sysfs_read+0x7a>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x40,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     11b9 <sysfs_read+0x69>
lea    -0x60(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x14(%rbp)
jmp    11c5 <sysfs_read+0x75>
lea    0xe42(%rip),%rdi
call   1060 <perror@plt>
jmp    11d6 <sysfs_read+0x86>
lea    0xe37(%rip),%rdi
call   1060 <perror@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x14(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_read>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
lea    0xea3(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     119f <sysfs_read+0x4f>
mov    %rsp,%rdi
mov    $0x40,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0xffffffff,%eax
je     11a8 <sysfs_read+0x58>
mov    %rsp,%rdi
call   1070 <atoi@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe62(%rip),%rdi
jmp    11af <sysfs_read+0x5f>
lea    0xe53(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebp
jmp    118e <sysfs_read+0x3e>

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

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