#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 */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ MS_SYNC;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SEEK_END;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int errno;
int /*<<< orphan*/ exit(int);
int ftruncate(int, size_t);
size_t lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
char *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int msync(char *, size_t, int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int main(void) {
  int fdWrite, fdRead;
  size_t n;
  char *read, *write;
  if ((fdWrite = open("bar", O_CREAT | O_RDWR | O_TRUNC,
                      S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) {
    printf("open error with errno = %d\n", errno);
    perror("bar");
    exit(1);
  }
  if ((fdRead = open("foo", O_RDONLY)) == -1) {
    printf("open error with errno = %d\n", errno);
    perror("foo");
    exit(1);
  }
  n = lseek(fdRead, 0, SEEK_END);
  if (ftruncate(fdWrite, n) == -1) {
    printf("ftruncate error with errno = %d\n", errno);
    perror("bar");
    exit(1);
  }
  if ((read = mmap(NULL, n, PROT_READ, MAP_PRIVATE, fdRead, 0)) == MAP_FAILED) {
    printf("mmap error with errno = %d\n", errno);
    perror("foo");
    exit(1);
  }
  if ((write = mmap(NULL, n, PROT_WRITE | PROT_READ, MAP_SHARED, fdWrite, 0)) ==
      MAP_FAILED) {
    printf("mmap error with errno = %d\n", errno);
    perror("bar");
    exit(1);
  }
  memcpy(write, read, n);
  if (msync(write, n, MS_SYNC) == -1) {
    printf("msync error with errno = %d\n", errno);
    perror("bar");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    0x2e19(%rip),%rax
or     (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
mov    0x2db7(%rip),%rax
mov    (%rax),%edx
mov    0x2de6(%rip),%rax
or     (%rax),%edx
mov    0x2dc5(%rip),%rax
or     (%rax),%edx
mov    0x2db4(%rip),%rax
or     (%rax),%edx
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1223 <main+0x93>
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
lea    0xdfc(%rip),%rdi
call   1050 <printf@plt>
lea    0xdec(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1271 <main+0xe1>
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
lea    0xdae(%rip),%rdi
call   1050 <printf@plt>
lea    0xdbe(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0xc(%rbp),%edi
mov    0x2d45(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <lseek@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1060 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    12c8 <main+0x138>
mov    0x2cfc(%rip),%rax
mov    (%rax),%esi
lea    0xd77(%rip),%rdi
call   1050 <printf@plt>
lea    0xd47(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rsi
mov    0x2d15(%rip),%rax
mov    (%rax),%edx
mov    0x2c94(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    0x2c6f(%rip),%rcx
cmp    (%rcx),%rax
jne    132d <main+0x19d>
mov    0x2c97(%rip),%rax
mov    (%rax),%esi
lea    0xd33(%rip),%rdi
call   1050 <printf@plt>
lea    0xd02(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rsi
mov    0x2c48(%rip),%rax
mov    (%rax),%edx
mov    0x2ca7(%rip),%rax
or     (%rax),%edx
mov    0x2c8e(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    0x2c01(%rip),%rcx
cmp    (%rcx),%rax
jne    139b <main+0x20b>
mov    0x2c29(%rip),%rax
mov    (%rax),%esi
lea    0xcc5(%rip),%rdi
call   1050 <printf@plt>
lea    0xc74(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1080 <memcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2bcd(%rip),%rax
mov    (%rax),%edx
call   1030 <msync@plt>
cmp    $0xffffffff,%eax
jne    13f6 <main+0x266>
mov    0x2bce(%rip),%rax
mov    (%rax),%esi
lea    0xc86(%rip),%rdi
call   1050 <printf@plt>
lea    0xc19(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e56(%rip),%rax
mov    0x2e1f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e3c(%rip),%rax
or     (%rax),%esi
mov    0x2dbb(%rip),%rax
mov    0x2dec(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dc9(%rip),%rax
or     (%rax),%edx
mov    0x2db8(%rip),%rax
or     (%rax),%edx
lea    0xe1f(%rip),%rdi
xor    %eax,%eax
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12e2 <main+0x152>
mov    %eax,%r14d
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
lea    0xe1c(%rip),%rdi
xor    %eax,%eax
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12f4 <main+0x164>
mov    %eax,%ebp
mov    0x2da3(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1070 <lseek@plt>
mov    %rax,%rbx
mov    %r14d,%edi
mov    %rax,%rsi
call   1060 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     1306 <main+0x176>
mov    0x2da2(%rip),%r13
mov    0x0(%r13),%edx
mov    0x2d1f(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2cfe(%rip),%r12
cmp    (%r12),%rax
je     1318 <main+0x188>
mov    %rax,%r15
mov    0x2d02(%rip),%rax
mov    0x0(%r13),%edx
or     (%rax),%edx
mov    0x2d4d(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    (%r12),%rax
je     1336 <main+0x1a6>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1080 <memcpy@plt>
mov    0x2cc9(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1030 <msync@plt>
cmp    $0xffffffff,%eax
je     1348 <main+0x1b8>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cb7(%rip),%rax
mov    (%rax),%esi
lea    0xd12(%rip),%rdi
jmp    1358 <main+0x1c8>
mov    0x2ca5(%rip),%rax
mov    (%rax),%esi
lea    0xd00(%rip),%rdi
jmp    1328 <main+0x198>
mov    0x2c93(%rip),%rax
mov    (%rax),%esi
lea    0xd0e(%rip),%rdi
jmp    1358 <main+0x1c8>
mov    0x2c81(%rip),%rax
mov    (%rax),%esi
lea    0xd1d(%rip),%rdi
call   1050 <printf@plt>
lea    0xcec(%rip),%rdi
jmp    1364 <main+0x1d4>
mov    0x2c63(%rip),%rax
mov    (%rax),%esi
lea    0xcff(%rip),%rdi
jmp    1358 <main+0x1c8>
mov    0x2c51(%rip),%rax
mov    (%rax),%esi
lea    0xd09(%rip),%rdi
call   1050 <printf@plt>
lea    0xc9c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"bar\00"
@O_CREAT = global i32 0
@O_RDWR = 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.1 = [28 x i8] c"open error with errno = %d\0A\00"
@errno = global i32 0
@.str.2 = [4 x i8] c"foo\00"
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@.str.3 = [33 x i8] c"ftruncate error with errno = %d\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [28 x i8] c"mmap error with errno = %d\0A\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MS_SYNC = global i32 0
@.str.5 = [29 x i8] c"msync error with errno = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
store i32 0, i32* %1
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_RDWR
%9 = or i32 %7, %8
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRUSR
%13 = load i32, i32* @S_IWUSR
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRGRP
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IROTH
%18 = or i32 %16, %17
%19 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %18)
store i32 %19, i32* %2
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %0
%22 = load i32, i32* @errno
%23 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %0
%27 = load i32, i32* @O_RDONLY
%28 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %27)
store i32 %28, i32* %3
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = load i32, i32* @errno
%32 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %26
%36 = load i32, i32* %3
%37 = load i32, i32* @SEEK_END
%38 = call i64 @lseek(i32 %36, i32 0, i32 %37)
store i64 %38, i64* %4
%39 = load i32, i32* %2
%40 = load i64, i64* %4
%41 = call i32 @ftruncate(i32 %39, i64 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %48
43: ; preds = %35
%44 = load i32, i32* @errno
%45 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %44)
%46 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %35
%49 = load i64, i64* %4
%50 = load i32, i32* @PROT_READ
%51 = load i32, i32* @MAP_PRIVATE
%52 = load i32, i32* %3
%53 = call i8* @mmap(i32* null, i64 %49, i32 %50, i32 %51, i32 %52, i32 0)
store i8* %53, i8** %5
%54 = load i8*, i8** @MAP_FAILED
%55 = icmp eq i8* %53, %54
br i1 %55, label %56, label %61
56: ; preds = %48
%57 = load i32, i32* @errno
%58 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %57)
%59 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %48
%62 = load i64, i64* %4
%63 = load i32, i32* @PROT_WRITE
%64 = load i32, i32* @PROT_READ
%65 = or i32 %63, %64
%66 = load i32, i32* @MAP_SHARED
%67 = load i32, i32* %2
%68 = call i8* @mmap(i32* null, i64 %62, i32 %65, i32 %66, i32 %67, i32 0)
store i8* %68, i8** %6
%69 = load i8*, i8** @MAP_FAILED
%70 = icmp eq i8* %68, %69
br i1 %70, label %71, label %76
71: ; preds = %61
%72 = load i32, i32* @errno
%73 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %72)
%74 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %61
%77 = load i8*, i8** %6
%78 = load i8*, i8** %5
%79 = load i64, i64* %4
%80 = call i32 @memcpy(i8* %77, i8* %78, i64 %79)
%81 = load i8*, i8** %6
%82 = load i64, i64* %4
%83 = load i32, i32* @MS_SYNC
%84 = call i32 @msync(i8* %81, i64 %82, i32 %83)
%85 = icmp eq i32 %84, -1
br i1 %85, label %86, label %91
86: ; preds = %76
%87 = load i32, i32* @errno
%88 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %87)
%89 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%90 = call i32 @exit(i32 1)
unreachable
91: ; preds = %76
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"bar\00"
@O_CREAT = global i32 0
@O_RDWR = 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.1 = [28 x i8] c"open error with errno = %d\0A\00"
@errno = global i32 0
@.str.2 = [4 x i8] c"foo\00"
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@.str.3 = [33 x i8] c"ftruncate error with errno = %d\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [28 x i8] c"mmap error with errno = %d\0A\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MS_SYNC = global i32 0
@.str.5 = [29 x i8] c"msync error with errno = %d\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_CREAT
%2 = load i32, i32* @O_RDWR
%3 = or i32 %2, %1
%4 = load i32, i32* @O_TRUNC
%5 = or i32 %3, %4
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %7, %6
%9 = load i32, i32* @S_IRGRP
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IROTH
%12 = or i32 %10, %11
%13 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %0
%16 = load i32, i32* @errno
%17 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %0
%21 = load i32, i32* @O_RDONLY
%22 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i32, i32* @errno
%26 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %20
%30 = load i32, i32* @SEEK_END
%31 = tail call i64 @lseek(i32 %22, i32 0, i32 %30)
%32 = tail call i32 @ftruncate(i32 %13, i64 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %39
34: ; preds = %29
%35 = load i32, i32* @errno
%36 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %29
%40 = load i32, i32* @PROT_READ
%41 = load i32, i32* @MAP_PRIVATE
%42 = tail call i8* @mmap(i32* null, i64 %31, i32 %40, i32 %41, i32 %22, i32 0)
%43 = load i8*, i8** @MAP_FAILED
%44 = icmp eq i8* %42, %43
br i1 %44, label %45, label %50
45: ; preds = %39
%46 = load i32, i32* @errno
%47 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %46)
%48 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%49 = tail call i32 @exit(i32 1)
unreachable
50: ; preds = %39
%51 = load i32, i32* @PROT_WRITE
%52 = load i32, i32* @PROT_READ
%53 = or i32 %52, %51
%54 = load i32, i32* @MAP_SHARED
%55 = tail call i8* @mmap(i32* null, i64 %31, i32 %53, i32 %54, i32 %13, i32 0)
%56 = load i8*, i8** @MAP_FAILED
%57 = icmp eq i8* %55, %56
br i1 %57, label %58, label %63
58: ; preds = %50
%59 = load i32, i32* @errno
%60 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %59)
%61 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%62 = tail call i32 @exit(i32 1)
unreachable
63: ; preds = %50
%64 = tail call i32 @memcpy(i8* %55, i8* %42, i64 %31)
%65 = load i32, i32* @MS_SYNC
%66 = tail call i32 @msync(i8* %55, i64 %31, i32 %65)
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %73
68: ; preds = %63
%69 = load i32, i32* @errno
%70 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %69)
%71 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%72 = tail call i32 @exit(i32 1)
unreachable
73: ; preds = %63
ret i32 0
}