#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 atoi(char *);
int /*<<< orphan*/ free(char **);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  char **p;
  int i;
  int N = 0;
  if (argc != 2) {
    printf("Usage: ./malloc N\n");
    return -1;
  }
  N = atoi(argv[1]);
  printf("you are try to alloc %dGb memory...\n", N);
  p = (char **)malloc(N * sizeof(char *));
  for (i = 0; i < N; i++) {
    p[i] = (char *)malloc(1024 * 1024 * 1024);
    if (p[i] == NULL) {
      perror("Warning");
      return -1;
    }
    memset(p[i], 1, 1024 * 1024 * 1024);
  }
  printf("malloc successfully\n");
  free(p);
  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)
movl   $0x0,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11a1 <main+0x41>
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <main+0x113>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movslq -0x20(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1255 <main+0xf5>
mov    $0x40000000,%edi
call   1060 <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
cmpq   $0x0,(%rax,%rcx,8)
jne    122c <main+0xcc>
lea    0xe1d(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <main+0x113>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    $0x1,%esi
mov    $0x40000000,%edx
call   1050 <memset@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11dc <main+0x7c>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@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   %rbx
push   %rax
cmp    $0x2,%edi
jne    1204 <main+0x94>
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
lea    0xe6e(%rip),%rdi
xor    %r14d,%r14d
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x0(,%rbp,8),%edi
call   1070 <malloc@plt>
mov    %rax,%r15
test   %ebp,%ebp
jle    11ee <main+0x7e>
mov    %ebp,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x40000000,%edi
call   1070 <malloc@plt>
mov    %rax,(%r15,%rbx,8)
test   %rax,%rax
je     1224 <main+0xb4>
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x40000000,%edx
call   1060 <memset@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11c0 <main+0x50>
lea    0xe38(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    1216 <main+0xa6>
lea    0xe36(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfa(%rip),%rdi
call   1080 <perror@plt>
jmp    1210 <main+0xa0>

LLVM IR (-O0):
@.str = [19 x i8] c"Usage: ./malloc N\0A\00"
@.str.1 = [37 x i8] c"you are try to alloc %dGb memory...\0A\00"
@.str.2 = [8 x i8] c"Warning\00"
@.str.3 = [21 x i8] c"malloc successfully\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
store i32 0, i32* %8
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %59
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @atoi(i8* %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %8
%21 = sext i32 %20 to i64
%22 = mul i64 %21, 8
%23 = trunc i64 %22 to i32
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i8**
store i8** %25, i8*** %6
store i32 0, i32* %7
br label %26
26: ; preds = %52, %13
%27 = load i32, i32* %7
%28 = load i32, i32* %8
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %55
30: ; preds = %26
%31 = call i64 @malloc(i32 1073741824)
%32 = inttoptr i64 %31 to i8*
%33 = load i8**, i8*** %6
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8*, i8** %33, i64 %35
store i8* %32, i8** %36
%37 = load i8**, i8*** %6
%38 = load i32, i32* %7
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8*, i8** %37, i64 %39
%41 = load i8*, i8** %40
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %45
43: ; preds = %30
%44 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %59
45: ; preds = %30
%46 = load i8**, i8*** %6
%47 = load i32, i32* %7
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8*, i8** %46, i64 %48
%50 = load i8*, i8** %49
%51 = call i32 @memset(i8* %50, i32 1, i32 1073741824)
br label %52
52: ; preds = %45
%53 = load i32, i32* %7
%54 = add nsw i32 %53, 1
store i32 %54, i32* %7
br label %26
55: ; preds = %26
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%57 = load i8**, i8*** %6
%58 = call i32 @free(i8** %57)
store i32 0, i32* %3
br label %59
59: ; preds = %55, %43, %11
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@.str.1 = [37 x i8] c"you are try to alloc %dGb memory...\0A\00"
@.str.2 = [8 x i8] c"Warning\00"
@str = [20 x i8] c"malloc successfully\00"
@str.4 = [18 x i8] c"Usage: ./malloc N\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.4, i64 0, i64 0))
br label %32
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = shl i32 %9, 3
%12 = tail call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8**
%14 = icmp sgt i32 %9, 0
br i1 %14, label %15, label %29
15: ; preds = %6
%16 = zext i32 %9 to i64
br label %17
17: ; preds = %15, %25
%18 = phi i64 [ 0, %15 ], [ %27, %25 ]
%19 = tail call i64 @malloc(i32 1073741824)
%20 = inttoptr i64 %19 to i8*
%21 = getelementptr inbounds i8*, i8** %13, i64 %18
store i8* %20, i8** %21
%22 = icmp eq i64 %19, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %32
25: ; preds = %17
%26 = tail call i32 @memset(i8* %20, i32 1, i32 1073741824)
%27 = add nuw nsw i64 %18, 1
%28 = icmp eq i64 %27, %16
br i1 %28, label %29, label %17
29: ; preds = %25, %6
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%31 = tail call i32 @free(i8** %13)
br label %32
32: ; preds = %29, %23, %4
%33 = phi i32 [ -1, %4 ], [ -1, %23 ], [ 0, %29 ]
ret i32 %33
}