#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 /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int read_int(char *path) {
  FILE *in = fopen(path, "r");
  if (in == NULL) {
    char buffer[1024];
    sprintf(buffer, "Couldn't open %s", path);
    perror(buffer);
    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    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe96(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ad <read_int+0x5d>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe72(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x420(%rbp),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <read_int+0xab>
mov    -0x18(%rbp),%rdi
lea    0xe5b(%rip),%rsi
lea    -0x424(%rbp),%rdx
call   1040 <fscanf@plt>
mov    %eax,-0x428(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x428(%rbp),%eax
mov    0x2e08(%rip),%rcx
cmp    (%rcx),%eax
jne    11f2 <read_int+0xa2>
movl   $0x0,-0x424(%rbp)
mov    -0x424(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_int>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%r14
lea    0xe9b(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11aa <read_int+0x5a>
mov    %rax,%rbx
lea    0xe9a(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2e53(%rip),%rcx
xor    %eax,%eax
cmp    (%rcx),%ebp
je     119e <read_int+0x4e>
mov    (%rsp),%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe51(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r14,%rdx
call   1070 <sprintf@plt>
mov    %rbx,%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    119e <read_int+0x4e>

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

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