#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 */
typedef int uint64_t;

/* Variables and functions */
char *malloc(int);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ pause();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int main(int argc, char *argv[]) {
  char *mem;
  uint64_t alloc_bytes;
  int niters;
  if (argc < ((2) + 1)) {
    printf("test_mem_alerts <alloc_bytes> <niter> \n");
    return 0;
  }
  sscanf(argv[1], "%llu", &alloc_bytes);
  sscanf(argv[2], "%d", &niters);
  while (niters--) {
    mem = malloc(alloc_bytes);
    if (!mem) {
      perror("malloc failed");
      return -1;
    }
    printf("Allocation of %llu bytes successful\n", alloc_bytes);
    printf("Touching memory...");
    memset(mem, 0xab, alloc_bytes);
    printf("done.\n");
  }
  printf("All allocations done.\n");
  pause();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    119a <main+0x3a>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    126c <main+0x10c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe7f(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1070 <sscanf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe6c(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1070 <sscanf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x20(%rbp)
cmp    $0x0,%eax
je     1250 <main+0xf0>
mov    -0x1c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    120d <main+0xad>
lea    0xe34(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <main+0x10c>
mov    -0x1c(%rbp),%esi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    $0xab,%esi
call   1040 <memset@plt>
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11ca <main+0x6a>
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <pause@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jg     1194 <main+0x24>
lea    0xedc(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    123e <main+0xce>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xe5e(%rip),%rsi
lea    0xc(%rsp),%rdx
call   1080 <sscanf@plt>
mov    0x10(%rbx),%rdi
lea    0xe4e(%rip),%rsi
lea    0x8(%rsp),%rdx
call   1080 <sscanf@plt>
subl   $0x1,0x8(%rsp)
jb     1229 <main+0xb9>
lea    0xe47(%rip),%r14
lea    0xe65(%rip),%r15
lea    0xe81(%rip),%r12
nopl   (%rax)
mov    0xc(%rsp),%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     124d <main+0xdd>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edx
mov    %rbx,%rdi
mov    $0xab,%esi
call   1050 <memset@plt>
mov    %r12,%rdi
call   1030 <puts@plt>
subl   $0x1,0x8(%rsp)
jae    11e0 <main+0x70>
lea    0xe1e(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <pause@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb4(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
jmp    123e <main+0xce>

LLVM IR (-O0):
@.str = [40 x i8] c"test_mem_alerts <alloc_bytes> <niter> \0A\00"
@.str.1 = [5 x i8] c"%llu\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [14 x i8] c"malloc failed\00"
@.str.4 = [37 x i8] c"Allocation of %llu bytes successful\0A\00"
@.str.5 = [19 x i8] c"Touching memory...\00"
@.str.6 = [7 x i8] c"done.\0A\00"
@.str.7 = [23 x i8] c"All allocations 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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 3
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %44
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %7)
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 2
%20 = load i8*, i8** %19
%21 = call i32 @sscanf(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %8)
br label %22
22: ; preds = %33, %13
%23 = load i32, i32* %8
%24 = add nsw i32 %23, -1
store i32 %24, i32* %8
%25 = icmp ne i32 %23, 0
br i1 %25, label %26, label %41
26: ; preds = %22
%27 = load i32, i32* %7
%28 = call i8* @malloc(i32 %27)
store i8* %28, i8** %6
%29 = load i8*, i8** %6
%30 = icmp ne i8* %29, null
br i1 %30, label %33, label %31
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %44
33: ; preds = %26
%34 = load i32, i32* %7
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%37 = load i8*, i8** %6
%38 = load i32, i32* %7
%39 = call i32 @memset(i8* %37, i32 171, i32 %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %22
41: ; preds = %22
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
%43 = call i32 (...) @pause()
store i32 0, i32* %3
br label %44
44: ; preds = %41, %31, %11
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"%llu\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [14 x i8] c"malloc failed\00"
@.str.4 = [37 x i8] c"Allocation of %llu bytes successful\0A\00"
@.str.5 = [19 x i8] c"Touching memory...\00"
@str = [22 x i8] c"All allocations done.\00"
@str.8 = [6 x i8] c"done.\00"
@str.9 = [39 x i8] c"test_mem_alerts <alloc_bytes> <niter> \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = icmp slt i32 %0, 3
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.9, i64 0, i64 0))
br label %38
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = call i32 @sscanf(i8* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = call i32 @sscanf(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%17 = load i32, i32* %4
%18 = add nsw i32 %17, -1
store i32 %18, i32* %4
%19 = icmp eq i32 %17, 0
br i1 %19, label %35, label %20
20: ; preds = %10, %26
%21 = load i32, i32* %3
%22 = call i8* @malloc(i32 %21)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %38
26: ; preds = %20
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%29 = load i32, i32* %3
%30 = call i32 @memset(i8* %22, i32 171, i32 %29)
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.8, i64 0, i64 0))
%32 = load i32, i32* %4
%33 = add nsw i32 %32, -1
store i32 %33, i32* %4
%34 = icmp eq i32 %32, 0
br i1 %34, label %35, label %20
35: ; preds = %26, %10
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%37 = call i32 (...) @pause()
br label %38
38: ; preds = %35, %24, %8
%39 = phi i32 [ 0, %8 ], [ -1, %24 ], [ 0, %35 ]
ret i32 %39
}