#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 */
struct btp_strbuf {
  int dummy;
};

/* Variables and functions */
int EOF;
char *btp_file_to_string(char *);
int /*<<< orphan*/ btp_strbuf_append_char(struct btp_strbuf *, char);
char *btp_strbuf_free_nobuf(struct btp_strbuf *);
struct btp_strbuf *btp_strbuf_new();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getchar();
int /*<<< orphan*/ stderr;
char *read_file(char *filename) {
  char *text = NULL;
  if (!filename) {
    struct btp_strbuf *input = btp_strbuf_new();
    int c;
    while ((c = getchar()) != EOF && c != '\0')
      btp_strbuf_append_char(input, (char)c);
    btp_strbuf_append_char(input, '\0');
    text = btp_strbuf_free_nobuf(input);
  } else {
    text = btp_file_to_string(filename);
    if (!text) {
      fprintf(stderr, "Failed to read the input file.\n");
      exit(1);
    }
  }
  return text;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1205 <read_file+0x95>
mov    $0x0,%al
call   1050 <btp_strbuf_new@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
xor    %eax,%eax
mov    0x2e39(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x1d(%rbp)
je     11c4 <read_file+0x54>
cmpl   $0x0,-0x1c(%rbp)
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    11d4 <read_file+0x64>
jmp    11e8 <read_file+0x78>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%eax
movsbl %al,%esi
call   1080 <btp_strbuf_append_char@plt>
jmp    119a <read_file+0x2a>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1080 <btp_strbuf_append_char@plt>
mov    -0x18(%rbp),%rdi
call   1040 <btp_strbuf_free_nobuf@plt>
mov    %rax,-0x10(%rbp)
jmp    1241 <read_file+0xd1>
mov    -0x8(%rbp),%rdi
call   1030 <btp_file_to_string@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    123c <read_file+0xcc>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xdd3(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1241 <read_file+0xd1>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     118b <read_file+0x1b>
call   1030 <btp_file_to_string@plt>
test   %rax,%rax
je     11e4 <read_file+0x74>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1050 <btp_strbuf_new@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <getchar@plt>
test   %eax,%eax
je     11cb <read_file+0x5b>
mov    0x2e41(%rip),%r14
nopw   0x0(%rax,%rax,1)
cmp    (%r14),%eax
je     11cb <read_file+0x5b>
movsbl %al,%esi
mov    %rbx,%rdi
call   1080 <btp_strbuf_append_char@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
test   %eax,%eax
jne    11b0 <read_file+0x40>
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <btp_strbuf_append_char@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <btp_strbuf_free_nobuf@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
lea    0xe0c(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.btp_strbuf = type { i32 }
@EOF = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"Failed to read the input file.\0A\00"
define i8* @read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.btp_strbuf*
%5 = alloca i32
store i8* %0, i8** %2
store i8* null, i8** %3
%6 = load i8*, i8** %2
%7 = icmp ne i8* %6, null
br i1 %7, label %29, label %8
8: ; preds = %1
%9 = call %struct.btp_strbuf* (...) @btp_strbuf_new()
store %struct.btp_strbuf* %9, %struct.btp_strbuf** %4
br label %10
10: ; preds = %19, %8
%11 = call i32 (...) @getchar()
store i32 %11, i32* %5
%12 = load i32, i32* @EOF
%13 = icmp ne i32 %11, %12
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* %5
%16 = icmp ne i32 %15, 0
br label %17
17: ; preds = %14, %10
%18 = phi i1 [ false, %10 ], [ %16, %14 ]
br i1 %18, label %19, label %24
19: ; preds = %17
%20 = load %struct.btp_strbuf*, %struct.btp_strbuf** %4
%21 = load i32, i32* %5
%22 = trunc i32 %21 to i8
%23 = call i32 @btp_strbuf_append_char(%struct.btp_strbuf* %20, i8 signext %22)
br label %10
24: ; preds = %17
%25 = load %struct.btp_strbuf*, %struct.btp_strbuf** %4
%26 = call i32 @btp_strbuf_append_char(%struct.btp_strbuf* %25, i8 signext 0)
%27 = load %struct.btp_strbuf*, %struct.btp_strbuf** %4
%28 = call i8* @btp_strbuf_free_nobuf(%struct.btp_strbuf* %27)
store i8* %28, i8** %3
br label %39
29: ; preds = %1
%30 = load i8*, i8** %2
%31 = call i8* @btp_file_to_string(i8* %30)
store i8* %31, i8** %3
%32 = load i8*, i8** %3
%33 = icmp ne i8* %32, null
br i1 %33, label %38, label %34
34: ; preds = %29
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %29
br label %39
39: ; preds = %38, %24
%40 = load i8*, i8** %3
ret i8* %40
}

LLVM IR (-O2):
%struct.btp_strbuf = type { i32 }
@EOF = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"Failed to read the input file.\0A\00"
define i8* @read_file(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %22
3: ; preds = %1
%4 = tail call %struct.btp_strbuf* (...) @btp_strbuf_new()
%5 = tail call i32 (...) @getchar()
%6 = load i32, i32* @EOF
%7 = icmp ne i32 %5, %6
%8 = icmp ne i32 %5, 0
%9 = and i1 %8, %7
br i1 %9, label %10, label %19
10: ; preds = %3, %10
%11 = phi i32 [ %14, %10 ], [ %5, %3 ]
%12 = trunc i32 %11 to i8
%13 = tail call i32 @btp_strbuf_append_char(%struct.btp_strbuf* %4, i8 signext %12)
%14 = tail call i32 (...) @getchar()
%15 = load i32, i32* @EOF
%16 = icmp ne i32 %14, %15
%17 = icmp ne i32 %14, 0
%18 = and i1 %17, %16
br i1 %18, label %10, label %19
19: ; preds = %10, %3
%20 = tail call i32 @btp_strbuf_append_char(%struct.btp_strbuf* %4, i8 signext 0)
%21 = tail call i8* @btp_strbuf_free_nobuf(%struct.btp_strbuf* %4)
br label %29
22: ; preds = %1
%23 = tail call i8* @btp_file_to_string(i8* %0)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %22, %19
%30 = phi i8* [ %23, %22 ], [ %21, %19 ]
ret i8* %30
}