#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 */
size_t atoi(char *);
int /*<<< orphan*/ memset(void *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int rand();
void *safe_malloc(int, char *);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  void **data;
  int i, r;
  size_t megs = 128;
  if ((argc >= 2) && (atoi(argv[1]) > 0))
    megs = atoi(argv[1]);
  data = safe_malloc(megs * sizeof(void *), "fill_mem 1");
  memset(data, 0, megs * sizeof(void *));
  srand(time(NULL));
  for (i = 0; (unsigned int)i < megs; i++) {
    data[i] = safe_malloc((1024 * 1024), "fill_mem 2");
    memset(data[i], i, (1024 * 1024));
    printf("malloc/memset %03d/%03u\n", i + 1, megs);
  }
  for (i = megs - 1; i >= 0; i--) {
    r = rand() % 200;
    memset(data[i], r, (1024 * 1024));
    printf("memset #2 %03d/%03u = %u\n", i + 1, megs, r);
  }
  printf("done\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x80,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11c0 <main+0x50>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
cmp    $0x0,%rax
jbe    11c0 <main+0x50>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
lea    0xe2f(%rip),%rsi
call   1030 <safe_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x28(%rbp),%rax
jae    1272 <main+0x102>
mov    $0x100000,%edi
lea    0xdec(%rip),%rsi
call   1030 <safe_malloc@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x1c(%rbp),%esi
mov    $0x100000,%edx
call   1050 <memset@plt>
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
mov    -0x28(%rbp),%rdx
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1206 <main+0x96>
mov    -0x28(%rbp),%rax
sub    $0x1,%rax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     12db <main+0x16b>
mov    $0x0,%al
call   1090 <rand@plt>
mov    $0xc8,%ecx
cltd
idiv   %ecx
mov    %edx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x20(%rbp),%esi
mov    $0x100000,%edx
call   1050 <memset@plt>
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%ecx
lea    0xd69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    127d <main+0x10d>
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x80,%r13d
cmp    $0x2,%edi
jl     11ac <main+0x2c>
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
test   %rax,%rax
mov    $0x80,%r13d
cmovne %rax,%r13
lea    0x0(,%r13,8),%ebx
lea    0xe45(%rip),%rsi
mov    %ebx,%edi
call   1030 <safe_malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1060 <memset@plt>
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
lea    0xe25(%rip),%r15
lea    0xe29(%rip),%r12
xor    %ebx,%ebx
nop
mov    $0x100000,%edi
mov    %r15,%rsi
call   1030 <safe_malloc@plt>
mov    %rax,(%r14,%rbx,8)
mov    %rax,%rdi
mov    %ebx,%esi
mov    $0x100000,%edx
call   1060 <memset@plt>
add    $0x1,%rbx
mov    %r12,%rdi
mov    %ebx,%esi
mov    %r13,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
cmp    %rbx,%r13
jne    11f0 <main+0x70>
mov    %r13d,%eax
add    $0xffffffff,%eax
js     129a <main+0x11a>
mov    %eax,%eax
lea    (%r14,%rax,8),%r15
lea    0xdf2(%rip),%r14
xor    %ebp,%ebp
nop
lea    0x0(%r13,%rbp,1),%r12d
xor    %eax,%eax
call   10a0 <rand@plt>
movslq %eax,%rbx
imul   $0x51eb851f,%rbx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
imul   $0xc8,%eax,%eax
sub    %eax,%ebx
mov    (%r15,%rbp,8),%rdi
mov    %ebx,%esi
mov    $0x100000,%edx
call   1060 <memset@plt>
mov    %r14,%rdi
mov    %r12d,%esi
mov    %r13,%rdx
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0xffffffffffffffff,%rbp
lea    0x0(%rbp,%r13,1),%eax
test   %eax,%eax
jg     1240 <main+0xc0>
lea    0xda8(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"fill_mem 1\00"
@.str.1 = [11 x i8] c"fill_mem 2\00"
@.str.2 = [25 x i8] c"malloc/memset %03d/%03u\0A\00"
@.str.3 = [26 x i8] c"memset %03d/%03u = %u\0A\00"
@.str.4 = [6 x i8] c"done\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 128, i64* %9
%10 = load i32, i32* %4
%11 = icmp sge i32 %10, 2
br i1 %11, label %12, label %23
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i64 @atoi(i8* %15)
%17 = icmp ugt i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %12
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i64 @atoi(i8* %21)
store i64 %22, i64* %9
br label %23
23: ; preds = %18, %12, %2
%24 = load i64, i64* %9
%25 = mul i64 %24, 8
%26 = trunc i64 %25 to i32
%27 = call i8* @safe_malloc(i32 %26, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%28 = bitcast i8* %27 to i8**
store i8** %28, i8*** %6
%29 = load i8**, i8*** %6
%30 = bitcast i8** %29 to i8*
%31 = load i64, i64* %9
%32 = mul i64 %31, 8
%33 = trunc i64 %32 to i32
%34 = call i32 @memset(i8* %30, i32 0, i32 %33)
%35 = call i32 @time(i32* null)
%36 = call i32 @srand(i32 %35)
store i32 0, i32* %7
br label %37
37: ; preds = %59, %23
%38 = load i32, i32* %7
%39 = zext i32 %38 to i64
%40 = load i64, i64* %9
%41 = icmp ult i64 %39, %40
br i1 %41, label %42, label %62
42: ; preds = %37
%43 = call i8* @safe_malloc(i32 1048576, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%44 = load i8**, i8*** %6
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8*, i8** %44, i64 %46
store i8* %43, i8** %47
%48 = load i8**, i8*** %6
%49 = load i32, i32* %7
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8*, i8** %48, i64 %50
%52 = load i8*, i8** %51
%53 = load i32, i32* %7
%54 = call i32 @memset(i8* %52, i32 %53, i32 1048576)
%55 = load i32, i32* %7
%56 = add nsw i32 %55, 1
%57 = load i64, i64* %9
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %56, i64 %57)
br label %59
59: ; preds = %42
%60 = load i32, i32* %7
%61 = add nsw i32 %60, 1
store i32 %61, i32* %7
br label %37
62: ; preds = %37
%63 = load i64, i64* %9
%64 = sub i64 %63, 1
%65 = trunc i64 %64 to i32
store i32 %65, i32* %7
br label %66
66: ; preds = %84, %62
%67 = load i32, i32* %7
%68 = icmp sge i32 %67, 0
br i1 %68, label %69, label %87
69: ; preds = %66
%70 = call i32 (...) @rand()
%71 = srem i32 %70, 200
store i32 %71, i32* %8
%72 = load i8**, i8*** %6
%73 = load i32, i32* %7
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i8*, i8** %72, i64 %74
%76 = load i8*, i8** %75
%77 = load i32, i32* %8
%78 = call i32 @memset(i8* %76, i32 %77, i32 1048576)
%79 = load i32, i32* %7
%80 = add nsw i32 %79, 1
%81 = load i64, i64* %9
%82 = load i32, i32* %8
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %80, i64 %81, i32 %82)
br label %84
84: ; preds = %69
%85 = load i32, i32* %7
%86 = add nsw i32 %85, -1
store i32 %86, i32* %7
br label %66
87: ; preds = %66
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"fill_mem 1\00"
@.str.1 = [11 x i8] c"fill_mem 2\00"
@.str.2 = [25 x i8] c"malloc/memset %03d/%03u\0A\00"
@.str.3 = [26 x i8] c"memset %03d/%03u = %u\0A\00"
@str = [5 x i8] c"done\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %10
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i64 @atoi(i8* %6)
%8 = icmp eq i64 %7, 0
%9 = select i1 %8, i64 128, i64 %7
br label %10
10: ; preds = %2, %4
%11 = phi i64 [ 128, %2 ], [ %9, %4 ]
%12 = trunc i64 %11 to i32
%13 = shl i32 %12, 3
%14 = tail call i8* @safe_malloc(i32 %13, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%15 = bitcast i8* %14 to i8**
%16 = tail call i32 @memset(i8* %14, i32 0, i32 %13)
%17 = tail call i32 @time(i32* null)
%18 = tail call i32 @srand(i32 %17)
br label %22
19: ; preds = %22
%20 = add i32 %12, -1
%21 = icmp sgt i32 %20, -1
br i1 %21, label %32, label %44
22: ; preds = %10, %22
%23 = phi i64 [ 0, %10 ], [ %28, %22 ]
%24 = tail call i8* @safe_malloc(i32 1048576, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%25 = getelementptr inbounds i8*, i8** %15, i64 %23
store i8* %24, i8** %25
%26 = trunc i64 %23 to i32
%27 = tail call i32 @memset(i8* %24, i32 %26, i32 1048576)
%28 = add nuw i64 %23, 1
%29 = trunc i64 %28 to i32
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %29, i64 %11)
%31 = icmp eq i64 %28, %11
br i1 %31, label %19, label %22
32: ; preds = %19, %32
%33 = phi i32 [ %42, %32 ], [ %20, %19 ]
%34 = phi i32 [ %33, %32 ], [ %12, %19 ]
%35 = tail call i32 (...) @rand()
%36 = srem i32 %35, 200
%37 = zext i32 %33 to i64
%38 = getelementptr inbounds i8*, i8** %15, i64 %37
%39 = load i8*, i8** %38
%40 = tail call i32 @memset(i8* %39, i32 %36, i32 1048576)
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %34, i64 %11, i32 %36)
%42 = add nsw i32 %33, -1
%43 = icmp sgt i32 %33, 0
br i1 %43, label %32, label %44
44: ; preds = %32, %19
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
ret i32 0
}