#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 */
struct stat {
  scalar_t__ st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fread(char *, int, scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *get_data_from_file(char *filename, int *len_return) {
  FILE *fp;
  struct stat statbuf;
  char *cp;
  int count;
  if (stat(filename, &statbuf) != 0 || statbuf.st_size < 0) {
    perror(filename);
    return NULL;
  }
  cp = malloc(statbuf.st_size + 1);
  if (!cp) {
    fprintf(stderr, "cannot get memory for message file\n");
    return NULL;
  }
  fp = fopen(filename, "r");
  if (!fp) {
    perror(filename);
    (void)free(cp);
    return NULL;
  }
  count = fread(cp, 1, statbuf.st_size, fp);
  if (count == 0 && statbuf.st_size != 0) {
    perror(filename);
    (void)free(cp);
    (void)fclose(fp);
    return NULL;
  }
  cp[count] = '\0';
  *len_return = count;
  (void)fclose(fp);
  return cp;
}


x86 assembly (clang, -O0):
<get_data_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    11b2 <get_data_from_file+0x32>
cmpq   $0x0,-0x28(%rbp)
jge    11c8 <get_data_from_file+0x48>
mov    -0x10(%rbp),%rdi
call   10a0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c0 <get_data_from_file+0x140>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1206 <get_data_from_file+0x86>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe0c(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c0 <get_data_from_file+0x140>
mov    -0x10(%rbp),%rdi
lea    0xe13(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1244 <get_data_from_file+0xc4>
mov    -0x10(%rbp),%rdi
call   10a0 <perror@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c0 <get_data_from_file+0x140>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    129a <get_data_from_file+0x11a>
cmpq   $0x0,-0x28(%rbp)
je     129a <get_data_from_file+0x11a>
mov    -0x10(%rbp),%rdi
call   10a0 <perror@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    12c0 <get_data_from_file+0x140>
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x34(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_data_from_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsp,%rsi
call   1070 <stat@plt>
test   %rax,%rax
jne    122d <get_data_from_file+0xad>
mov    (%rsp),%rdi
test   %rdi,%rdi
js     122d <get_data_from_file+0xad>
add    $0x1,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1207 <get_data_from_file+0x87>
mov    %rax,%rbx
lea    0xe60(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1237 <get_data_from_file+0xb7>
mov    %rax,%r12
mov    (%rsp),%rdx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rax,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jne    11f3 <get_data_from_file+0x73>
cmpq   $0x0,(%rsp)
jne    1249 <get_data_from_file+0xc9>
movslq %eax,%rcx
movb   $0x0,(%rbx,%rcx,1)
mov    %eax,(%r15)
mov    %r12,%rdi
call   1050 <fclose@plt>
jmp    121e <get_data_from_file+0x9e>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
call   1060 <fprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
call   10a0 <perror@plt>
jmp    121c <get_data_from_file+0x9c>
mov    %r14,%rdi
call   10a0 <perror@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    121c <get_data_from_file+0x9c>
mov    %r14,%rdi
call   10a0 <perror@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
jmp    121c <get_data_from_file+0x9c>

LLVM IR (-O0):
%struct.stat = type { i64 }
@stderr = global i32 0
@.str = [36 x i8] c"cannot get memory for message file\0A\00"
@.str.1 = [2 x i8] c"r\00"
define i8* @get_data_from_file(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca %struct.stat
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%10 = load i8*, i8** %4
%11 = call i64 @stat(i8* %10, %struct.stat* %7)
%12 = icmp ne i64 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %2
%14 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%15 = load i64, i64* %14
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13, %2
%18 = load i8*, i8** %4
%19 = call i32 @perror(i8* %18)
store i8* null, i8** %3
br label %69
20: ; preds = %13
%21 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%22 = load i64, i64* %21
%23 = add nsw i64 %22, 1
%24 = call i8* @malloc(i64 %23)
store i8* %24, i8** %8
%25 = load i8*, i8** %8
%26 = icmp ne i8* %25, null
br i1 %26, label %30, label %27
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %69
30: ; preds = %20
%31 = load i8*, i8** %4
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %32, i32** %6
%33 = load i32*, i32** %6
%34 = icmp ne i32* %33, null
br i1 %34, label %40, label %35
35: ; preds = %30
%36 = load i8*, i8** %4
%37 = call i32 @perror(i8* %36)
%38 = load i8*, i8** %8
%39 = call i32 @free(i8* %38)
store i8* null, i8** %3
br label %69
40: ; preds = %30
%41 = load i8*, i8** %8
%42 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%43 = load i64, i64* %42
%44 = load i32*, i32** %6
%45 = call i32 @fread(i8* %41, i32 1, i64 %43, i32* %44)
store i32 %45, i32* %9
%46 = load i32, i32* %9
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %59
48: ; preds = %40
%49 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%50 = load i64, i64* %49
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %59
52: ; preds = %48
%53 = load i8*, i8** %4
%54 = call i32 @perror(i8* %53)
%55 = load i8*, i8** %8
%56 = call i32 @free(i8* %55)
%57 = load i32*, i32** %6
%58 = call i32 @fclose(i32* %57)
store i8* null, i8** %3
br label %69
59: ; preds = %48, %40
%60 = load i8*, i8** %8
%61 = load i32, i32* %9
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
store i8 0, i8* %63
%64 = load i32, i32* %9
%65 = load i32*, i32** %5
store i32 %64, i32* %65
%66 = load i32*, i32** %6
%67 = call i32 @fclose(i32* %66)
%68 = load i8*, i8** %8
store i8* %68, i8** %3
br label %69
69: ; preds = %59, %52, %35, %27, %17
%70 = load i8*, i8** %3
ret i8* %70
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@stderr = global i32 0
@.str = [36 x i8] c"cannot get memory for message file\0A\00"
@.str.1 = [2 x i8] c"r\00"
define noalias i8* @get_data_from_file(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* %0, %struct.stat* %3)
%6 = icmp ne i64 %5, 0
%7 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%8 = load i64, i64* %7
%9 = icmp slt i64 %8, 0
%10 = select i1 %6, i1 true, i1 %9
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @perror(i8* %0)
br label %41
13: ; preds = %2
%14 = add nuw nsw i64 %8, 1
%15 = call i8* @malloc(i64 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %41
20: ; preds = %13
%21 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = call i32 @perror(i8* %0)
%25 = call i32 @free(i8* %15)
br label %41
26: ; preds = %20
%27 = load i64, i64* %7
%28 = call i32 @fread(i8* %15, i32 1, i64 %27, i32* %21)
%29 = icmp eq i32 %28, 0
%30 = load i64, i64* %7
%31 = icmp ne i64 %30, 0
%32 = select i1 %29, i1 %31, i1 false
br i1 %32, label %33, label %37
33: ; preds = %26
%34 = call i32 @perror(i8* %0)
%35 = call i32 @free(i8* %15)
%36 = call i32 @fclose(i32* %21)
br label %41
37: ; preds = %26
%38 = sext i32 %28 to i64
%39 = getelementptr inbounds i8, i8* %15, i64 %38
store i8 0, i8* %39
store i32 %28, i32* %1
%40 = call i32 @fclose(i32* %21)
br label %41
41: ; preds = %37, %33, %23, %17, %11
%42 = phi i8* [ null, %11 ], [ null, %33 ], [ %15, %37 ], [ null, %23 ], [ null, %17 ]
ret i8* %42
}