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

/* Variables and functions */
long atol(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int read_proc_val(const char *valname, long *rval) {
  FILE *fp;
  char buf[120];
  fp = fopen(valname, "r");
  if (fp == 0) {
    return -1;
  }
  fread(buf, 1, sizeof(buf), fp);
  *rval = atol(buf);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<read_proc_val>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117e <read_proc_val+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <read_proc_val+0x7e>
lea    -0xa0(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x78,%edx
call   1030 <fread@plt>
lea    -0xa0(%rbp),%rdi
call   1050 <atol@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_proc_val>:
push   %r15
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %rsi,%r14
lea    0xeaa(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1192 <read_proc_val+0x52>
mov    %rax,%rbx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x78,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %r15,%rdi
call   1050 <atol@plt>
mov    %rax,(%r14)
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    1197 <read_proc_val+0x57>
mov    $0xffffffff,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @read_proc_val(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32*
%7 = alloca [120 x i8]
store i8* %0, i8** %4
store i64* %1, i64** %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 eq i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %22
13: ; preds = %2
%14 = getelementptr inbounds [120 x i8], [120 x i8]* %7, i64 0, i64 0
%15 = load i32*, i32** %6
%16 = call i32 @fread(i8* %14, i32 1, i32 120, i32* %15)
%17 = getelementptr inbounds [120 x i8], [120 x i8]* %7, i64 0, i64 0
%18 = call i64 @atol(i8* %17)
%19 = load i64*, i64** %5
store i64 %18, i64* %19
%20 = load i32*, i32** %6
%21 = call i32 @fclose(i32* %20)
store i32 0, i32* %3
br label %22
22: ; preds = %13, %12
%23 = load i32, i32* %3
ret i32 %23
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @read_proc_val(i8* nocapture %0, i64* nocapture writeonly %1) {
%3 = alloca [120 x i8]
%4 = getelementptr inbounds [120 x i8], [120 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 %11, label %7
7: ; preds = %2
%8 = call i32 @fread(i8* %4, i32 1, i32 120, i32* %5)
%9 = call i64 @atol(i8* %4)
store i64 %9, i64* %1
%10 = tail call i32 @fclose(i32* %5)
br label %11
11: ; preds = %2, %7
%12 = phi i32 [ 0, %7 ], [ -1, %2 ]
ret i32 %12
}