#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ memcpy(void *, void *, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
void init(char *toRead, char *toWrite) {
  int fd;
  int fd2;
  struct stat mystat;
  struct stat mystat2;
  void *pmap;
  void *pmap2;
  fd = open(toRead, O_RDONLY);
  if (fd == -1) {
    perror("open");
    exit(1);
  }
  fd2 = open(toWrite, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  if (fd == -1) {
    perror("open2");
    exit(1);
  }
  if (fstat(fd, &mystat) < 0) {
    perror("fstat");
    close(fd);
    exit(1);
  }
  if (fstat(fd2, &mystat2) < 0) {
    perror("fstat2");
    close(fd2);
    exit(1);
  }
  pmap = mmap(0, mystat.st_size, PROT_READ, MAP_SHARED, fd, 0);
  pmap2 = mmap(0, mystat2.st_size, PROT_WRITE, MAP_SHARED, fd2, 0);
  if (pmap == MAP_FAILED) {
    perror("mmap");
    close(fd);
    exit(1);
  }
  if (pmap2 == MAP_FAILED) {
    perror("mmap2");
    close(fd2);
    exit(1);
  }
  memcpy(pmap2, pmap, mystat.st_size);
  close(fd);
  close(fd2);
}


x86 assembly (clang, -O0):
<init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b7 <init+0x47>
lea    0xe58(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    0x2e2d(%rip),%rax
or     (%rax),%esi
mov    0x2e1c(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1205 <init+0x95>
lea    0xe0f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
jge    1239 <init+0xc9>
lea    0xde9(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
jge    126d <init+0xfd>
lea    0xdbb(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%esi
mov    0x2d71(%rip),%rax
mov    (%rax),%edx
mov    0x2d58(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%esi
mov    0x2d19(%rip),%rax
mov    (%rax),%edx
mov    0x2d30(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2ce8(%rip),%rcx
cmp    (%rcx),%rax
jne    12ef <init+0x17f>
lea    0xd40(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x38(%rbp),%rax
mov    0x2cb6(%rip),%rcx
cmp    (%rcx),%rax
jne    1321 <init+0x1b1>
lea    0xd13(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1276 <init+0x106>
mov    %eax,%r15d
mov    0x2dfe(%rip),%rax
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e3c(%rip),%rax
or     (%rax),%esi
mov    %rbp,%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%r14d
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
call   1090 <fstat@plt>
test   %rax,%rax
js     128c <init+0x11c>
mov    %rsp,%rsi
mov    %r14d,%edi
call   1090 <fstat@plt>
test   %rax,%rax
js     1295 <init+0x125>
mov    0x8(%rsp),%ebp
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
mov    0x2dd2(%rip),%r12
mov    (%r12),%ecx
xor    %edi,%edi
mov    %ebp,%esi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    (%rsp),%esi
mov    0x2d92(%rip),%rax
mov    (%rax),%edx
mov    (%r12),%ecx
xor    %edi,%edi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2d70(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,%rbx
je     129e <init+0x12e>
cmp    %rcx,%rax
je     12af <init+0x13f>
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
mov    %r15d,%edi
call   1040 <close@plt>
mov    %r14d,%edi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <close@plt>
lea    0xd83(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xd72(%rip),%rdi
jmp    12a5 <init+0x135>
lea    0xd6f(%rip),%rdi
jmp    12b6 <init+0x146>
lea    0xd6d(%rip),%rdi
call   1070 <perror@plt>
mov    %r15d,%edi
jmp    12be <init+0x14e>
lea    0xd61(%rip),%rdi
call   1070 <perror@plt>
mov    %r14d,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [6 x i8] c"open2\00"
@.str.2 = [6 x i8] c"fstat\00"
@.str.3 = [7 x i8] c"fstat2\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [5 x i8] c"mmap\00"
@.str.5 = [6 x i8] c"mmap2\00"
define void @init(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca %struct.stat
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%11 = load i8*, i8** %3
%12 = load i32, i32* @O_RDONLY
%13 = call i32 (i8*, i32, ...) @open(i8* %11, i32 %12)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8*, i8** %4
%21 = load i32, i32* @O_WRONLY
%22 = load i32, i32* @O_CREAT
%23 = or i32 %21, %22
%24 = load i32, i32* @O_TRUNC
%25 = or i32 %23, %24
%26 = call i32 (i8*, i32, ...) @open(i8* %20, i32 %25, i32 420)
store i32 %26, i32* %6
%27 = load i32, i32* %5
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %19
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %19
%33 = load i32, i32* %5
%34 = call i64 @fstat(i32 %33, %struct.stat* %7)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* %5
%39 = call i32 @close(i32 %38)
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %32
%42 = load i32, i32* %6
%43 = call i64 @fstat(i32 %42, %struct.stat* %8)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %41
%46 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %41
%51 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%52 = load i32, i32* %51
%53 = load i32, i32* @PROT_READ
%54 = load i32, i32* @MAP_SHARED
%55 = load i32, i32* %5
%56 = call i8* @mmap(i32 0, i32 %52, i32 %53, i32 %54, i32 %55, i32 0)
store i8* %56, i8** %9
%57 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%58 = load i32, i32* %57
%59 = load i32, i32* @PROT_WRITE
%60 = load i32, i32* @MAP_SHARED
%61 = load i32, i32* %6
%62 = call i8* @mmap(i32 0, i32 %58, i32 %59, i32 %60, i32 %61, i32 0)
store i8* %62, i8** %10
%63 = load i8*, i8** %9
%64 = load i8*, i8** @MAP_FAILED
%65 = icmp eq i8* %63, %64
br i1 %65, label %66, label %71
66: ; preds = %50
%67 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%68 = load i32, i32* %5
%69 = call i32 @close(i32 %68)
%70 = call i32 @exit(i32 1)
unreachable
71: ; preds = %50
%72 = load i8*, i8** %10
%73 = load i8*, i8** @MAP_FAILED
%74 = icmp eq i8* %72, %73
br i1 %74, label %75, label %80
75: ; preds = %71
%76 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%77 = load i32, i32* %6
%78 = call i32 @close(i32 %77)
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %71
%81 = load i8*, i8** %10
%82 = load i8*, i8** %9
%83 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%84 = load i32, i32* %83
%85 = call i32 @memcpy(i8* %81, i8* %82, i32 %84)
%86 = load i32, i32* %5
%87 = call i32 @close(i32 %86)
%88 = load i32, i32* %6
%89 = call i32 @close(i32 %88)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [6 x i8] c"fstat\00"
@.str.3 = [7 x i8] c"fstat2\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [5 x i8] c"mmap\00"
@.str.5 = [6 x i8] c"mmap2\00"
define void @init(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %3 to i8*
%6 = bitcast %struct.stat* %4 to i8*
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %15, %14
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %18, i32 420)
%20 = call i64 @fstat(i32 %8, %struct.stat* %3)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %13
%23 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%24 = tail call i32 @close(i32 %8)
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %13
%27 = call i64 @fstat(i32 %19, %struct.stat* %4)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%31 = tail call i32 @close(i32 %19)
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %26
%34 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* @PROT_READ
%37 = load i32, i32* @MAP_SHARED
%38 = tail call i8* @mmap(i32 0, i32 %35, i32 %36, i32 %37, i32 %8, i32 0)
%39 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%40 = load i32, i32* %39
%41 = load i32, i32* @PROT_WRITE
%42 = load i32, i32* @MAP_SHARED
%43 = tail call i8* @mmap(i32 0, i32 %40, i32 %41, i32 %42, i32 %19, i32 0)
%44 = load i8*, i8** @MAP_FAILED
%45 = icmp eq i8* %38, %44
br i1 %45, label %46, label %50
46: ; preds = %33
%47 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%48 = tail call i32 @close(i32 %8)
%49 = tail call i32 @exit(i32 1)
unreachable
50: ; preds = %33
%51 = icmp eq i8* %43, %44
br i1 %51, label %52, label %56
52: ; preds = %50
%53 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%54 = tail call i32 @close(i32 %19)
%55 = tail call i32 @exit(i32 1)
unreachable
56: ; preds = %50
%57 = tail call i32 @memcpy(i8* %43, i8* %38, i32 %35)
%58 = tail call i32 @close(i32 %8)
%59 = tail call i32 @close(i32 %19)
ret void
}