#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 EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
int read_file(char *filename, char *dest, int maxlen) {
  FILE *file;
  int pos, temp, i;
  file = fopen(filename, "r");
  if (NULL == file) {
    fprintf(stderr, "open %s error\n", filename);
    return -1;
  }
  pos = 0;
  for (i = 0; i < 1024 - 1; i++) {
    temp = fgetc(file);
    if (EOF == temp)
      break;
    dest[pos++] = temp;
  }
  fclose(file);
  dest[pos] = 0;
  return pos;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    1198 <read_file+0x58>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe7b(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <read_file+0xd9>
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x3ff,-0x34(%rbp)
jge    11fe <read_file+0xbe>
mov    -0x28(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x30(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
cmp    -0x30(%rbp),%eax
jne    11d6 <read_file+0x96>
jmp    11fe <read_file+0xbe>
mov    -0x30(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x2c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11a6 <read_file+0x66>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xeab(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11a5 <read_file+0x65>
mov    %rax,%r15
xor    %ebx,%ebx
mov    0x2e7d(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1040 <fgetc@plt>
cmp    %eax,(%r12)
je     1194 <read_file+0x54>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    $0x3ff,%rbx
jne    1170 <read_file+0x30>
mov    $0x3ff,%ebx
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
jmp    11c2 <read_file+0x82>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    0xe4d(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"open %s error\0A\00"
@EOF = global i32 0
define i32 @read_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i8*, i8** %5
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %8
%14 = load i32*, i32** %8
%15 = icmp eq i32* null, %14
br i1 %15, label %16, label %20
16: ; preds = %3
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %5
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %18)
store i32 -1, i32* %4
br label %50
20: ; preds = %3
store i32 0, i32* %9
store i32 0, i32* %11
br label %21
21: ; preds = %39, %20
%22 = load i32, i32* %11
%23 = icmp slt i32 %22, 1023
br i1 %23, label %24, label %42
24: ; preds = %21
%25 = load i32*, i32** %8
%26 = call i32 @fgetc(i32* %25)
store i32 %26, i32* %10
%27 = load i32, i32* @EOF
%28 = load i32, i32* %10
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %24
br label %42
31: ; preds = %24
%32 = load i32, i32* %10
%33 = trunc i32 %32 to i8
%34 = load i8*, i8** %6
%35 = load i32, i32* %9
%36 = add nsw i32 %35, 1
store i32 %36, i32* %9
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
store i8 %33, i8* %38
br label %39
39: ; preds = %31
%40 = load i32, i32* %11
%41 = add nsw i32 %40, 1
store i32 %41, i32* %11
br label %21
42: ; preds = %30, %21
%43 = load i32*, i32** %8
%44 = call i32 @fclose(i32* %43)
%45 = load i8*, i8** %6
%46 = load i32, i32* %9
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 0, i8* %48
%49 = load i32, i32* %9
store i32 %49, i32* %4
br label %50
50: ; preds = %42, %16
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"open %s error\0A\00"
@EOF = global i32 0
define i32 @read_file(i8* %0, i8* nocapture writeonly %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %26
9: ; preds = %3, %14
%10 = phi i64 [ %16, %14 ], [ 0, %3 ]
%11 = tail call i32 @fgetc(i32* %4)
%12 = load i32, i32* @EOF
%13 = icmp eq i32 %12, %11
br i1 %13, label %19, label %14
14: ; preds = %9
%15 = trunc i32 %11 to i8
%16 = add nuw nsw i64 %10, 1
%17 = getelementptr inbounds i8, i8* %1, i64 %10
store i8 %15, i8* %17
%18 = icmp eq i64 %16, 1023
br i1 %18, label %21, label %9
19: ; preds = %9
%20 = trunc i64 %10 to i32
br label %21
21: ; preds = %14, %19
%22 = phi i32 [ %20, %19 ], [ 1023, %14 ]
%23 = tail call i32 @fclose(i32* %4)
%24 = zext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %1, i64 %24
store i8 0, i8* %25
br label %26
26: ; preds = %21, %6
%27 = phi i32 [ -1, %6 ], [ %22, %21 ]
ret i32 %27
}