#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 /*<<< orphan*/ errno;
char *malloc(size_t);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int test_huge_alloc(void) {
  size_t terabyte = (size_t)(1L << 42);
  char *limit;
  char *x = malloc(terabyte);
  if (x == NULL) {
    printf("Error: %s\n", strerror(errno));
    return 1;
  }
  printf("malloc'd (1 Terabyte), starts at %p\n", (void *)x);
  printf("Attempting to write to every at page...\n");
  limit = x + terabyte - 0x1000;
  for (; x < limit; x += 0x1000) {
    *x = 'A';
  }
  return 0;
}


x86 assembly (clang, -O0):
<test_huge_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movabs $0x40000000000,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1189 <test_huge_alloc+0x59>
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %rax,%rsi
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11ea <test_huge_alloc+0xba>
mov    -0x20(%rbp),%rsi
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
add    -0x10(%rbp),%rax
add    $0xfffffffffffff000,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11e3 <test_huge_alloc+0xb3>
mov    -0x20(%rbp),%rax
movb   $0x41,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1000,%rax
mov    %rax,-0x20(%rbp)
jmp    11bb <test_huge_alloc+0x8b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_huge_alloc>:
push   %rbp
push   %r14
push   %rbx
movabs $0x3ffffff8000,%rbp
lea    0x8000(%rbp),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     11d5 <test_huge_alloc+0x95>
mov    %rax,%rbx
lea    0xea2(%rip),%rdi
xor    %r14d,%r14d
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xeb3(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffffffff9000,%rax
nopl   0x0(%rax)
movb   $0x41,0x7000(%rbx,%rax,1)
41
movb   $0x41,0x8000(%rbx,%rax,1)
41
movb   $0x41,0x9000(%rbx,%rax,1)
41
movb   $0x41,0xa000(%rbx,%rax,1)
41
movb   $0x41,0xb000(%rbx,%rax,1)
41
movb   $0x41,0xc000(%rbx,%rax,1)
41
movb   $0x41,0xd000(%rbx,%rax,1)
41
add    $0x7000,%rax
cmp    %rbp,%rax
jb     1190 <test_huge_alloc+0x50>
jmp    11fa <test_huge_alloc+0xba>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe16(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"Error: %s\0A\00"
@errno = global i32 0
@.str.1 = [37 x i8] c"malloc'd (1 Terabyte), starts at %p\0A\00"
@.str.2 = [41 x i8] c"Attempting to write to every at page...\0A\00"
define i32 @test_huge_alloc() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i8*
store i64 4398046511104, i64* %2
%5 = load i64, i64* %2
%6 = call i8* @malloc(i64 %5)
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = call i8* @strerror(i32 %10)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %11)
store i32 1, i32* %1
br label %31
13: ; preds = %0
%14 = load i8*, i8** %4
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%17 = load i8*, i8** %4
%18 = load i64, i64* %2
%19 = getelementptr inbounds i8, i8* %17, i64 %18
%20 = getelementptr inbounds i8, i8* %19, i64 -4096
store i8* %20, i8** %3
br label %21
21: ; preds = %27, %13
%22 = load i8*, i8** %4
%23 = load i8*, i8** %3
%24 = icmp ult i8* %22, %23
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = load i8*, i8** %4
store i8 65, i8* %26
br label %27
27: ; preds = %25
%28 = load i8*, i8** %4
%29 = getelementptr inbounds i8, i8* %28, i64 4096
store i8* %29, i8** %4
br label %21
30: ; preds = %21
store i32 0, i32* %1
br label %31
31: ; preds = %30, %9
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
@.str = [11 x i8] c"Error: %s\0A\00"
@errno = global i32 0
@.str.1 = [37 x i8] c"malloc'd (1 Terabyte), starts at %p\0A\00"
@str = [40 x i8] c"Attempting to write to every at page...\00"
define i32 @test_huge_alloc() {
%1 = tail call dereferenceable_or_null(4398046511104) i8* @malloc(i64 4398046511104)
%2 = icmp eq i8* %1, null
br i1 %2, label %3, label %7
3: ; preds = %0
%4 = load i32, i32* @errno
%5 = tail call i8* @strerror(i32 %4)
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %27
7: ; preds = %0
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
br label %10
10: ; preds = %10, %7
%11 = phi i64 [ 0, %7 ], [ %25, %10 ]
%12 = getelementptr inbounds i8, i8* %1, i64 %11
store i8 65, i8* %12
%13 = add nuw nsw i64 %11, 4096
%14 = getelementptr inbounds i8, i8* %1, i64 %13
store i8 65, i8* %14
%15 = add nuw nsw i64 %11, 8192
%16 = getelementptr inbounds i8, i8* %1, i64 %15
store i8 65, i8* %16
%17 = add nuw nsw i64 %11, 12288
%18 = getelementptr inbounds i8, i8* %1, i64 %17
store i8 65, i8* %18
%19 = add nuw nsw i64 %11, 16384
%20 = getelementptr inbounds i8, i8* %1, i64 %19
store i8 65, i8* %20
%21 = add nuw nsw i64 %11, 20480
%22 = getelementptr inbounds i8, i8* %1, i64 %21
store i8 65, i8* %22
%23 = add nuw nsw i64 %11, 24576
%24 = getelementptr inbounds i8, i8* %1, i64 %23
store i8 65, i8* %24
%25 = add nuw nsw i64 %11, 28672
%26 = icmp ult i64 %11, 4398046478336
br i1 %26, label %10, label %27
27: ; preds = %10, %3
%28 = phi i32 [ 1, %3 ], [ 0, %10 ]
ret i32 %28
}