#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 */
size_t INTPTR_MAX;
void *calloc(size_t, size_t);
int /*<<< orphan*/ free(void *);
void *malloc(size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, void *);
int main(int argc, char **argv) {
  size_t huge = INTPTR_MAX;
  void *buf = malloc(huge * huge);
  if (!buf)
    perror("malloc failed");
  printf("malloc(huge * huge) returned: %p\n", buf);
  free(buf);
  buf = calloc(huge, huge);
  if (!buf)
    perror("calloc failed");
  printf("calloc(huge, huge) returned: %p\n", buf);
  free(buf);
  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)
mov    0x2e7b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
imul   -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119d <main+0x4d>
lea    0xe68(%rip),%rdi
call   1070 <perror@plt>
mov    -0x20(%rbp),%rsi
lea    0xe66(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11de <main+0x8e>
lea    0xe57(%rip),%rdi
call   1070 <perror@plt>
mov    -0x20(%rbp),%rsi
lea    0xe55(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e8d(%rip),%rax
mov    (%rax),%r14
mov    %r14,%rdi
imul   %r14,%rdi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11bd <main+0x6d>
lea    0xe95(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
mov    %r14,%rsi
call   1050 <calloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11cb <main+0x7b>
lea    0xe9b(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe3c(%rip),%rdi
call   1070 <perror@plt>
jmp    1172 <main+0x22>
lea    0xe5e(%rip),%rdi
call   1070 <perror@plt>
jmp    119c <main+0x4c>

LLVM IR (-O0):
@INTPTR_MAX = global i64 0
@.str = [14 x i8] c"malloc failed\00"
@.str.1 = [34 x i8] c"malloc(huge * huge) returned: %p\0A\00"
@.str.2 = [14 x i8] c"calloc failed\00"
@.str.3 = [33 x i8] c"calloc(huge, huge) returned: %p\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i64, i64* @INTPTR_MAX
store i64 %8, i64* %6
%9 = load i64, i64* %6
%10 = load i64, i64* %6
%11 = mul i64 %9, %10
%12 = call i8* @malloc(i64 %11)
store i8* %12, i8** %7
%13 = load i8*, i8** %7
%14 = icmp ne i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = load i8*, i8** %7
%19 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i8*, i8** %7
%21 = call i32 @free(i8* %20)
%22 = load i64, i64* %6
%23 = load i64, i64* %6
%24 = call i8* @calloc(i64 %22, i64 %23)
store i8* %24, i8** %7
%25 = load i8*, i8** %7
%26 = icmp ne i8* %25, null
br i1 %26, label %29, label %27
27: ; preds = %17
%28 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %17
%30 = load i8*, i8** %7
%31 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = load i8*, i8** %7
%33 = call i32 @free(i8* %32)
ret i32 0
}

LLVM IR (-O2):
@INTPTR_MAX = global i64 0
@.str = [14 x i8] c"malloc failed\00"
@.str.1 = [34 x i8] c"malloc(huge * huge) returned: %p\0A\00"
@.str.2 = [14 x i8] c"calloc failed\00"
@.str.3 = [33 x i8] c"calloc(huge, huge) returned: %p\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i64, i64* @INTPTR_MAX
%4 = mul i64 %3, %3
%5 = tail call i8* @malloc(i64 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%11 = tail call i32 @free(i8* %5)
%12 = tail call i8* @calloc(i64 %3, i64 %3)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %9
%17 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %12)
%18 = tail call i32 @free(i8* %12)
ret i32 0
}