#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 */
scalar_t__ alloca(int);
int atoi(char *);
int /*<<< orphan*/ exit(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv) {
  size_t i = 5000;
  if (argc != 4) {
    printf("call as: eatmem <stacksize(Kb)> <heapsize(Kb)> <period(sec)>\n");
    exit(1);
  }
  int stacksize = atoi(argv[1]);
  int heapsize = atoi(argv[2]);
  int period = atoi(argv[3]);
  if (stacksize == 0 || heapsize == 0 || period == 0) {
    printf("error in stacksize, heapsize or period\n");
    exit(2);
  }
  printf("Starting with: stacksize=%d, heapsize=%d, period=%d\n", stacksize,
         heapsize, period);
  int stackstep = stacksize / period;
  int heapstep = stacksize / period;
  int stackcur = 0;
  int heapcur = 0;
  int curperiod = 0;
  char *buf;
  for (curperiod = 0; curperiod < period; curperiod++) {
    buf = (char *)malloc(stackstep * 1024);
    if (buf == NULL) {
      printf("no malloc buf");
      exit(3);
    }
    buf = (char *)alloca(heapstep * 1024);
    if (buf == NULL) {
      printf("no alloca buf");
      exit(4);
    }
    sleep(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x1388,-0x18(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11a0 <main+0x40>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11ee <main+0x8e>
cmpl   $0x0,-0x20(%rbp)
je     11ee <main+0x8e>
cmpl   $0x0,-0x24(%rbp)
jne    1206 <main+0xa6>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
cltd
idivl  -0x24(%rbp)
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
cltd
idivl  -0x24(%rbp)
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12d5 <main+0x175>
mov    -0x28(%rbp),%edi
shl    $0xa,%edi
call   1050 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    128b <main+0x12b>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x3,%edi
call   1070 <exit@plt>
mov    -0x2c(%rbp),%edi
shl    $0xa,%edi
call   1040 <alloca@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12bd <main+0x15d>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x4,%edi
call   1070 <exit@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    124d <main+0xed>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x4,%edi
jne    123e <main+0xce>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
call   1070 <atoi@plt>
mov    %eax,%ebp
mov    0x18(%rbx),%rdi
call   1070 <atoi@plt>
test   %r14d,%r14d
je     1254 <main+0xe4>
test   %ebp,%ebp
je     1254 <main+0xe4>
mov    %eax,%ebx
test   %eax,%eax
je     1254 <main+0xe4>
lea    0xe3e(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
jle    1207 <main+0x97>
mov    %r14d,%eax
cltd
idiv   %ebx
mov    %eax,%ebp
shl    $0xa,%ebp
nop
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     120e <main+0x9e>
mov    %ebp,%edi
call   1050 <alloca@plt>
test   %rax,%rax
je     1226 <main+0xb6>
mov    $0x1,%edi
call   1090 <sleep@plt>
add    $0xffffffff,%ebx
jne    11e0 <main+0x70>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe20(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x3,%edi
call   1080 <exit@plt>
lea    0xe16(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
lea    0xe33(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdf6(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [62 x i8] c"call as: eatmem <stacksize(Kb)> <heapsize(Kb)> <period(sec)>\0A\00"
@.str.1 = [40 x i8] c"error in stacksize, heapsize or period\0A\00"
@.str.2 = [53 x i8] c"Starting with: stacksize=%d, heapsize=%d, period=%d\0A\00"
@.str.3 = [14 x i8] c"no malloc buf\00"
@.str.4 = [14 x i8] c"no alloca buf\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 5000, i64* %6
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 4
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %7
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
store i32 %29, i32* %8
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 3
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
store i32 %33, i32* %9
%34 = load i32, i32* %7
%35 = icmp eq i32 %34, 0
br i1 %35, label %42, label %36
36: ; preds = %21
%37 = load i32, i32* %8
%38 = icmp eq i32 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %36
%40 = load i32, i32* %9
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39, %36, %21
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @exit(i32 2)
unreachable
45: ; preds = %39
%46 = load i32, i32* %7
%47 = load i32, i32* %8
%48 = load i32, i32* %9
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i32 %46, i32 %47, i32 %48)
%50 = load i32, i32* %7
%51 = load i32, i32* %9
%52 = sdiv i32 %50, %51
store i32 %52, i32* %10
%53 = load i32, i32* %7
%54 = load i32, i32* %9
%55 = sdiv i32 %53, %54
store i32 %55, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
store i32 0, i32* %14
br label %56
56: ; preds = %82, %45
%57 = load i32, i32* %14
%58 = load i32, i32* %9
%59 = icmp slt i32 %57, %58
br i1 %59, label %60, label %85
60: ; preds = %56
%61 = load i32, i32* %10
%62 = mul nsw i32 %61, 1024
%63 = call i64 @malloc(i32 %62)
%64 = inttoptr i64 %63 to i8*
store i8* %64, i8** %15
%65 = load i8*, i8** %15
%66 = icmp eq i8* %65, null
br i1 %66, label %67, label %70
67: ; preds = %60
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%69 = call i32 @exit(i32 3)
unreachable
70: ; preds = %60
%71 = load i32, i32* %11
%72 = mul nsw i32 %71, 1024
%73 = call i64 @alloca(i32 %72)
%74 = inttoptr i64 %73 to i8*
store i8* %74, i8** %15
%75 = load i8*, i8** %15
%76 = icmp eq i8* %75, null
br i1 %76, label %77, label %80
77: ; preds = %70
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%79 = call i32 @exit(i32 4)
unreachable
80: ; preds = %70
%81 = call i32 @sleep(i32 1)
br label %82
82: ; preds = %80
%83 = load i32, i32* %14
%84 = add nsw i32 %83, 1
store i32 %84, i32* %14
br label %56
85: ; preds = %56
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [53 x i8] c"Starting with: stacksize=%d, heapsize=%d, period=%d\0A\00"
@.str.3 = [14 x i8] c"no malloc buf\00"
@.str.4 = [14 x i8] c"no alloca buf\00"
@str = [39 x i8] c"error in stacksize, heapsize or period\00"
@str.5 = [61 x i8] c"call as: eatmem <stacksize(Kb)> <heapsize(Kb)> <period(sec)>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 4
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str.5, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
%14 = getelementptr inbounds i8*, i8** %1, i64 3
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
%17 = icmp eq i32 %10, 0
%18 = icmp eq i32 %13, 0
%19 = select i1 %17, i1 true, i1 %18
%20 = icmp eq i32 %16, 0
%21 = select i1 %19, i1 true, i1 %20
br i1 %21, label %22, label %25
22: ; preds = %7
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%24 = tail call i32 @exit(i32 2)
unreachable
25: ; preds = %7
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i32 %10, i32 %13, i32 %16)
%27 = icmp sgt i32 %16, 0
br i1 %27, label %28, label %48
28: ; preds = %25
%29 = sdiv i32 %10, %16
%30 = shl nsw i32 %29, 10
br label %31
31: ; preds = %28, %44
%32 = phi i32 [ 0, %28 ], [ %46, %44 ]
%33 = tail call i64 @malloc(i32 %30)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%37 = tail call i32 @exit(i32 3)
unreachable
38: ; preds = %31
%39 = tail call i64 @alloca(i32 %30)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%43 = tail call i32 @exit(i32 4)
unreachable
44: ; preds = %38
%45 = tail call i32 @sleep(i32 1)
%46 = add nuw nsw i32 %32, 1
%47 = icmp eq i32 %46, %16
br i1 %47, label %48, label %31
48: ; preds = %44, %25
ret i32 0
}