#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 *const, char *);
long read_stream(int /*<<< orphan*/ *, int const);
int /*<<< orphan*/ scripted();
int /*<<< orphan*/ set_error_msg(char *);
int /*<<< orphan*/ show_strerror(char const *const, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strip_escapes(char const *const);
int read_file(const char *const 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;
  }
  size = read_stream(fp, addr);
  if (size < 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    1300 <read_file+0x150>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <read_stream@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1263 <read_file+0xb3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <read_file+0x150>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x21,%eax
jne    1285 <read_file+0xd5>
mov    -0x20(%rbp),%rdi
call   1040 <pclose@plt>
mov    %rax,-0x30(%rbp)
jmp    1292 <read_file+0xe2>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    $0x0,%rax
jge    12ca <read_file+0x11a>
mov    -0x10(%rbp),%rdi
mov    0x2d2d(%rip),%rax
mov    (%rax),%esi
call   10d0 <show_strerror@plt>
lea    0xd60(%rip),%rdi
call   1050 <set_error_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <read_file+0x150>
mov    $0x0,%al
call   10c0 <scripted@plt>
cmp    $0x0,%eax
jne    12f3 <read_file+0x143>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xd43(%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 %65
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 = load i64, i64* %7
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %28
store i32 -1, i32* %3
br label %65
35: ; preds = %28
%36 = load i8*, i8** %4
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 33
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i32*, i32** %6
%42 = call i64 @pclose(i32* %41)
br label %46
43: ; preds = %35
%44 = load i32*, i32** %6
%45 = call i64 @fclose(i32* %44)
br label %46
46: ; preds = %43, %40
%47 = phi i64 [ %42, %40 ], [ %45, %43 ]
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %46
%50 = load i8*, i8** %4
%51 = load i32, i32* @errno
%52 = call i32 @show_strerror(i8* %50, i32 %51)
%53 = 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 %65
54: ; preds = %46
%55 = call i32 (...) @scripted()
%56 = icmp ne i32 %55, 0
br i1 %56, label %61, label %57
57: ; preds = %54
%58 = load i32, i32* @stderr
%59 = load i64, i64* %7
%60 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %59)
br label %61
61: ; preds = %57, %54
%62 = call i32 (...) @current_addr()
%63 = load i32, i32* %5
%64 = sub nsw i32 %62, %63
store i32 %64, i32* %3
br label %65
65: ; preds = %61, %49, %34, %23
%66 = load i32, i32* %3
ret i32 %66
}

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
}