#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 */
struct stat {
  int /*<<< orphan*/ st_size;
};

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char, char, char);
int /*<<< orphan*/ sleep(int);
int main() {
  int fd = open("file", O_RDWR);
  if (fd == -1) {
    perror("open");
    return 1;
  }
  struct stat s;
  fstat(fd, &s);
  char *p = mmap(NULL, s.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (fork() == 0) {
    int i;
    for (i = 0; i < 3; i++)
      p[i] ^= 1 << 5;
    sleep(5);
  } else {
    int i;
    sleep(2);
    printf("%c%c%c\n", p[0], p[1], p[2]);
    for (i = 5; i < 9; i++)
      p[i] ^= 1 << 5;
  }
  munmap(p, s.st_size);
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
lea    0xe51(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11d9 <main+0x49>
lea    0xe3d(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e3 <main+0x153>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   10a0 <fstat@plt>
mov    -0x10(%rbp),%esi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
mov    0x2dd0(%rip),%rax
or     (%rax),%edx
mov    0x2de7(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
jne    1268 <main+0xd8>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x3,-0x1c(%rbp)
jge    1259 <main+0xc9>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
xor    $0x20,%edx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    122f <main+0x9f>
mov    $0x5,%edi
call   1090 <sleep@plt>
jmp    12cf <main+0x13f>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%dl
mov    -0x18(%rbp),%rax
mov    0x1(%rax),%cl
mov    -0x18(%rbp),%rax
lea    0xd80(%rip),%rdi
movsbl %dl,%esi
movsbl %cl,%edx
movsbl 0x2(%rax),%ecx
call   1040 <printf@plt>
movl   $0x5,-0x20(%rbp)
cmpl   $0x9,-0x20(%rbp)
jge    12ca <main+0x13a>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
xor    $0x20,%edx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    12a0 <main+0x110>
jmp    12cf <main+0x13f>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1060 <munmap@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    0xe58(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     126a <main+0xda>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
mov    0x8(%rsp),%r14d
mov    0x2e28(%rip),%rax
mov    0x2df1(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e06(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     1237 <main+0xa7>
mov    $0x2,%edi
call   1090 <sleep@plt>
movsbl 0x0(%rbp),%esi
movsbl 0x1(%rbp),%edx
movsbl 0x2(%rbp),%ecx
lea    0xdea(%rip),%rdi
call   1040 <printf@plt>
xorb   $0x20,0x5(%rbp)
xorb   $0x20,0x6(%rbp)
xorb   $0x20,0x7(%rbp)
xorb   $0x20,0x8(%rbp)
jmp    124d <main+0xbd>
xorb   $0x20,0x0(%rbp)
xorb   $0x20,0x1(%rbp)
xorb   $0x20,0x2(%rbp)
mov    $0x5,%edi
call   1090 <sleep@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1060 <munmap@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd94(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    1261 <main+0xd1>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [5 x i8] c"file\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [8 x i8] c"%c%c%c\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.stat
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %79
13: ; preds = %0
%14 = load i32, i32* %2
%15 = call i32 @fstat(i32 %14, %struct.stat* %3)
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @PROT_WRITE
%20 = or i32 %18, %19
%21 = load i32, i32* @MAP_SHARED
%22 = load i32, i32* %2
%23 = call i8* @mmap(i32* null, i32 %17, i32 %20, i32 %21, i32 %22, i32 0)
store i8* %23, i8** %4
%24 = call i64 (...) @fork()
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %44
26: ; preds = %13
store i32 0, i32* %5
br label %27
27: ; preds = %39, %26
%28 = load i32, i32* %5
%29 = icmp slt i32 %28, 3
br i1 %29, label %30, label %42
30: ; preds = %27
%31 = load i8*, i8** %4
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = xor i32 %36, 32
%38 = trunc i32 %37 to i8
store i8 %38, i8* %34
br label %39
39: ; preds = %30
%40 = load i32, i32* %5
%41 = add nsw i32 %40, 1
store i32 %41, i32* %5
br label %27
42: ; preds = %27
%43 = call i32 @sleep(i32 5)
br label %72
44: ; preds = %13
%45 = call i32 @sleep(i32 2)
%46 = load i8*, i8** %4
%47 = getelementptr inbounds i8, i8* %46, i64 0
%48 = load i8, i8* %47
%49 = load i8*, i8** %4
%50 = getelementptr inbounds i8, i8* %49, i64 1
%51 = load i8, i8* %50
%52 = load i8*, i8** %4
%53 = getelementptr inbounds i8, i8* %52, i64 2
%54 = load i8, i8* %53
%55 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8 signext %48, i8 signext %51, i8 signext %54)
store i32 5, i32* %6
br label %56
56: ; preds = %68, %44
%57 = load i32, i32* %6
%58 = icmp slt i32 %57, 9
br i1 %58, label %59, label %71
59: ; preds = %56
%60 = load i8*, i8** %4
%61 = load i32, i32* %6
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
%64 = load i8, i8* %63
%65 = sext i8 %64 to i32
%66 = xor i32 %65, 32
%67 = trunc i32 %66 to i8
store i8 %67, i8* %63
br label %68
68: ; preds = %59
%69 = load i32, i32* %6
%70 = add nsw i32 %69, 1
store i32 %70, i32* %6
br label %56
71: ; preds = %56
br label %72
72: ; preds = %71, %42
%73 = load i8*, i8** %4
%74 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%75 = load i32, i32* %74
%76 = call i32 @munmap(i8* %73, i32 %75)
%77 = load i32, i32* %2
%78 = call i32 @close(i32 %77)
br label %79
79: ; preds = %72, %11
%80 = load i32, i32* %1
ret i32 %80
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"file\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [8 x i8] c"%c%c%c\0A\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %52
7: ; preds = %0
%8 = bitcast %struct.stat* %1 to i8*
%9 = call i32 @fstat(i32 %3, %struct.stat* %1)
%10 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%11 = load i32, i32* %10
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %13, %12
%15 = load i32, i32* @MAP_SHARED
%16 = tail call i8* @mmap(i32* null, i32 %11, i32 %14, i32 %15, i32 %3, i32 0)
%17 = tail call i64 (...) @fork()
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %29
19: ; preds = %7
%20 = load i8, i8* %16
%21 = xor i8 %20, 32
store i8 %21, i8* %16
%22 = getelementptr inbounds i8, i8* %16, i64 1
%23 = load i8, i8* %22
%24 = xor i8 %23, 32
store i8 %24, i8* %22
%25 = getelementptr inbounds i8, i8* %16, i64 2
%26 = load i8, i8* %25
%27 = xor i8 %26, 32
store i8 %27, i8* %25
%28 = tail call i32 @sleep(i32 5)
br label %49
29: ; preds = %7
%30 = tail call i32 @sleep(i32 2)
%31 = load i8, i8* %16
%32 = getelementptr inbounds i8, i8* %16, i64 1
%33 = load i8, i8* %32
%34 = getelementptr inbounds i8, i8* %16, i64 2
%35 = load i8, i8* %34
%36 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8 signext %31, i8 signext %33, i8 signext %35)
%37 = getelementptr inbounds i8, i8* %16, i64 5
%38 = load i8, i8* %37
%39 = xor i8 %38, 32
store i8 %39, i8* %37
%40 = getelementptr inbounds i8, i8* %16, i64 6
%41 = load i8, i8* %40
%42 = xor i8 %41, 32
store i8 %42, i8* %40
%43 = getelementptr inbounds i8, i8* %16, i64 7
%44 = load i8, i8* %43
%45 = xor i8 %44, 32
store i8 %45, i8* %43
%46 = getelementptr inbounds i8, i8* %16, i64 8
%47 = load i8, i8* %46
%48 = xor i8 %47, 32
store i8 %48, i8* %46
br label %49
49: ; preds = %29, %19
%50 = tail call i32 @munmap(i8* %16, i32 %11)
%51 = tail call i32 @close(i32 %3)
br label %52
52: ; preds = %49, %5
%53 = phi i32 [ 1, %5 ], [ 0, %49 ]
ret i32 %53
}