#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*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EINVAL;
int EIO;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int strtol(char *, int /*<<< orphan*/ *, int);
int sysfs_readu(const char *path, int *result) {
  FILE *fp;
  char line[24];
  fp = fopen(path, "r");
  if (!fp)
    return -EIO;
  if (!fgets(line, sizeof(line), fp)) {
    fclose(fp);
    return -EIO;
  }
  *result = strtol(line, NULL, 10);
  fclose(fp);
  return (*result >= 0) ? 0 : -EINVAL;
}


x86 assembly (clang, -O0):
<sysfs_readu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1182 <sysfs_readu+0x42>
xor    %eax,%eax
mov    0x2e68(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1207 <sysfs_readu+0xc7>
lea    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x18,%esi
call   1040 <fgets@plt>
cmp    $0x0,%eax
jne    11b9 <sysfs_readu+0x79>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
mov    0x2e31(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1207 <sysfs_readu+0xc7>
lea    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jl     11f3 <sysfs_readu+0xb3>
xor    %eax,%eax
mov    %eax,-0x44(%rbp)
jmp    1201 <sysfs_readu+0xc1>
xor    %eax,%eax
mov    0x2ddc(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_readu>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
lea    0xead(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11ae <sysfs_readu+0x6e>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x18,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %eax,%eax
je     11a6 <sysfs_readu+0x66>
xor    %r15d,%r15d
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,(%r14)
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    (%r14),%eax
mov    0x2e3d(%rip),%rcx
sub    (%rcx),%r15d
sar    $0x1f,%eax
and    %r15d,%eax
jmp    11b9 <sysfs_readu+0x79>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2e2b(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@EIO = global i32 0
@EINVAL = global i32 0
define i32 @sysfs_readu(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca [24 x i8]
store i8* %0, i8** %4
store i32* %1, i32** %5
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i32, i32* @EIO
%14 = sub nsw i32 0, %13
store i32 %14, i32* %3
br label %40
15: ; preds = %2
%16 = getelementptr inbounds [24 x i8], [24 x i8]* %7, i64 0, i64 0
%17 = load i32*, i32** %6
%18 = call i32 @fgets(i8* %16, i32 24, i32* %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = load i32*, i32** %6
%22 = call i32 @fclose(i32* %21)
%23 = load i32, i32* @EIO
%24 = sub nsw i32 0, %23
store i32 %24, i32* %3
br label %40
25: ; preds = %15
%26 = getelementptr inbounds [24 x i8], [24 x i8]* %7, i64 0, i64 0
%27 = call i32 @strtol(i8* %26, i32* null, i32 10)
%28 = load i32*, i32** %5
store i32 %27, i32* %28
%29 = load i32*, i32** %6
%30 = call i32 @fclose(i32* %29)
%31 = load i32*, i32** %5
%32 = load i32, i32* %31
%33 = icmp sge i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %25
br label %38
35: ; preds = %25
%36 = load i32, i32* @EINVAL
%37 = sub nsw i32 0, %36
br label %38
38: ; preds = %35, %34
%39 = phi i32 [ 0, %34 ], [ %37, %35 ]
store i32 %39, i32* %3
br label %40
40: ; preds = %38, %20, %12
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EIO = global i32 0
@EINVAL = global i32 0
define i32 @sysfs_readu(i8* nocapture %0, i32* nocapture %1) {
%3 = alloca [24 x i8]
%4 = getelementptr inbounds [24 x i8], [24 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @EIO
%9 = sub nsw i32 0, %8
br label %25
10: ; preds = %2
%11 = call i32 @fgets(i8* %4, i32 24, i32* %5)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = call i32 @fclose(i32* %5)
%15 = load i32, i32* @EIO
%16 = sub nsw i32 0, %15
br label %25
17: ; preds = %10
%18 = call i32 @strtol(i8* nocapture %4, i32* null, i32 10)
store i32 %18, i32* %1
%19 = call i32 @fclose(i32* %5)
%20 = load i32, i32* %1
%21 = load i32, i32* @EINVAL
%22 = sub nsw i32 0, %21
%23 = icmp slt i32 %20, 0
%24 = select i1 %23, i32 %22, i32 0
br label %25
25: ; preds = %17, %13, %7
%26 = phi i32 [ %24, %17 ], [ %16, %13 ], [ %9, %7 ]
ret i32 %26
}