#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ exit(int);
scalar_t__ mmap(int /*<<< orphan*/, int, int /*<<< orphan*/, int /*<<< orphan*/,
                int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main() {
  char *p;
  int fd;
  int i;
  fd = open("buffer", O_RDWR);
  if (fd == -1) {
    perror("Error in open");
    exit(1);
  }
  p = (char *)mmap(0, 4096, PROT_WRITE, MAP_SHARED, fd, 0);
  if ((int)p == -1) {
    perror("Error in mmap");
    exit(2);
  }
  for (i = 0; i < 1000; i++) {
    sprintf(p, "%d", i);
    printf("%s\n", p);
    sleep(1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b7 <main+0x47>
lea    0xe5f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
mov    0x2e29(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x1000,%esi
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    $0xffffffff,%eax
jne    1204 <main+0x94>
lea    0xe20(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x3e8,-0x18(%rbp)
jge    1253 <main+0xe3>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    0xdfd(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x10(%rbp),%rsi
lea    0xdf0(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    120b <main+0x9b>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     120b <main+0x9b>
mov    0x2e37(%rip),%rcx
mov    (%rcx),%edx
mov    0x2e4e(%rip),%rcx
mov    (%rcx),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r14
cmp    $0xffffffff,%r14d
je     1221 <main+0xb1>
lea    0xe5b(%rip),%r15
lea    0xe57(%rip),%r12
nop
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1070 <sprintf@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
add    $0x1,%ebx
cmp    $0x3e8,%ebx
jne    11d0 <main+0x60>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdf5(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xded(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"buffer\00"
@O_RDWR = global i32 0
@.str.1 = [14 x i8] c"Error in open\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [14 x i8] c"Error in mmap\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i32, i32* @PROT_WRITE
%14 = load i32, i32* @MAP_SHARED
%15 = load i32, i32* %3
%16 = call i64 @mmap(i32 0, i32 4096, i32 %13, i32 %14, i32 %15, i32 0)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %2
%18 = load i8*, i8** %2
%19 = ptrtoint i8* %18 to i32
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %12
%22 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 2)
unreachable
24: ; preds = %12
store i32 0, i32* %4
br label %25
25: ; preds = %35, %24
%26 = load i32, i32* %4
%27 = icmp slt i32 %26, 1000
br i1 %27, label %28, label %38
28: ; preds = %25
%29 = load i8*, i8** %2
%30 = load i32, i32* %4
%31 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %30)
%32 = load i8*, i8** %2
%33 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %32)
%34 = call i32 @sleep(i32 1)
br label %35
35: ; preds = %28
%36 = load i32, i32* %4
%37 = add nsw i32 %36, 1
store i32 %37, i32* %4
br label %25
38: ; preds = %25
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@.str = [7 x i8] c"buffer\00"
@O_RDWR = global i32 0
@.str.1 = [14 x i8] c"Error in open\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [14 x i8] c"Error in mmap\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = load i32, i32* @PROT_WRITE
%9 = load i32, i32* @MAP_SHARED
%10 = tail call i64 @mmap(i32 0, i32 4096, i32 %8, i32 %9, i32 %2, i32 0)
%11 = inttoptr i64 %10 to i8*
%12 = trunc i64 %10 to i32
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %7
%15 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @exit(i32 2)
unreachable
17: ; preds = %7, %17
%18 = phi i32 [ %22, %17 ], [ 0, %7 ]
%19 = tail call i32 @sprintf(i8* dereferenceable(1) %11, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %11)
%21 = tail call i32 @sleep(i32 1)
%22 = add nuw nsw i32 %18, 1
%23 = icmp eq i32 %22, 1000
br i1 %23, label %24, label %17
24: ; preds = %17
ret i32 0
}