#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 atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int read_int_from_sysfs_node(const char *filename) {
  int fd;
  int ret = -1;
  char buf[64] = {0};
  fd = open(filename, O_RDONLY);
  if (fd == -1) {
    fprintf(stderr, "can't read from file '%s': %s\n", filename,
            strerror(errno));
    return ret;
  }
  (void)read(fd, buf, 63);
  ret = atoi(buf);
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<read_int_from_sysfs_node>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f5 <read_int_from_sysfs_node+0x85>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rdx
mov    %rax,%rcx
lea    0xe1b(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1220 <read_int_from_sysfs_node+0xb0>
mov    -0x14(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x3f,%edx
call   1050 <read@plt>
lea    -0x60(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_int_from_sysfs_node>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    %rdi,%rbx
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11b9 <read_int_from_sysfs_node+0x59>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x3f,%edx
call   1040 <read@plt>
mov    %rbx,%rdi
call   1070 <atoi@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
jmp    11e9 <read_int_from_sysfs_node+0x89>
mov    0x2e18(%rip),%rax
mov    (%rax),%ebp
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe29(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"can't read from file '%s': %s\0A\00"
@errno = global i32 0
define i32 @read_int_from_sysfs_node(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [64 x i8]
store i8* %0, i8** %3
store i32 -1, i32* %5
%7 = bitcast [64 x i8]* %6 to i8*
%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 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %20
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %3
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %17)
%19 = load i32, i32* %5
store i32 %19, i32* %2
br label %29
20: ; preds = %1
%21 = load i32, i32* %4
%22 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 63)
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %5
%26 = load i32, i32* %4
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %5
store i32 %28, i32* %2
br label %29
29: ; preds = %20, %13
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"can't read from file '%s': %s\0A\00"
@errno = global i32 0
define i32 @read_int_from_sysfs_node(i8* %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %10)
br label %16
12: ; preds = %1
%13 = call i32 @read(i32 %5, i8* %3, i32 63)
%14 = call i32 @atoi(i8* %3)
%15 = tail call i32 @close(i32 %5)
br label %16
16: ; preds = %12, %7
%17 = phi i32 [ -1, %7 ], [ %14, %12 ]
ret i32 %17
}