#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ stderr;
int readint(char *filename) {
  int result = 0;
  FILE *tmp_file = fopen(filename, "r");
  if (!tmp_file) {
    fprintf(stderr, "%s: failed to open for reading\n", filename);
    return 0;
  }
  if (!fscanf(tmp_file, "%d", &result)) {
    fprintf(stderr, "failed to read int from %s", filename);
    return 0;
  }
  fclose(tmp_file);
  return result;
}


x86 assembly (clang, -O0):
<readint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1197 <readint+0x57>
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe7c(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e8 <readint+0xa8>
mov    -0x20(%rbp),%rdi
lea    0xe80(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x0,%eax
jne    11d9 <readint+0x99>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe5d(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e8 <readint+0xa8>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readint>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
movl   $0x0,0x4(%rsp)
lea    0xeaa(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1189 <readint+0x49>
mov    %rax,%rbx
lea    0xeb8(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
test   %eax,%eax
je     119b <readint+0x5b>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x4(%rsp),%eax
jmp    11b5 <readint+0x75>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
lea    0xe69(%rip),%rsi
jmp    11ab <readint+0x6b>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
lea    0xe7a(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"%s: failed to open for reading\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [27 x i8] c"failed to read int from %s\00"
define i32 @readint(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
store i8* %0, i8** %3
store i32 0, i32* %4
%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** %5
%8 = load i32*, i32** %5
%9 = icmp ne i32* %8, null
br i1 %9, label %14, label %10
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %3
%13 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %12)
store i32 0, i32* %2
br label %26
14: ; preds = %1
%15 = load i32*, i32** %5
%16 = call i32 @fscanf(i32* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%17 = icmp ne i32 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %3
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %20)
store i32 0, i32* %2
br label %26
22: ; preds = %14
%23 = load i32*, i32** %5
%24 = call i32 @fclose(i32* %23)
%25 = load i32, i32* %4
store i32 %25, i32* %2
br label %26
26: ; preds = %22, %18, %10
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"%s: failed to open for reading\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [27 x i8] c"failed to read int from %s\00"
define i32 @readint(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%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 %9
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %18
9: ; preds = %1
%10 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %18
15: ; preds = %9
%16 = call i32 @fclose(i32* %4)
%17 = load i32, i32* %2
br label %18
18: ; preds = %15, %12, %6
%19 = phi i32 [ %17, %15 ], [ 0, %12 ], [ 0, %6 ]
ret i32 %19
}