#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 O_CREAT;
int O_RDWR;
int O_TRUNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, char *, int /*<<< orphan*/);
scalar_t__ mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
                int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd = -1;
  char buf[] = "quick brown fox jumps over the lazy dog";
  char *ptr = NULL;
  fd = open("mmap.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
  if (-1 == fd) {
    return -1;
  }
  lseek(fd, 80, SEEK_SET);
  write(fd, " ", 1);
  ptr = (char *)mmap(NULL, 80, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if ((char *)-1 == ptr) {
    printf("mmap failure!\n");
    close(fd);
    return -1;
  }
  memcpy(ptr + 16, buf, strlen(buf));
  munmap(ptr, 80);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xe48(%rip),%rsi
mov    $0x28,%edx
call   1090 <memcpy@plt>
movq   $0x0,-0x48(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    0x2e1e(%rip),%rax
or     (%rax),%esi
mov    0x2e0d(%rip),%rax
or     (%rax),%esi
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
lea    0xe33(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    1217 <main+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ef <main+0x15f>
mov    -0x14(%rbp),%edi
mov    0x2d8f(%rip),%rax
mov    (%rax),%edx
mov    $0x50,%esi
call   1070 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    0xdfa(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%edx
mov    0x2d57(%rip),%rax
or     (%rax),%edx
mov    0x2d7e(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x50,%esi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x48(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    -0x48(%rbp),%rax
jne    12a6 <main+0x116>
lea    0xda6(%rip),%rdi
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ef <main+0x15f>
mov    -0x48(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x58(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1090 <memcpy@plt>
mov    -0x48(%rbp),%rdi
mov    $0x50,%esi
call   10a0 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
movaps 0xe6f(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe53(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0x676f6420797a61,%rax
mov    %rax,0x20(%rsp)
mov    0x2df9(%rip),%rax
mov    0x2e2a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e17(%rip),%rax
or     (%rax),%esi
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
lea    0xe3d(%rip),%rdi
call   10b0 <open@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     12aa <main+0x11a>
mov    %eax,%ebx
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x50,%esi
call   1070 <lseek@plt>
lea    0xe14(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <write@plt>
mov    0x2db8(%rip),%rax
mov    0x2d71(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d96(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x50,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     1297 <main+0x107>
mov    %rax,%r15
mov    %rax,%rbp
add    $0x10,%rbp
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1090 <memcpy@plt>
mov    %r15,%rdi
mov    $0x50,%esi
call   10a0 <munmap@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %r14d,%r14d
jmp    12aa <main+0x11a>
lea    0xd95(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.buf = [40 x i8] c"quick brown fox jumps over the lazy dog\00"
@.str = [9 x i8] c"mmap.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [2 x i8] c" \00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [15 x i8] c"mmap failure!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [40 x i8]
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
%9 = bitcast [40 x i8]* %7 to i8*
store i8* null, i8** %8
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = load i32, i32* @O_TRUNC
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRWXU
%16 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 -1, %17
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 -1, i32* %3
br label %50
20: ; preds = %2
%21 = load i32, i32* %6
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @lseek(i32 %21, i32 80, i32 %22)
%24 = load i32, i32* %6
%25 = call i32 @write(i32 %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @PROT_WRITE
%28 = or i32 %26, %27
%29 = load i32, i32* @MAP_SHARED
%30 = load i32, i32* %6
%31 = call i64 @mmap(i32* null, i32 80, i32 %28, i32 %29, i32 %30, i32 0)
%32 = inttoptr i64 %31 to i8*
store i8* %32, i8** %8
%33 = load i8*, i8** %8
%34 = icmp eq i8* inttoptr (i64 -1 to i8*), %33
br i1 %34, label %35, label %39
35: ; preds = %20
%36 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
store i32 -1, i32* %3
br label %50
39: ; preds = %20
%40 = load i8*, i8** %8
%41 = getelementptr inbounds i8, i8* %40, i64 16
%42 = getelementptr inbounds [40 x i8], [40 x i8]* %7, i64 0, i64 0
%43 = getelementptr inbounds [40 x i8], [40 x i8]* %7, i64 0, i64 0
%44 = call i32 @strlen(i8* %43)
%45 = call i32 @memcpy(i8* %41, i8* %42, i32 %44)
%46 = load i8*, i8** %8
%47 = call i32 @munmap(i8* %46, i32 80)
%48 = load i32, i32* %6
%49 = call i32 @close(i32 %48)
store i32 0, i32* %3
br label %50
50: ; preds = %39, %35, %19
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@__const.main.buf = [40 x i8] c"quick brown fox jumps over the lazy dog\00"
@.str = [9 x i8] c"mmap.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [2 x i8] c" \00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@str = [14 x i8] c"mmap failure!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [40 x i8]
%4 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRWXU
%11 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %33, label %13
13: ; preds = %2
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @lseek(i32 %11, i32 80, i32 %14)
%16 = tail call i32 @write(i32 %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @PROT_WRITE
%19 = or i32 %18, %17
%20 = load i32, i32* @MAP_SHARED
%21 = tail call i64 @mmap(i32* null, i32 80, i32 %19, i32 %20, i32 %11, i32 0)
%22 = inttoptr i64 %21 to i8*
%23 = icmp eq i8* %22, inttoptr (i64 -1 to i8*)
br i1 %23, label %24, label %27
24: ; preds = %13
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%26 = tail call i32 @close(i32 %11)
br label %33
27: ; preds = %13
%28 = getelementptr inbounds i8, i8* %22, i64 16
%29 = call i32 @strlen(i8* %4)
%30 = call i32 @memcpy(i8* %28, i8* %4, i32 %29)
%31 = call i32 @munmap(i8* %22, i32 80)
%32 = call i32 @close(i32 %11)
br label %33
33: ; preds = %2, %27, %24
%34 = phi i32 [ -1, %24 ], [ 0, %27 ], [ -1, %2 ]
ret i32 %34
}