#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ readn(int, char *, int);
char *strdup(char *);
char *__attribute__((no)) read_file(char *file) {
  char buf[4096 + 1] = {0};
  int fd = open(file, O_RDONLY);
  if (fd == -1)
    _exit(0);
  readn(fd, buf, sizeof(buf) - 1);
  close(fd);
  return strdup(buf);
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1001,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0xffffffff,-0x1014(%rbp)
jne    11ae <read_file+0x4e>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    -0x1014(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <readn@plt>
mov    -0x1014(%rbp),%edi
call   1060 <close@plt>
lea    -0x1010(%rbp),%rdi
call   1080 <strdup@plt>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rdi,%rbx
mov    %rsp,%rdi
mov    $0x1001,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11c0 <read_file+0x60>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1050 <readn@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r14,%rdi
call   1080 <strdup@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %r14
ret
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i8* @read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca [4097 x i8]
%4 = alloca i32
store i8* %0, i8** %2
%5 = bitcast [4097 x i8]* %3 to i8*
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @_exit(i32 0)
unreachable
13: ; preds = %1
%14 = load i32, i32* %4
%15 = getelementptr inbounds [4097 x i8], [4097 x i8]* %3, i64 0, i64 0
%16 = call i32 @readn(i32 %14, i8* %15, i32 4096)
%17 = load i32, i32* %4
%18 = call i32 @close(i32 %17)
%19 = getelementptr inbounds [4097 x i8], [4097 x i8]* %3, i64 0, i64 0
%20 = call i8* @strdup(i8* %19)
ret i8* %20
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define noalias i8* @read_file(i8* nocapture %0) {
%2 = alloca [4097 x i8]
%3 = getelementptr inbounds [4097 x i8], [4097 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @_exit(i32 0)
unreachable
9: ; preds = %1
%10 = call i32 @readn(i32 %5, i8* %3, i32 4096)
%11 = call i32 @close(i32 %5)
%12 = call i8* @strdup(i8* %3)
ret i8* %12
}