#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 EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ *open_driver_file(char const *, char const *, char *);
int read_int(const char *file, const char *driver) {
  FILE *in = open_driver_file(file, driver, "r");
  if (in == NULL) {
    return -1;
  }
  int value;
  int ok = fscanf(in, "%d", &value);
  fclose(in);
  if (ok == EOF) {
    value = 0;
  }
  return value;
}


x86 assembly (clang, -O0):
<read_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    0xeb1(%rip),%rdx
call   1030 <open_driver_file@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    116f <read_int+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ae <read_int+0x7e>
mov    -0x20(%rbp),%rdi
lea    0xe88(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1050 <fscanf@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%eax
mov    0x2e4f(%rip),%rcx
cmp    (%rcx),%eax
jne    11a8 <read_int+0x78>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_int>:
push   %rbp
push   %rbx
push   %rax
lea    0xec6(%rip),%rdx
call   1030 <open_driver_file@plt>
test   %rax,%rax
je     1178 <read_int+0x48>
mov    %rax,%rbx
lea    0xeb4(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2e7c(%rip),%rcx
xor    %eax,%eax
cmp    (%rcx),%ebp
je     117d <read_int+0x4d>
mov    0x4(%rsp),%eax
jmp    117d <read_int+0x4d>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%d\00"
@EOF = global i32 0
define i32 @read_int(i8* %0, i8* %1) {
%3 = alloca i32
%4 = tail call i32* @open_driver_file(i8* %0, i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %14, label %6
6: ; preds = %2
%7 = bitcast i32* %3 to i8*
%8 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%9 = call i32 @fclose(i32* %4)
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %8, %10
%12 = load i32, i32* %3
%13 = select i1 %11, i32 0, i32 %12
br label %14
14: ; preds = %2, %6
%15 = phi i32 [ %13, %6 ], [ -1, %2 ]
ret i32 %15
}