#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 */
typedef char *caddr_t;

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ fsync(int);
scalar_t__ lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int, int);
char *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ unlink(char *);
scalar_t__ write(int, char *, int);
int main(int argc, char **argv) {
  int fd, fd2;
  caddr_t addr;
  char zeros[4096];
  char ones[200];
  memset(zeros, 0, sizeof zeros);
  memset(ones, 1, sizeof ones);
  fd = open("test1.data", O_RDWR);
  if (fd < 0)
    err(1, "opening file");
  if (lseek(fd, 600, SEEK_SET) < 0)
    err(1, "seeking");
  if (write(fd, ones, sizeof ones) < 0)
    err(1, "writing ones");
  fsync(fd);
  addr = mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  if (addr == MAP_FAILED)
    err(1, "mapping");
  unlink("test1.scratch");
  fd2 = open("test1.scratch", O_RDWR | O_CREAT, 0666);
  if (fd2 < 0)
    err(1, "creating scratch");
  if (write(fd2, addr, 4096) < 0)
    err(1, "writing scratch");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10f0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1070 <memset@plt>
lea    -0x10f0(%rbp),%rdi
mov    $0x1,%esi
mov    $0xc8,%edx
call   1070 <memset@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11f7 <main+0x77>
mov    $0x1,%edi
lea    0xe19(%rip),%rsi
call   1080 <err@plt>
mov    -0x14(%rbp),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%edx
mov    $0x258,%esi
call   1060 <lseek@plt>
cmp    $0x0,%rax
jge    1228 <main+0xa8>
mov    $0x1,%edi
lea    0xdf5(%rip),%rsi
call   1080 <err@plt>
mov    -0x14(%rbp),%edi
lea    -0x10f0(%rbp),%rsi
mov    $0xc8,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    1257 <main+0xd7>
mov    $0x1,%edi
lea    0xdce(%rip),%rsi
call   1080 <err@plt>
mov    -0x14(%rbp),%edi
call   1090 <fsync@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edx
mov    0x2d51(%rip),%rax
or     (%rax),%edx
mov    0x2d40(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x1000,%esi
mov    %r9d,%edi
call   1050 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d13(%rip),%rcx
cmp    (%rcx),%rax
jne    12b7 <main+0x137>
mov    $0x1,%edi
lea    0xd7b(%rip),%rsi
call   1080 <err@plt>
lea    0xd77(%rip),%rdi
call   1030 <unlink@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%esi
mov    0x2d25(%rip),%rax
or     (%rax),%esi
lea    0xd59(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1306 <main+0x186>
mov    $0x1,%edi
lea    0xd42(%rip),%rsi
call   1080 <err@plt>
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    1332 <main+0x1b2>
mov    $0x1,%edi
lea    0xd27(%rip),%rsi
call   1080 <err@plt>
mov    -0x4(%rbp),%eax
add    $0x10f0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10d0,%rsp
lea    0xd0(%rsp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1070 <memset@plt>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0xc8,%edx
call   1070 <memset@plt>
mov    0x2e20(%rip),%r14
mov    (%r14),%esi
lea    0xe3e(%rip),%rdi
xor    %eax,%eax
call   10a0 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11e0 <main+0x60>
lea    0xe35(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x258,%esi
call   1060 <lseek@plt>
test   %rax,%rax
jns    120b <main+0x8b>
lea    0xe17(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc8,%edx
call   1040 <write@plt>
test   %rax,%rax
jns    1230 <main+0xb0>
lea    0xdfa(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    %ebx,%edi
call   1090 <fsync@plt>
mov    0x2db2(%rip),%rax
mov    0x2d7b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d68(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2d42(%rip),%rax
cmp    (%rax),%rbx
jne    1284 <main+0x104>
lea    0xdb3(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
lea    0xdaa(%rip),%rbp
mov    %rbp,%rdi
call   1030 <unlink@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    %rbp,%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   10a0 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12c5 <main+0x145>
lea    0xd88(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
test   %rax,%rax
jns    12ea <main+0x16a>
lea    0xd74(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
xor    %eax,%eax
add    $0x10d0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"test1.data\00"
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"opening file\00"
@SEEK_SET = global i32 0
@.str.2 = [8 x i8] c"seeking\00"
@.str.3 = [13 x i8] c"writing ones\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [8 x i8] c"mapping\00"
@.str.5 = [14 x i8] c"test1.scratch\00"
@O_CREAT = global i32 0
@.str.6 = [17 x i8] c"creating scratch\00"
@.str.7 = [16 x i8] c"writing scratch\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 [4096 x i8]
%10 = alloca [200 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%12 = call i32 @memset(i8* %11, i32 0, i32 4096)
%13 = getelementptr inbounds [200 x i8], [200 x i8]* %10, i64 0, i64 0
%14 = call i32 @memset(i8* %13, i32 1, i32 200)
%15 = load i32, i32* @O_RDWR
%16 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
%22 = load i32, i32* %6
%23 = load i32, i32* @SEEK_SET
%24 = call i64 @lseek(i32 %22, i32 600, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %21
%29 = load i32, i32* %6
%30 = getelementptr inbounds [200 x i8], [200 x i8]* %10, i64 0, i64 0
%31 = call i64 @write(i32 %29, i8* %30, i32 200)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %33, %28
%36 = load i32, i32* %6
%37 = call i32 @fsync(i32 %36)
%38 = load i32, i32* @PROT_READ
%39 = load i32, i32* @PROT_WRITE
%40 = or i32 %38, %39
%41 = load i32, i32* @MAP_PRIVATE
%42 = load i32, i32* %6
%43 = call i8* @mmap(i32 0, i32 4096, i32 %40, i32 %41, i32 %42, i32 0)
store i8* %43, i8** %8
%44 = load i8*, i8** %8
%45 = load i8*, i8** @MAP_FAILED
%46 = icmp eq i8* %44, %45
br i1 %46, label %47, label %49
47: ; preds = %35
%48 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %49
49: ; preds = %47, %35
%50 = call i32 @unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%51 = load i32, i32* @O_RDWR
%52 = load i32, i32* @O_CREAT
%53 = or i32 %51, %52
%54 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %53, i32 438)
store i32 %54, i32* %7
%55 = load i32, i32* %7
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %49
%58 = call i32 @err(i32 1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
br label %59
59: ; preds = %57, %49
%60 = load i32, i32* %7
%61 = load i8*, i8** %8
%62 = call i64 @write(i32 %60, i8* %61, i32 4096)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %59
%65 = call i32 @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
br label %66
66: ; preds = %64, %59
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@.str = [11 x i8] c"test1.data\00"
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"opening file\00"
@SEEK_SET = global i32 0
@.str.2 = [8 x i8] c"seeking\00"
@.str.3 = [13 x i8] c"writing ones\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [8 x i8] c"mapping\00"
@.str.5 = [14 x i8] c"test1.scratch\00"
@O_CREAT = global i32 0
@.str.6 = [17 x i8] c"creating scratch\00"
@.str.7 = [16 x i8] c"writing scratch\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [4096 x i8]
%4 = alloca [200 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0
%7 = call i32 @memset(i8* %5, i32 0, i32 4096)
%8 = call i32 @memset(i8* %6, i32 1, i32 200)
%9 = load i32, i32* @O_RDWR
%10 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = load i32, i32* @SEEK_SET
%16 = call i64 @lseek(i32 %10, i32 600, i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = call i64 @write(i32 %10, i8* %6, i32 200)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %23, %20
%26 = call i32 @fsync(i32 %10)
%27 = load i32, i32* @PROT_READ
%28 = load i32, i32* @PROT_WRITE
%29 = or i32 %28, %27
%30 = load i32, i32* @MAP_PRIVATE
%31 = call i8* @mmap(i32 0, i32 4096, i32 %29, i32 %30, i32 %10, i32 0)
%32 = load i8*, i8** @MAP_FAILED
%33 = icmp eq i8* %31, %32
br i1 %33, label %34, label %36
34: ; preds = %25
%35 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %34, %25
%37 = call i32 @unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%38 = load i32, i32* @O_RDWR
%39 = load i32, i32* @O_CREAT
%40 = or i32 %39, %38
%41 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %40, i32 438)
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = call i32 @err(i32 1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
br label %45
45: ; preds = %43, %36
%46 = call i64 @write(i32 %41, i8* %31, i32 4096)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
br label %50
50: ; preds = %48, %45
ret i32 0
}