#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 EXIT_FAILURE;
int EXIT_SUCCESS;
long atoll(char *);
char **calloc(int, long);
int /*<<< orphan*/ confirm_continue();
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int, long);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ usage(int, char **);
int main(int argc, char *argv[]) {
  int exitcode = EXIT_FAILURE;
  long int blk_size = 1;
  long int blk_cnt = 1;
  char **allocated;
  if (argc < 2)
    usage(argc, argv);
  blk_size = atoll(argv[1]);
  blk_cnt = atoll(argv[2]);
  printf("Block(size: %ld, count: %ld)\n", blk_size, blk_cnt);
  allocated = calloc(sizeof(char *), blk_cnt);
  if (allocated == NULL) {
    printf("Error allocating memory.\n");
    goto end;
  }
  for (long int i = 0; i < blk_cnt; i++) {
    allocated[i] = malloc(sizeof(char) * blk_size);
    printf("Allocate block %04ld@%p\n", i, &allocated[i]);
    if (allocated[i] == NULL) {
      printf("Unable to allocated memory\n");
      goto end;
    }
  }
  if (!confirm_continue()) {
    exitcode = EXIT_SUCCESS;
    goto end;
  }
  for (long int i = 0; i < blk_cnt; i++) {
    memset(allocated[i], 1, blk_size);
    printf("initialise block %ld\n", i);
  }
  if (!confirm_continue()) {
    exitcode = EXIT_SUCCESS;
    goto end;
  }
  exitcode = EXIT_SUCCESS;
end:
  if (allocated != NULL) {
    for (long int i = 0; i < blk_cnt; i++) {
      printf("free block %ld from %p\n", i, (allocated + i));
      free(allocated[i]);
    }
  }
  printf("Memory stress test completed.\n\n");
  return exitcode;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movq   $0x1,-0x20(%rbp)
movq   $0x1,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11c8 <main+0x48>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   10a0 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoll@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoll@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rsi
mov    $0x8,%edi
call   1070 <calloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1230 <main+0xb0>
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1362 <main+0x1e2>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    12c1 <main+0x141>
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,%rdx
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdx
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    12ab <main+0x12b>
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1362 <main+0x1e2>
jmp    12b0 <main+0x130>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1238 <main+0xb8>
mov    $0x0,%al
call   1060 <confirm_continue@plt>
cmp    $0x0,%eax
jne    12e2 <main+0x162>
mov    0x2d00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    1362 <main+0x1e2>
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    1335 <main+0x1b5>
mov    -0x30(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x1,%esi
call   1050 <memset@plt>
mov    -0x40(%rbp),%rsi
lea    0xd50(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    12ea <main+0x16a>
mov    $0x0,%al
call   1060 <confirm_continue@plt>
cmp    $0x0,%eax
jne    1356 <main+0x1d6>
mov    0x2c8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    1362 <main+0x1e2>
mov    0x2c7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     13cb <main+0x24b>
movq   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    13c6 <main+0x246>
mov    -0x48(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x48(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdx
lea    0xce6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    1375 <main+0x1f5>
jmp    13cb <main+0x24b>
lea    0xcc9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x2e3b(%rip),%rax
mov    (%rax),%r15d
cmp    $0x1,%edi
jg     11b5 <main+0x25>
mov    %rbx,%rsi
call   10b0 <usage@plt>
mov    0x8(%rbx),%rdi
call   1090 <atoll@plt>
mov    %rax,%r12
mov    0x10(%rbx),%rdi
call   1090 <atoll@plt>
mov    %rax,%r14
lea    0xe2c(%rip),%rdi
mov    %r12,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x8,%edi
mov    %r14,%rsi
call   1080 <calloc@plt>
test   %rax,%rax
je     129a <main+0x10a>
mov    %rax,%r13
test   %r14,%r14
jle    12a8 <main+0x118>
mov    %r15d,0x4(%rsp)
lea    0xe0f(%rip),%rbx
xor    %r15d,%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12d,%edi
call   10a0 <malloc@plt>
lea    (%r15,%r13,1),%rdx
mov    %rax,0x0(%r13,%rbp,8)
mov    %rbx,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
cmpq   $0x0,0x0(%r13,%rbp,8)
je     12cb <main+0x13b>
add    $0x1,%rbp
add    $0x8,%r15
cmp    %rbp,%r14
jne    1220 <main+0x90>
xor    %eax,%eax
call   1070 <confirm_continue@plt>
test   %eax,%eax
je     12ba <main+0x12a>
test   %r14,%r14
jle    12b3 <main+0x123>
lea    0xdc9(%rip),%r15
xor    %ebp,%ebp
mov    0x0(%r13,%rbp,8),%rdi
mov    $0x1,%esi
mov    %r12,%rdx
call   1060 <memset@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%r14
jne    1270 <main+0xe0>
jmp    12b3 <main+0x123>
lea    0xdfe(%rip),%rdi
call   1040 <puts@plt>
jmp    1318 <main+0x188>
xor    %eax,%eax
call   1070 <confirm_continue@plt>
test   %eax,%eax
je     12ba <main+0x12a>
xor    %eax,%eax
call   1070 <confirm_continue@plt>
mov    0x2d17(%rip),%rax
mov    (%rax),%r15d
test   %r14,%r14
jg     12e1 <main+0x151>
jmp    1318 <main+0x188>
lea    0xdb2(%rip),%rdi
call   1040 <puts@plt>
mov    0x4(%rsp),%r15d
test   %r14,%r14
jle    1318 <main+0x188>
lea    0xd65(%rip),%r12
xor    %ebx,%ebx
xor    %ebp,%ebp
nopl   0x0(%rax)
lea    (%rbx,%r13,1),%rdx
mov    %r12,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x0(%r13,%rbp,8),%rdi
call   1030 <free@plt>
add    $0x1,%rbp
add    $0x8,%rbx
cmp    %rbp,%r14
jne    12f0 <main+0x160>
lea    0xd46(%rip),%rdi
call   1040 <puts@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [30 x i8] c"Block(size: %ld, count: %ld)\0A\00"
@.str.1 = [26 x i8] c"Error allocating memory.\0A\00"
@.str.2 = [25 x i8] c"Allocate block %04ld@%p\0A\00"
@.str.3 = [28 x i8] c"Unable to allocated memory\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [22 x i8] c"initialise block %ld\0A\00"
@.str.5 = [24 x i8] c"free block %ld from %p\0A\00"
@.str.6 = [32 x i8] c"Memory stress test completed.\0A\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8**
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @EXIT_FAILURE
store i32 %13, i32* %6
store i64 1, i64* %7
store i64 1, i64* %8
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* %4
%18 = load i8**, i8*** %5
%19 = call i32 @usage(i32 %17, i8** %18)
br label %20
20: ; preds = %16, %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i64 @atoll(i8* %23)
store i64 %24, i64* %7
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i64 @atoll(i8* %27)
store i64 %28, i64* %8
%29 = load i64, i64* %7
%30 = load i64, i64* %8
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i64 %29, i64 %30)
%32 = load i64, i64* %8
%33 = call i8** @calloc(i32 8, i64 %32)
store i8** %33, i8*** %9
%34 = load i8**, i8*** %9
%35 = icmp eq i8** %34, null
br i1 %35, label %36, label %38
36: ; preds = %20
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %96
38: ; preds = %20
store i64 0, i64* %10
br label %39
39: ; preds = %64, %38
%40 = load i64, i64* %10
%41 = load i64, i64* %8
%42 = icmp slt i64 %40, %41
br i1 %42, label %43, label %67
43: ; preds = %39
%44 = load i64, i64* %7
%45 = mul i64 1, %44
%46 = trunc i64 %45 to i32
%47 = call i8* @malloc(i32 %46)
%48 = load i8**, i8*** %9
%49 = load i64, i64* %10
%50 = getelementptr inbounds i8*, i8** %48, i64 %49
store i8* %47, i8** %50
%51 = load i64, i64* %10
%52 = load i8**, i8*** %9
%53 = load i64, i64* %10
%54 = getelementptr inbounds i8*, i8** %52, i64 %53
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i64 %51, i8** %54)
%56 = load i8**, i8*** %9
%57 = load i64, i64* %10
%58 = getelementptr inbounds i8*, i8** %56, i64 %57
%59 = load i8*, i8** %58
%60 = icmp eq i8* %59, null
br i1 %60, label %61, label %63
61: ; preds = %43
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %96
63: ; preds = %43
br label %64
64: ; preds = %63
%65 = load i64, i64* %10
%66 = add nsw i64 %65, 1
store i64 %66, i64* %10
br label %39
67: ; preds = %39
%68 = call i32 (...) @confirm_continue()
%69 = icmp ne i32 %68, 0
br i1 %69, label %72, label %70
70: ; preds = %67
%71 = load i32, i32* @EXIT_SUCCESS
store i32 %71, i32* %6
br label %96
72: ; preds = %67
store i64 0, i64* %11
br label %73
73: ; preds = %86, %72
%74 = load i64, i64* %11
%75 = load i64, i64* %8
%76 = icmp slt i64 %74, %75
br i1 %76, label %77, label %89
77: ; preds = %73
%78 = load i8**, i8*** %9
%79 = load i64, i64* %11
%80 = getelementptr inbounds i8*, i8** %78, i64 %79
%81 = load i8*, i8** %80
%82 = load i64, i64* %7
%83 = call i32 @memset(i8* %81, i32 1, i64 %82)
%84 = load i64, i64* %11
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i64 %84)
br label %86
86: ; preds = %77
%87 = load i64, i64* %11
%88 = add nsw i64 %87, 1
store i64 %88, i64* %11
br label %73
89: ; preds = %73
%90 = call i32 (...) @confirm_continue()
%91 = icmp ne i32 %90, 0
br i1 %91, label %94, label %92
92: ; preds = %89
%93 = load i32, i32* @EXIT_SUCCESS
store i32 %93, i32* %6
br label %96
94: ; preds = %89
%95 = load i32, i32* @EXIT_SUCCESS
store i32 %95, i32* %6
br label %96
96: ; preds = %94, %92, %70, %61, %36
%97 = load i8**, i8*** %9
%98 = icmp ne i8** %97, null
br i1 %98, label %99, label %119
99: ; preds = %96
store i64 0, i64* %12
br label %100
100: ; preds = %115, %99
%101 = load i64, i64* %12
%102 = load i64, i64* %8
%103 = icmp slt i64 %101, %102
br i1 %103, label %104, label %118
104: ; preds = %100
%105 = load i64, i64* %12
%106 = load i8**, i8*** %9
%107 = load i64, i64* %12
%108 = getelementptr inbounds i8*, i8** %106, i64 %107
%109 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i64 %105, i8** %108)
%110 = load i8**, i8*** %9
%111 = load i64, i64* %12
%112 = getelementptr inbounds i8*, i8** %110, i64 %111
%113 = load i8*, i8** %112
%114 = call i32 @free(i8* %113)
br label %115
115: ; preds = %104
%116 = load i64, i64* %12
%117 = add nsw i64 %116, 1
store i64 %117, i64* %12
br label %100
118: ; preds = %100
br label %119
119: ; preds = %118, %96
%120 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
%121 = load i32, i32* %6
ret i32 %121
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [30 x i8] c"Block(size: %ld, count: %ld)\0A\00"
@.str.2 = [25 x i8] c"Allocate block %04ld@%p\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [22 x i8] c"initialise block %ld\0A\00"
@.str.5 = [24 x i8] c"free block %ld from %p\0A\00"
@str = [31 x i8] c"Memory stress test completed.\0A\00"
@str.7 = [27 x i8] c"Unable to allocated memory\00"
@str.8 = [25 x i8] c"Error allocating memory.\00"
define i32 @main(i32 %0, i8** %1) {
%3 = load i32, i32* @EXIT_FAILURE
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @usage(i32 %0, i8** %1)
br label %7
7: ; preds = %5, %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i64 @atoll(i8* %9)
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i64 @atoll(i8* %12)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i64 %10, i64 %13)
%15 = tail call i8** @calloc(i32 8, i64 %13)
%16 = icmp eq i8** %15, null
br i1 %16, label %21, label %17
17: ; preds = %7
%18 = icmp sgt i64 %13, 0
br i1 %18, label %19, label %38
19: ; preds = %17
%20 = trunc i64 %10 to i32
br label %25
21: ; preds = %7
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.8, i64 0, i64 0))
br label %66
23: ; preds = %25
%24 = icmp eq i64 %32, %13
br i1 %24, label %35, label %25
25: ; preds = %19, %23
%26 = phi i64 [ 0, %19 ], [ %32, %23 ]
%27 = tail call i8* @malloc(i32 %20)
%28 = getelementptr inbounds i8*, i8** %15, i64 %26
store i8* %27, i8** %28
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i64 %26, i8** %28)
%30 = load i8*, i8** %28
%31 = icmp eq i8* %30, null
%32 = add nuw nsw i64 %26, 1
br i1 %31, label %33, label %23
33: ; preds = %25
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.7, i64 0, i64 0))
br label %55
35: ; preds = %23
%36 = tail call i32 (...) @confirm_continue()
%37 = icmp eq i32 %36, 0
br i1 %37, label %42, label %41
38: ; preds = %17
%39 = tail call i32 (...) @confirm_continue()
%40 = icmp eq i32 %39, 0
br i1 %40, label %42, label %44
41: ; preds = %35
br i1 %18, label %47, label %44
42: ; preds = %38, %35
%43 = load i32, i32* @EXIT_SUCCESS
br label %55
44: ; preds = %47, %38, %41
%45 = tail call i32 (...) @confirm_continue()
%46 = load i32, i32* @EXIT_SUCCESS
br label %55
47: ; preds = %41, %47
%48 = phi i64 [ %53, %47 ], [ 0, %41 ]
%49 = getelementptr inbounds i8*, i8** %15, i64 %48
%50 = load i8*, i8** %49
%51 = tail call i32 @memset(i8* %50, i32 1, i64 %10)
%52 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i64 %48)
%53 = add nuw nsw i64 %48, 1
%54 = icmp eq i64 %53, %13
br i1 %54, label %44, label %47
55: ; preds = %44, %33, %42
%56 = phi i32 [ %3, %33 ], [ %43, %42 ], [ %46, %44 ]
%57 = icmp slt i64 %13, 1
br i1 %57, label %66, label %58
58: ; preds = %55, %58
%59 = phi i64 [ %64, %58 ], [ 0, %55 ]
%60 = getelementptr inbounds i8*, i8** %15, i64 %59
%61 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i64 %59, i8** %60)
%62 = load i8*, i8** %60
%63 = tail call i32 @free(i8* %62)
%64 = add nuw nsw i64 %59, 1
%65 = icmp eq i64 %64, %13
br i1 %65, label %66, label %58
66: ; preds = %58, %21, %55
%67 = phi i32 [ %3, %21 ], [ %56, %55 ], [ %56, %58 ]
%68 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
ret i32 %67
}