#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 */
struct stat {
  scalar_t__ st_size;
};

/* Variables and functions */
void *MAP_FAILED;
int MAP_NORESERVE;
int MAP_PRIVATE;
int MAP_SHARED;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ SEEK_SET;
int S_IRWXG;
int S_IRWXU;
scalar_t__ fstat(int, struct stat *);
scalar_t__ lseek(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *mmap(int /*<<< orphan*/, size_t, int /*<<< orphan*/, int, int,
           int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int src, dest;
  void *sm;
  void *dm;
  struct stat sb;
  if (argc != 3) {
    printf("usage:cpmm <src> <dest>\n");
    return 1;
  }
  if ((src = open(argv[1], O_RDONLY)) < 0) {
    printf("open src failed %d\n", src);
    return 1;
  }
  if ((dest = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG)) <
      0) {
    printf("open dest failed %d\n", dest);
    return 1;
  }
  if ((fstat(src, &sb)) < 0) {
    printf("fstat src failed\n");
    return 1;
  }
  if ((lseek(dest, sb.st_size - 1, SEEK_SET)) < 0) {
    printf("lseek dest failed\n");
    return 1;
  }
  sm = mmap(0, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, src,
            0);
  if (sm == MAP_FAILED) {
    printf("sm map failed\n");
    return 1;
  }
  dm = mmap(0, (size_t)sb.st_size, PROT_WRITE, MAP_SHARED, dest, 0);
  if (dm == MAP_FAILED) {
    printf("dm map failed\n");
    return 1;
  }
  memcpy(dm, sm, (size_t)sb.st_size);
  printf("copy succeed\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     119a <main+0x3a>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    138a <main+0x22a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11db <main+0x7b>
mov    -0x14(%rbp),%esi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    138a <main+0x22a>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    0x2e05(%rip),%rax
or     (%rax),%esi
mov    0x2df4(%rip),%rax
or     (%rax),%esi
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
mov    0x2da2(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1240 <main+0xe0>
mov    -0x18(%rbp),%esi
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    138a <main+0x22a>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
jge    1270 <main+0x110>
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    138a <main+0x22a>
mov    -0x18(%rbp),%edi
mov    -0x30(%rbp),%rsi
sub    $0x1,%rsi
mov    0x2d16(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0x0,%rax
jge    12ad <main+0x14d>
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    138a <main+0x22a>
mov    -0x30(%rbp),%rsi
mov    0x2d30(%rip),%rax
mov    (%rax),%edx
mov    0x2cc7(%rip),%rax
mov    (%rax),%ecx
mov    0x2cee(%rip),%rax
or     (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2c96(%rip),%rcx
cmp    (%rcx),%rax
jne    130d <main+0x1ad>
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    138a <main+0x22a>
mov    -0x30(%rbp),%rsi
mov    0x2c78(%rip),%rax
mov    (%rax),%edx
mov    0x2cb7(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2c3f(%rip),%rcx
cmp    (%rcx),%rax
jne    1364 <main+0x204>
lea    0xd25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    138a <main+0x22a>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <memcpy@plt>
lea    0xd09(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x3,%edi
jne    12a4 <main+0x134>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     12b2 <main+0x142>
mov    0x10(%rbp),%rdi
mov    0x2e0f(%rip),%rax
mov    0x2e40(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e2d(%rip),%rax
or     (%rax),%esi
mov    0x2dfc(%rip),%rax
mov    0x2ddd(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12bd <main+0x14d>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1090 <fstat@plt>
test   %rax,%rax
js     12e1 <main+0x171>
mov    0x8(%rsp),%r14
lea    -0x1(%r14),%rsi
mov    0x2d8b(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1060 <lseek@plt>
test   %rax,%rax
js     12ea <main+0x17a>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edx
mov    0x2d59(%rip),%rax
mov    0x2d82(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %r14,%rsi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2d2f(%rip),%r12
cmp    (%r12),%rax
je     12f3 <main+0x183>
mov    %rax,%r15
mov    0x2d2b(%rip),%rax
mov    (%rax),%edx
mov    0x2d6a(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    %r14,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    (%r12),%rax
je     12fc <main+0x18c>
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1070 <memcpy@plt>
lea    0xd8c(%rip),%rdi
call   1030 <puts@plt>
jmp    12d2 <main+0x162>
lea    0xdca(%rip),%rdi
call   1030 <puts@plt>
jmp    12cd <main+0x15d>
lea    0xd47(%rip),%rdi
mov    %ebx,%esi
jmp    12c6 <main+0x156>
lea    0xd50(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd7c(%rip),%rdi
jmp    12ab <main+0x13b>
lea    0xd61(%rip),%rdi
jmp    12ab <main+0x13b>
lea    0xd4a(%rip),%rdi
jmp    12ab <main+0x13b>
lea    0xd33(%rip),%rdi
jmp    12ab <main+0x13b>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [25 x i8] c"usage:cpmm <src> <dest>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [20 x i8] c"open src failed %d\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@.str.2 = [21 x i8] c"open dest failed %d\0A\00"
@.str.3 = [18 x i8] c"fstat src failed\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [19 x i8] c"lseek dest failed\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_NORESERVE = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [15 x i8] c"sm map failed\0A\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.6 = [15 x i8] c"dm map failed\0A\00"
@.str.7 = [14 x i8] c"copy succeed\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %91
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_RDONLY
%20 = call i32 (i8*, i32, ...) @open(i8* %18, i32 %19)
store i32 %20, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i32, i32* %6
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %23)
store i32 1, i32* %3
br label %91
25: ; preds = %15
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = load i32, i32* @O_RDWR
%30 = load i32, i32* @O_CREAT
%31 = or i32 %29, %30
%32 = load i32, i32* @O_TRUNC
%33 = or i32 %31, %32
%34 = load i32, i32* @S_IRWXU
%35 = load i32, i32* @S_IRWXG
%36 = or i32 %34, %35
%37 = call i32 (i8*, i32, ...) @open(i8* %28, i32 %33, i32 %36)
store i32 %37, i32* %7
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %25
%40 = load i32, i32* %7
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %40)
store i32 1, i32* %3
br label %91
42: ; preds = %25
%43 = load i32, i32* %6
%44 = call i64 @fstat(i32 %43, %struct.stat* %10)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %91
48: ; preds = %42
%49 = load i32, i32* %7
%50 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%51 = load i64, i64* %50
%52 = sub nsw i64 %51, 1
%53 = load i32, i32* @SEEK_SET
%54 = call i64 @lseek(i32 %49, i64 %52, i32 %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %48
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %91
58: ; preds = %48
%59 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%60 = load i64, i64* %59
%61 = load i32, i32* @PROT_READ
%62 = load i32, i32* @MAP_PRIVATE
%63 = load i32, i32* @MAP_NORESERVE
%64 = or i32 %62, %63
%65 = load i32, i32* %6
%66 = call i8* @mmap(i32 0, i64 %60, i32 %61, i32 %64, i32 %65, i32 0)
store i8* %66, i8** %8
%67 = load i8*, i8** %8
%68 = load i8*, i8** @MAP_FAILED
%69 = icmp eq i8* %67, %68
br i1 %69, label %70, label %72
70: ; preds = %58
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %91
72: ; preds = %58
%73 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%74 = load i64, i64* %73
%75 = load i32, i32* @PROT_WRITE
%76 = load i32, i32* @MAP_SHARED
%77 = load i32, i32* %7
%78 = call i8* @mmap(i32 0, i64 %74, i32 %75, i32 %76, i32 %77, i32 0)
store i8* %78, i8** %9
%79 = load i8*, i8** %9
%80 = load i8*, i8** @MAP_FAILED
%81 = icmp eq i8* %79, %80
br i1 %81, label %82, label %84
82: ; preds = %72
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %91
84: ; preds = %72
%85 = load i8*, i8** %9
%86 = load i8*, i8** %8
%87 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%88 = load i64, i64* %87
%89 = call i32 @memcpy(i8* %85, i8* %86, i64 %88)
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %3
br label %91
91: ; preds = %84, %82, %70, %56, %46, %39, %22, %13
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str.1 = [20 x i8] c"open src failed %d\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@.str.2 = [21 x i8] c"open dest failed %d\0A\00"
@SEEK_SET = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_NORESERVE = global i32 0
@MAP_FAILED = global i8* null
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@str = [13 x i8] c"copy succeed\00"
@str.8 = [14 x i8] c"dm map failed\00"
@str.9 = [14 x i8] c"sm map failed\00"
@str.10 = [18 x i8] c"lseek dest failed\00"
@str.11 = [17 x i8] c"fstat src failed\00"
@str.12 = [24 x i8] c"usage:cpmm <src> <dest>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.12, i64 0, i64 0))
br label %66
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 (i8*, i32, ...) @open(i8* %10, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %66
16: ; preds = %8
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_CREAT
%21 = or i32 %20, %19
%22 = load i32, i32* @O_TRUNC
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRWXU
%25 = load i32, i32* @S_IRWXG
%26 = or i32 %25, %24
%27 = tail call i32 (i8*, i32, ...) @open(i8* %18, i32 %23, i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %16
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %66
31: ; preds = %16
%32 = call i64 @fstat(i32 %12, %struct.stat* %3)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.11, i64 0, i64 0))
br label %66
36: ; preds = %31
%37 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%38 = load i64, i64* %37
%39 = add nsw i64 %38, -1
%40 = load i32, i32* @SEEK_SET
%41 = tail call i64 @lseek(i32 %27, i64 %39, i32 %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.10, i64 0, i64 0))
br label %66
45: ; preds = %36
%46 = load i32, i32* @PROT_READ
%47 = load i32, i32* @MAP_PRIVATE
%48 = load i32, i32* @MAP_NORESERVE
%49 = or i32 %48, %47
%50 = tail call i8* @mmap(i32 0, i64 %38, i32 %46, i32 %49, i32 %12, i32 0)
%51 = load i8*, i8** @MAP_FAILED
%52 = icmp eq i8* %50, %51
br i1 %52, label %53, label %55
53: ; preds = %45
%54 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
br label %66
55: ; preds = %45
%56 = load i32, i32* @PROT_WRITE
%57 = load i32, i32* @MAP_SHARED
%58 = tail call i8* @mmap(i32 0, i64 %38, i32 %56, i32 %57, i32 %27, i32 0)
%59 = load i8*, i8** @MAP_FAILED
%60 = icmp eq i8* %58, %59
br i1 %60, label %61, label %63
61: ; preds = %55
%62 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.8, i64 0, i64 0))
br label %66
63: ; preds = %55
%64 = tail call i32 @memcpy(i8* %58, i8* %50, i64 %38)
%65 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %66
66: ; preds = %63, %61, %53, %43, %34, %29, %14, %6
%67 = phi i32 [ 1, %6 ], [ 1, %14 ], [ 1, %29 ], [ 1, %34 ], [ 1, %43 ], [ 1, %53 ], [ 1, %61 ], [ 0, %63 ]
ret i32 %67
}