#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 /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
void *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int fd;
  void *v_addr;
  printf("\nApplication to test mmap usage.\n");
  fd = open("/dev/vma", O_RDWR);
  if (fd == -1) {
    printf("cannot access /dev/vma\n");
    return -1;
  }
  v_addr = mmap(0, 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  printf("v_addr is 0x%08x\n", (int)v_addr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
lea    0xec5(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1189 <main+0x59>
lea    0xeb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <main+0xa7>
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
mov    0x2e2f(%rip),%rax
or     (%rax),%edx
mov    0x2e46(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x400,%esi
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xeca(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
lea    0xee4(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11a8 <main+0x68>
mov    0x2e8a(%rip),%rcx
mov    0x2e53(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e68(%rip),%rcx
mov    (%rcx),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0x400,%esi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
lea    0xe63(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11b9 <main+0x79>
lea    0xe84(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [34 x i8] c"\0AApplication to test mmap usage.\0A\00"
@.str.1 = [9 x i8] c"/dev/vma\00"
@O_RDWR = global i32 0
@.str.2 = [24 x i8] c"cannot access /dev/vma\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [18 x i8] c"v_addr is 0x%08x\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
store i32 0, i32* %1
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %21
11: ; preds = %0
%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* %2
%17 = call i8* @mmap(i32 0, i32 1024, i32 %14, i32 %15, i32 %16, i32 0)
store i8* %17, i8** %3
%18 = load i8*, i8** %3
%19 = ptrtoint i8* %18 to i32
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %19)
store i32 0, i32* %1
br label %21
21: ; preds = %11, %9
%22 = load i32, i32* %1
ret i32 %22
}

LLVM IR (-O2):
@.str.1 = [9 x i8] c"/dev/vma\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [18 x i8] c"v_addr is 0x%08x\0A\00"
@str = [33 x i8] c"\0AApplication to test mmap usage.\00"
@str.4 = [23 x i8] c"cannot access /dev/vma\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.4, i64 0, i64 0))
br label %16
7: ; preds = %0
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %9, %8
%11 = load i32, i32* @MAP_SHARED
%12 = tail call i8* @mmap(i32 0, i32 1024, i32 %10, i32 %11, i32 %3, i32 0)
%13 = ptrtoint i8* %12 to i64
%14 = trunc i64 %13 to i32
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %7, %5
%17 = phi i32 [ -1, %5 ], [ 0, %7 ]
ret i32 %17
}