#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 current_addr();
int /*<<< orphan*/ errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, long);
scalar_t__ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *, char *);
long read_stream(int /*<<< orphan*/ *, int const);
int /*<<< orphan*/ scripted();
int /*<<< orphan*/ set_error_msg(char *);
int /*<<< orphan*/ show_strerror(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strip_escapes(char const *);
int read_file(const char *filename, const int addr) {
  FILE *fp;
  long size;
  if (*filename == '!')
    fp = popen(filename + 1, "r");
  else
    fp = fopen(strip_escapes(filename), "r");
  if (!fp) {
    show_strerror(filename, errno);
    set_error_msg("Cannot open input file");
    return -1;
  }
  if ((size = read_stream(fp, addr)) < 0)
    return -1;
  if (((*filename == '!') ? pclose(fp) : fclose(fp)) < 0) {
    show_strerror(filename, errno);
    set_error_msg("Cannot close input file");
    return -1;
  }
  if (!scripted())
    fprintf(stderr, "%lu\n", size);
  return current_addr() - addr;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x21,%eax
jne    11ec <read_file+0x3c>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
lea    0xe22(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x20(%rbp)
jmp    1207 <read_file+0x57>
mov    -0x10(%rbp),%rdi
call   1080 <strip_escapes@plt>
mov    %eax,%edi
lea    0xe02(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    123c <read_file+0x8c>
mov    -0x10(%rbp),%rdi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
call   10d0 <show_strerror@plt>
lea    0xdd7(%rip),%rdi
call   1050 <set_error_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ff <read_file+0x14f>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <read_stream@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jge    1262 <read_file+0xb2>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ff <read_file+0x14f>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x21,%eax
jne    1284 <read_file+0xd4>
mov    -0x20(%rbp),%rdi
call   1040 <pclose@plt>
mov    %rax,-0x30(%rbp)
jmp    1291 <read_file+0xe1>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    $0x0,%rax
jge    12c9 <read_file+0x119>
mov    -0x10(%rbp),%rdi
mov    0x2d2e(%rip),%rax
mov    (%rax),%esi
call   10d0 <show_strerror@plt>
lea    0xd61(%rip),%rdi
call   1050 <set_error_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ff <read_file+0x14f>
mov    $0x0,%al
call   10c0 <scripted@plt>
cmp    $0x0,%eax
jne    12f2 <read_file+0x142>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xd44(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x0,%al
call   10b0 <current_addr@plt>
sub    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
cmpb   $0x21,(%rdi)
jne    11d5 <read_file+0x25>
lea    0x1(%rbx),%rdi
lea    0xe32(%rip),%rsi
call   1090 <popen@plt>
jmp    11eb <read_file+0x3b>
mov    %rbx,%rdi
call   1080 <strip_escapes@plt>
lea    0xe1c(%rip),%rsi
mov    %eax,%edi
call   10a0 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1254 <read_file+0xa4>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1060 <read_stream@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
js     12a3 <read_file+0xf3>
mov    %rax,%r15
cmpb   $0x21,(%rbx)
jne    1279 <read_file+0xc9>
mov    %rbp,%rdi
call   1040 <pclose@plt>
test   %rax,%rax
js     1286 <read_file+0xd6>
xor    %eax,%eax
call   10c0 <scripted@plt>
test   %eax,%eax
jne    1245 <read_file+0x95>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
mov    %r15,%rdx
call   1070 <fprintf@plt>
xor    %eax,%eax
call   10b0 <current_addr@plt>
mov    %eax,%r12d
sub    %r14d,%r12d
jmp    12a3 <read_file+0xf3>
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10d0 <show_strerror@plt>
lea    0xd96(%rip),%rdi
call   1050 <set_error_msg@plt>
mov    $0xffffffff,%r12d
jmp    12a3 <read_file+0xf3>
mov    %rbp,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
jns    1222 <read_file+0x72>
mov    0x2d4b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10d0 <show_strerror@plt>
lea    0xd7b(%rip),%rdi
call   1050 <set_error_msg@plt>
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
@.str.1 = [23 x i8] c"Cannot open input file\00"
@.str.2 = [24 x i8] c"Cannot close input file\00"
@stderr = global i32 0
@.str.3 = [5 x i8] c"%lu\0A\00"
define i32 @read_file(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 33
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = call i32* @popen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %6
br label %20
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = call i32 @strip_escapes(i8* %17)
%19 = call i32* @fopen(i32 %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %6
br label %20
20: ; preds = %16, %12
%21 = load i32*, i32** %6
%22 = icmp ne i32* %21, null
br i1 %22, label %28, label %23
23: ; preds = %20
%24 = load i8*, i8** %4
%25 = load i32, i32* @errno
%26 = call i32 @show_strerror(i8* %24, i32 %25)
%27 = call i32 @set_error_msg(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %64
28: ; preds = %20
%29 = load i32*, i32** %6
%30 = load i32, i32* %5
%31 = call i64 @read_stream(i32* %29, i32 %30)
store i64 %31, i64* %7
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %28
store i32 -1, i32* %3
br label %64
34: ; preds = %28
%35 = load i8*, i8** %4
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 33
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = load i32*, i32** %6
%41 = call i64 @pclose(i32* %40)
br label %45
42: ; preds = %34
%43 = load i32*, i32** %6
%44 = call i64 @fclose(i32* %43)
br label %45
45: ; preds = %42, %39
%46 = phi i64 [ %41, %39 ], [ %44, %42 ]
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %45
%49 = load i8*, i8** %4
%50 = load i32, i32* @errno
%51 = call i32 @show_strerror(i8* %49, i32 %50)
%52 = call i32 @set_error_msg(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %64
53: ; preds = %45
%54 = call i32 (...) @scripted()
%55 = icmp ne i32 %54, 0
br i1 %55, label %60, label %56
56: ; preds = %53
%57 = load i32, i32* @stderr
%58 = load i64, i64* %7
%59 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %58)
br label %60
60: ; preds = %56, %53
%61 = call i32 (...) @current_addr()
%62 = load i32, i32* %5
%63 = sub nsw i32 %61, %62
store i32 %63, i32* %3
br label %64
64: ; preds = %60, %48, %33, %23
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
@.str.1 = [23 x i8] c"Cannot open input file\00"
@.str.2 = [24 x i8] c"Cannot close input file\00"
@stderr = global i32 0
@.str.3 = [5 x i8] c"%lu\0A\00"
define i32 @read_file(i8* %0, i32 %1) {
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 33
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = getelementptr inbounds i8, i8* %0, i64 1
%7 = tail call i32* @popen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %11
8: ; preds = %2
%9 = tail call i32 @strip_escapes(i8* %0)
%10 = tail call i32* @fopen(i32 %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %8, %5
%12 = phi i32* [ %7, %5 ], [ %10, %8 ]
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @errno
%16 = tail call i32 @show_strerror(i8* %0, i32 %15)
%17 = tail call i32 @set_error_msg(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %44
18: ; preds = %11
%19 = tail call i64 @read_stream(i32* %12, i32 %1)
%20 = icmp slt i64 %19, 0
br i1 %20, label %44, label %21
21: ; preds = %18
%22 = load i8, i8* %0
%23 = icmp eq i8 %22, 33
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = tail call i64 @pclose(i32* %12)
br label %28
26: ; preds = %21
%27 = tail call i64 @fclose(i32* %12)
br label %28
28: ; preds = %26, %24
%29 = phi i64 [ %25, %24 ], [ %27, %26 ]
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = load i32, i32* @errno
%33 = tail call i32 @show_strerror(i8* %0, i32 %32)
%34 = tail call i32 @set_error_msg(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %44
35: ; preds = %28
%36 = tail call i32 (...) @scripted()
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32, i32* @stderr
%40 = tail call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %19)
br label %41
41: ; preds = %38, %35
%42 = tail call i32 (...) @current_addr()
%43 = sub nsw i32 %42, %1
br label %44
44: ; preds = %18, %41, %31, %14
%45 = phi i32 [ -1, %31 ], [ %43, %41 ], [ -1, %14 ], [ -1, %18 ]
ret i32 %45
}