#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 *);
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("test2.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");
  addr = mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  if (addr == MAP_FAILED)
    err(1, "mapping");
  unlink("test2.scratch");
  fd2 = open("test2.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    0x2e1f(%rip),%rax
mov    (%rax),%esi
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e7 <main+0x77>
mov    $0x1,%edi
lea    0xe29(%rip),%rsi
call   1080 <err@plt>
mov    -0x14(%rbp),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
mov    $0x258,%esi
call   1060 <lseek@plt>
cmp    $0x0,%rax
jge    1218 <main+0xa8>
mov    $0x1,%edi
lea    0xe05(%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    1247 <main+0xd7>
mov    $0x1,%edi
lea    0xdde(%rip),%rsi
call   1080 <err@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edx
mov    0x2d69(%rip),%rax
or     (%rax),%edx
mov    0x2d58(%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    0x2d2b(%rip),%rcx
cmp    (%rcx),%rax
jne    129f <main+0x12f>
mov    $0x1,%edi
lea    0xd93(%rip),%rsi
call   1080 <err@plt>
lea    0xd8f(%rip),%rdi
call   1030 <unlink@plt>
mov    0x2d26(%rip),%rax
mov    (%rax),%esi
mov    0x2d3d(%rip),%rax
or     (%rax),%esi
lea    0xd71(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12ee <main+0x17e>
mov    $0x1,%edi
lea    0xd5a(%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    131a <main+0x1aa>
mov    $0x1,%edi
lea    0xd3f(%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    0x2e30(%rip),%r14
mov    (%r14),%esi
lea    0xe4e(%rip),%rdi
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11d0 <main+0x60>
lea    0xe45(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x258,%esi
call   1060 <lseek@plt>
test   %rax,%rax
jns    11fb <main+0x8b>
lea    0xe27(%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    1220 <main+0xb0>
lea    0xe0a(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    0x2dc9(%rip),%rax
mov    0x2d92(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d7f(%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    0x2d59(%rip),%rax
cmp    (%rax),%rbx
jne    126d <main+0xfd>
lea    0xdca(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
lea    0xdc1(%rip),%rbp
mov    %rbp,%rdi
call   1030 <unlink@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    %rbp,%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12ae <main+0x13e>
lea    0xd9f(%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    12d3 <main+0x163>
lea    0xd8b(%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"test2.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"test2.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* @PROT_READ
%37 = load i32, i32* @PROT_WRITE
%38 = or i32 %36, %37
%39 = load i32, i32* @MAP_PRIVATE
%40 = load i32, i32* %6
%41 = call i8* @mmap(i32 0, i32 4096, i32 %38, i32 %39, i32 %40, i32 0)
store i8* %41, i8** %8
%42 = load i8*, i8** %8
%43 = load i8*, i8** @MAP_FAILED
%44 = icmp eq i8* %42, %43
br i1 %44, label %45, label %47
45: ; preds = %35
%46 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %47
47: ; preds = %45, %35
%48 = call i32 @unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%49 = load i32, i32* @O_RDWR
%50 = load i32, i32* @O_CREAT
%51 = or i32 %49, %50
%52 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %51, i32 438)
store i32 %52, i32* %7
%53 = load i32, i32* %7
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %47
%56 = call i32 @err(i32 1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
br label %57
57: ; preds = %55, %47
%58 = load i32, i32* %7
%59 = load i8*, i8** %8
%60 = call i64 @write(i32 %58, i8* %59, i32 4096)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %57
%63 = call i32 @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
br label %64
64: ; preds = %62, %57
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
@.str = [11 x i8] c"test2.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"test2.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 = load i32, i32* @PROT_READ
%27 = load i32, i32* @PROT_WRITE
%28 = or i32 %27, %26
%29 = load i32, i32* @MAP_PRIVATE
%30 = call i8* @mmap(i32 0, i32 4096, i32 %28, i32 %29, i32 %10, i32 0)
%31 = load i8*, i8** @MAP_FAILED
%32 = icmp eq i8* %30, %31
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %35
35: ; preds = %33, %25
%36 = call i32 @unlink(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%37 = load i32, i32* @O_RDWR
%38 = load i32, i32* @O_CREAT
%39 = or i32 %38, %37
%40 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %39, i32 438)
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %35
%43 = call i32 @err(i32 1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
br label %44
44: ; preds = %42, %35
%45 = call i64 @write(i32 %40, i8* %30, i32 4096)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = call i32 @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
br label %49
49: ; preds = %47, %44
ret i32 0
}