#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_READ;
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 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_READ, MAP_SHARED, fd, 0);
  if ((int)p == -1) {
    perror("Error in mmap");
    exit(2);
  }
  for (i = 0; i < 1000; i++) {
    printf("%s\n", p);
    sleep(3);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a7 <main+0x47>
lea    0xe6f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
mov    0x2e31(%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    11f4 <main+0x94>
lea    0xe30(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x3e8,-0x18(%rbp)
jge    1230 <main+0xd0>
mov    -0x10(%rbp),%rsi
lea    0xe10(%rip),%rdi
call   1040 <printf@plt>
mov    $0x3,%edi
call   1080 <sleep@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11fb <main+0x9b>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
lea    0xe8c(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11e1 <main+0x81>
mov    0x2e73(%rip),%rcx
mov    (%rcx),%edx
mov    0x2e5a(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
cmp    $0xffffffff,%ebx
je     11f7 <main+0x97>
mov    $0x3e8,%ebp
lea    0xe6d(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    $0x3,%edi
call   1080 <sleep@plt>
add    $0xffffffff,%ebp
jne    11c0 <main+0x60>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe1f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe17(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%edi
call   1070 <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_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [14 x i8] c"Error in mmap\00"
@.str.3 = [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_READ
%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 = %32, %24
%26 = load i32, i32* %4
%27 = icmp slt i32 %26, 1000
br i1 %27, label %28, label %35
28: ; preds = %25
%29 = load i8*, i8** %2
%30 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %29)
%31 = call i32 @sleep(i32 3)
br label %32
32: ; preds = %28
%33 = load i32, i32* %4
%34 = add nsw i32 %33, 1
store i32 %34, i32* %4
br label %25
35: ; preds = %25
%36 = load i32, i32* %1
ret i32 %36
}

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_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [14 x i8] c"Error in mmap\00"
@.str.3 = [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_READ
%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 [ %21, %17 ], [ 0, %7 ]
%19 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %11)
%20 = tail call i32 @sleep(i32 3)
%21 = add nuw nsw i32 %18, 1
%22 = icmp eq i32 %21, 1000
br i1 %22, label %23, label %17
23: ; preds = %17
ret i32 0
}