#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
char *strchr(char *, char);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *getMessage(char *filename, char *msg) {
  int fileLength, msgLength;
  char *tempMsg = ((char *)malloc(sizeof(char) * 500 + 1));
  FILE *fp = fopen(filename, "r");
  if (fp) {
    fseek(fp, 0, SEEK_END);
    fileLength = ftell(fp);
    fseek(fp, SEEK_SET, 0);
    fread(tempMsg, sizeof(char), fileLength, fp);
    char *endp = strchr(tempMsg, '|');
    if (endp != NULL) {
      msgLength = ((char *)endp - (char *)tempMsg);
      strncpy(msg, tempMsg, msgLength);
      msg[msgLength] = '\0';
    } else {
      return "Error: No terminating character found.";
    }
    free(tempMsg);
    fclose(fp);
    return msg;
  } else {
    return "Error: Message file failed to open. Please contact the poor "
           "students who made this.";
  }
}


x86 assembly (clang, -O0):
<getMessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x1f5,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe47(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1291 <getMessage+0x101>
mov    -0x30(%rbp),%rdi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <fseek@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    -0x28(%rbp),%rdi
mov    $0x7c,%esi
call   1070 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1262 <getMessage+0xd2>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1272 <getMessage+0xe2>
lea    0xd99(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    129c <getMessage+0x10c>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    129c <getMessage+0x10c>
lea    0xd91(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getMessage>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x1f5,%edi
call   1090 <malloc@plt>
mov    %rax,%r15
lea    0xe4e(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1247 <getMessage+0xb7>
mov    %rax,%r12
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r12,%rdi
call   1080 <ftell@plt>
mov    %eax,%ebx
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
xor    %edx,%edx
call   10a0 <fseek@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %r12,%rcx
call   1050 <fread@plt>
mov    %r15,%rdi
mov    $0x7c,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     1250 <getMessage+0xc0>
mov    %rax,%rbx
sub    %r15,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <strncpy@plt>
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
jmp    1257 <getMessage+0xc7>
lea    0xddb(%rip),%r14
jmp    1257 <getMessage+0xc7>
lea    0xdab(%rip),%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [39 x i8] c"Error: No terminating character found.\00"
@.str.2 = [84 x i8] c"Error: Message file failed to open. Please contact the poor students who made this.\00"
define i8* @getMessage(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = call i64 @malloc(i32 501)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %8
%13 = load i8*, i8** %4
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %9
%15 = load i32*, i32** %9
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %56
17: ; preds = %2
%18 = load i32*, i32** %9
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %9
%22 = call i32 @ftell(i32* %21)
store i32 %22, i32* %6
%23 = load i32*, i32** %9
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @fseek(i32* %23, i32 %24, i32 0)
%26 = load i8*, i8** %8
%27 = load i32, i32* %6
%28 = load i32*, i32** %9
%29 = call i32 @fread(i8* %26, i32 1, i32 %27, i32* %28)
%30 = load i8*, i8** %8
%31 = call i8* @strchr(i8* %30, i8 signext 124)
store i8* %31, i8** %10
%32 = load i8*, i8** %10
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %49
34: ; preds = %17
%35 = load i8*, i8** %10
%36 = load i8*, i8** %8
%37 = ptrtoint i8* %35 to i64
%38 = ptrtoint i8* %36 to i64
%39 = sub i64 %37, %38
%40 = trunc i64 %39 to i32
store i32 %40, i32* %7
%41 = load i8*, i8** %5
%42 = load i8*, i8** %8
%43 = load i32, i32* %7
%44 = call i32 @strncpy(i8* %41, i8* %42, i32 %43)
%45 = load i8*, i8** %5
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 0, i8* %48
br label %50
49: ; preds = %17
store i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8** %3
br label %57
50: ; preds = %34
%51 = load i8*, i8** %8
%52 = call i32 @free(i8* %51)
%53 = load i32*, i32** %9
%54 = call i32 @fclose(i32* %53)
%55 = load i8*, i8** %5
store i8* %55, i8** %3
br label %57
56: ; preds = %2
store i8* getelementptr inbounds ([84 x i8], [84 x i8]* @.str.2, i64 0, i64 0), i8** %3
br label %57
57: ; preds = %56, %50, %49
%58 = load i8*, i8** %3
ret i8* %58
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [39 x i8] c"Error: No terminating character found.\00"
@.str.2 = [84 x i8] c"Error: Message file failed to open. Please contact the poor students who made this.\00"
define i8* @getMessage(i8* nocapture %0, i8* %1) {
%3 = tail call i64 @malloc(i32 501)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %26, label %7
7: ; preds = %2
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %5, i32 0, i32 %8)
%10 = tail call i32 @ftell(i32* %5)
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @fseek(i32* %5, i32 %11, i32 0)
%13 = tail call i32 @fread(i8* %4, i32 1, i32 %10, i32* %5)
%14 = tail call i8* @strchr(i8* dereferenceable(1) %4, i8 signext 124)
%15 = icmp eq i8* %14, null
br i1 %15, label %26, label %16
16: ; preds = %7
%17 = ptrtoint i8* %14 to i64
%18 = sub i64 %17, %3
%19 = trunc i64 %18 to i32
%20 = tail call i32 @strncpy(i8* %1, i8* %4, i32 %19)
%21 = shl i64 %18, 32
%22 = ashr exact i64 %21, 32
%23 = getelementptr inbounds i8, i8* %1, i64 %22
store i8 0, i8* %23
%24 = tail call i32 @free(i8* %4)
%25 = tail call i32 @fclose(i32* %5)
br label %26
26: ; preds = %2, %16, %7
%27 = phi i8* [ %1, %16 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), %7 ], [ getelementptr inbounds ([84 x i8], [84 x i8]* @.str.2, i64 0, i64 0), %2 ]
ret i8* %27
}