#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ PROT_WRITE;
int S_IRUSR;
int S_IWUSR;
size_t atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
size_t file_size(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ ftruncate(int, size_t);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, size_t);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc < 2) {
    fprintf(stderr, "usage: read_write <repeats>\n");
    exit(EXIT_FAILURE);
  }
  size_t repeats = atoi(argv[1]);
  for (int i = 0; i < repeats; ++i) {
    int src = open("test.dat", O_RDONLY);
    if (src == -1)
      do {
        perror("fstat");
        exit(EXIT_FAILURE);
      } while (0);
    int dst = open("output.dat", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
    if (dst == -1)
      do {
        perror("open");
        exit(EXIT_FAILURE);
      } while (0);
    size_t size = file_size(src);
    if (ftruncate(dst, size))
      do {
        perror("ftruncate");
        exit(EXIT_FAILURE);
      } while (0);
    void *file1, *file2;
    file1 = mmap(NULL, size, PROT_READ, MAP_PRIVATE, src, 0);
    file2 = mmap(NULL, size, PROT_WRITE, MAP_SHARED, dst, 0);
    memcpy(file2, file1, size);
    munmap(file2, size);
    close(src);
    close(dst);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11f3 <main+0x43>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1394 <main+0x1e4>
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    125c <main+0xac>
jmp    1242 <main+0x92>
lea    0xddd(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
mov    0x2d8c(%rip),%rax
or     (%rax),%esi
mov    0x2d7b(%rip),%rax
or     (%rax),%esi
mov    0x2d12(%rip),%rax
mov    (%rax),%edx
mov    0x2d31(%rip),%rax
or     (%rax),%edx
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12c3 <main+0x113>
jmp    12a9 <main+0xf9>
lea    0xd87(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x20(%rbp),%edi
call   1060 <file_size@plt>
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rsi
call   1040 <ftruncate@plt>
cmp    $0x0,%rax
je     1304 <main+0x154>
jmp    12ea <main+0x13a>
lea    0xd4b(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2ca3(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x30(%rbp),%rsi
mov    0x2cd9(%rip),%rax
mov    (%rax),%edx
mov    0x2c68(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rsi
mov    0x2c5f(%rip),%rax
mov    (%rax),%edx
mov    0x2c96(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1080 <memcpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1090 <munmap@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    120b <main+0x5b>
mov    0x2bed(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     11da <main+0x2a>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1326 <main+0x176>
mov    0x8(%rsi),%rdi
call   10c0 <atoi@plt>
test   %rax,%rax
jne    11f6 <main+0x46>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    %rax,%r12
nopl   0x0(%rax)
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
lea    0xe0d(%rip),%rdi
xor    %eax,%eax
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1308 <main+0x158>
mov    %eax,%r13d
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    0x2d95(%rip),%rax
or     (%rax),%esi
mov    0x2db4(%rip),%rax
or     (%rax),%esi
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
mov    0x2d42(%rip),%rax
or     (%rax),%edx
lea    0xdd5(%rip),%rdi
xor    %eax,%eax
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1311 <main+0x161>
mov    %eax,%ebx
mov    %r13d,%edi
call   1060 <file_size@plt>
mov    %rax,%rbp
mov    %ebx,%edi
mov    %rax,%rsi
call   1040 <ftruncate@plt>
test   %rax,%rax
jne    131a <main+0x16a>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edx
mov    0x2ce9(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbp,%rsi
mov    %r13d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r14
mov    0x2ce5(%rip),%rax
mov    (%rax),%edx
mov    0x2d1c(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbp,%rsi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
call   1080 <memcpy@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
call   1090 <munmap@plt>
mov    %r13d,%edi
call   1050 <close@plt>
mov    %ebx,%edi
call   1050 <close@plt>
add    $0xffffffffffffffff,%r12
jne    1200 <main+0x50>
jmp    11e8 <main+0x38>
lea    0xd17(%rip),%rdi
jmp    1321 <main+0x171>
lea    0xd1f(%rip),%rdi
jmp    1321 <main+0x171>
lea    0xd1b(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c73(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"usage: read_write <repeats>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"test.dat\00"
@O_RDONLY = global i32 0
@.str.2 = [6 x i8] c"fstat\00"
@.str.3 = [11 x i8] c"output.dat\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [10 x i8] c"ftruncate\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i64 @atoi(i8* %23)
store i64 %24, i64* %6
store i32 0, i32* %7
br label %25
25: ; preds = %94, %20
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = load i64, i64* %6
%29 = icmp ult i64 %27, %28
br i1 %29, label %30, label %97
30: ; preds = %25
%31 = load i32, i32* @O_RDONLY
%32 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %31)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %41
35: ; preds = %30
br label %36
36: ; preds = %35
%37 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; No predecessors!
br label %41
41: ; preds = %40, %30
%42 = load i32, i32* @O_RDWR
%43 = load i32, i32* @O_CREAT
%44 = or i32 %42, %43
%45 = load i32, i32* @O_TRUNC
%46 = or i32 %44, %45
%47 = load i32, i32* @S_IRUSR
%48 = load i32, i32* @S_IWUSR
%49 = or i32 %47, %48
%50 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %46, i32 %49)
store i32 %50, i32* %9
%51 = load i32, i32* %9
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %59
53: ; preds = %41
br label %54
54: ; preds = %53
%55 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; No predecessors!
br label %59
59: ; preds = %58, %41
%60 = load i32, i32* %8
%61 = call i64 @file_size(i32 %60)
store i64 %61, i64* %10
%62 = load i32, i32* %9
%63 = load i64, i64* %10
%64 = call i64 @ftruncate(i32 %62, i64 %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %72
66: ; preds = %59
br label %67
67: ; preds = %66
%68 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%69 = load i32, i32* @EXIT_FAILURE
%70 = call i32 @exit(i32 %69)
unreachable
71: ; No predecessors!
br label %72
72: ; preds = %71, %59
%73 = load i64, i64* %10
%74 = load i32, i32* @PROT_READ
%75 = load i32, i32* @MAP_PRIVATE
%76 = load i32, i32* %8
%77 = call i8* @mmap(i32* null, i64 %73, i32 %74, i32 %75, i32 %76, i32 0)
store i8* %77, i8** %11
%78 = load i64, i64* %10
%79 = load i32, i32* @PROT_WRITE
%80 = load i32, i32* @MAP_SHARED
%81 = load i32, i32* %9
%82 = call i8* @mmap(i32* null, i64 %78, i32 %79, i32 %80, i32 %81, i32 0)
store i8* %82, i8** %12
%83 = load i8*, i8** %12
%84 = load i8*, i8** %11
%85 = load i64, i64* %10
%86 = call i32 @memcpy(i8* %83, i8* %84, i64 %85)
%87 = load i8*, i8** %12
%88 = load i64, i64* %10
%89 = call i32 @munmap(i8* %87, i64 %88)
%90 = load i32, i32* %8
%91 = call i32 @close(i32 %90)
%92 = load i32, i32* %9
%93 = call i32 @close(i32 %92)
br label %94
94: ; preds = %72
%95 = load i32, i32* %7
%96 = add nsw i32 %95, 1
store i32 %96, i32* %7
br label %25
97: ; preds = %25
%98 = load i32, i32* @EXIT_SUCCESS
%99 = call i32 @exit(i32 %98)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"usage: read_write <repeats>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"test.dat\00"
@O_RDONLY = global i32 0
@.str.2 = [6 x i8] c"fstat\00"
@.str.3 = [11 x i8] c"output.dat\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [10 x i8] c"ftruncate\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i64 @atoi(i8* %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %49, %9
%15 = load i32, i32* @EXIT_SUCCESS
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %9, %49
%18 = phi i64 [ %60, %49 ], [ 0, %9 ]
%19 = load i32, i32* @O_RDONLY
%20 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %17
%27 = load i32, i32* @O_RDWR
%28 = load i32, i32* @O_CREAT
%29 = or i32 %28, %27
%30 = load i32, i32* @O_TRUNC
%31 = or i32 %29, %30
%32 = load i32, i32* @S_IRUSR
%33 = load i32, i32* @S_IWUSR
%34 = or i32 %33, %32
%35 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %31, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %41
37: ; preds = %26
%38 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = tail call i32 @exit(i32 %39)
unreachable
41: ; preds = %26
%42 = tail call i64 @file_size(i32 %20)
%43 = tail call i64 @ftruncate(i32 %35, i64 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %49, label %45
45: ; preds = %41
%46 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = tail call i32 @exit(i32 %47)
unreachable
49: ; preds = %41
%50 = load i32, i32* @PROT_READ
%51 = load i32, i32* @MAP_PRIVATE
%52 = tail call i8* @mmap(i32* null, i64 %42, i32 %50, i32 %51, i32 %20, i32 0)
%53 = load i32, i32* @PROT_WRITE
%54 = load i32, i32* @MAP_SHARED
%55 = tail call i8* @mmap(i32* null, i64 %42, i32 %53, i32 %54, i32 %35, i32 0)
%56 = tail call i32 @memcpy(i8* %55, i8* %52, i64 %42)
%57 = tail call i32 @munmap(i8* %55, i64 %42)
%58 = tail call i32 @close(i32 %20)
%59 = tail call i32 @close(i32 %35)
%60 = add nuw i64 %18, 1
%61 = icmp eq i64 %60, %12
br i1 %61, label %14, label %17
}