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


x86 assembly (clang, -O0):
<readfile>:
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 <readfile+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    1211 <readfile+0xd1>
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1202 <readfile+0xc2>
mov    -0x28(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x30(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
cmp    -0x30(%rbp),%eax
jne    11da <readfile+0x9a>
jmp    1202 <readfile+0xc2>
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 <readfile+0x66>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
lea    0xea5(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11a8 <readfile+0x68>
mov    %rax,%rbx
xor    %ebp,%ebp
cmp    $0x2,%r14d
jl     119e <readfile+0x5e>
add    $0xffffffff,%r14d
mov    %r14d,%r12d
xor    %ebp,%ebp
mov    0x2e68(%rip),%r13
mov    %rbx,%rdi
call   1040 <fgetc@plt>
cmp    %eax,0x0(%r13)
je     119e <readfile+0x5e>
mov    %al,(%r15,%rbp,1)
add    $0x1,%rbp
cmp    %rbp,%r12
jne    1180 <readfile+0x40>
mov    %r14d,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    11c5 <readfile+0x85>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
mov    %rbp,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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