#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 */
scalar_t__ calloc(int, unsigned long long);
int /*<<< orphan*/ fail(char *);
unsigned char random();
unsigned char *alignedcalloc(unsigned long long len) {
  unsigned char *x = (unsigned char *)calloc(1, len + 256);
  long long i;
  if (!x)
    fail("out of memory");
  for (i = 0; i < len + 256; ++i)
    x[i] = random();
  x += 64;
  x += 63 & (-(unsigned long)x);
  for (i = 0; i < len; ++i)
    x[i] = 0;
  return x;
}


x86 assembly (clang, -O0):
<alignedcalloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
add    $0x100,%rsi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116c <alignedcalloc+0x3c>
lea    0xe99(%rip),%rdi
call   1030 <fail@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
add    $0x100,%rcx
cmp    %rcx,%rax
jae    11b1 <alignedcalloc+0x81>
mov    $0x0,%al
call   1050 <random@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1174 <alignedcalloc+0x44>
mov    -0x10(%rbp),%rax
add    $0x40,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
and    $0x3f,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jae    1205 <alignedcalloc+0xd5>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11da <alignedcalloc+0xaa>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alignedcalloc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0x100(%rdi),%r15
mov    $0x1,%edi
mov    %r15,%rsi
call   1050 <calloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     11b1 <alignedcalloc+0x71>
test   %r15,%r15
je     11c2 <alignedcalloc+0x82>
mov    $0xffffffffffffff00,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1060 <random@plt>
mov    %al,0x100(%r12,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1180 <alignedcalloc+0x40>
lea    0x40(%r12),%rax
neg    %eax
and    $0x3f,%eax
lea    (%r12,%rax,1),%rbx
add    $0x40,%rbx
test   %r14,%r14
jne    11d4 <alignedcalloc+0x94>
jmp    11e1 <alignedcalloc+0xa1>
lea    0xe48(%rip),%rdi
call   1030 <fail@plt>
test   %r15,%r15
jne    116c <alignedcalloc+0x2c>
lea    0x40(%r12),%rax
neg    %eax
and    $0x3f,%eax
lea    (%r12,%rax,1),%rbx
add    $0x40,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1040 <memset@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"out of memory\00"
define i8* @alignedcalloc(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
store i64 %0, i64* %2
%5 = load i64, i64* %2
%6 = add i64 %5, 256
%7 = call i64 @calloc(i32 1, i64 %6)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %3
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %1
%12 = call i32 @fail(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
store i64 0, i64* %4
br label %14
14: ; preds = %24, %13
%15 = load i64, i64* %4
%16 = load i64, i64* %2
%17 = add i64 %16, 256
%18 = icmp ult i64 %15, %17
br i1 %18, label %19, label %27
19: ; preds = %14
%20 = call zeroext i8 (...) @random()
%21 = load i8*, i8** %3
%22 = load i64, i64* %4
%23 = getelementptr inbounds i8, i8* %21, i64 %22
store i8 %20, i8* %23
br label %24
24: ; preds = %19
%25 = load i64, i64* %4
%26 = add nsw i64 %25, 1
store i64 %26, i64* %4
br label %14
27: ; preds = %14
%28 = load i8*, i8** %3
%29 = getelementptr inbounds i8, i8* %28, i64 64
store i8* %29, i8** %3
%30 = load i8*, i8** %3
%31 = ptrtoint i8* %30 to i64
%32 = sub i64 0, %31
%33 = and i64 63, %32
%34 = load i8*, i8** %3
%35 = getelementptr inbounds i8, i8* %34, i64 %33
store i8* %35, i8** %3
store i64 0, i64* %4
br label %36
36: ; preds = %44, %27
%37 = load i64, i64* %4
%38 = load i64, i64* %2
%39 = icmp ult i64 %37, %38
br i1 %39, label %40, label %47
40: ; preds = %36
%41 = load i8*, i8** %3
%42 = load i64, i64* %4
%43 = getelementptr inbounds i8, i8* %41, i64 %42
store i8 0, i8* %43
br label %44
44: ; preds = %40
%45 = load i64, i64* %4
%46 = add nsw i64 %45, 1
store i64 %46, i64* %4
br label %36
47: ; preds = %36
%48 = load i8*, i8** %3
ret i8* %48
}

LLVM IR (-O2):
@.str = [14 x i8] c"out of memory\00"
define i8* @alignedcalloc(i64 %0) {
%2 = add i64 %0, 256
%3 = tail call i64 @calloc(i32 1, i64 %2)
%4 = inttoptr i64 %3 to i8*
%5 = icmp eq i64 %3, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @fail(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = icmp eq i64 %2, 0
br i1 %9, label %10, label %16
10: ; preds = %8
%11 = getelementptr inbounds i8, i8* %4, i64 64
%12 = ptrtoint i8* %11 to i64
%13 = sub i64 0, %12
%14 = and i64 %13, 63
%15 = getelementptr i8, i8* %11, i64 %14
br label %29
16: ; preds = %8, %16
%17 = phi i64 [ %20, %16 ], [ 0, %8 ]
%18 = tail call zeroext i8 (...) @random()
%19 = getelementptr inbounds i8, i8* %4, i64 %17
store i8 %18, i8* %19
%20 = add nuw i64 %17, 1
%21 = icmp eq i64 %20, %2
br i1 %21, label %22, label %16
22: ; preds = %16
%23 = getelementptr inbounds i8, i8* %4, i64 64
%24 = ptrtoint i8* %23 to i64
%25 = sub i64 0, %24
%26 = and i64 %25, 63
%27 = getelementptr i8, i8* %23, i64 %26
%28 = icmp eq i64 %0, 0
br i1 %28, label %31, label %29
29: ; preds = %10, %22
%30 = phi i8* [ %15, %10 ], [ %27, %22 ]
br label %31
31: ; preds = %29, %22
%32 = phi i8* [ %30, %29 ], [ %27, %22 ]
ret i8* %32
}