#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;
char *MAP_FAILED;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ memset(char *, int, int);
char *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int fill_mem(int mb_count) {
  int i, j;
  char *ptr = NULL;
  for (j = 0; j < mb_count; j++) {
    do {
      ptr = mmap(NULL, (1024 * 1024) * sizeof(char), PROT_READ | PROT_WRITE,
                 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
    } while (ptr == MAP_FAILED);
    memset(ptr, 1, (1024 * 1024) * sizeof(char));
  }
  return 0;
}


x86 assembly (clang, -O0):
<fill_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11bf <fill_mem+0x9f>
jmp    114b <fill_mem+0x2b>
mov    0x2ea6(%rip),%rax
mov    (%rax),%edx
mov    0x2e75(%rip),%rax
or     (%rax),%edx
mov    0x2e7c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e5b(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x100000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%rax
je     114b <fill_mem+0x2b>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
mov    $0x100000,%edx
call   1040 <memset@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    113a <fill_mem+0x1a>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fill_mem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,0x4(%rsp)
test   %edi,%edi
jle    11a4 <fill_mem+0x84>
xor    %r15d,%r15d
mov    0x2ebb(%rip),%r12
mov    0x2e8c(%rip),%r13
mov    0x2e95(%rip),%rbp
mov    0x2e76(%rip),%rbx
mov    0x2e67(%rip),%r14
nopl   0x0(%rax)
mov    0x0(%r13),%edx
or     (%r12),%edx
mov    (%rbx),%ecx
or     0x0(%rbp),%ecx
xor    %edi,%edi
mov    $0x100000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    (%r14),%rax
je     1160 <fill_mem+0x40>
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x100000,%edx
call   1040 <memset@plt>
add    $0x1,%r15d
cmp    0x4(%rsp),%r15d
jne    1160 <fill_mem+0x40>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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
define i32 @fill_mem(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %2
store i8* null, i8** %5
store i32 0, i32* %4
br label %6
6: ; preds = %26, %1
%7 = load i32, i32* %4
%8 = load i32, i32* %2
%9 = icmp slt i32 %7, %8
br i1 %9, label %10, label %29
10: ; preds = %6
br label %11
11: ; preds = %19, %10
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %12, %13
%15 = load i32, i32* @MAP_ANONYMOUS
%16 = load i32, i32* @MAP_PRIVATE
%17 = or i32 %15, %16
%18 = call i8* @mmap(i32* null, i32 1048576, i32 %14, i32 %17, i32 -1, i32 0)
store i8* %18, i8** %5
br label %19
19: ; preds = %11
%20 = load i8*, i8** %5
%21 = load i8*, i8** @MAP_FAILED
%22 = icmp eq i8* %20, %21
br i1 %22, label %11, label %23
23: ; preds = %19
%24 = load i8*, i8** %5
%25 = call i32 @memset(i8* %24, i32 1, i32 1048576)
br label %26
26: ; preds = %23
%27 = load i32, i32* %4
%28 = add nsw i32 %27, 1
store i32 %28, i32* %4
br label %6
29: ; preds = %6
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
define i32 @fill_mem(i32 %0) {
%2 = icmp sgt i32 %0, 0
br i1 %2, label %3, label %19
3: ; preds = %1, %15
%4 = phi i32 [ %17, %15 ], [ 0, %1 ]
br label %5
5: ; preds = %3, %5
%6 = load i32, i32* @PROT_READ
%7 = load i32, i32* @PROT_WRITE
%8 = or i32 %7, %6
%9 = load i32, i32* @MAP_ANONYMOUS
%10 = load i32, i32* @MAP_PRIVATE
%11 = or i32 %10, %9
%12 = tail call i8* @mmap(i32* null, i32 1048576, i32 %8, i32 %11, i32 -1, i32 0)
%13 = load i8*, i8** @MAP_FAILED
%14 = icmp eq i8* %12, %13
br i1 %14, label %5, label %15
15: ; preds = %5
%16 = tail call i32 @memset(i8* %12, i32 1, i32 1048576)
%17 = add nuw nsw i32 %4, 1
%18 = icmp eq i32 %17, %0
br i1 %18, label %19, label %3
19: ; preds = %15, %1
ret i32 0
}