#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 */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ *memcpy(char *, char *, size_t);
char *mmap(int /*<<< orphan*/, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, size_t);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int cp_map(int srcfd, int dstfd, size_t len) {
  char *src, *dst;
  if ((src = mmap(0, len, PROT_READ, MAP_SHARED, srcfd, 0)) == MAP_FAILED) {
    printf("mmap src error");
    printf(":%s\n", strerror(errno));
    return -1;
  }
  if ((dst = mmap(0, len, PROT_WRITE, MAP_SHARED, dstfd, 0)) == MAP_FAILED) {
    printf("mmap dst error");
    printf(":%s\n", strerror(errno));
    return -1;
  }
  if (memcpy(dst, src, len) == NULL) {
    printf("memcpy error");
    printf(":%s\n", strerror(errno));
    return -1;
  }
  munmap(src, len);
  munmap(dst, len);
  return 0;
}


x86 assembly (clang, -O0):
<cp_map>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
mov    0x2e8b(%rip),%rax
mov    (%rax),%edx
mov    0x2e72(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e2e(%rip),%rcx
cmp    (%rcx),%rax
jne    11d4 <cp_map+0x84>
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bb <cp_map+0x16b>
mov    -0x18(%rbp),%rsi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
mov    0x2e00(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    0x2dbc(%rip),%rcx
cmp    (%rcx),%rax
jne    1246 <cp_map+0xf6>
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bb <cp_map+0x16b>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
cmp    $0x0,%rax
jne    129a <cp_map+0x14a>
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bb <cp_map+0x16b>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <munmap@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <munmap@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cp_map>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,%r8d
mov    0x2e91(%rip),%rax
mov    (%rax),%edx
mov    0x2e78(%rip),%r15
mov    (%r15),%ecx
xor    %edi,%edi
mov    %r14,%rsi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2e39(%rip),%r12
cmp    (%r12),%rax
je     11e2 <cp_map+0x92>
mov    %rax,%rbx
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
mov    (%r15),%ecx
xor    %edi,%edi
mov    %r14,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    (%r12),%rax
je     11eb <cp_map+0x9b>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1050 <memcpy@plt>
test   %rax,%rax
je     11f4 <cp_map+0xa4>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <munmap@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1060 <munmap@plt>
xor    %eax,%eax
jmp    1226 <cp_map+0xd6>
lea    0xe17(%rip),%rdi
jmp    11fb <cp_map+0xab>
lea    0xe22(%rip),%rdi
jmp    11fb <cp_map+0xab>
lea    0xe28(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdf8(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [15 x i8] c"mmap src error\00"
@.str.1 = [5 x i8] c":%s\0A\00"
@errno = global i32 0
@PROT_WRITE = global i32 0
@.str.2 = [15 x i8] c"mmap dst error\00"
@.str.3 = [13 x i8] c"memcpy error\00"
define i32 @cp_map(i32 %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64 %2, i64* %7
%10 = load i64, i64* %7
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @MAP_SHARED
%13 = load i32, i32* %5
%14 = call i8* @mmap(i32 0, i64 %10, i32 %11, i32 %12, i32 %13, i32 0)
store i8* %14, i8** %8
%15 = load i8*, i8** @MAP_FAILED
%16 = icmp eq i8* %14, %15
br i1 %16, label %17, label %22
17: ; preds = %3
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %20)
store i32 -1, i32* %4
br label %53
22: ; preds = %3
%23 = load i64, i64* %7
%24 = load i32, i32* @PROT_WRITE
%25 = load i32, i32* @MAP_SHARED
%26 = load i32, i32* %6
%27 = call i8* @mmap(i32 0, i64 %23, i32 %24, i32 %25, i32 %26, i32 0)
store i8* %27, i8** %9
%28 = load i8*, i8** @MAP_FAILED
%29 = icmp eq i8* %27, %28
br i1 %29, label %30, label %35
30: ; preds = %22
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %33)
store i32 -1, i32* %4
br label %53
35: ; preds = %22
%36 = load i8*, i8** %9
%37 = load i8*, i8** %8
%38 = load i64, i64* %7
%39 = call i32* @memcpy(i8* %36, i8* %37, i64 %38)
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %46
41: ; preds = %35
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %44)
store i32 -1, i32* %4
br label %53
46: ; preds = %35
%47 = load i8*, i8** %8
%48 = load i64, i64* %7
%49 = call i32 @munmap(i8* %47, i64 %48)
%50 = load i8*, i8** %9
%51 = load i64, i64* %7
%52 = call i32 @munmap(i8* %50, i64 %51)
store i32 0, i32* %4
br label %53
53: ; preds = %46, %41, %30, %17
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [15 x i8] c"mmap src error\00"
@.str.1 = [5 x i8] c":%s\0A\00"
@errno = global i32 0
@PROT_WRITE = global i32 0
@.str.2 = [15 x i8] c"mmap dst error\00"
@.str.3 = [13 x i8] c"memcpy error\00"
define i32 @cp_map(i32 %0, i32 %1, i64 %2) {
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @MAP_SHARED
%6 = tail call i8* @mmap(i32 0, i64 %2, i32 %4, i32 %5, i32 %0, i32 0)
%7 = load i8*, i8** @MAP_FAILED
%8 = icmp eq i8* %6, %7
br i1 %8, label %9, label %14
9: ; preds = %3
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %12)
br label %36
14: ; preds = %3
%15 = load i32, i32* @PROT_WRITE
%16 = load i32, i32* @MAP_SHARED
%17 = tail call i8* @mmap(i32 0, i64 %2, i32 %15, i32 %16, i32 %1, i32 0)
%18 = load i8*, i8** @MAP_FAILED
%19 = icmp eq i8* %17, %18
br i1 %19, label %20, label %25
20: ; preds = %14
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @errno
%23 = tail call i8* @strerror(i32 %22)
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %36
25: ; preds = %14
%26 = tail call i32* @memcpy(i8* %17, i8* %6, i64 %2)
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %33
28: ; preds = %25
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%30 = load i32, i32* @errno
%31 = tail call i8* @strerror(i32 %30)
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %31)
br label %36
33: ; preds = %25
%34 = tail call i32 @munmap(i8* %6, i64 %2)
%35 = tail call i32 @munmap(i8* %17, i64 %2)
br label %36
36: ; preds = %33, %28, %20, %9
%37 = phi i32 [ -1, %9 ], [ -1, %20 ], [ -1, %28 ], [ 0, %33 ]
ret i32 %37
}