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

/* Variables and functions */
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int vfscanf(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/);
int sol_util_vread_file(const char *path, const char *fmt, va_list args) {
  FILE *fp;
  int ret;
  fp = fopen(path, "re");
  if (!fp)
    return -errno;
  ret = vfscanf(fp, fmt, args);
  fclose(fp);
  return ret;
}


x86 assembly (clang, -O0):
<sol_util_vread_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb2(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1175 <sol_util_vread_file+0x45>
xor    %eax,%eax
mov    0x2e75(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1197 <sol_util_vread_file+0x67>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <vfscanf@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sol_util_vread_file>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
lea    0xebf(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     116a <sol_util_vread_file+0x3a>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1050 <vfscanf@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
jmp    1175 <sol_util_vread_file+0x45>
mov    0x2e6f(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"re\00"
@errno = global i32 0
define i32 @sol_util_vread_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp ne i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = load i32, i32* @errno
%16 = sub nsw i32 0, %15
store i32 %16, i32* %4
br label %25
17: ; preds = %3
%18 = load i32*, i32** %8
%19 = load i8*, i8** %6
%20 = load i32, i32* %7
%21 = call i32 @vfscanf(i32* %18, i8* %19, i32 %20)
store i32 %21, i32* %9
%22 = load i32*, i32** %8
%23 = call i32 @fclose(i32* %22)
%24 = load i32, i32* %9
store i32 %24, i32* %4
br label %25
25: ; preds = %17, %14
%26 = load i32, i32* %4
ret i32 %26
}

LLVM IR (-O2):
@.str = [3 x i8] c"re\00"
@errno = global i32 0
define i32 @sol_util_vread_file(i8* nocapture %0, i8* %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = sub nsw i32 0, %7
br label %12
9: ; preds = %3
%10 = tail call i32 @vfscanf(i32* %4, i8* %1, i32 %2)
%11 = tail call i32 @fclose(i32* %4)
br label %12
12: ; preds = %9, %6
%13 = phi i32 [ %10, %9 ], [ %8, %6 ]
ret i32 %13
}