#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 */
char *MAP_FAILED;
int MAP_FILE;
int MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
scalar_t__ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ ftruncate(int, int);
char *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sleep(int);
int main(void) {
  char c;
  int err, shmid;
  char *shm, *s;
  if ((shmid = open("/tmp/mymemory", O_RDWR | O_CREAT, 0666)) < 0) {
    perror("open");
    exit(1);
  }
  if (ftruncate(shmid, 27) < 0) {
    perror("ftruncate");
    exit(1);
  }
  if ((shm = mmap(NULL, 27, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED,
                  shmid, 0)) == MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  s = shm;
  for (c = 'a'; c <= 'z'; c++) {
    *s++ = c;
  }
  *s = '\0';
  while (*shm != '*') {
    sleep(1);
  }
  if (close(shmid) < 0) {
    perror("close");
    exit(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    0x2e69(%rip),%rax
or     (%rax),%esi
lea    0xe68(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    11c4 <main+0x54>
lea    0xe59(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%edi
mov    $0x1b,%esi
call   1040 <ftruncate@plt>
cmp    $0x0,%rax
jge    11f1 <main+0x81>
lea    0xe31(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
mov    0x2db7(%rip),%rax
or     (%rax),%edx
mov    0x2dbe(%rip),%rax
mov    (%rax),%ecx
mov    0x2dcd(%rip),%rax
or     (%rax),%ecx
mov    -0x10(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1b,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    0x2d7b(%rip),%rcx
cmp    (%rcx),%rax
jne    1254 <main+0xe4>
lea    0xdd8(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movb   $0x61,-0x5(%rbp)
movsbl -0x5(%rbp),%eax
cmp    $0x7a,%eax
jg     128e <main+0x11e>
mov    -0x5(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x5(%rbp),%al
add    $0x1,%al
mov    %al,-0x5(%rbp)
jmp    1260 <main+0xf0>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2a,%eax
je     12b4 <main+0x144>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    1295 <main+0x125>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    12dc <main+0x16c>
lea    0xd55(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e56(%rip),%rax
mov    0x2e77(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe84(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
test   %eax,%eax
jns    11a6 <main+0x36>
lea    0xe7d(%rip),%rdi
jmp    1262 <main+0xf2>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x1b,%esi
call   1040 <ftruncate@plt>
test   %rax,%rax
jns    11c5 <main+0x55>
lea    0xe63(%rip),%rdi
jmp    1262 <main+0xf2>
mov    0x2e24(%rip),%rax
mov    0x2de5(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dea(%rip),%rax
mov    0x2dfb(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x1b,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2dab(%rip),%rax
cmp    (%rax),%rbx
jne    1213 <main+0xa3>
lea    0xe1c(%rip),%rdi
jmp    1262 <main+0xf2>
movaps 0xde6(%rip),%xmm0
movups %xmm0,(%rbx)
movabs $0x7877767574737271,%rax
76 77 78
mov    %rax,0x10(%rbx)
movw   $0x7a79,0x18(%rbx)
movb   $0x0,0x1a(%rbx)
cmpb   $0x2a,(%rbx)
je     124f <main+0xdf>
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1090 <sleep@plt>
cmpb   $0x2a,(%rbx)
jne    1240 <main+0xd0>
mov    %ebp,%edi
call   1050 <close@plt>
test   %rax,%rax
jns    1271 <main+0x101>
lea    0xdd0(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/tmp/mymemory\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_FILE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [6 x i8] c"close\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i32 0, i32* %1
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %9, i32 438)
store i32 %10, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = load i32, i32* %4
%17 = call i64 @ftruncate(i32 %16, i32 27)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%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 = %15
%23 = load i32, i32* @PROT_READ
%24 = load i32, i32* @PROT_WRITE
%25 = or i32 %23, %24
%26 = load i32, i32* @MAP_FILE
%27 = load i32, i32* @MAP_SHARED
%28 = or i32 %26, %27
%29 = load i32, i32* %4
%30 = call i8* @mmap(i32* null, i32 27, i32 %25, i32 %28, i32 %29, i32 0)
store i8* %30, i8** %5
%31 = load i8*, i8** @MAP_FAILED
%32 = icmp eq i8* %30, %31
br i1 %32, label %33, label %36
33: ; preds = %22
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %22
%37 = load i8*, i8** %5
store i8* %37, i8** %6
store i8 97, i8* %2
br label %38
38: ; preds = %46, %36
%39 = load i8, i8* %2
%40 = sext i8 %39 to i32
%41 = icmp sle i32 %40, 122
br i1 %41, label %42, label %49
42: ; preds = %38
%43 = load i8, i8* %2
%44 = load i8*, i8** %6
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %6
store i8 %43, i8* %44
br label %46
46: ; preds = %42
%47 = load i8, i8* %2
%48 = add i8 %47, 1
store i8 %48, i8* %2
br label %38
49: ; preds = %38
%50 = load i8*, i8** %6
store i8 0, i8* %50
br label %51
51: ; preds = %56, %49
%52 = load i8*, i8** %5
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp ne i32 %54, 42
br i1 %55, label %56, label %58
56: ; preds = %51
%57 = call i32 @sleep(i32 1)
br label %51
58: ; preds = %51
%59 = load i32, i32* %4
%60 = call i64 @close(i32 %59)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %58
%63 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %58
%66 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [14 x i8] c"/tmp/mymemory\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_FILE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [6 x i8] c"close\00"
define i32 @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 ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %3, i32 438)
%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 27)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
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_FILE
%20 = load i32, i32* @MAP_SHARED
%21 = or i32 %20, %19
%22 = tail call i8* @mmap(i32* null, i32 27, i32 %18, i32 %21, i32 %4, i32 0)
%23 = load i8*, i8** @MAP_FAILED
%24 = icmp eq i8* %22, %23
br i1 %24, label %40, label %25
25: ; preds = %15
%26 = getelementptr inbounds i8, i8* %22, i64 16
%27 = bitcast i8* %22 to <16 x i8>*
store <16 x i8> <i8 97, i8 98, i8 99, i8 100, i8 101, i8 102, i8 103, i8 104, i8 105, i8 106, i8 107, i8 108, i8 109, i8 110, i8 111, i8 112>, <16 x i8>* %27
%28 = getelementptr inbounds i8, i8* %22, i64 17
store i8 113, i8* %26
%29 = getelementptr inbounds i8, i8* %22, i64 18
store i8 114, i8* %28
%30 = getelementptr inbounds i8, i8* %22, i64 19
store i8 115, i8* %29
%31 = getelementptr inbounds i8, i8* %22, i64 20
store i8 116, i8* %30
%32 = getelementptr inbounds i8, i8* %22, i64 21
store i8 117, i8* %31
%33 = getelementptr inbounds i8, i8* %22, i64 22
store i8 118, i8* %32
%34 = getelementptr inbounds i8, i8* %22, i64 23
store i8 119, i8* %33
%35 = getelementptr inbounds i8, i8* %22, i64 24
store i8 120, i8* %34
%36 = getelementptr inbounds i8, i8* %22, i64 25
store i8 121, i8* %35
%37 = getelementptr inbounds i8, i8* %22, i64 26
store i8 122, i8* %36
store i8 0, i8* %37
%38 = load i8, i8* %22
%39 = icmp eq i8 %38, 42
br i1 %39, label %47, label %43
40: ; preds = %15
%41 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%42 = tail call i32 @exit(i32 1)
unreachable
43: ; preds = %25, %43
%44 = tail call i32 @sleep(i32 1)
%45 = load i8, i8* %22
%46 = icmp eq i8 %45, 42
br i1 %46, label %47, label %43
47: ; preds = %43, %25
%48 = tail call i64 @close(i32 %4)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%52 = tail call i32 @exit(i32 1)
unreachable
53: ; preds = %47
%54 = tail call i32 @exit(i32 0)
unreachable
}