#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;
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(int /*<<< orphan*/, char *);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *const, char *);
int /*<<< orphan*/ printf(char *, long);
long read_stream(char const *const, int /*<<< orphan*/ *, int const);
int /*<<< orphan*/ scripted();
int /*<<< orphan*/ set_error_msg(char *);
int /*<<< orphan*/ show_strerror(char const *const, int /*<<< orphan*/);
int /*<<< orphan*/ strip_escapes(char const *const);
int read_file(const char *const filename, const int addr) {
  FILE *fp;
  long size;
  int ret;
  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(filename, fp, addr);
  if (*filename == '!')
    ret = pclose(fp);
  else
    ret = fclose(fp);
  if (size < 0)
    return -1;
  if (ret != 0) {
    show_strerror(filename, errno);
    set_error_msg("Cannot close input file");
    return -1;
  }
  if (!scripted())
    printf("%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    0x2dc3(%rip),%rax
mov    (%rax),%esi
call   10d0 <show_strerror@plt>
lea    0xdd7(%rip),%rdi
call   1060 <set_error_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f5 <read_file+0x145>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <read_stream@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x21,%eax
jne    1271 <read_file+0xc1>
mov    -0x20(%rbp),%rdi
call   1050 <pclose@plt>
mov    %eax,-0x2c(%rbp)
jmp    127d <read_file+0xcd>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x2c(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1294 <read_file+0xe4>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f5 <read_file+0x145>
cmpl   $0x0,-0x2c(%rbp)
je     12c8 <read_file+0x118>
mov    -0x10(%rbp),%rdi
mov    0x2d37(%rip),%rax
mov    (%rax),%esi
call   10d0 <show_strerror@plt>
lea    0xd62(%rip),%rdi
call   1060 <set_error_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f5 <read_file+0x145>
mov    $0x0,%al
call   10c0 <scripted@plt>
cmp    $0x0,%eax
jne    12e8 <read_file+0x138>
mov    -0x28(%rbp),%rsi
lea    0xd4e(%rip),%rdi
call   1040 <printf@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   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
cmpb   $0x21,(%rdi)
jne    11d4 <read_file+0x24>
lea    0x1(%rbx),%rdi
lea    0xe33(%rip),%rsi
call   1090 <popen@plt>
jmp    11ea <read_file+0x3a>
mov    %rbx,%rdi
call   1080 <strip_escapes@plt>
lea    0xe1d(%rip),%rsi
mov    %eax,%edi
call   10a0 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1212 <read_file+0x62>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1070 <read_stream@plt>
mov    %rax,%r15
cmpb   $0x21,(%rbx)
jne    1236 <read_file+0x86>
mov    %rbp,%rdi
call   1050 <pclose@plt>
jmp    123e <read_file+0x8e>
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10d0 <show_strerror@plt>
lea    0xdd8(%rip),%rdi
call   1060 <set_error_msg@plt>
mov    $0xffffffff,%ebp
jmp    1291 <read_file+0xe1>
mov    %rbp,%rdi
call   1030 <fclose@plt>
mov    $0xffffffff,%ebp
test   %r15,%r15
js     1291 <read_file+0xe1>
test   %eax,%eax
je     126b <read_file+0xbb>
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10d0 <show_strerror@plt>
lea    0xdb5(%rip),%rdi
call   1060 <set_error_msg@plt>
jmp    1291 <read_file+0xe1>
xor    %eax,%eax
call   10c0 <scripted@plt>
test   %eax,%eax
jne    1285 <read_file+0xd5>
lea    0xdb4(%rip),%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
call   10b0 <current_addr@plt>
mov    %eax,%ebp
sub    %r14d,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
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"
@.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
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %11, 33
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = getelementptr inbounds i8, i8* %14, i64 1
%16 = call i32* @popen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %6
br label %21
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call i32 @strip_escapes(i8* %18)
%20 = call i32* @fopen(i32 %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %6
br label %21
21: ; preds = %17, %13
%22 = load i32*, i32** %6
%23 = icmp ne i32* %22, null
br i1 %23, label %29, label %24
24: ; preds = %21
%25 = load i8*, i8** %4
%26 = load i32, i32* @errno
%27 = call i32 @show_strerror(i8* %25, i32 %26)
%28 = 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 %66
29: ; preds = %21
%30 = load i8*, i8** %4
%31 = load i32*, i32** %6
%32 = load i32, i32* %5
%33 = call i64 @read_stream(i8* %30, i32* %31, i32 %32)
store i64 %33, i64* %7
%34 = load i8*, i8** %4
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 33
br i1 %37, label %38, label %41
38: ; preds = %29
%39 = load i32*, i32** %6
%40 = call i32 @pclose(i32* %39)
store i32 %40, i32* %8
br label %44
41: ; preds = %29
%42 = load i32*, i32** %6
%43 = call i32 @fclose(i32* %42)
store i32 %43, i32* %8
br label %44
44: ; preds = %41, %38
%45 = load i64, i64* %7
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %44
store i32 -1, i32* %3
br label %66
48: ; preds = %44
%49 = load i32, i32* %8
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %48
%52 = load i8*, i8** %4
%53 = load i32, i32* @errno
%54 = call i32 @show_strerror(i8* %52, i32 %53)
%55 = 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 %66
56: ; preds = %48
%57 = call i32 (...) @scripted()
%58 = icmp ne i32 %57, 0
br i1 %58, label %62, label %59
59: ; preds = %56
%60 = load i64, i64* %7
%61 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %60)
br label %62
62: ; preds = %59, %56
%63 = call i32 (...) @current_addr()
%64 = load i32, i32* %5
%65 = sub nsw i32 %63, %64
store i32 %65, i32* %3
br label %66
66: ; preds = %62, %51, %47, %24
%67 = load i32, i32* %3
ret i32 %67
}

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"
@.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 %43
18: ; preds = %11
%19 = tail call i64 @read_stream(i8* %0, i32* %12, i32 %1)
%20 = load i8, i8* %0
%21 = icmp eq i8 %20, 33
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = tail call i32 @pclose(i32* %12)
br label %26
24: ; preds = %18
%25 = tail call i32 @fclose(i32* %12)
br label %26
26: ; preds = %24, %22
%27 = phi i32 [ %23, %22 ], [ %25, %24 ]
%28 = icmp slt i64 %19, 0
br i1 %28, label %43, label %29
29: ; preds = %26
%30 = icmp eq i32 %27, 0
br i1 %30, label %35, label %31
31: ; preds = %29
%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 %43
35: ; preds = %29
%36 = tail call i32 (...) @scripted()
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %19)
br label %40
40: ; preds = %38, %35
%41 = tail call i32 (...) @current_addr()
%42 = sub nsw i32 %41, %1
br label %43
43: ; preds = %26, %40, %31, %14
%44 = phi i32 [ -1, %31 ], [ %42, %40 ], [ -1, %14 ], [ -1, %26 ]
ret i32 %44
}