#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_FILE;
int MAP_PRIVATE;
int MAP_SHARED;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SEEK_SET;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fstat(int, struct stat *);
int lseek(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(void *, void *, scalar_t__);
void *mmap(int /*<<< orphan*/, scalar_t__, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, scalar_t__);
int open(char *, int, ...);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, char **argv) {
  int fd;
  struct stat fst;
  void *in_mem, *out_mem;
  char *in_filename, *out_filename;
  in_filename = argc > 1 ? argv[1] : "mmap-cp.c";
  out_filename = argc > 2 ? argv[2] : "mmap-cp.c.copy";
  fd = open(in_filename, O_RDONLY);
  if (fd == -1) {
    fprintf(stderr, "open(\"%s\", O_RDONLY)\n", in_filename);
    return -1;
  }
  if (fstat(fd, &fst) == -1) {
    fprintf(stderr, "fstat(fd, &fst) == -1\n");
    return -2;
  }
  if (!fst.st_size) {
    fprintf(stderr, "size is 0\n");
  } else {
    in_mem = mmap(0, fst.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  }
  if (fd != -1) {
    close(fd);
  }
  if (!in_mem) {
    fprintf(stderr, "in_mem is NULL\n");
  } else if (in_mem == MAP_FAILED) {
    fprintf(stderr, "in_mem == MAP_FAILED\n");
  } else {
    if (0) {
      fprintf(stderr, "%s", (char *)in_mem);
    }
  }
  fd = open(out_filename, O_RDWR | O_CREAT | O_TRUNC,
            S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (fd == -1) {
    fprintf(stderr, "open(\"%s\", O_WRONLY | O_CREATE)\n", out_filename);
    return -3;
  }
  if (lseek(fd, fst.st_size - 1, SEEK_SET) == -1) {
    fprintf(stderr, "lseek 1");
  } else if (write(fd, "", 1) != 1) {
    fprintf(stderr, "write byte");
  } else if (lseek(fd, 0, SEEK_SET) == -1) {
    fprintf(stderr, "lseek 2");
  }
  out_mem = mmap(0, fst.st_size, PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
  if (!out_mem) {
    fprintf(stderr, "out_mem is NULL\n");
  } else if (out_mem == MAP_FAILED) {
    fprintf(stderr, "out_mem == MAP_FAILED\n");
  } else if (in_mem) {
    memcpy(out_mem, in_mem, fst.st_size);
  } else {
    fprintf(stderr, "no in_mem to copy.\n");
  }
  if (in_mem) {
    munmap(in_mem, fst.st_size);
  }
  if (out_mem) {
    munmap(out_mem, fst.st_size);
  }
  if (fd != -1) {
    close(fd);
  }
  return fd;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11c1 <main+0x31>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    11d1 <main+0x41>
lea    0xe38(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    11d1 <main+0x41>
mov    -0x48(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11f4 <main+0x64>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1204 <main+0x74>
lea    0xe0f(%rip),%rax
mov    %rax,-0x50(%rbp)
jmp    1204 <main+0x74>
mov    -0x50(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1254 <main+0xc4>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xdd8(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    159f <main+0x40f>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    128c <main+0xfc>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    159f <main+0x40f>
cmpq   $0x0,-0x20(%rbp)
jne    12b3 <main+0x123>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
lea    0xd9f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    12e5 <main+0x155>
mov    -0x20(%rbp),%rsi
mov    0x2d2a(%rip),%rax
mov    (%rax),%edx
mov    0x2cb9(%rip),%rax
or     (%rax),%edx
mov    0x2ca0(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     12f7 <main+0x167>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
cmpq   $0x0,-0x28(%rbp)
jne    131e <main+0x18e>
mov    0x2c97(%rip),%rax
mov    (%rax),%edi
lea    0xd3f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1358 <main+0x1c8>
mov    -0x28(%rbp),%rax
mov    0x2c3f(%rip),%rcx
cmp    (%rcx),%rax
jne    134e <main+0x1be>
mov    0x2c67(%rip),%rax
mov    (%rax),%edi
lea    0xd1f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1353 <main+0x1c3>
jmp    1353 <main+0x1c3>
jmp    1358 <main+0x1c8>
mov    -0x40(%rbp),%rdi
mov    0x2c65(%rip),%rax
mov    (%rax),%esi
mov    0x2c8c(%rip),%rax
or     (%rax),%esi
mov    0x2c7b(%rip),%rax
or     (%rax),%esi
mov    0x2bfa(%rip),%rax
mov    (%rax),%edx
mov    0x2c29(%rip),%rax
or     (%rax),%edx
mov    0x2c00(%rip),%rax
or     (%rax),%edx
mov    0x2bef(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    13d6 <main+0x246>
mov    0x2bea(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rdx
lea    0xcb4(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    159f <main+0x40f>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
sub    $0x1,%rsi
mov    0x2bb0(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1414 <main+0x284>
mov    0x2ba1(%rip),%rax
mov    (%rax),%edi
lea    0xc90(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    148c <main+0x2fc>
mov    -0x14(%rbp),%edi
lea    0xc79(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     144d <main+0x2bd>
mov    0x2b68(%rip),%rax
mov    (%rax),%edi
lea    0xc5f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1487 <main+0x2f7>
mov    -0x14(%rbp),%edi
mov    0x2b41(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1482 <main+0x2f2>
mov    0x2b2e(%rip),%rax
mov    (%rax),%edi
lea    0xc30(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1487 <main+0x2f7>
jmp    148c <main+0x2fc>
mov    -0x20(%rbp),%rsi
mov    0x2ae9(%rip),%rax
mov    (%rax),%edx
mov    0x2b18(%rip),%rax
mov    (%rax),%ecx
mov    0x2b2f(%rip),%rax
or     (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    14e5 <main+0x355>
mov    0x2ad0(%rip),%rax
mov    (%rax),%edi
lea    0xbda(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1557 <main+0x3c7>
mov    -0x30(%rbp),%rax
mov    0x2a78(%rip),%rcx
cmp    (%rcx),%rax
jne    1515 <main+0x385>
mov    0x2aa0(%rip),%rax
mov    (%rax),%edi
lea    0xbbb(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1552 <main+0x3c2>
cmpq   $0x0,-0x28(%rbp)
je     1536 <main+0x3a6>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1080 <memcpy@plt>
jmp    154d <main+0x3bd>
mov    0x2a63(%rip),%rax
mov    (%rax),%edi
lea    0xb95(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1552 <main+0x3c2>
jmp    1557 <main+0x3c7>
cmpq   $0x0,-0x28(%rbp)
je     156f <main+0x3df>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <munmap@plt>
cmpq   $0x0,-0x30(%rbp)
je     1587 <main+0x3f7>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <munmap@plt>
cmpl   $0xffffffff,-0x14(%rbp)
je     1599 <main+0x409>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe68(%rip),%r12
cmp    $0x2,%edi
jl     11b3 <main+0x23>
mov    0x8(%rsi),%rbx
je     11ba <main+0x2a>
mov    0x10(%rsi),%r12
jmp    11ba <main+0x2a>
lea    0xe46(%rip),%rbx
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     124e <main+0xbe>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
cmp    $0xffffffff,%eax
je     1272 <main+0xe2>
mov    (%rsp),%r15
test   %r15,%r15
je     1293 <main+0x103>
mov    0x2ded(%rip),%rax
mov    0x2d7e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d63(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1060 <close@plt>
test   %r14,%r14
je     12c0 <main+0x130>
mov    0x2d2f(%rip),%rax
xor    %r13d,%r13d
cmp    (%rax),%r14
jne    12dd <main+0x14d>
lea    0xe15(%rip),%rsi
jmp    12cd <main+0x13d>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0xdbb(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1482 <main+0x2f2>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
lea    0xdad(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xfffffffe,%ebp
jmp    1482 <main+0x2f2>
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
lea    0xda3(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0x1,%r13b
lea    0xd93(%rip),%rsi
jmp    12cd <main+0x13d>
mov    $0x1,%r13b
lea    0xd87(%rip),%rsi
xor    %r14d,%r14d
mov    0x2ccc(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2ce4(%rip),%rax
mov    0x2d0d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2cfa(%rip),%rax
or     (%rax),%esi
mov    0x2c79(%rip),%rax
mov    0x2caa(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2c7f(%rip),%rax
or     (%rax),%edx
mov    0x2c6e(%rip),%rax
or     (%rax),%edx
mov    %r12,%rdi
xor    %eax,%eax
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1377 <main+0x1e7>
mov    %eax,%ebp
lea    -0x1(%r15),%rsi
mov    0x2c60(%rip),%rbx
mov    (%rbx),%edx
mov    %eax,%edi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     139b <main+0x20b>
lea    0xd4a(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    13a4 <main+0x214>
mov    (%rbx),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
jne    13bb <main+0x22b>
lea    0xd36(%rip),%rsi
jmp    13ab <main+0x21b>
mov    0x2c22(%rip),%rax
mov    (%rax),%edi
lea    0xcf0(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xfffffffd,%ebp
jmp    1482 <main+0x2f2>
lea    0xcf6(%rip),%rsi
jmp    13ab <main+0x21b>
lea    0xcf5(%rip),%rsi
mov    0x2bee(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2bbe(%rip),%rax
mov    (%rax),%edx
mov    0x2bed(%rip),%rax
mov    0x2c06(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
test   %rax,%rax
je     1418 <main+0x288>
mov    %rax,%rbx
mov    0x2b73(%rip),%rax
cmp    (%rax),%rbx
je     1436 <main+0x2a6>
test   %r13b,%r13b
je     1454 <main+0x2c4>
mov    0x2b9a(%rip),%rax
mov    (%rax),%edi
lea    0xccc(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    146d <main+0x2dd>
mov    0x2b81(%rip),%rax
mov    (%rax),%edi
lea    0xc8b(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
test   %r13b,%r13b
je     1470 <main+0x2e0>
jmp    147b <main+0x2eb>
mov    0x2b63(%rip),%rax
mov    (%rax),%edi
lea    0xc7e(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
test   %r13b,%r13b
je     1462 <main+0x2d2>
jmp    146d <main+0x2dd>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <memcpy@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <munmap@plt>
mov    %rbx,%r14
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <munmap@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [10 x i8] c"mmap-cp.c\00"
@.str.1 = [15 x i8] c"mmap-cp.c.copy\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.2 = [22 x i8] c"open(\22%s\22, O_RDONLY)\0A\00"
@.str.3 = [23 x i8] c"fstat(fd, &fst) == -1\0A\00"
@.str.4 = [11 x i8] c"size is 0\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@.str.5 = [16 x i8] c"in_mem is NULL\0A\00"
@MAP_FAILED = global i8* null
@.str.6 = [22 x i8] c"in_mem == MAP_FAILED\0A\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.7 = [33 x i8] c"open(\22%s\22, O_WRONLY | O_CREATE)\0A\00"
@SEEK_SET = global i32 0
@.str.8 = [8 x i8] c"lseek 1\00"
@.str.9 = [1 x i8] zeroinitializer
@.str.10 = [11 x i8] c"write byte\00"
@.str.11 = [8 x i8] c"lseek 2\00"
@MAP_FILE = global i32 0
@MAP_SHARED = global i32 0
@.str.12 = [17 x i8] c"out_mem is NULL\0A\00"
@.str.13 = [23 x i8] c"out_mem == MAP_FAILED\0A\00"
@.str.14 = [20 x i8] c"no in_mem to copy.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
br label %19
18: ; preds = %2
br label %19
19: ; preds = %18, %14
%20 = phi i8* [ %17, %14 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %18 ]
store i8* %20, i8** %10
%21 = load i32, i32* %4
%22 = icmp sgt i32 %21, 2
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
br label %28
27: ; preds = %19
br label %28
28: ; preds = %27, %23
%29 = phi i8* [ %26, %23 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %27 ]
store i8* %29, i8** %11
%30 = load i8*, i8** %10
%31 = load i32, i32* @O_RDONLY
%32 = call i32 (i8*, i32, ...) @open(i8* %30, i32 %31)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %39
35: ; preds = %28
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %10
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %37)
store i32 -1, i32* %3
br label %191
39: ; preds = %28
%40 = load i32, i32* %6
%41 = call i32 @fstat(i32 %40, %struct.stat* %7)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
store i32 -2, i32* %3
br label %191
46: ; preds = %39
%47 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%48 = load i64, i64* %47
%49 = icmp ne i64 %48, 0
br i1 %49, label %53, label %50
50: ; preds = %46
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %62
53: ; preds = %46
%54 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%55 = load i64, i64* %54
%56 = load i32, i32* @PROT_READ
%57 = load i32, i32* @PROT_WRITE
%58 = or i32 %56, %57
%59 = load i32, i32* @MAP_PRIVATE
%60 = load i32, i32* %6
%61 = call i8* @mmap(i32 0, i64 %55, i32 %58, i32 %59, i32 %60, i32 0)
store i8* %61, i8** %8
br label %62
62: ; preds = %53, %50
%63 = load i32, i32* %6
%64 = icmp ne i32 %63, -1
br i1 %64, label %65, label %68
65: ; preds = %62
%66 = load i32, i32* %6
%67 = call i32 @close(i32 %66)
br label %68
68: ; preds = %65, %62
%69 = load i8*, i8** %8
%70 = icmp ne i8* %69, null
br i1 %70, label %74, label %71
71: ; preds = %68
%72 = load i32, i32* @stderr
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %83
74: ; preds = %68
%75 = load i8*, i8** %8
%76 = load i8*, i8** @MAP_FAILED
%77 = icmp eq i8* %75, %76
br i1 %77, label %78, label %81
78: ; preds = %74
%79 = load i32, i32* @stderr
%80 = call i32 (i32, i8*, ...) @fprintf(i32 %79, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
br label %82
81: ; preds = %74
br label %82
82: ; preds = %81, %78
br label %83
83: ; preds = %82, %71
%84 = load i8*, i8** %11
%85 = load i32, i32* @O_RDWR
%86 = load i32, i32* @O_CREAT
%87 = or i32 %85, %86
%88 = load i32, i32* @O_TRUNC
%89 = or i32 %87, %88
%90 = load i32, i32* @S_IRUSR
%91 = load i32, i32* @S_IWUSR
%92 = or i32 %90, %91
%93 = load i32, i32* @S_IRGRP
%94 = or i32 %92, %93
%95 = load i32, i32* @S_IROTH
%96 = or i32 %94, %95
%97 = call i32 (i8*, i32, ...) @open(i8* %84, i32 %89, i32 %96)
store i32 %97, i32* %6
%98 = load i32, i32* %6
%99 = icmp eq i32 %98, -1
br i1 %99, label %100, label %104
100: ; preds = %83
%101 = load i32, i32* @stderr
%102 = load i8*, i8** %11
%103 = call i32 (i32, i8*, ...) @fprintf(i32 %101, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0), i8* %102)
store i32 -3, i32* %3
br label %191
104: ; preds = %83
%105 = load i32, i32* %6
%106 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%107 = load i64, i64* %106
%108 = sub nsw i64 %107, 1
%109 = load i32, i32* @SEEK_SET
%110 = call i32 @lseek(i32 %105, i64 %108, i32 %109)
%111 = icmp eq i32 %110, -1
br i1 %111, label %112, label %115
112: ; preds = %104
%113 = load i32, i32* @stderr
%114 = call i32 (i32, i8*, ...) @fprintf(i32 %113, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
br label %132
115: ; preds = %104
%116 = load i32, i32* %6
%117 = call i32 @write(i32 %116, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.9, i64 0, i64 0), i32 1)
%118 = icmp ne i32 %117, 1
br i1 %118, label %119, label %122
119: ; preds = %115
%120 = load i32, i32* @stderr
%121 = call i32 (i32, i8*, ...) @fprintf(i32 %120, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.10, i64 0, i64 0))
br label %131
122: ; preds = %115
%123 = load i32, i32* %6
%124 = load i32, i32* @SEEK_SET
%125 = call i32 @lseek(i32 %123, i64 0, i32 %124)
%126 = icmp eq i32 %125, -1
br i1 %126, label %127, label %130
127: ; preds = %122
%128 = load i32, i32* @stderr
%129 = call i32 (i32, i8*, ...) @fprintf(i32 %128, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.11, i64 0, i64 0))
br label %130
130: ; preds = %127, %122
br label %131
131: ; preds = %130, %119
br label %132
132: ; preds = %131, %112
%133 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%134 = load i64, i64* %133
%135 = load i32, i32* @PROT_WRITE
%136 = load i32, i32* @MAP_FILE
%137 = load i32, i32* @MAP_SHARED
%138 = or i32 %136, %137
%139 = load i32, i32* %6
%140 = call i8* @mmap(i32 0, i64 %134, i32 %135, i32 %138, i32 %139, i32 0)
store i8* %140, i8** %9
%141 = load i8*, i8** %9
%142 = icmp ne i8* %141, null
br i1 %142, label %146, label %143
143: ; preds = %132
%144 = load i32, i32* @stderr
%145 = call i32 (i32, i8*, ...) @fprintf(i32 %144, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0))
br label %167
146: ; preds = %132
%147 = load i8*, i8** %9
%148 = load i8*, i8** @MAP_FAILED
%149 = icmp eq i8* %147, %148
br i1 %149, label %150, label %153
150: ; preds = %146
%151 = load i32, i32* @stderr
%152 = call i32 (i32, i8*, ...) @fprintf(i32 %151, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.13, i64 0, i64 0))
br label %166
153: ; preds = %146
%154 = load i8*, i8** %8
%155 = icmp ne i8* %154, null
br i1 %155, label %156, label %162
156: ; preds = %153
%157 = load i8*, i8** %9
%158 = load i8*, i8** %8
%159 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%160 = load i64, i64* %159
%161 = call i32 @memcpy(i8* %157, i8* %158, i64 %160)
br label %165
162: ; preds = %153
%163 = load i32, i32* @stderr
%164 = call i32 (i32, i8*, ...) @fprintf(i32 %163, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.14, i64 0, i64 0))
br label %165
165: ; preds = %162, %156
br label %166
166: ; preds = %165, %150
br label %167
167: ; preds = %166, %143
%168 = load i8*, i8** %8
%169 = icmp ne i8* %168, null
br i1 %169, label %170, label %175
170: ; preds = %167
%171 = load i8*, i8** %8
%172 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%173 = load i64, i64* %172
%174 = call i32 @munmap(i8* %171, i64 %173)
br label %175
175: ; preds = %170, %167
%176 = load i8*, i8** %9
%177 = icmp ne i8* %176, null
br i1 %177, label %178, label %183
178: ; preds = %175
%179 = load i8*, i8** %9
%180 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%181 = load i64, i64* %180
%182 = call i32 @munmap(i8* %179, i64 %181)
br label %183
183: ; preds = %178, %175
%184 = load i32, i32* %6
%185 = icmp ne i32 %184, -1
br i1 %185, label %186, label %189
186: ; preds = %183
%187 = load i32, i32* %6
%188 = call i32 @close(i32 %187)
br label %189
189: ; preds = %186, %183
%190 = load i32, i32* %6
store i32 %190, i32* %3
br label %191
191: ; preds = %189, %100, %43, %35
%192 = load i32, i32* %3
ret i32 %192
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [10 x i8] c"mmap-cp.c\00"
@.str.1 = [15 x i8] c"mmap-cp.c.copy\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.2 = [22 x i8] c"open(\22%s\22, O_RDONLY)\0A\00"
@.str.3 = [23 x i8] c"fstat(fd, &fst) == -1\0A\00"
@.str.4 = [11 x i8] c"size is 0\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@.str.5 = [16 x i8] c"in_mem is NULL\0A\00"
@MAP_FAILED = global i8* null
@.str.6 = [22 x i8] c"in_mem == MAP_FAILED\0A\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.7 = [33 x i8] c"open(\22%s\22, O_WRONLY | O_CREATE)\0A\00"
@SEEK_SET = global i32 0
@.str.8 = [8 x i8] c"lseek 1\00"
@.str.9 = [1 x i8] zeroinitializer
@.str.10 = [11 x i8] c"write byte\00"
@.str.11 = [8 x i8] c"lseek 2\00"
@MAP_FILE = global i32 0
@MAP_SHARED = global i32 0
@.str.12 = [17 x i8] c"out_mem is NULL\0A\00"
@.str.13 = [23 x i8] c"out_mem == MAP_FAILED\0A\00"
@.str.14 = [20 x i8] c"no in_mem to copy.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %13
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = icmp eq i32 %0, 2
br i1 %9, label %13, label %10
10: ; preds = %6
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
br label %13
13: ; preds = %2, %6, %10
%14 = phi i8* [ %8, %10 ], [ %8, %6 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %2 ]
%15 = phi i8* [ %12, %10 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %6 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %2 ]
%16 = load i32, i32* @O_RDONLY
%17 = tail call i32 (i8*, i32, ...) @open(i8* %14, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %14)
br label %118
22: ; preds = %13
%23 = call i32 @fstat(i32 %17, %struct.stat* %3)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %118
28: ; preds = %22
%29 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%30 = load i64, i64* %29
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%35 = tail call i32 @close(i32 %17)
br label %47
36: ; preds = %28
%37 = load i32, i32* @PROT_READ
%38 = load i32, i32* @PROT_WRITE
%39 = or i32 %38, %37
%40 = load i32, i32* @MAP_PRIVATE
%41 = tail call i8* @mmap(i32 0, i64 %30, i32 %39, i32 %40, i32 %17, i32 0)
%42 = tail call i32 @close(i32 %17)
%43 = icmp eq i8* %41, null
br i1 %43, label %47, label %44
44: ; preds = %36
%45 = load i8*, i8** @MAP_FAILED
%46 = icmp eq i8* %41, %45
br i1 %46, label %47, label %53
47: ; preds = %44, %36, %32
%48 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), %32 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), %36 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), %44 ]
%49 = phi i1 [ true, %32 ], [ true, %36 ], [ false, %44 ]
%50 = phi i8* [ null, %32 ], [ null, %36 ], [ %41, %44 ]
%51 = load i32, i32* @stderr
%52 = tail call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* %48)
br label %53
53: ; preds = %47, %44
%54 = phi i1 [ false, %44 ], [ %49, %47 ]
%55 = phi i8* [ %41, %44 ], [ %50, %47 ]
%56 = load i32, i32* @O_RDWR
%57 = load i32, i32* @O_CREAT
%58 = or i32 %57, %56
%59 = load i32, i32* @O_TRUNC
%60 = or i32 %58, %59
%61 = load i32, i32* @S_IRUSR
%62 = load i32, i32* @S_IWUSR
%63 = or i32 %62, %61
%64 = load i32, i32* @S_IRGRP
%65 = or i32 %63, %64
%66 = load i32, i32* @S_IROTH
%67 = or i32 %65, %66
%68 = tail call i32 (i8*, i32, ...) @open(i8* %15, i32 %60, i32 %67)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %73
70: ; preds = %53
%71 = load i32, i32* @stderr
%72 = tail call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0), i8* %15)
br label %118
73: ; preds = %53
%74 = add nsw i64 %30, -1
%75 = load i32, i32* @SEEK_SET
%76 = tail call i32 @lseek(i32 %68, i64 %74, i32 %75)
%77 = icmp eq i32 %76, -1
br i1 %77, label %85, label %78
78: ; preds = %73
%79 = tail call i32 @write(i32 %68, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.9, i64 0, i64 0), i32 1)
%80 = icmp eq i32 %79, 1
br i1 %80, label %81, label %85
81: ; preds = %78
%82 = load i32, i32* @SEEK_SET
%83 = tail call i32 @lseek(i32 %68, i64 0, i32 %82)
%84 = icmp eq i32 %83, -1
br i1 %84, label %85, label %89
85: ; preds = %81, %78, %73
%86 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0), %73 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.10, i64 0, i64 0), %78 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.11, i64 0, i64 0), %81 ]
%87 = load i32, i32* @stderr
%88 = tail call i32 (i32, i8*, ...) @fprintf(i32 %87, i8* %86)
br label %89
89: ; preds = %85, %81
%90 = load i32, i32* @PROT_WRITE
%91 = load i32, i32* @MAP_FILE
%92 = load i32, i32* @MAP_SHARED
%93 = or i32 %92, %91
%94 = tail call i8* @mmap(i32 0, i64 %30, i32 %90, i32 %93, i32 %68, i32 0)
%95 = icmp eq i8* %94, null
br i1 %95, label %108, label %96
96: ; preds = %89
%97 = load i8*, i8** @MAP_FAILED
%98 = icmp eq i8* %94, %97
br i1 %98, label %99, label %102
99: ; preds = %96
%100 = load i32, i32* @stderr
%101 = tail call i32 (i32, i8*, ...) @fprintf(i32 %100, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.13, i64 0, i64 0))
br i1 %54, label %113, label %111
102: ; preds = %96
br i1 %54, label %105, label %103
103: ; preds = %102
%104 = tail call i32 @memcpy(i8* %94, i8* %55, i64 %30)
br label %111
105: ; preds = %102
%106 = load i32, i32* @stderr
%107 = tail call i32 (i32, i8*, ...) @fprintf(i32 %106, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.14, i64 0, i64 0))
br label %113
108: ; preds = %89
%109 = load i32, i32* @stderr
%110 = tail call i32 (i32, i8*, ...) @fprintf(i32 %109, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0))
br i1 %54, label %116, label %113
111: ; preds = %103, %99
%112 = tail call i32 @munmap(i8* %55, i64 %30)
br label %113
113: ; preds = %111, %99, %105, %108
%114 = phi i8* [ %55, %108 ], [ %94, %105 ], [ %94, %99 ], [ %94, %111 ]
%115 = tail call i32 @munmap(i8* %114, i64 %30)
br label %116
116: ; preds = %113, %108
%117 = tail call i32 @close(i32 %68)
br label %118
118: ; preds = %116, %70, %25, %19
%119 = phi i32 [ -1, %19 ], [ -2, %25 ], [ -3, %70 ], [ %68, %116 ]
ret i32 %119
}