#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 */
void *malloc(size_t);
void *realloc(void *, int);
void *array_size_check(void *arraymem, size_t size, int n) {
  if (n == 0) {
    return malloc(size);
  } else if ((n & (n - 1)) == 0) {
    return realloc(arraymem, (n << 1) * size);
  }
  return arraymem;
}


x86 assembly (clang, -O0):
<array_size_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    114f <array_size_check+0x2f>
mov    -0x18(%rbp),%rdi
call   1030 <malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    1191 <array_size_check+0x71>
mov    -0x1c(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
jne    1184 <array_size_check+0x64>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
cltq
imul   -0x18(%rbp),%rax
mov    %eax,%esi
call   1040 <realloc@plt>
mov    %rax,-0x8(%rbp)
jmp    1191 <array_size_check+0x71>
jmp    1189 <array_size_check+0x69>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<array_size_check>:
test   %edx,%edx
je     1137 <array_size_check+0x17>
lea    -0x1(%rdx),%eax
test   %eax,%edx
jne    113f <array_size_check+0x1f>
imul   %esi,%edx
add    %edx,%edx
mov    %edx,%esi
jmp    1040 <realloc@plt>
mov    %rsi,%rdi
jmp    1030 <malloc@plt>
mov    %rdi,%rax
ret

LLVM IR (-O0):
define i8* @array_size_check(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%8 = load i32, i32* %7
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = load i64, i64* %6
%12 = call i8* @malloc(i64 %11)
store i8* %12, i8** %4
br label %31
13: ; preds = %3
%14 = load i32, i32* %7
%15 = load i32, i32* %7
%16 = sub nsw i32 %15, 1
%17 = and i32 %14, %16
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %13
%20 = load i8*, i8** %5
%21 = load i32, i32* %7
%22 = shl i32 %21, 1
%23 = sext i32 %22 to i64
%24 = load i64, i64* %6
%25 = mul i64 %23, %24
%26 = trunc i64 %25 to i32
%27 = call i8* @realloc(i8* %20, i32 %26)
store i8* %27, i8** %4
br label %31
28: ; preds = %13
br label %29
29: ; preds = %28
%30 = load i8*, i8** %5
store i8* %30, i8** %4
br label %31
31: ; preds = %29, %19, %10
%32 = load i8*, i8** %4
ret i8* %32
}

LLVM IR (-O2):
define i8* @array_size_check(i8* %0, i64 %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i8* @malloc(i64 %1)
br label %15
7: ; preds = %3
%8 = tail call i32 @llvm.ctpop.i32(i32 %2)
%9 = icmp ult i32 %8, 2
br i1 %9, label %10, label %15
10: ; preds = %7
%11 = shl i32 %2, 1
%12 = trunc i64 %1 to i32
%13 = mul i32 %11, %12
%14 = tail call i8* @realloc(i8* %0, i32 %13)
br label %15
15: ; preds = %7, %10, %5
%16 = phi i8* [ %6, %5 ], [ %14, %10 ], [ %0, %7 ]
ret i8* %16
}