#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 */
void *malloc(size_t);
int /*<<< orphan*/ memset(void *, int, size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int main() {
  size_t size = 128 * 1024 * 1024;
  void *p = malloc(size);
  if (p == NULL) {
    perror("malloc");
    return 1;
  }
  puts("Thrashing memory . . . (Ctrl-C to stop)");
  int counter;
  while (1) {
    memset(p, counter & 0xFF, size);
    counter++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x8000000,-0x10(%rbp)
08
mov    -0x10(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1186 <main+0x46>
lea    0xe8a(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbp
ret
lea    0xe7a(%rip),%rdi
call   1030 <puts@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
and    $0xff,%esi
mov    %esi,-0x24(%rbp)
mov    -0x10(%rbp),%rdx
call   1040 <memset@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    %eax,-0x1c(%rbp)
jmp    1192 <main+0x52>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    $0x8000000,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1186 <main+0x46>
mov    %rax,%rbx
lea    0xeab(%rip),%rdi
call   1030 <puts@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl %bpl,%esi
mov    $0x8000000,%edx
mov    %rbx,%rdi
call   1040 <memset@plt>
add    $0x1,%ebp
jmp    1170 <main+0x30>
lea    0xe73(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"malloc\00"
@.str.1 = [40 x i8] c"Thrashing memory . . . (Ctrl-C to stop)\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
store i32 0, i32* %1
store i64 134217728, i64* %2
%5 = load i64, i64* %2
%6 = call i8* @malloc(i64 %5)
store i8* %6, i8** %3
%7 = load i8*, i8** %3
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
ret i32 1
11: ; preds = %0
%12 = call i32 @puts(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %13
%14 = load i8*, i8** %3
%15 = load i32, i32* %4
%16 = and i32 %15, 255
%17 = load i64, i64* %2
%18 = call i32 @memset(i8* %14, i32 %16, i64 %17)
%19 = load i32, i32* %4
%20 = add nsw i32 %19, 1
store i32 %20, i32* %4
br label %13
}

LLVM IR (-O2):
@.str = [7 x i8] c"malloc\00"
@.str.1 = [40 x i8] c"Thrashing memory . . . (Ctrl-C to stop)\00"
define i32 @main() {
%1 = tail call dereferenceable_or_null(134217728) i8* @malloc(i64 134217728)
%2 = icmp eq i8* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
ret i32 1
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %7
7: ; preds = %7, %5
%8 = phi i32 [ undef, %5 ], [ %11, %7 ]
%9 = and i32 %8, 255
%10 = tail call i32 @memset(i8* %1, i32 %9, i64 134217728)
%11 = add nsw i32 %8, 1
br label %7
}