#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_PRIVATE;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ getchar();
scalar_t__ mmap(void *, int, int, int /*<<< orphan*/, int, int /*<<< orphan*/);
int munmap(unsigned char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
main() {
  int fd, i;
  unsigned char *ptr = NULL, *temp;
  fd = open("/dev/zero", O_RDWR);
  if (fd < 0) {
    perror("open ");
    exit(1);
  }
  ptr = (char *)mmap((void *)0, 60, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  if (ptr == NULL) {
    perror("Maping Failed");
    exit(1);
  }
  close(fd);
  getchar();
  temp = ptr;
  for (i = 0; i < 60; i++, ptr++)
    *ptr = (char)(i + 65);
  ptr = temp;
  for (i = 0; i < 60; i++, ptr++)
    printf("%c", *ptr);
  i = munmap(temp, 60);
  getchar();
  if (i != 0)
    printf(" failed to unmap\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe59(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11cf <main+0x4f>
lea    0xe4a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
mov    0x2df1(%rip),%rax
or     (%rax),%edx
mov    0x2de0(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x3c,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1224 <main+0xa4>
lea    0xdfb(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x3c,-0xc(%rbp)
jge    1274 <main+0xf4>
mov    -0xc(%rbp),%eax
add    $0x41,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1242 <main+0xc2>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x3c,-0xc(%rbp)
jge    12bc <main+0x13c>
mov    -0x18(%rbp),%rax
movzbl (%rax),%esi
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1283 <main+0x103>
mov    -0x20(%rbp),%rdi
mov    $0x3c,%esi
call   1070 <munmap@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1060 <getchar@plt>
cmpl   $0x0,-0xc(%rbp)
je     12ec <main+0x16c>
lea    0xd3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
lea    0xe8d(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1271 <main+0xe1>
mov    %eax,%ebp
mov    0x2e3f(%rip),%rax
mov    0x2e10(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dfd(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x3c,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
test   %rax,%rax
je     127a <main+0xea>
mov    %rax,%rbx
mov    %ebp,%edi
call   1060 <close@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <getchar@plt>
movaps 0xdfe(%rip),%xmm0
movups %xmm0,(%rbx)
movaps 0xe04(%rip),%xmm0
movups %xmm0,0x10(%rbx)
movaps 0xe09(%rip),%xmm0
movups %xmm0,0x20(%rbx)
movabs $0x7877767574737271,%rax
76 77 78
mov    %rax,0x30(%rbx)
movl   $0x7c7b7a79,0x38(%rbx)
movzbl (%rbx,%rbp,1),%edi
call   1030 <putchar@plt>
add    $0x1,%rbp
cmp    $0x3c,%ebp
jne    1230 <main+0xa0>
mov    %rbx,%rdi
mov    $0x3c,%esi
call   1080 <munmap@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getchar@plt>
test   %ebx,%ebx
je     1268 <main+0xd8>
lea    0xdeb(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdc2(%rip),%rdi
jmp    1281 <main+0xf1>
lea    0xdbf(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDWR = global i32 0
@.str.1 = [6 x i8] c"open \00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@.str.2 = [14 x i8] c"Maping Failed\00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [18 x i8] c" failed to unmap\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
store i32 0, i32* %1
store i8* null, i8** %4
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %14, %15
%17 = load i32, i32* @MAP_PRIVATE
%18 = load i32, i32* %2
%19 = call i64 @mmap(i8* null, i32 60, i32 %16, i32 %17, i32 %18, i32 0)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %13
%24 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %13
%27 = load i32, i32* %2
%28 = call i32 @close(i32 %27)
%29 = call i32 (...) @getchar()
%30 = load i8*, i8** %4
store i8* %30, i8** %5
store i32 0, i32* %3
br label %31
31: ; preds = %39, %26
%32 = load i32, i32* %3
%33 = icmp slt i32 %32, 60
br i1 %33, label %34, label %44
34: ; preds = %31
%35 = load i32, i32* %3
%36 = add nsw i32 %35, 65
%37 = trunc i32 %36 to i8
%38 = load i8*, i8** %4
store i8 %37, i8* %38
br label %39
39: ; preds = %34
%40 = load i32, i32* %3
%41 = add nsw i32 %40, 1
store i32 %41, i32* %3
%42 = load i8*, i8** %4
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %4
br label %31
44: ; preds = %31
%45 = load i8*, i8** %5
store i8* %45, i8** %4
store i32 0, i32* %3
br label %46
46: ; preds = %54, %44
%47 = load i32, i32* %3
%48 = icmp slt i32 %47, 60
br i1 %48, label %49, label %59
49: ; preds = %46
%50 = load i8*, i8** %4
%51 = load i8, i8* %50
%52 = zext i8 %51 to i32
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %49
%55 = load i32, i32* %3
%56 = add nsw i32 %55, 1
store i32 %56, i32* %3
%57 = load i8*, i8** %4
%58 = getelementptr inbounds i8, i8* %57, i32 1
store i8* %58, i8** %4
br label %46
59: ; preds = %46
%60 = load i8*, i8** %5
%61 = call i32 @munmap(i8* %60, i32 60)
store i32 %61, i32* %3
%62 = call i32 (...) @getchar()
%63 = load i32, i32* %3
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %59
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %67
67: ; preds = %65, %59
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDWR = global i32 0
@.str.1 = [6 x i8] c"open \00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@.str.2 = [14 x i8] c"Maping Failed\00"
@str = [17 x i8] c" failed to unmap\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 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* @PROT_WRITE
%10 = or i32 %9, %8
%11 = load i32, i32* @MAP_PRIVATE
%12 = tail call i64 @mmap(i8* null, i32 60, i32 %10, i32 %11, i32 %2, i32 0)
%13 = inttoptr i64 %12 to i8*
%14 = icmp eq i64 %12, 0
br i1 %14, label %15, label %18
15: ; preds = %7
%16 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %7
%19 = tail call i32 @close(i32 %2)
%20 = tail call i32 (...) @getchar()
%21 = getelementptr i8, i8* %13, i64 48
%22 = inttoptr i64 %12 to <16 x i8>*
store <16 x i8> <i8 65, i8 66, i8 67, i8 68, i8 69, i8 70, i8 71, i8 72, i8 73, i8 74, i8 75, i8 76, i8 77, i8 78, i8 79, i8 80>, <16 x i8>* %22
%23 = getelementptr i8, i8* %13, i64 16
%24 = bitcast i8* %23 to <16 x i8>*
store <16 x i8> <i8 81, i8 82, i8 83, i8 84, i8 85, i8 86, i8 87, i8 88, i8 89, i8 90, i8 91, i8 92, i8 93, i8 94, i8 95, i8 96>, <16 x i8>* %24
%25 = getelementptr i8, i8* %13, i64 32
%26 = bitcast i8* %25 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>* %26
store i8 113, i8* %21
%27 = getelementptr i8, i8* %13, i64 49
store i8 114, i8* %27
%28 = getelementptr i8, i8* %13, i64 50
store i8 115, i8* %28
%29 = getelementptr i8, i8* %13, i64 51
store i8 116, i8* %29
%30 = getelementptr i8, i8* %13, i64 52
store i8 117, i8* %30
%31 = getelementptr i8, i8* %13, i64 53
store i8 118, i8* %31
%32 = getelementptr i8, i8* %13, i64 54
store i8 119, i8* %32
%33 = getelementptr i8, i8* %13, i64 55
store i8 120, i8* %33
%34 = getelementptr i8, i8* %13, i64 56
store i8 121, i8* %34
%35 = getelementptr i8, i8* %13, i64 57
store i8 122, i8* %35
%36 = getelementptr i8, i8* %13, i64 58
store i8 123, i8* %36
%37 = getelementptr i8, i8* %13, i64 59
store i8 124, i8* %37
br label %38
38: ; preds = %18, %38
%39 = phi i32 [ %44, %38 ], [ 0, %18 ]
%40 = phi i8* [ %45, %38 ], [ %13, %18 ]
%41 = load i8, i8* %40
%42 = zext i8 %41 to i32
%43 = tail call i32 @putchar(i32 %42)
%44 = add nuw nsw i32 %39, 1
%45 = getelementptr inbounds i8, i8* %40, i64 1
%46 = icmp eq i32 %44, 60
br i1 %46, label %47, label %38
47: ; preds = %38
%48 = tail call i32 @munmap(i8* %13, i32 60)
%49 = tail call i32 (...) @getchar()
%50 = icmp eq i32 %48, 0
br i1 %50, label %53, label %51
51: ; preds = %47
%52 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %53
53: ; preds = %51, %47
ret i32 0
}