#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 atoi(char *);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int read_int(const char *path) {
  FILE *f;
  char buffer[256];
  f = fopen(path, "r");
  if (f) {
    if (fgets(buffer, 256, f)) {
      fclose(f);
      return atoi(buffer);
    } else {
      fclose(f);
      error("Error reading\n");
    }
  } else {
    error("Error opening for reading\n");
  }
}


x86 assembly (clang, -O0):
<read_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe96(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d4 <read_int+0x84>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11ba <read_int+0x6a>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x120(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x4(%rbp)
jmp    11e0 <read_int+0x90>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe38(%rip),%rdi
call   1050 <error@plt>
jmp    11e0 <read_int+0x90>
lea    0xe36(%rip),%rdi
call   1050 <error@plt>
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_int>:
push   %r14
push   %rbx
sub    $0x108,%rsp
lea    0xe9f(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1198 <read_int+0x48>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %r14,%r14
je     11a1 <read_int+0x51>
mov    %rsp,%rdi
call   1070 <atoi@plt>
jmp    11ad <read_int+0x5d>
lea    0xe72(%rip),%rdi
jmp    11a8 <read_int+0x58>
lea    0xe5a(%rip),%rdi
call   1050 <error@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [15 x i8] c"Error reading\0A\00"
@.str.2 = [27 x i8] c"Error opening for reading\0A\00"
define i32 @read_int(i8* nocapture %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%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 %14, label %6
6: ; preds = %1
%7 = call i64 @fgets(i8* %3, i32 256, i32* %4)
%8 = icmp eq i64 %7, 0
%9 = call i32 @fclose(i32* %4)
br i1 %8, label %12, label %10
10: ; preds = %6
%11 = call i32 @atoi(i8* %3)
br label %16
12: ; preds = %6
%13 = call i32 @error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %16
14: ; preds = %1
%15 = tail call i32 @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %12, %14, %10
%17 = phi i32 [ %11, %10 ], [ undef, %14 ], [ undef, %12 ]
ret i32 %17
}