#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 *);
int /*<<< orphan*/ stderr;
int readfile(const char *filename, char *buffer, int maxlen) {
  FILE *file;
  int temp, pos;
  file = fopen(filename, "r");
  if (NULL == file) {
    fprintf(stderr, "Can not open the file  \n");
    return -1;
  }
  pos = 0;
  for (int i = 0; i < maxlen - 1; i++) {
    temp = fgetc(file);
    if (EOF == temp)
      break;
    buffer[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    1194 <readfile+0x54>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
lea    0xe7f(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <readfile+0xcd>
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11fe <readfile+0xbe>
mov    -0x28(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
cmp    -0x2c(%rbp),%eax
jne    11d6 <readfile+0x96>
jmp    11fe <readfile+0xbe>
mov    -0x2c(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x30(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11a2 <readfile+0x62>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x30(%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
lea    0xea8(%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    0x2e6b(%rip),%r13
nopl   (%rax)
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    11c2 <readfile+0x82>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
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 = [25 x i8] c"Can not open the file \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 %19
16: ; preds = %3
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %47
19: ; preds = %3
store i32 0, i32* %10
store i32 0, i32* %11
br label %20
20: ; preds = %40, %19
%21 = load i32, i32* %11
%22 = load i32, i32* %7
%23 = sub nsw i32 %22, 1
%24 = icmp slt i32 %21, %23
br i1 %24, label %25, label %43
25: ; preds = %20
%26 = load i32*, i32** %8
%27 = call i32 @fgetc(i32* %26)
store i32 %27, i32* %9
%28 = load i32, i32* @EOF
%29 = load i32, i32* %9
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %25
br label %43
32: ; preds = %25
%33 = load i32, i32* %9
%34 = trunc i32 %33 to i8
%35 = load i8*, i8** %6
%36 = load i32, i32* %10
%37 = add nsw i32 %36, 1
store i32 %37, i32* %10
%38 = sext i32 %36 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
store i8 %34, i8* %39
br label %40
40: ; preds = %32
%41 = load i32, i32* %11
%42 = add nsw i32 %41, 1
store i32 %42, i32* %11
br label %20
43: ; preds = %31, %20
%44 = load i32*, i32** %8
%45 = call i32 @fclose(i32* %44)
%46 = load i32, i32* %10
store i32 %46, i32* %4
br label %47
47: ; preds = %43, %16
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Can not open the file \0A\00"
@EOF = global i32 0
define i32 @readfile(i8* nocapture %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 ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 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
}