#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_APPEND;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ PROT_WRITE;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
void *mmap(int /*<<< orphan*/, int, int /*<<< orphan*/, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int munmap(void *, int);
int open(char const *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int truncate(char const *, int);
int main(int argc, char const *argv[]) {
  int fd, i;
  void *file_memory;
  i = truncate(argv[1], 400);
  if (i == -1) {
    perror("Error truncate");
    exit(1);
  }
  fd = open(argv[1], O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    perror("Error open");
    exit(1);
  }
  file_memory = mmap(0, 400, PROT_WRITE, MAP_SHARED, fd, 0);
  if (file_memory == MAP_FAILED) {
    perror("Error mmap");
    exit(1);
  }
  for (i = 0; i < 100; i++) {
    sprintf((char *)file_memory + (i * 4), "\n%03d", i);
  }
  i = munmap(file_memory, 400);
  if (i == -1) {
    perror("Error munmap");
    exit(1);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x190,%esi
call   1050 <truncate@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11cb <main+0x4b>
lea    0xe44(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    0x2e15(%rip),%rax
or     (%rax),%esi
mov    0x2db4(%rip),%rax
or     (%rax),%esi
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
mov    0x2dd2(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1228 <main+0xa8>
lea    0xdf6(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
mov    0x2db0(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x190,%esi
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d53(%rip),%rcx
cmp    (%rcx),%rax
jne    127c <main+0xfc>
lea    0xdad(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x64,-0x18(%rbp)
jge    12b9 <main+0x139>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%eax
shl    $0x2,%eax
cltq
add    %rax,%rdi
mov    -0x18(%rbp),%edx
lea    0xd7f(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1283 <main+0x103>
mov    -0x20(%rbp),%rdi
mov    $0x190,%esi
call   1060 <munmap@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12ea <main+0x16a>
lea    0xd50(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    $0x190,%esi
call   1050 <truncate@plt>
cmp    $0xffffffff,%eax
je     1270 <main+0xf0>
mov    0x8(%rbx),%rdi
mov    0x2e2b(%rip),%rax
mov    0x2e44(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2de1(%rip),%rax
or     (%rax),%esi
mov    0x2df0(%rip),%rax
mov    0x2e01(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1279 <main+0xf9>
mov    %eax,%r14d
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
mov    0x2df4(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x190,%esi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r15
mov    0x2d9e(%rip),%rax
cmp    (%rax),%r15
je     1282 <main+0x102>
xor    %ebp,%ebp
lea    0xe05(%rip),%r12
mov    %r15,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1090 <sprintf@plt>
add    $0x1,%rbp
add    $0x4,%rbx
cmp    $0x64,%rbp
jne    1230 <main+0xb0>
mov    %r15,%rdi
mov    $0x190,%esi
call   1060 <munmap@plt>
cmp    $0xffffffff,%eax
je     128b <main+0x10b>
mov    %r14d,%edi
call   1040 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd89(%rip),%rdi
jmp    1292 <main+0x112>
lea    0xd8f(%rip),%rdi
jmp    1292 <main+0x112>
lea    0xd91(%rip),%rdi
jmp    1292 <main+0x112>
lea    0xd99(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"Error truncate\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [11 x i8] c"Error open\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [11 x i8] c"Error mmap\00"
@.str.3 = [6 x i8] c"\0A%03d\00"
@.str.4 = [13 x i8] c"Error munmap\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*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 1
%11 = load i8*, i8** %10
%12 = call i32 @truncate(i8* %11, i32 400)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = load i32, i32* @O_RDWR
%23 = load i32, i32* @O_CREAT
%24 = or i32 %22, %23
%25 = load i32, i32* @O_APPEND
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IRUSR
%28 = load i32, i32* @S_IWUSR
%29 = or i32 %27, %28
%30 = call i32 @open(i8* %21, i32 %26, i32 %29)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %18
%34 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %18
%37 = load i32, i32* @PROT_WRITE
%38 = load i32, i32* @MAP_SHARED
%39 = load i32, i32* %6
%40 = call i8* @mmap(i32 0, i32 400, i32 %37, i32 %38, i32 %39, i32 0)
store i8* %40, i8** %8
%41 = load i8*, i8** %8
%42 = load i8*, i8** @MAP_FAILED
%43 = icmp eq i8* %41, %42
br i1 %43, label %44, label %47
44: ; preds = %36
%45 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %36
store i32 0, i32* %7
br label %48
48: ; preds = %59, %47
%49 = load i32, i32* %7
%50 = icmp slt i32 %49, 100
br i1 %50, label %51, label %62
51: ; preds = %48
%52 = load i8*, i8** %8
%53 = load i32, i32* %7
%54 = mul nsw i32 %53, 4
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %52, i64 %55
%57 = load i32, i32* %7
%58 = call i32 @sprintf(i8* %56, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %57)
br label %59
59: ; preds = %51
%60 = load i32, i32* %7
%61 = add nsw i32 %60, 1
store i32 %61, i32* %7
br label %48
62: ; preds = %48
%63 = load i8*, i8** %8
%64 = call i32 @munmap(i8* %63, i32 400)
store i32 %64, i32* %7
%65 = load i32, i32* %7
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %70
67: ; preds = %62
%68 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %62
%71 = load i32, i32* %6
%72 = call i32 @close(i32 %71)
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"Error truncate\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [11 x i8] c"Error open\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [11 x i8] c"Error mmap\00"
@.str.3 = [6 x i8] c"\0A%03d\00"
@.str.4 = [13 x i8] c"Error munmap\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i32 @truncate(i8* %4, i32 400)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = load i8*, i8** %3
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = load i32, i32* @O_APPEND
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IRUSR
%18 = load i32, i32* @S_IWUSR
%19 = or i32 %18, %17
%20 = tail call i32 @open(i8* %11, i32 %16, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %10
%23 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %10
%26 = load i32, i32* @PROT_WRITE
%27 = load i32, i32* @MAP_SHARED
%28 = tail call i8* @mmap(i32 0, i32 400, i32 %26, i32 %27, i32 %20, i32 0)
%29 = load i8*, i8** @MAP_FAILED
%30 = icmp eq i8* %28, %29
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %25, %34
%35 = phi i64 [ %40, %34 ], [ 0, %25 ]
%36 = shl nuw nsw i64 %35, 2
%37 = getelementptr inbounds i8, i8* %28, i64 %36
%38 = trunc i64 %35 to i32
%39 = tail call i32 @sprintf(i8* dereferenceable(1) %37, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %38)
%40 = add nuw nsw i64 %35, 1
%41 = icmp eq i64 %40, 100
br i1 %41, label %42, label %34
42: ; preds = %34
%43 = tail call i32 @munmap(i8* %28, i32 400)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%47 = tail call i32 @exit(i32 1)
unreachable
48: ; preds = %42
%49 = tail call i32 @close(i32 %20)
ret i32 0
}