#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fileRead(char *name, char *buff, int bufflen) {
  int readlen;
  FILE *fp = fopen(name, "rb");
  if (fp != NULL) {
    readlen = fread(buff, 1, bufflen, fp);
    if (ferror(fp) != 0) {
      return -1;
    } else {
      buff[readlen] = '\0';
    }
    fclose(fp);
  } else {
    return 0;
  }
  return readlen;
}


x86 assembly (clang, -O0):
<fileRead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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)
cmpq   $0x0,-0x28(%rbp)
je     11c3 <fileRead+0x83>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11a9 <fileRead+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d5 <fileRead+0x95>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
jmp    11cf <fileRead+0x8f>
movl   $0x0,-0x4(%rbp)
jmp    11d5 <fileRead+0x95>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fileRead>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
lea    0xeb0(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     119a <fileRead+0x5a>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <ferror@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    119c <fileRead+0x5c>
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %ebp,%eax
jmp    119c <fileRead+0x5c>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
define i32 @fileRead(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %9
%12 = load i32*, i32** %9
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %31
14: ; preds = %3
%15 = load i8*, i8** %6
%16 = load i32, i32* %7
%17 = load i32*, i32** %9
%18 = call i32 @fread(i8* %15, i32 1, i32 %16, i32* %17)
store i32 %18, i32* %8
%19 = load i32*, i32** %9
%20 = call i64 @ferror(i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %14
store i32 -1, i32* %4
br label %34
23: ; preds = %14
%24 = load i8*, i8** %6
%25 = load i32, i32* %8
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %23
%29 = load i32*, i32** %9
%30 = call i32 @fclose(i32* %29)
br label %32
31: ; preds = %3
store i32 0, i32* %4
br label %34
32: ; preds = %28
%33 = load i32, i32* %8
store i32 %33, i32* %4
br label %34
34: ; preds = %32, %31, %22
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
define i32 @fileRead(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %14, label %6
6: ; preds = %3
%7 = tail call i32 @fread(i8* %1, i32 1, i32 %2, i32* %4)
%8 = tail call i64 @ferror(i32* %4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = sext i32 %7 to i64
%12 = getelementptr inbounds i8, i8* %1, i64 %11
store i8 0, i8* %12
%13 = tail call i32 @fclose(i32* %4)
br label %14
14: ; preds = %3, %6, %10
%15 = phi i32 [ %7, %10 ], [ -1, %6 ], [ 0, %3 ]
ret i32 %15
}