#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 EINVAL;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int read_sys_file_int(const char *filename, int *value) {
  int err;
  FILE *f = fopen(filename, "r");
  if (f == NULL)
    return -errno;
  err = fscanf(f, "%d", value);
  fclose(f);
  if (err != 1)
    return (err < 0) ? err : -EINVAL;
  return 0;
}


x86 assembly (clang, -O0):
<read_sys_file_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1172 <read_sys_file_int+0x42>
xor    %eax,%eax
mov    0x2e78(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11d1 <read_sys_file_int+0xa1>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe81(%rip),%rsi
call   1040 <fscanf@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x1,-0x1c(%rbp)
je     11ca <read_sys_file_int+0x9a>
cmpl   $0x0,-0x1c(%rbp)
jge    11b1 <read_sys_file_int+0x81>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    11bf <read_sys_file_int+0x8f>
xor    %eax,%eax
mov    0x2e1e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d1 <read_sys_file_int+0xa1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_sys_file_int>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
lea    0xec2(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1180 <read_sys_file_int+0x50>
mov    %rax,%rbx
lea    0xeb0(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1040 <fscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
cmp    $0x1,%ebp
je     118b <read_sys_file_int+0x5b>
mov    0x2e63(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
test   %ebp,%ebp
cmovs  %ebp,%eax
jmp    118b <read_sys_file_int+0x5b>
mov    0x2e59(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@EINVAL = global i32 0
define i32 @read_sys_file_int(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32*
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** %7
%10 = load i32*, i32** %7
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = sub nsw i32 0, %13
store i32 %14, i32* %3
br label %34
15: ; preds = %2
%16 = load i32*, i32** %7
%17 = load i32*, i32** %5
%18 = call i32 @fscanf(i32* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %17)
store i32 %18, i32* %6
%19 = load i32*, i32** %7
%20 = call i32 @fclose(i32* %19)
%21 = load i32, i32* %6
%22 = icmp ne i32 %21, 1
br i1 %22, label %23, label %33
23: ; preds = %15
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = load i32, i32* %6
br label %31
28: ; preds = %23
%29 = load i32, i32* @EINVAL
%30 = sub nsw i32 0, %29
br label %31
31: ; preds = %28, %26
%32 = phi i32 [ %27, %26 ], [ %30, %28 ]
store i32 %32, i32* %3
br label %34
33: ; preds = %15
store i32 0, i32* %3
br label %34
34: ; preds = %33, %31, %12
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@EINVAL = global i32 0
define i32 @read_sys_file_int(i8* nocapture %0, i32* %1) {
%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 %5, label %8
5: ; preds = %2
%6 = load i32, i32* @errno
%7 = sub nsw i32 0, %6
br label %17
8: ; preds = %2
%9 = tail call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%10 = tail call i32 @fclose(i32* %3)
%11 = icmp eq i32 %9, 1
br i1 %11, label %17, label %12
12: ; preds = %8
%13 = icmp slt i32 %9, 0
%14 = load i32, i32* @EINVAL
%15 = sub nsw i32 0, %14
%16 = select i1 %13, i32 %9, i32 %15
br label %17
17: ; preds = %8, %12, %5
%18 = phi i32 [ %7, %5 ], [ %16, %12 ], [ 0, %8 ]
ret i32 %18
}