#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 */
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
scalar_t__ ftruncate(int, int);
char *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ write(int, char *, int);
void main() {
  int fd = open("/tmp/file", O_RDWR | O_CREAT, 0600);
  if (fd < 0) {
    perror("open");
    exit(1);
  }
  int len = 128;
  if (ftruncate(fd, len) != 0) {
    perror("ftruncate");
    exit(1);
  }
  char *addr = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  for (int i = 0; i < 100; i++) {
    sprintf(addr, "%d", i);
    write(1, ".", 1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e70(%rip),%rax
or     (%rax),%esi
lea    0xe6f(%rip),%rdi
mov    $0x180,%edx
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11be <main+0x4e>
lea    0xe5b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x80,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1050 <ftruncate@plt>
cmp    $0x0,%rax
je     11f0 <main+0x80>
lea    0xe2e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%esi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
mov    0x2dbd(%rip),%rax
or     (%rax),%edx
mov    0x2dd4(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x64,-0x14(%rbp)
jge    1266 <main+0xf6>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xdd9(%rip),%rsi
call   1080 <sprintf@plt>
mov    $0x1,%edx
lea    0xdcb(%rip),%rsi
mov    %edx,%edi
call   1030 <write@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1228 <main+0xb8>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e52(%rip),%rax
mov    0x2e73(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe70(%rip),%rdi
mov    $0x180,%edx
call   1060 <open@plt>
test   %eax,%eax
js     1232 <main+0xc2>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x80,%esi
call   1050 <ftruncate@plt>
test   %rax,%rax
jne    123b <main+0xcb>
mov    0x2e30(%rip),%rax
mov    0x2df9(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0e(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0x80,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
lea    0xe27(%rip),%r14
lea    0xe23(%rip),%r15
nopl   0x0(%rax)
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1080 <sprintf@plt>
mov    $0x1,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
add    $0x1,%ebx
cmp    $0x64,%ebx
jne    1200 <main+0x90>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd1(%rip),%rdi
jmp    1242 <main+0xd2>
lea    0xdcd(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/tmp/file\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [10 x i8] c"ftruncate\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [2 x i8] c".\00"
define void @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7, i32 384)
store i32 %8, i32* %1
%9 = load i32, i32* %1
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
store i32 128, i32* %2
%15 = load i32, i32* %1
%16 = load i32, i32* %2
%17 = call i64 @ftruncate(i32 %15, i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %14
%23 = load i32, i32* %2
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @PROT_WRITE
%26 = or i32 %24, %25
%27 = load i32, i32* @MAP_SHARED
%28 = load i32, i32* %1
%29 = call i8* @mmap(i32 0, i32 %23, i32 %26, i32 %27, i32 %28, i32 0)
store i8* %29, i8** %3
store i32 0, i32* %4
br label %30
30: ; preds = %38, %22
%31 = load i32, i32* %4
%32 = icmp slt i32 %31, 100
br i1 %32, label %33, label %41
33: ; preds = %30
%34 = load i8*, i8** %3
%35 = load i32, i32* %4
%36 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
br label %38
38: ; preds = %33
%39 = load i32, i32* %4
%40 = add nsw i32 %39, 1
store i32 %40, i32* %4
br label %30
41: ; preds = %30
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"/tmp/file\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [10 x i8] c"ftruncate\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [2 x i8] c".\00"
define void @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3, i32 384)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = tail call i64 @ftruncate(i32 %4, i32 128)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %9
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @PROT_WRITE
%18 = or i32 %17, %16
%19 = load i32, i32* @MAP_SHARED
%20 = tail call i8* @mmap(i32 0, i32 128, i32 %18, i32 %19, i32 %4, i32 0)
br label %22
21: ; preds = %22
ret void
22: ; preds = %15, %22
%23 = phi i32 [ 0, %15 ], [ %26, %22 ]
%24 = tail call i32 @sprintf(i8* dereferenceable(1) %20, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
%26 = add nuw nsw i32 %23, 1
%27 = icmp eq i32 %26, 100
br i1 %27, label %21, label %22
}