#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 */
scalar_t__ EOF;
int asprintf(char **, char *, char const *, char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ fscanf(int /*<<< orphan*/ *, char const *, void *);
int file_read_value(const char *path, const char *name, const char *format,
                    void *value) {
  FILE *file;
  char *rpath;
  int ret;
  ret = asprintf(&rpath, "%s/%s", path, name);
  if (ret < 0)
    return ret;
  file = fopen(rpath, "r");
  if (!file) {
    ret = -1;
    goto out_free;
  }
  ret = fscanf(file, format, value) == EOF ? -1 : 0;
  fclose(file);
out_free:
  free(rpath);
  return ret;
}


x86 assembly (clang, -O0):
<file_read_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x38(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1060 <asprintf@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1198 <file_read_value+0x48>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1209 <file_read_value+0xb9>
mov    -0x38(%rbp),%rdi
lea    0xe63(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c3 <file_read_value+0x73>
movl   $0xffffffff,-0x3c(%rbp)
jmp    11fa <file_read_value+0xaa>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <fscanf@plt>
mov    %rax,%rdx
mov    0x2e0a(%rip),%rax
mov    (%rax),%rsi
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    %rsi,%rdx
cmove  %ecx,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read_value>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rcx
mov    %rdi,%rdx
lea    0xe96(%rip),%rsi
mov    %rsp,%rdi
call   1060 <asprintf@plt>
test   %eax,%eax
js     11b8 <file_read_value+0x68>
mov    (%rsp),%rdi
lea    0xe85(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11bc <file_read_value+0x6c>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <fscanf@plt>
mov    0x2e45(%rip),%rcx
xor    %ebp,%ebp
cmp    (%rcx),%rax
sete   %bpl
neg    %ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11c1 <file_read_value+0x71>
mov    %eax,%ebp
jmp    11ca <file_read_value+0x7a>
mov    $0xffffffff,%ebp
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i64 0
define i32 @file_read_value(i8* %0, i8* %1, i8* nocapture %2, i8* %3) {
%5 = alloca i8*
%6 = bitcast i8** %5 to i8*
%7 = call i32 @asprintf(i8** %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%8 = icmp slt i32 %7, 0
br i1 %8, label %23, label %9
9: ; preds = %4
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %19, label %13
13: ; preds = %9
%14 = call i64 @fscanf(i32* %11, i8* %2, i8* %3)
%15 = load i64, i64* @EOF
%16 = icmp eq i64 %14, %15
%17 = sext i1 %16 to i32
%18 = call i32 @fclose(i32* %11)
br label %19
19: ; preds = %9, %13
%20 = phi i32 [ %17, %13 ], [ -1, %9 ]
%21 = load i8*, i8** %5
%22 = call i32 @free(i8* %21)
br label %23
23: ; preds = %4, %19
%24 = phi i32 [ %20, %19 ], [ %7, %4 ]
ret i32 %24
}