#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
char *strerror(int /*<<< orphan*/);
char *doread(const char *fn) {
  static char buf[1000];
  FILE *f = fopen(fn, "r");
  int s;
  if (f == NULL) {
    printf("failed: %s\n", strerror(errno));
    return NULL;
  }
  s = fread(buf, 1, sizeof(buf) - 1, f);
  buf[s] = 0;
  fclose(f);
  return buf;
}


x86 assembly (clang, -O0):
<doread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a5 <doread+0x55>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe6f(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e5 <doread+0x95>
mov    -0x18(%rbp),%rcx
lea    0x2eb0(%rip),%rdi
mov    $0x1,%esi
mov    $0x3e7,%edx
call   1030 <fread@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rcx
lea    0x2e93(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
lea    0x2e7f(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doread>:
push   %r14
push   %rbx
push   %rax
lea    0xea5(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1195 <doread+0x45>
mov    %rax,%rbx
lea    0x2ef1(%rip),%r14
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x3e7,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
cltq
movb   $0x0,(%rax,%r14,1)
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11b5 <doread+0x65>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe58(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@doread.buf = internal global [1000 x i8] zeroinitializer
@.str = [2 x i8] c"r\00"
@.str.1 = [12 x i8] c"failed: %s\0A\00"
@errno = global i32 0
define i8* @doread(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %12)
store i8* null, i8** %2
br label %22
14: ; preds = %1
%15 = load i32*, i32** %4
%16 = call i32 @fread(i8* getelementptr inbounds ([1000 x i8], [1000 x i8]* @doread.buf, i64 0, i64 0), i32 1, i32 999, i32* %15)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [1000 x i8], [1000 x i8]* @doread.buf, i64 0, i64 %18
store i8 0, i8* %19
%20 = load i32*, i32** %4
%21 = call i32 @fclose(i32* %20)
store i8* getelementptr inbounds ([1000 x i8], [1000 x i8]* @doread.buf, i64 0, i64 0), i8** %2
br label %22
22: ; preds = %14, %10
%23 = load i8*, i8** %2
ret i8* %23
}

LLVM IR (-O2):
@doread.buf = internal global [1000 x i8] zeroinitializer
@.str = [2 x i8] c"r\00"
@.str.1 = [12 x i8] c"failed: %s\0A\00"
@errno = global i32 0
define i8* @doread(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = load i32, i32* @errno
%6 = tail call i8* @strerror(i32 %5)
%7 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %13
8: ; preds = %1
%9 = tail call i32 @fread(i8* getelementptr inbounds ([1000 x i8], [1000 x i8]* @doread.buf, i64 0, i64 0), i32 1, i32 999, i32* %2)
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [1000 x i8], [1000 x i8]* @doread.buf, i64 0, i64 %10
store i8 0, i8* %11
%12 = tail call i32 @fclose(i32* %2)
br label %13
13: ; preds = %8, %4
%14 = phi i8* [ null, %4 ], [ getelementptr inbounds ([1000 x i8], [1000 x i8]* @doread.buf, i64 0, i64 0), %8 ]
ret i8* %14
}