#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_ANON;
int MAP_PRIVATE;
int /*<<< orphan*/ PROT_NONE;
int /*<<< orphan*/ free(char *);
char getchar();
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
void *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ shared_memory_test();
int /*<<< orphan*/ thread_test(int);
int main(int argc, char *argv[]) {
  char *p[1000];
  char input_c;
  int i = 0, j = 0;
  void *array;
  while (1) {
    p[i] = malloc((1024 * 1024 * 3));
    if (p) {
      printf("malloc 3M done!\n");
    }
    input_c = getchar();
    switch (input_c) {
    case 'f':
      for (j = 0; j <= i; j++)
        free(p[j]);
      i = 0;
      printf("free all p\n");
      break;
    case 's':
      array = mmap(NULL, (1024 * 1024 * 3) * 5, PROT_NONE,
                   MAP_PRIVATE | MAP_ANON, -1, 0);
      if (array)
        printf("mmap 9M done!\n");
      i++;
      break;
    case 't':
      thread_test(100);
      printf("thread_test done\n");
      break;
    case 'm':
      shared_memory_test();
      break;
    case 'w':
      memset(p[i], 0, (1024 * 1024 * 3));
    default:
      i++;
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1f70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1f58(%rbp)
movl   $0x0,-0x1f5c(%rbp)
mov    $0x300000,%edi
call   10a0 <malloc@plt>
mov    %rax,%rcx
movslq -0x1f58(%rbp),%rax
mov    %rcx,-0x1f50(%rbp,%rax,8)
ff
lea    -0x1f50(%rbp),%rax
cmp    $0x0,%rax
je     11e6 <main+0x66>
lea    0xe67(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
call   1090 <getchar@plt>
mov    %al,-0x1f51(%rbp)
movsbl -0x1f51(%rbp),%eax
add    $0xffffff9a,%eax
mov    %eax,%ecx
mov    %rcx,-0x1f70(%rbp)
sub    $0x11,%eax
ja     132e <main+0x1ae>
mov    -0x1f70(%rbp),%rax
lea    0xde3(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x0,-0x1f5c(%rbp)
mov    -0x1f5c(%rbp),%eax
cmp    -0x1f58(%rbp),%eax
jg     126a <main+0xea>
movslq -0x1f5c(%rbp),%rax
mov    -0x1f50(%rbp,%rax,8),%rdi
ff
call   1030 <free@plt>
mov    -0x1f5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1f5c(%rbp)
jmp    1230 <main+0xb0>
movl   $0x0,-0x1f58(%rbp)
lea    0xdde(%rip),%rdi
call   1060 <printf@plt>
jmp    133d <main+0x1bd>
mov    0x2d64(%rip),%rax
mov    (%rax),%edx
mov    0x2d3b(%rip),%rax
mov    (%rax),%ecx
mov    0x2d3a(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xf00000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x1f68(%rbp)
cmpq   $0x0,-0x1f68(%rbp)
je     12d8 <main+0x158>
lea    0xd92(%rip),%rdi
call   1060 <printf@plt>
mov    -0x1f58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1f58(%rbp)
jmp    133d <main+0x1bd>
mov    $0x64,%edi
call   1050 <thread_test@plt>
lea    0xd77(%rip),%rdi
call   1060 <printf@plt>
jmp    133d <main+0x1bd>
mov    $0x0,%al
call   1070 <shared_memory_test@plt>
jmp    133d <main+0x1bd>
movslq -0x1f58(%rbp),%rax
mov    -0x1f50(%rbp,%rax,8),%rdi
ff
xor    %esi,%esi
mov    $0x300000,%edx
call   1080 <memset@plt>
mov    -0x1f58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1f58(%rbp)
jmp    11ad <main+0x2d>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1f48,%rsp
lea    0xeb0(%rip),%r13
lea    0xe61(%rip),%rbp
lea    0xeb2(%rip),%r12
jmp    11bc <main+0x3c>
nopl   0x0(%rax,%rax,1)
lea    0xec0(%rip),%rdi
call   1040 <puts@plt>
xor    %r15d,%r15d
movslq %r15d,%r14
jmp    11d7 <main+0x57>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1070 <shared_memory_test@plt>
mov    $0x300000,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
mov    %r13,%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1090 <getchar@plt>
movsbl %al,%eax
add    $0xffffff9a,%eax
cmp    $0x11,%eax
ja     1290 <main+0x110>
movslq 0x0(%rbp,%rax,4),%rax
add    %rbp,%rax
jmp    *%rax
mov    $0x64,%edi
call   1060 <thread_test@plt>
mov    %r12,%rdi
call   1040 <puts@plt>
jmp    11d7 <main+0x57>
mov    %rbx,(%rsp,%r14,8)
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
mov    0x2da4(%rip),%rax
mov    (%rax),%ecx
mov    0x2d93(%rip),%rax
or     (%rax),%ecx
xor    %edi,%edi
mov    $0xf00000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
test   %rax,%rax
je     1265 <main+0xe5>
lea    0xe09(%rip),%rdi
call   1040 <puts@plt>
add    $0x1,%r15d
jmp    11bf <main+0x3f>
xchg   %ax,%ax
mov    %rbx,(%rsp,%r14,8)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x300000,%edx
call   1080 <memset@plt>
add    $0x1,%r15d
jmp    11bf <main+0x3f>
nopl   0x0(%rax)
mov    %rbx,(%rsp,%r14,8)
add    $0x1,%r15d
jmp    11bf <main+0x3f>
nopl   (%rax)
mov    %rbx,(%rsp,%r14,8)
test   %r15d,%r15d
js     11b0 <main+0x30>
add    $0x1,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%rsp,%rbx,8),%rdi
call   1030 <free@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    12c0 <main+0x140>
jmp    11b0 <main+0x30>

LLVM IR (-O0):
@.str = [17 x i8] c"malloc 3M done!\0A\00"
@.str.1 = [12 x i8] c"free all p\0A\00"
@PROT_NONE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANON = global i32 0
@.str.2 = [15 x i8] c"mmap 9M done!\0A\00"
@.str.3 = [18 x i8] c"thread_test done\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1000 x i8*]
%7 = alloca i8
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
store i32 0, i32* %9
br label %11
11: ; preds = %2, %67
%12 = call i8* @malloc(i32 3145728)
%13 = load i32, i32* %8
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %6, i64 0, i64 %14
store i8* %12, i8** %15
%16 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %6, i64 0, i64 0
%17 = icmp ne i8** %16, null
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %11
%21 = call signext i8 (...) @getchar()
store i8 %21, i8* %7
%22 = load i8, i8* %7
%23 = sext i8 %22 to i32
switch i32 %23, label %64 [
i32 102, label %24
i32 115, label %40
i32 116, label %53
i32 109, label %56
i32 119, label %58
]
24: ; preds = %20
store i32 0, i32* %9
br label %25
25: ; preds = %35, %24
%26 = load i32, i32* %9
%27 = load i32, i32* %8
%28 = icmp sle i32 %26, %27
br i1 %28, label %29, label %38
29: ; preds = %25
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %6, i64 0, i64 %31
%33 = load i8*, i8** %32
%34 = call i32 @free(i8* %33)
br label %35
35: ; preds = %29
%36 = load i32, i32* %9
%37 = add nsw i32 %36, 1
store i32 %37, i32* %9
br label %25
38: ; preds = %25
store i32 0, i32* %8
%39 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %67
40: ; preds = %20
%41 = load i32, i32* @PROT_NONE
%42 = load i32, i32* @MAP_PRIVATE
%43 = load i32, i32* @MAP_ANON
%44 = or i32 %42, %43
%45 = call i8* @mmap(i32* null, i32 15728640, i32 %41, i32 %44, i32 -1, i32 0)
store i8* %45, i8** %10
%46 = load i8*, i8** %10
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %50
48: ; preds = %40
%49 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %50
50: ; preds = %48, %40
%51 = load i32, i32* %8
%52 = add nsw i32 %51, 1
store i32 %52, i32* %8
br label %67
53: ; preds = %20
%54 = call i32 @thread_test(i32 100)
%55 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %67
56: ; preds = %20
%57 = call i32 (...) @shared_memory_test()
br label %67
58: ; preds = %20
%59 = load i32, i32* %8
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %6, i64 0, i64 %60
%62 = load i8*, i8** %61
%63 = call i32 @memset(i8* %62, i32 0, i32 3145728)
br label %64
64: ; preds = %20, %58
%65 = load i32, i32* %8
%66 = add nsw i32 %65, 1
store i32 %66, i32* %8
br label %67
67: ; preds = %64, %56, %53, %50, %38
br label %11
}

LLVM IR (-O2):
@PROT_NONE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANON = global i32 0
@str = [16 x i8] c"malloc 3M done!\00"
@str.4 = [17 x i8] c"thread_test done\00"
@str.5 = [14 x i8] c"mmap 9M done!\00"
@str.6 = [11 x i8] c"free all p\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1000 x i8*]
%4 = bitcast [1000 x i8*]* %3 to i8*
br label %5
5: ; preds = %50, %2
%6 = phi i32 [ 0, %2 ], [ %51, %50 ]
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %3, i64 0, i64 %7
br label %9
9: ; preds = %44, %5
%10 = tail call dereferenceable_or_null(3145728) i8* @malloc(i32 3145728)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%12 = tail call signext i8 (...) @getchar()
%13 = sext i8 %12 to i32
switch i32 %13, label %47 [
i32 102, label %14
i32 115, label %28
i32 116, label %39
i32 109, label %42
i32 119, label %45
]
14: ; preds = %9
store i8* %10, i8** %8
%15 = icmp slt i32 %6, 0
br i1 %15, label %26, label %16
16: ; preds = %14
%17 = add nuw i32 %6, 1
%18 = zext i32 %17 to i64
br label %19
19: ; preds = %16, %19
%20 = phi i64 [ 0, %16 ], [ %24, %19 ]
%21 = getelementptr inbounds [1000 x i8*], [1000 x i8*]* %3, i64 0, i64 %20
%22 = load i8*, i8** %21
%23 = tail call i32 @free(i8* %22)
%24 = add nuw nsw i64 %20, 1
%25 = icmp eq i64 %24, %18
br i1 %25, label %26, label %19
26: ; preds = %19, %14
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.6, i64 0, i64 0))
br label %50
28: ; preds = %9
store i8* %10, i8** %8
%29 = load i32, i32* @PROT_NONE
%30 = load i32, i32* @MAP_PRIVATE
%31 = load i32, i32* @MAP_ANON
%32 = or i32 %31, %30
%33 = tail call i8* @mmap(i32* null, i32 15728640, i32 %29, i32 %32, i32 -1, i32 0)
%34 = icmp eq i8* %33, null
br i1 %34, label %37, label %35
35: ; preds = %28
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.5, i64 0, i64 0))
br label %37
37: ; preds = %35, %28
%38 = add nsw i32 %6, 1
br label %50
39: ; preds = %9
%40 = tail call i32 @thread_test(i32 100)
%41 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.4, i64 0, i64 0))
br label %44
42: ; preds = %9
%43 = tail call i32 (...) @shared_memory_test()
br label %44
44: ; preds = %42, %39
br label %9
45: ; preds = %9
store i8* %10, i8** %8
%46 = tail call i32 @memset(i8* %10, i32 0, i32 3145728)
br label %48
47: ; preds = %9
store i8* %10, i8** %8
br label %48
48: ; preds = %47, %45
%49 = add nsw i32 %6, 1
br label %50
50: ; preds = %48, %37, %26
%51 = phi i32 [ 0, %26 ], [ %38, %37 ], [ %49, %48 ]
br label %5
}