#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, long);
int 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;
  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(fp, addr);
  if (size < 0)
    return -1;
  if (*filename == '!')
    ret = pclose(fp);
  else
    ret = fclose(fp);
  if (ret != 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    12fa <read_file+0x14a>
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    12fa <read_file+0x14a>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x21,%eax
jne    1284 <read_file+0xd4>
mov    -0x20(%rbp),%rdi
call   1040 <pclose@plt>
mov    %eax,-0x2c(%rbp)
jmp    1290 <read_file+0xe0>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     12c4 <read_file+0x114>
mov    -0x10(%rbp),%rdi
mov    0x2d33(%rip),%rax
mov    (%rax),%esi
call   10d0 <show_strerror@plt>
lea    0xd66(%rip),%rdi
call   1050 <set_error_msg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fa <read_file+0x14a>
mov    $0x0,%al
call   10c0 <scripted@plt>
cmp    $0x0,%eax
jne    12ed <read_file+0x13d>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xd49(%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     1240 <read_file+0x90>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1060 <read_stream@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
js     12a1 <read_file+0xf1>
mov    %rax,%r15
cmpb   $0x21,(%rbx)
jne    1265 <read_file+0xb5>
mov    %rbp,%rdi
call   1040 <pclose@plt>
test   %eax,%eax
je     1271 <read_file+0xc1>
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10d0 <show_strerror@plt>
lea    0xde0(%rip),%rdi
call   1050 <set_error_msg@plt>
jmp    12a1 <read_file+0xf1>
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10d0 <show_strerror@plt>
lea    0xdaa(%rip),%rdi
call   1050 <set_error_msg@plt>
mov    $0xffffffff,%r12d
jmp    12a1 <read_file+0xf1>
mov    %rbp,%rdi
call   1030 <fclose@plt>
test   %eax,%eax
jne    1221 <read_file+0x71>
xor    %eax,%eax
call   10c0 <scripted@plt>
test   %eax,%eax
jne    1294 <read_file+0xe4>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
mov    %r15,%rdx
call   1070 <fprintf@plt>
xor    %eax,%eax
call   10b0 <current_addr@plt>
mov    %eax,%r12d
sub    %r14d,%r12d
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
%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 i32*, i32** %6
%31 = load i32, i32* %5
%32 = call i64 @read_stream(i32* %30, i32 %31)
store i64 %32, i64* %7
%33 = load i64, i64* %7
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %29
store i32 -1, i32* %3
br label %66
36: ; preds = %29
%37 = load i8*, i8** %4
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 33
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i32*, i32** %6
%43 = call i32 @pclose(i32* %42)
store i32 %43, i32* %8
br label %47
44: ; preds = %36
%45 = load i32*, i32** %6
%46 = call i32 @fclose(i32* %45)
store i32 %46, i32* %8
br label %47
47: ; preds = %44, %41
%48 = load i32, i32* %8
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %55
50: ; preds = %47
%51 = load i8*, i8** %4
%52 = load i32, i32* @errno
%53 = call i32 @show_strerror(i8* %51, i32 %52)
%54 = 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
55: ; preds = %47
%56 = call i32 (...) @scripted()
%57 = icmp ne i32 %56, 0
br i1 %57, label %62, label %58
58: ; preds = %55
%59 = load i32, i32* @stderr
%60 = load i64, i64* %7
%61 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %60)
br label %62
62: ; preds = %58, %55
%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, %50, %35, %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"
@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 i32 @pclose(i32* %12)
br label %28
26: ; preds = %21
%27 = tail call i32 @fclose(i32* %12)
br label %28
28: ; preds = %26, %24
%29 = phi i32 [ %25, %24 ], [ %27, %26 ]
%30 = icmp eq i32 %29, 0
br i1 %30, label %35, label %31
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
}