#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*/ EXIT_FAILURE;
int /*<<< orphan*/ check_cache_friendliness(void *, size_t const, size_t const,
                                            size_t const, int const);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, size_t const);
int posix_memalign(void **, size_t const, size_t const);
int /*<<< orphan*/ printf(char *, unsigned int);
int /*<<< orphan*/ stdout;
void *alloc_friendly(const size_t size, const size_t pagesize) {
  const int verbose = 1;
  void *alloc;
  const size_t cachesize = 65536;
  const int associativity = 2;
  unsigned int tries = 0;
  do {
    int i;
    tries++;
    i = posix_memalign(&alloc, pagesize, size);
    if (i != 0)
      exit(EXIT_FAILURE);
    memset(alloc, 0, size);
    if (1 && size <= cachesize &&
        !check_cache_friendliness(alloc, size, pagesize, cachesize,
                                  associativity)) {
      alloc = NULL;
    }
  } while (alloc == NULL);
  if (verbose) {
    printf("Getting cache-friendly pages took %u tries\n", tries);
    fflush(stdout);
  }
  return alloc;
}


x86 assembly (clang, -O0):
<alloc_friendly>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
movq   $0x10000,-0x28(%rbp)
movl   $0x2,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rdx
lea    -0x20(%rbp),%rdi
call   1080 <posix_memalign@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     11c2 <alloc_friendly+0x62>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
cmpq   $0x10000,-0x8(%rbp)
ja     120c <alloc_friendly+0xac>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    $0x10000,%ecx
mov    $0x2,%r8d
call   1060 <check_cache_friendliness@plt>
cmp    $0x0,%eax
jne    120c <alloc_friendly+0xac>
movq   $0x0,-0x20(%rbp)
jmp    1211 <alloc_friendly+0xb1>
cmpq   $0x0,-0x20(%rbp)
je     118d <alloc_friendly+0x2d>
mov    -0x30(%rbp),%esi
lea    0xdda(%rip),%rdi
call   1030 <printf@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_friendly>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
mov    %rsp,%r12
jmp    118c <alloc_friendly+0x2c>
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rsp),%rax
add    $0x1,%ebx
test   %rax,%rax
jne    11df <alloc_friendly+0x7f>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <posix_memalign@plt>
test   %eax,%eax
jne    120b <alloc_friendly+0xab>
mov    (%rsp),%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1040 <memset@plt>
cmp    $0x10000,%r15
ja     1180 <alloc_friendly+0x20>
mov    (%rsp),%rdi
mov    $0x10000,%ecx
mov    %r15,%rsi
mov    %r14,%rdx
mov    $0x2,%r8d
call   1060 <check_cache_friendliness@plt>
test   %eax,%eax
jne    1180 <alloc_friendly+0x20>
movq   $0x0,(%rsp)
xor    %eax,%eax
jmp    1184 <alloc_friendly+0x24>
lea    0xe1a(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [44 x i8] c"Getting cache-friendly pages took %u tries\0A\00"
@stdout = global i32 0
define i8* @alloc_friendly(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i64 %0, i64* %3
store i64 %1, i64* %4
store i32 1, i32* %5
store i64 65536, i64* %7
store i32 2, i32* %8
store i32 0, i32* %9
br label %11
11: ; preds = %36, %2
%12 = load i32, i32* %9
%13 = add i32 %12, 1
store i32 %13, i32* %9
%14 = load i64, i64* %4
%15 = load i64, i64* %3
%16 = call i32 @posix_memalign(i8** %6, i64 %14, i64 %15)
store i32 %16, i32* %10
%17 = load i32, i32* %10
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %11
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %11
%23 = load i8*, i8** %6
%24 = load i64, i64* %3
%25 = call i32 @memset(i8* %23, i32 0, i64 %24)
%26 = load i64, i64* %3
%27 = icmp ule i64 %26, 65536
br i1 %27, label %28, label %35
28: ; preds = %22
%29 = load i8*, i8** %6
%30 = load i64, i64* %3
%31 = load i64, i64* %4
%32 = call i32 @check_cache_friendliness(i8* %29, i64 %30, i64 %31, i64 65536, i32 2)
%33 = icmp ne i32 %32, 0
br i1 %33, label %35, label %34
34: ; preds = %28
store i8* null, i8** %6
br label %35
35: ; preds = %34, %28, %22
br label %36
36: ; preds = %35
%37 = load i8*, i8** %6
%38 = icmp eq i8* %37, null
br i1 %38, label %11, label %39
39: ; preds = %36
%40 = load i32, i32* %9
%41 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %40)
%42 = load i32, i32* @stdout
%43 = call i32 @fflush(i32 %42)
%44 = load i8*, i8** %6
ret i8* %44
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [44 x i8] c"Getting cache-friendly pages took %u tries\0A\00"
@stdout = global i32 0
define i8* @alloc_friendly(i64 %0, i64 %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = icmp ult i64 %0, 65537
br label %6
6: ; preds = %24, %2
%7 = phi i32 [ 0, %2 ], [ %8, %24 ]
%8 = add i32 %7, 1
%9 = call i32 @posix_memalign(i8** %3, i64 %1, i64 %0)
%10 = icmp eq i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %6
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %6
%15 = load i8*, i8** %3
%16 = call i32 @memset(i8* %15, i32 0, i64 %0)
br i1 %5, label %17, label %22
17: ; preds = %14
%18 = load i8*, i8** %3
%19 = call i32 @check_cache_friendliness(i8* %18, i64 %0, i64 %1, i64 65536, i32 2)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17
store i8* null, i8** %3
br label %24
22: ; preds = %14, %17
%23 = load i8*, i8** %3
br label %24
24: ; preds = %22, %21
%25 = phi i8* [ %23, %22 ], [ null, %21 ]
%26 = icmp eq i8* %25, null
br i1 %26, label %6, label %27
27: ; preds = %24
%28 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %8)
%29 = load i32, i32* @stdout
%30 = call i32 @fflush(i32 %29)
%31 = load i8*, i8** %3
ret i8* %31
}