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

/* Variables and functions */
int /*<<< orphan*/ MyExit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char *, struct stat *);
char *readmessagefile(char *name) {
  FILE *stream;
  struct stat sb;
  char *p;
  if (stat(name, &sb) != 0)
    goto error;
  if ((stream = fopen(name, "r")) == NULL)
    goto error;
  if ((p = malloc(sb.st_size + 1)) == NULL)
    goto error;
  if (fread(p, sizeof(char), sb.st_size, stream) != sb.st_size)
    goto error;
  p[sb.st_size + 1] = '\0';
  fclose(stream);
  return p;
error:
  perror(name);
  MyExit(1);
}


x86 assembly (clang, -O0):
<readmessagefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     1198 <readmessagefile+0x28>
jmp    1223 <readmessagefile+0xb3>
mov    -0x10(%rbp),%rdi
lea    0xe5d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11bb <readmessagefile+0x4b>
jmp    1223 <readmessagefile+0xb3>
mov    -0x20(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11d9 <readmessagefile+0x69>
jmp    1223 <readmessagefile+0xb3>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x20(%rbp),%eax
je     11fc <readmessagefile+0x8c>
jmp    1223 <readmessagefile+0xb3>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1236 <readmessagefile+0xc6>
mov    -0x10(%rbp),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <MyExit@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readmessagefile>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rsp,%rsi
call   1050 <stat@plt>
test   %rax,%rax
jne    11de <readmessagefile+0x6e>
lea    0xe71(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11de <readmessagefile+0x6e>
mov    %rax,%r15
mov    (%rsp),%r12d
lea    0x1(%r12),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11de <readmessagefile+0x6e>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
cmp    (%rsp),%eax
jne    11de <readmessagefile+0x6e>
cltq
movb   $0x0,0x1(%rax,%rbx,1)
mov    %r15,%rdi
call   1040 <fclose@plt>
jmp    11f0 <readmessagefile+0x80>
mov    %r14,%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <MyExit@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
define i8* @readmessagefile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.stat
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i64 @stat(i8* %7, %struct.stat* %5)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
br label %43
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %4
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %11
br label %43
16: ; preds = %11
%17 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%18 = load i32, i32* %17
%19 = add nsw i32 %18, 1
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** %6
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %16
br label %43
23: ; preds = %16
%24 = load i8*, i8** %6
%25 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%26 = load i32, i32* %25
%27 = load i32*, i32** %4
%28 = call i32 @fread(i8* %24, i32 1, i32 %26, i32* %27)
%29 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%30 = load i32, i32* %29
%31 = icmp ne i32 %28, %30
br i1 %31, label %32, label %33
32: ; preds = %23
br label %43
33: ; preds = %23
%34 = load i8*, i8** %6
%35 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%36 = load i32, i32* %35
%37 = add nsw i32 %36, 1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %34, i64 %38
store i8 0, i8* %39
%40 = load i32*, i32** %4
%41 = call i32 @fclose(i32* %40)
%42 = load i8*, i8** %6
store i8* %42, i8** %2
br label %47
43: ; preds = %32, %22, %15, %10
%44 = load i8*, i8** %3
%45 = call i32 @perror(i8* %44)
%46 = call i32 @MyExit(i32 1)
br label %47
47: ; preds = %43, %33
%48 = load i8*, i8** %2
ret i8* %48
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
define noalias i8* @readmessagefile(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %24
6: ; preds = %1
%7 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %24, label %9
9: ; preds = %6
%10 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = add nsw i32 %11, 1
%13 = call i8* @malloc(i32 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %24, label %15
15: ; preds = %9
%16 = call i32 @fread(i8* %13, i32 1, i32 %11, i32* %7)
%17 = load i32, i32* %10
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = add nsw i32 %16, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %13, i64 %21
store i8 0, i8* %22
%23 = call i32 @fclose(i32* %7)
br label %27
24: ; preds = %15, %9, %6, %1
%25 = call i32 @perror(i8* %0)
%26 = call i32 @MyExit(i32 1)
br label %27
27: ; preds = %24, %19
%28 = phi i8* [ undef, %24 ], [ %13, %19 ]
ret i8* %28
}