#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*/ MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int ftruncate(int, size_t);
int memfd_create(char const *, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
void *msgs_memfile(const char *filepath, size_t count) {
  int fd = memfd_create(filepath, 0);
  if (fd < 0) {
    perror("open");
    return NULL;
  }
  int al = ftruncate(fd, count);
  if (al == -1) {
    perror("ftruncate");
    return NULL;
  }
  void *ptr = mmap(NULL, count, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (ptr == (void *)-1) {
    perror("mmap");
    return NULL;
  }
  return ptr;
}


x86 assembly (clang, -O0):
<msgs_memfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1030 <memfd_create@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1181 <msgs_memfile+0x41>
lea    0xe91(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1218 <msgs_memfile+0xd8>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1050 <ftruncate@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11b3 <msgs_memfile+0x73>
lea    0xe64(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1218 <msgs_memfile+0xd8>
mov    -0x18(%rbp),%rsi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    0x2e09(%rip),%rax
or     (%rax),%edx
mov    0x2e18(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x28(%rbp)
jne    1210 <msgs_memfile+0xd0>
lea    0xe11(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1218 <msgs_memfile+0xd8>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msgs_memfile>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
xor    %esi,%esi
call   1030 <memfd_create@plt>
test   %eax,%eax
js     119a <msgs_memfile+0x5a>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     11a3 <msgs_memfile+0x63>
mov    0x2e8f(%rip),%rax
mov    0x2e60(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e6d(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     11ac <msgs_memfile+0x6c>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe5f(%rip),%rdi
jmp    11b3 <msgs_memfile+0x73>
lea    0xe5b(%rip),%rdi
jmp    11b3 <msgs_memfile+0x73>
lea    0xe5c(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
jmp    1193 <msgs_memfile+0x53>

LLVM IR (-O0):
@.str = [5 x i8] c"open\00"
@.str.1 = [10 x i8] c"ftruncate\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [5 x i8] c"mmap\00"
define i8* @msgs_memfile(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%9 = load i8*, i8** %4
%10 = call i32 @memfd_create(i8* %9, i32 0)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %37
15: ; preds = %2
%16 = load i32, i32* %6
%17 = load i64, i64* %5
%18 = call i32 @ftruncate(i32 %16, i64 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %3
br label %37
23: ; preds = %15
%24 = load i64, i64* %5
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @PROT_WRITE
%27 = or i32 %25, %26
%28 = load i32, i32* @MAP_SHARED
%29 = load i32, i32* %6
%30 = call i8* @mmap(i32* null, i64 %24, i32 %27, i32 %28, i32 %29, i32 0)
store i8* %30, i8** %8
%31 = load i8*, i8** %8
%32 = icmp eq i8* %31, inttoptr (i64 -1 to i8*)
br i1 %32, label %33, label %35
33: ; preds = %23
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %3
br label %37
35: ; preds = %23
%36 = load i8*, i8** %8
store i8* %36, i8** %3
br label %37
37: ; preds = %35, %33, %21, %13
%38 = load i8*, i8** %3
ret i8* %38
}

LLVM IR (-O2):
@.str = [5 x i8] c"open\00"
@.str.1 = [10 x i8] c"ftruncate\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [5 x i8] c"mmap\00"
define i8* @msgs_memfile(i8* %0, i64 %1) {
%3 = tail call i32 @memfd_create(i8* %0, i32 0)
%4 = icmp slt i32 %3, 0
br i1 %4, label %15, label %5
5: ; preds = %2
%6 = tail call i32 @ftruncate(i32 %3, i64 %1)
%7 = icmp eq i32 %6, -1
br i1 %7, label %15, label %8
8: ; preds = %5
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %10, %9
%12 = load i32, i32* @MAP_SHARED
%13 = tail call i8* @mmap(i32* null, i64 %1, i32 %11, i32 %12, i32 %3, i32 0)
%14 = icmp eq i8* %13, inttoptr (i64 -1 to i8*)
br i1 %14, label %15, label %18
15: ; preds = %8, %5, %2
%16 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %5 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %8 ]
%17 = tail call i32 @perror(i8* %16)
br label %18
18: ; preds = %15, %8
%19 = phi i8* [ %13, %8 ], [ null, %15 ]
ret i8* %19
}