#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ftruncate(int, int);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
void *fetch_mmap(char *file_path) {
  int fd = open(file_path, O_RDWR | O_CREAT);
  ftruncate(fd, 512);
  void *mem = mmap(NULL, 512, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (mem == MAP_FAILED)
    printf("Error Mapping !");
  printf("Mapped Memory Adrress : %ld \n", (unsigned long)mem);
  close(fd);
  return mem;
}


x86 assembly (clang, -O0):
<fetch_mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    0x2e88(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x200,%esi
call   1050 <ftruncate@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
mov    0x2e29(%rip),%rax
or     (%rax),%edx
mov    0x2e40(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x200,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2df2(%rip),%rcx
cmp    (%rcx),%rax
jne    11dd <fetch_mmap+0x8d>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fetch_mmap>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e76(%rip),%rax
mov    0x2e97(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x200,%esi
call   1050 <ftruncate@plt>
mov    0x2e71(%rip),%rax
mov    0x2e3a(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e4f(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x200,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e09(%rip),%rax
cmp    (%rax),%rbx
jne    11c2 <fetch_mmap+0x72>
lea    0xe45(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe47(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [16 x i8] c"Error Mapping !\00"
@.str.1 = [30 x i8] c"Mapped Memory Adrress : %ld \0A\00"
define i8* @fetch_mmap(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = call i32 @open(i8* %5, i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = call i32 @ftruncate(i32 %10, i32 512)
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %12, %13
%15 = load i32, i32* @MAP_SHARED
%16 = load i32, i32* %3
%17 = call i8* @mmap(i32* null, i32 512, i32 %14, i32 %15, i32 %16, i32 0)
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = load i8*, i8** @MAP_FAILED
%20 = icmp eq i8* %18, %19
br i1 %20, label %21, label %23
21: ; preds = %1
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %1
%24 = load i8*, i8** %4
%25 = ptrtoint i8* %24 to i64
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i64 %25)
%27 = load i32, i32* %3
%28 = call i32 @close(i32 %27)
%29 = load i8*, i8** %4
ret i8* %29
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [16 x i8] c"Error Mapping !\00"
@.str.1 = [30 x i8] c"Mapped Memory Adrress : %ld \0A\00"
define i8* @fetch_mmap(i8* nocapture %0) {
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = tail call i32 @ftruncate(i32 %5, i32 512)
%7 = load i32, i32* @PROT_READ
%8 = load i32, i32* @PROT_WRITE
%9 = or i32 %8, %7
%10 = load i32, i32* @MAP_SHARED
%11 = tail call i8* @mmap(i32* null, i32 512, i32 %9, i32 %10, i32 %5, i32 0)
%12 = load i8*, i8** @MAP_FAILED
%13 = icmp eq i8* %11, %12
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %1
%17 = ptrtoint i8* %11 to i64
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i64 %17)
%19 = tail call i32 @close(i32 %5)
ret i8* %11
}