#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_EXCL;
int O_RDWR;
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(void) {
  int fd = -1;
  char buf[] = "quick brown fox jumps over the dog";
  char *ptr = NULL;
  char filename[] = "mmap.txt";
  fd = open(filename, O_RDWR | O_CREAT | O_EXCL, S_IRWXU);
  if (-1 == fd) {
    printf("Open file %s failure.\n", filename);
    return -1;
  }
  lseek(fd, 80 - 1, SEEK_SET);
  write(fd, "a", 1);
  ptr = (char *)mmap(NULL, 80, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if ((char *)-1 == ptr) {
    printf("mmap falure");
    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)
movl   $0xffffffff,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xe4f(%rip),%rsi
mov    $0x23,%edx
call   1090 <memcpy@plt>
movq   $0x0,-0x38(%rbp)
mov    0xe59(%rip),%rax
mov    %rax,-0x41(%rbp)
mov    0xe57(%rip),%al
mov    %al,-0x39(%rbp)
lea    -0x41(%rbp),%rdi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
mov    0x2e0d(%rip),%rax
or     (%rax),%esi
mov    0x2dc4(%rip),%rax
or     (%rax),%esi
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    1233 <main+0xa3>
lea    -0x41(%rbp),%rsi
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130d <main+0x17d>
mov    -0x8(%rbp),%edi
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
mov    $0x4f,%esi
call   1070 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    0xdf0(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edx
mov    0x2d3b(%rip),%rax
or     (%rax),%edx
mov    0x2d6a(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x50,%esi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    -0x38(%rbp),%rax
jne    12c4 <main+0x134>
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130d <main+0x17d>
mov    -0x38(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x58(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1090 <memcpy@plt>
mov    -0x38(%rbp),%rdi
mov    $0x50,%esi
call   10a0 <munmap@plt>
mov    -0x8(%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   %r12
push   %rbx
sub    $0x40,%rsp
movaps 0xe6d(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe51(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x676f64,0x2f(%rsp)
movabs $0x7478742e70616d6d,%rax
74 78 74
mov    %rax,(%rsp)
movb   $0x0,0x8(%rsp)
mov    0x2df2(%rip),%rax
mov    0x2e1b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dd0(%rip),%rax
or     (%rax),%esi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     129e <main+0x10e>
mov    %eax,%ebp
mov    0x2da3(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    $0x4f,%esi
call   1070 <lseek@plt>
lea    0xe21(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    0x2dbb(%rip),%rax
mov    0x2d6c(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d99(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    $0x50,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     12b1 <main+0x121>
mov    %rax,%r15
mov    %rax,%rbx
add    $0x10,%rbx
lea    0x10(%rsp),%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1090 <memcpy@plt>
mov    %r15,%rdi
mov    $0x50,%esi
call   10a0 <munmap@plt>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    12cc <main+0x13c>
lea    0xd87(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12c6 <main+0x136>
lea    0xd8d(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@__const.main.buf = [35 x i8] c"quick brown fox jumps over the dog\00"
@__const.main.filename = [9 x i8] c"mmap.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRWXU = global i32 0
@.str = [23 x i8] c"Open file %s failure.\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [2 x i8] c"a\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [12 x i8] c"mmap falure\00"
define i32 @main() {
%1 = alloca [35 x i8]
%2 = alloca [9 x i8]
%3 = getelementptr inbounds [35 x i8], [35 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [9 x i8], [9 x i8]* %2, 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_EXCL
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRWXU
%11 = call i32 @open(i8* %4, i32 %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %4)
br label %35
15: ; preds = %0
%16 = load i32, i32* @SEEK_SET
%17 = tail call i32 @lseek(i32 %11, i32 79, i32 %16)
%18 = tail call i32 @write(i32 %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_WRITE
%21 = or i32 %20, %19
%22 = load i32, i32* @MAP_SHARED
%23 = tail call i64 @mmap(i32* null, i32 80, i32 %21, i32 %22, i32 %11, i32 0)
%24 = inttoptr i64 %23 to i8*
%25 = icmp eq i8* %24, inttoptr (i64 -1 to i8*)
br i1 %25, label %26, label %29
26: ; preds = %15
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%28 = tail call i32 @close(i32 %11)
br label %35
29: ; preds = %15
%30 = getelementptr inbounds i8, i8* %24, i64 16
%31 = call i32 @strlen(i8* %3)
%32 = call i32 @memcpy(i8* %30, i8* %3, i32 %31)
%33 = call i32 @munmap(i8* %24, i32 80)
%34 = call i32 @close(i32 %11)
br label %35
35: ; preds = %29, %26, %13
%36 = phi i32 [ -1, %13 ], [ -1, %26 ], [ 0, %29 ]
ret i32 %36
}