#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 MAP_ANONYMOUS;
void *MAP_FAILED;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
void *mmap(void *, int, int, int, int /*<<< orphan*/, int /*<<< orphan*/);
void *mremap(void *, int, int, int /*<<< orphan*/);
scalar_t__ munmap(void *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcpy(void *, char *);
int main(void) {
  void *initial_area = mmap((void *)0x10000000, 40960, PROT_READ | PROT_WRITE,
                            MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  if (initial_area == MAP_FAILED)
    perror("initial area");
  printf("initial_area= %p\n", initial_area);
  if (munmap(initial_area, 40960) != 0)
    perror("munmap initial_area");
  void *area = mmap(initial_area, 4096, PROT_READ | PROT_WRITE,
                    MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  if (area == MAP_FAILED)
    perror("area");
  if (area != initial_area)
    printf("FAILED : was expecting to get back the initial_area\n");
  printf("area= %p\n", area);
  strcpy(area, "Hello World");
  void *a2 = mremap(area, 4096, 40960, 0);
  if (a2 == MAP_FAILED) {
    perror("mremap");
  }
  if (a2 != initial_area)
    printf("FAILED : was expecting to get back the same area increased\n");
  printf("increased area= %p\n", a2);
  printf("%s\n", (char *)a2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edx
mov    0x2e51(%rip),%rax
or     (%rax),%edx
mov    0x2e58(%rip),%rax
mov    (%rax),%ecx
mov    0x2e37(%rip),%rax
or     (%rax),%ecx
mov    $0x10000000,%edi
mov    $0xa000,%esi
xor    %r9d,%r9d
mov    %r9d,%r8d
call   1040 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e09(%rip),%rcx
cmp    (%rcx),%rax
jne    11cc <main+0x6c>
lea    0xe39(%rip),%rdi
call   1080 <perror@plt>
mov    -0x10(%rbp),%rsi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0xa000,%esi
call   1070 <munmap@plt>
cmp    $0x0,%rax
je     1202 <main+0xa2>
lea    0xe22(%rip),%rdi
call   1080 <perror@plt>
mov    -0x10(%rbp),%rdi
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
mov    0x2dba(%rip),%rax
or     (%rax),%edx
mov    0x2dc1(%rip),%rax
mov    (%rax),%ecx
mov    0x2da0(%rip),%rax
or     (%rax),%ecx
mov    $0x1000,%esi
xor    %r9d,%r9d
mov    %r9d,%r8d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d77(%rip),%rcx
cmp    (%rcx),%rax
jne    125e <main+0xfe>
lea    0xdaf(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     127a <main+0x11a>
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xdd1(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    $0x1000,%esi
mov    $0xa000,%edx
xor    %ecx,%ecx
call   1060 <mremap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d00(%rip),%rcx
cmp    (%rcx),%rax
jne    12d5 <main+0x175>
lea    0xda4(%rip),%rdi
call   1080 <perror@plt>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     12f1 <main+0x191>
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e76(%rip),%r12
mov    0x2e47(%rip),%rbx
mov    (%rbx),%edx
or     (%r12),%edx
mov    0x2e4a(%rip),%r13
mov    0x2e2b(%rip),%rbp
mov    0x0(%rbp),%ecx
or     0x0(%r13),%ecx
mov    $0x10000000,%edi
mov    $0xa000,%esi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%r14
mov    0x2dfd(%rip),%r15
cmp    (%r15),%rax
je     12a7 <main+0x137>
lea    0xe3a(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
mov    $0xa000,%esi
call   1080 <munmap@plt>
test   %rax,%rax
jne    12b8 <main+0x148>
mov    (%rbx),%edx
or     (%r12),%edx
mov    0x0(%rbp),%ecx
or     0x0(%r13),%ecx
mov    %r14,%rdi
mov    $0x1000,%esi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
cmp    (%r15),%rax
je     12c9 <main+0x159>
cmp    %r14,%rbx
je     1230 <main+0xc0>
lea    0xe6a(%rip),%rdi
call   1040 <puts@plt>
lea    0xe19(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdeb(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    $0xa000,%edx
xor    %ecx,%ecx
call   1070 <mremap@plt>
mov    %rax,%rbx
cmp    (%r15),%rax
je     12e3 <main+0x173>
cmp    %r14,%rbx
je     127d <main+0x10d>
lea    0xde2(%rip),%rdi
call   1040 <puts@plt>
lea    0xdc2(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd52(%rip),%rdi
call   1090 <perror@plt>
jmp    11cc <main+0x5c>
lea    0xd60(%rip),%rdi
call   1090 <perror@plt>
jmp    11f3 <main+0x83>
lea    0xd38(%rip),%rdi
call   1090 <perror@plt>
cmp    %r14,%rbx
jne    1224 <main+0xb4>
jmp    1230 <main+0xc0>
lea    0xd55(%rip),%rdi
call   1090 <perror@plt>
cmp    %r14,%rbx
jne    1271 <main+0x101>
jmp    127d <main+0x10d>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str = [13 x i8] c"initial area\00"
@.str.1 = [18 x i8] c"initial_area= %p\0A\00"
@.str.2 = [20 x i8] c"munmap initial_area\00"
@.str.3 = [5 x i8] c"area\00"
@.str.4 = [53 x i8] c"FAILED : was expecting to get back the initial_area\0A\00"
@.str.5 = [10 x i8] c"area= %p\0A\00"
@.str.6 = [12 x i8] c"Hello World\00"
@.str.7 = [7 x i8] c"mremap\00"
@.str.8 = [60 x i8] c"FAILED : was expecting to get back the same area increased\0A\00"
@.str.9 = [20 x i8] c"increased area= %p\0A\00"
@.str.10 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i32 0, i32* %1
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @PROT_WRITE
%7 = or i32 %5, %6
%8 = load i32, i32* @MAP_ANONYMOUS
%9 = load i32, i32* @MAP_PRIVATE
%10 = or i32 %8, %9
%11 = call i8* @mmap(i8* inttoptr (i64 268435456 to i8*), i32 40960, i32 %7, i32 %10, i32 0, i32 0)
store i8* %11, i8** %2
%12 = load i8*, i8** %2
%13 = load i8*, i8** @MAP_FAILED
%14 = icmp eq i8* %12, %13
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %0
%18 = load i8*, i8** %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i8*, i8** %2
%21 = call i64 @munmap(i8* %20, i32 40960)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %17
%26 = load i8*, i8** %2
%27 = load i32, i32* @PROT_READ
%28 = load i32, i32* @PROT_WRITE
%29 = or i32 %27, %28
%30 = load i32, i32* @MAP_ANONYMOUS
%31 = load i32, i32* @MAP_PRIVATE
%32 = or i32 %30, %31
%33 = call i8* @mmap(i8* %26, i32 4096, i32 %29, i32 %32, i32 0, i32 0)
store i8* %33, i8** %3
%34 = load i8*, i8** %3
%35 = load i8*, i8** @MAP_FAILED
%36 = icmp eq i8* %34, %35
br i1 %36, label %37, label %39
37: ; preds = %25
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %25
%40 = load i8*, i8** %3
%41 = load i8*, i8** %2
%42 = icmp ne i8* %40, %41
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %43, %39
%46 = load i8*, i8** %3
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %46)
%48 = load i8*, i8** %3
%49 = call i32 @strcpy(i8* %48, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%50 = load i8*, i8** %3
%51 = call i8* @mremap(i8* %50, i32 4096, i32 40960, i32 0)
store i8* %51, i8** %4
%52 = load i8*, i8** %4
%53 = load i8*, i8** @MAP_FAILED
%54 = icmp eq i8* %52, %53
br i1 %54, label %55, label %57
55: ; preds = %45
%56 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %57
57: ; preds = %55, %45
%58 = load i8*, i8** %4
%59 = load i8*, i8** %2
%60 = icmp ne i8* %58, %59
br i1 %60, label %61, label %63
61: ; preds = %57
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.8, i64 0, i64 0))
br label %63
63: ; preds = %61, %57
%64 = load i8*, i8** %4
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0), i8* %64)
%66 = load i8*, i8** %4
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.10, i64 0, i64 0), i8* %66)
ret i32 0
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str = [13 x i8] c"initial area\00"
@.str.1 = [18 x i8] c"initial_area= %p\0A\00"
@.str.2 = [20 x i8] c"munmap initial_area\00"
@.str.3 = [5 x i8] c"area\00"
@.str.5 = [10 x i8] c"area= %p\0A\00"
@.str.6 = [12 x i8] c"Hello World\00"
@.str.7 = [7 x i8] c"mremap\00"
@.str.9 = [20 x i8] c"increased area= %p\0A\00"
@str = [59 x i8] c"FAILED : was expecting to get back the same area increased\00"
@str.11 = [52 x i8] c"FAILED : was expecting to get back the initial_area\00"
define i32 @main() {
%1 = load i32, i32* @PROT_READ
%2 = load i32, i32* @PROT_WRITE
%3 = or i32 %2, %1
%4 = load i32, i32* @MAP_ANONYMOUS
%5 = load i32, i32* @MAP_PRIVATE
%6 = or i32 %5, %4
%7 = tail call i8* @mmap(i8* inttoptr (i64 268435456 to i8*), i32 40960, i32 %3, i32 %6, i32 0, i32 0)
%8 = load i8*, i8** @MAP_FAILED
%9 = icmp eq i8* %7, %8
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%14 = tail call i64 @munmap(i8* %7, i32 40960)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_WRITE
%21 = or i32 %20, %19
%22 = load i32, i32* @MAP_ANONYMOUS
%23 = load i32, i32* @MAP_PRIVATE
%24 = or i32 %23, %22
%25 = tail call i8* @mmap(i8* %7, i32 4096, i32 %21, i32 %24, i32 0, i32 0)
%26 = load i8*, i8** @MAP_FAILED
%27 = icmp eq i8* %25, %26
br i1 %27, label %28, label %30
28: ; preds = %18
%29 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %28, %18
%31 = icmp eq i8* %25, %7
br i1 %31, label %34, label %32
32: ; preds = %30
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str.11, i64 0, i64 0))
br label %34
34: ; preds = %32, %30
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %25)
%36 = tail call i32 @strcpy(i8* %25, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%37 = tail call i8* @mremap(i8* %25, i32 4096, i32 40960, i32 0)
%38 = load i8*, i8** @MAP_FAILED
%39 = icmp eq i8* %37, %38
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %42
42: ; preds = %40, %34
%43 = icmp eq i8* %37, %7
br i1 %43, label %46, label %44
44: ; preds = %42
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @str, i64 0, i64 0))
br label %46
46: ; preds = %44, %42
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0), i8* %37)
%48 = tail call i32 @puts(i8* dereferenceable(1) %37)
ret i32 0
}