#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 */

/* Variables and functions */
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ fileLength(int);
scalar_t__ malloc(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
char *mkPmem(char *pFilename) {
  int hFile, fl;
  char *pmem;
  hFile = open(pFilename, 0 | 0);
  if (hFile != -1) {
    fl = (int)fileLength(hFile);
    printf("The length in bytes is %d\n", fl);
    pmem = (char *)(malloc(fl));
    if (!pmem) {
      printf("Failed to allocate memory\n");
      pmem = ((void *)0);
    } else {
      printf("Successfully allocated memory for pmem\n");
    }
    close(hFile);
  } else {
    printf("Error %d Openning File %s for Read\n", errno, pFilename);
    pmem = ((void *)0);
  }
  return (pmem);
}


x86 assembly (clang, -O0):
<mkPmem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
je     11dd <mkPmem+0x8d>
mov    -0xc(%rbp),%edi
call   1030 <fileLength@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c2 <mkPmem+0x72>
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x18(%rbp)
jmp    11d0 <mkPmem+0x80>
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
jmp    1200 <mkPmem+0xb0>
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rdx
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkPmem>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
xor    %esi,%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11bc <mkPmem+0x5c>
mov    %eax,%ebp
mov    %eax,%edi
call   1040 <fileLength@plt>
mov    %rax,%rbx
lea    0xe7a(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
lea    0xe9c(%rip),%rax
lea    0xeaf(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    11d9 <mkPmem+0x79>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
lea    0xe4f(%rip),%rdi
xor    %r14d,%r14d
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"The length in bytes is %d\0A\00"
@.str.1 = [27 x i8] c"Failed to allocate memory\0A\00"
@.str.2 = [40 x i8] c"Successfully allocated memory for pmem\0A\00"
@.str.3 = [36 x i8] c"Error %d Openning File %s for Read\0A\00"
@errno = global i32 0
define i8* @mkPmem(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @open(i8* %6, i32 0)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp ne i32 %8, -1
br i1 %9, label %10, label %28
10: ; preds = %1
%11 = load i32, i32* %3
%12 = call i64 @fileLength(i32 %11)
%13 = trunc i64 %12 to i32
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = load i32, i32* %4
%17 = call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = icmp ne i8* %19, null
br i1 %20, label %23, label %21
21: ; preds = %10
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %5
br label %25
23: ; preds = %10
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %21
%26 = load i32, i32* %3
%27 = call i32 @close(i32 %26)
br label %32
28: ; preds = %1
%29 = load i32, i32* @errno
%30 = load i8*, i8** %2
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %29, i8* %30)
store i8* null, i8** %5
br label %32
32: ; preds = %28, %25
%33 = load i8*, i8** %5
ret i8* %33
}

LLVM IR (-O2):
@.str = [27 x i8] c"The length in bytes is %d\0A\00"
@.str.3 = [36 x i8] c"Error %d Openning File %s for Read\0A\00"
@errno = global i32 0
@str = [26 x i8] c"Failed to allocate memory\00"
@str.4 = [39 x i8] c"Successfully allocated memory for pmem\00"
define i8* @mkPmem(i8* %0) {
%2 = tail call i32 @open(i8* %0, i32 0)
%3 = icmp eq i32 %2, -1
br i1 %3, label %14, label %4
4: ; preds = %1
%5 = tail call i64 @fileLength(i32 %2)
%6 = trunc i64 %5 to i32
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = tail call i64 @malloc(i32 %6)
%9 = icmp eq i64 %8, 0
%10 = inttoptr i64 %8 to i8*
%11 = select i1 %9, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([39 x i8], [39 x i8]* @str.4, i64 0, i64 0)
%12 = tail call i32 @puts(i8* dereferenceable(1) %11)
%13 = tail call i32 @close(i32 %2)
br label %17
14: ; preds = %1
%15 = load i32, i32* @errno
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %15, i8* %0)
br label %17
17: ; preds = %14, %4
%18 = phi i8* [ %10, %4 ], [ null, %14 ]
ret i8* %18
}