#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 {
  int /*<<< orphan*/ st_size;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int PROT_READ;
int PROT_WRITE;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fstat(int, struct stat *);
int ftruncate(int, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(void *, void *, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/, int /*<<< orphan*/, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(int argc, char *argv[]) {
  int fdin, fdout;
  void *src, *dst;
  struct stat statbuf;
  if (argc != 3)
    do {
      perror("wrong usage");
      exit(EXIT_FAILURE);
    } while (0);
  fdin = open(argv[1], O_RDONLY);
  if (fdin < 0)
    do {
      perror("open input");
      exit(EXIT_FAILURE);
    } while (0);
  fdout = open(argv[2], O_RDWR | O_CREAT | O_TRUNC,
               (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
  if (fdout < 0)
    do {
      perror("create output");
      exit(EXIT_FAILURE);
    } while (0);
  if (fstat(fdin, &statbuf) < 0)
    do {
      perror("fstat");
      exit(EXIT_FAILURE);
    } while (0);
  if (ftruncate(fdout, statbuf.st_size) == -1)
    do {
      perror("ftruncate");
      exit(EXIT_FAILURE);
    } while (0);
  src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0);
  if (src == MAP_FAILED)
    do {
      perror("mmap input");
      exit(EXIT_FAILURE);
    } while (0);
  dst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fdout, 0);
  if (dst == MAP_FAILED)
    do {
      perror("mmap output");
      exit(EXIT_FAILURE);
    } while (0);
  memcpy(dst, src, statbuf.st_size);
  printf("copy over\n");
  exit(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     11bf <main+0x3f>
jmp    11a5 <main+0x25>
lea    0xe54(%rip),%rdi
call   1080 <perror@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1203 <main+0x83>
jmp    11e9 <main+0x69>
lea    0xe1c(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
mov    0x2ddd(%rip),%rax
or     (%rax),%esi
mov    0x2dcc(%rip),%rax
or     (%rax),%esi
mov    0x2d5b(%rip),%rax
mov    (%rax),%edx
mov    0x2d82(%rip),%rax
or     (%rax),%edx
mov    0x2d69(%rip),%rax
or     (%rax),%edx
mov    0x2d58(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    127d <main+0xfd>
jmp    1263 <main+0xe3>
lea    0xdad(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0x0,%rax
jge    12b2 <main+0x132>
jmp    1298 <main+0x118>
lea    0xd86(%rip),%rdi
call   1080 <perror@plt>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x30(%rbp),%esi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    12e5 <main+0x165>
jmp    12cb <main+0x14b>
lea    0xd59(%rip),%rdi
call   1080 <perror@plt>
mov    0x2cba(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x30(%rbp),%esi
mov    0x2cf9(%rip),%rax
mov    (%rax),%edx
mov    0x2ce0(%rip),%rax
mov    (%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    0x2c68(%rip),%rcx
cmp    (%rcx),%rax
jne    1340 <main+0x1c0>
jmp    1326 <main+0x1a6>
lea    0xd08(%rip),%rdi
call   1080 <perror@plt>
mov    0x2c5f(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x30(%rbp),%esi
mov    0x2c9e(%rip),%rax
mov    (%rax),%edx
mov    0x2c3d(%rip),%rax
or     (%rax),%edx
mov    0x2c7c(%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    0x2c04(%rip),%rcx
cmp    (%rcx),%rax
jne    13a4 <main+0x224>
jmp    138a <main+0x20a>
lea    0xcaf(%rip),%rdi
call   1080 <perror@plt>
mov    0x2bfb(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1060 <memcpy@plt>
lea    0xc91(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    11b4 <main+0x34>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
jns    11ce <main+0x4e>
lea    0xe5a(%rip),%rdi
jmp    11bb <main+0x3b>
lea    0xe45(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    %eax,%ebx
mov    0x2df1(%rip),%rax
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e07(%rip),%rax
or     (%rax),%esi
mov    0x2d96(%rip),%rax
mov    0x2dbf(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2da4(%rip),%rax
or     (%rax),%edx
mov    0x10(%rbp),%rdi
mov    0x2d8f(%rip),%rax
or     (%rax),%edx
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
jns    1227 <main+0xa7>
lea    0xdf2(%rip),%rdi
jmp    11bb <main+0x3b>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %ebx,%edi
call   10a0 <fstat@plt>
test   %rax,%rax
jns    1244 <main+0xc4>
lea    0xde6(%rip),%rdi
jmp    11bb <main+0x3b>
mov    (%rsp),%r14d
mov    %ebp,%edi
mov    %r14d,%esi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    1263 <main+0xe3>
lea    0xdcd(%rip),%rdi
jmp    11bb <main+0x3b>
mov    0x2d7e(%rip),%r15
mov    (%r15),%edx
mov    0x2d64(%rip),%r12
mov    (%r12),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2cf1(%rip),%r13
cmp    0x0(%r13),%rax
jne    12a1 <main+0x121>
lea    0xd99(%rip),%rdi
jmp    11bb <main+0x3b>
mov    %rax,%rbx
mov    0x2ce5(%rip),%rax
mov    (%rax),%edx
or     (%r15),%edx
mov    (%r12),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    0x0(%r13),%rax
jne    12d6 <main+0x156>
lea    0xd6f(%rip),%rdi
jmp    11bb <main+0x3b>
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1060 <memcpy@plt>
lea    0xd61(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [12 x i8] c"wrong usage\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [11 x i8] c"open input\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
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.2 = [14 x i8] c"create output\00"
@.str.3 = [6 x i8] c"fstat\00"
@.str.4 = [10 x i8] c"ftruncate\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [11 x i8] c"mmap input\00"
@PROT_WRITE = global i32 0
@.str.6 = [12 x i8] c"mmap output\00"
@.str.7 = [11 x i8] c"copy over\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 %19
13: ; preds = %2
br label %14
14: ; preds = %13
%15 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; No predecessors!
br label %19
19: ; preds = %18, %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDONLY
%24 = call i32 (i8*, i32, ...) @open(i8* %22, i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %19
br label %28
28: ; preds = %27
%29 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; No predecessors!
br label %33
33: ; preds = %32, %19
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 2
%36 = load i8*, i8** %35
%37 = load i32, i32* @O_RDWR
%38 = load i32, i32* @O_CREAT
%39 = or i32 %37, %38
%40 = load i32, i32* @O_TRUNC
%41 = or i32 %39, %40
%42 = load i32, i32* @S_IRUSR
%43 = load i32, i32* @S_IWUSR
%44 = or i32 %42, %43
%45 = load i32, i32* @S_IRGRP
%46 = or i32 %44, %45
%47 = load i32, i32* @S_IROTH
%48 = or i32 %46, %47
%49 = call i32 (i8*, i32, ...) @open(i8* %36, i32 %41, i32 %48)
store i32 %49, i32* %7
%50 = load i32, i32* %7
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %33
br label %53
53: ; preds = %52
%54 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; No predecessors!
br label %58
58: ; preds = %57, %33
%59 = load i32, i32* %6
%60 = call i64 @fstat(i32 %59, %struct.stat* %10)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %68
62: ; preds = %58
br label %63
63: ; preds = %62
%64 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
%66 = call i32 @exit(i32 %65)
unreachable
67: ; No predecessors!
br label %68
68: ; preds = %67, %58
%69 = load i32, i32* %7
%70 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%71 = load i32, i32* %70
%72 = call i32 @ftruncate(i32 %69, i32 %71)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %80
74: ; preds = %68
br label %75
75: ; preds = %74
%76 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%77 = load i32, i32* @EXIT_FAILURE
%78 = call i32 @exit(i32 %77)
unreachable
79: ; No predecessors!
br label %80
80: ; preds = %79, %68
%81 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%82 = load i32, i32* %81
%83 = load i32, i32* @PROT_READ
%84 = load i32, i32* @MAP_SHARED
%85 = load i32, i32* %6
%86 = call i8* @mmap(i32 0, i32 %82, i32 %83, i32 %84, i32 %85, i32 0)
store i8* %86, i8** %8
%87 = load i8*, i8** %8
%88 = load i8*, i8** @MAP_FAILED
%89 = icmp eq i8* %87, %88
br i1 %89, label %90, label %96
90: ; preds = %80
br label %91
91: ; preds = %90
%92 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%93 = load i32, i32* @EXIT_FAILURE
%94 = call i32 @exit(i32 %93)
unreachable
95: ; No predecessors!
br label %96
96: ; preds = %95, %80
%97 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%98 = load i32, i32* %97
%99 = load i32, i32* @PROT_READ
%100 = load i32, i32* @PROT_WRITE
%101 = or i32 %99, %100
%102 = load i32, i32* @MAP_SHARED
%103 = load i32, i32* %7
%104 = call i8* @mmap(i32 0, i32 %98, i32 %101, i32 %102, i32 %103, i32 0)
store i8* %104, i8** %9
%105 = load i8*, i8** %9
%106 = load i8*, i8** @MAP_FAILED
%107 = icmp eq i8* %105, %106
br i1 %107, label %108, label %114
108: ; preds = %96
br label %109
109: ; preds = %108
%110 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%111 = load i32, i32* @EXIT_FAILURE
%112 = call i32 @exit(i32 %111)
unreachable
113: ; No predecessors!
br label %114
114: ; preds = %113, %96
%115 = load i8*, i8** %9
%116 = load i8*, i8** %8
%117 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%118 = load i32, i32* %117
%119 = call i32 @memcpy(i8* %115, i8* %116, i32 %118)
%120 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
%121 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [12 x i8] c"wrong usage\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [11 x i8] c"open input\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
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.2 = [14 x i8] c"create output\00"
@.str.3 = [6 x i8] c"fstat\00"
@.str.4 = [10 x i8] c"ftruncate\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.5 = [11 x i8] c"mmap input\00"
@PROT_WRITE = global i32 0
@.str.6 = [12 x i8] c"mmap output\00"
@str = [10 x i8] c"copy over\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 %10, label %6
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 (i8*, i32, ...) @open(i8* %12, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %10
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDWR
%24 = load i32, i32* @O_CREAT
%25 = or i32 %24, %23
%26 = load i32, i32* @O_TRUNC
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IRUSR
%29 = load i32, i32* @S_IWUSR
%30 = or i32 %29, %28
%31 = load i32, i32* @S_IRGRP
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IROTH
%34 = or i32 %32, %33
%35 = tail call i32 (i8*, i32, ...) @open(i8* %22, i32 %27, i32 %34)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %20
%38 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = tail call i32 @exit(i32 %39)
unreachable
41: ; preds = %20
%42 = call i64 @fstat(i32 %14, %struct.stat* %3)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %41
%45 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%46 = load i32, i32* @EXIT_FAILURE
%47 = tail call i32 @exit(i32 %46)
unreachable
48: ; preds = %41
%49 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%50 = load i32, i32* %49
%51 = tail call i32 @ftruncate(i32 %35, i32 %50)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %57
53: ; preds = %48
%54 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%55 = load i32, i32* @EXIT_FAILURE
%56 = tail call i32 @exit(i32 %55)
unreachable
57: ; preds = %48
%58 = load i32, i32* @PROT_READ
%59 = load i32, i32* @MAP_SHARED
%60 = tail call i8* @mmap(i32 0, i32 %50, i32 %58, i32 %59, i32 %14, i32 0)
%61 = load i8*, i8** @MAP_FAILED
%62 = icmp eq i8* %60, %61
br i1 %62, label %63, label %67
63: ; preds = %57
%64 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
%66 = tail call i32 @exit(i32 %65)
unreachable
67: ; preds = %57
%68 = load i32, i32* @PROT_READ
%69 = load i32, i32* @PROT_WRITE
%70 = or i32 %69, %68
%71 = load i32, i32* @MAP_SHARED
%72 = tail call i8* @mmap(i32 0, i32 %50, i32 %70, i32 %71, i32 %35, i32 0)
%73 = load i8*, i8** @MAP_FAILED
%74 = icmp eq i8* %72, %73
br i1 %74, label %75, label %79
75: ; preds = %67
%76 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%77 = load i32, i32* @EXIT_FAILURE
%78 = tail call i32 @exit(i32 %77)
unreachable
79: ; preds = %67
%80 = tail call i32 @memcpy(i8* %72, i8* %60, i32 %50)
%81 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%82 = tail call i32 @exit(i32 0)
unreachable
}