#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 */
unsigned int UINT_MAX;
int /*<<< orphan*/ assert(int);
unsigned darray_next_alloc(unsigned alloc, unsigned need, unsigned itemSize) {
  assert(need < UINT_MAX / itemSize / 2);
  if (alloc == 0)
    alloc = 4;
  while (alloc < need)
    alloc *= 2;
  return alloc;
}


x86 assembly (clang, -O0):
<darray_next_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2eba(%rip),%rax
mov    (%rax),%eax
xor    %edx,%edx
divl   -0xc(%rbp)
mov    %eax,%ecx
mov    -0x10(%rbp),%eax
shr    $0x1,%ecx
cmp    %ecx,%eax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpl   $0x0,-0x4(%rbp)
jne    115d <darray_next_alloc+0x4d>
movl   $0x4,-0x4(%rbp)
jmp    1162 <darray_next_alloc+0x52>
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    117c <darray_next_alloc+0x6c>
mov    -0x4(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1162 <darray_next_alloc+0x52>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<darray_next_alloc>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ecx
mov    %esi,%ebx
mov    %edi,%ebp
mov    0x2ec8(%rip),%rax
mov    (%rax),%eax
xor    %edx,%edx
div    %ecx
shr    %eax
xor    %edi,%edi
cmp    %esi,%eax
seta   %dil
call   1030 <assert@plt>
test   %ebp,%ebp
mov    $0x4,%ecx
cmovne %ebp,%ecx
nop
mov    %ecx,%eax
lea    (%rax,%rax,1),%ecx
cmp    %ebx,%eax
jb     1140 <darray_next_alloc+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@UINT_MAX = global i32 0
define i32 @darray_next_alloc(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%7 = load i32, i32* %5
%8 = load i32, i32* @UINT_MAX
%9 = load i32, i32* %6
%10 = udiv i32 %8, %9
%11 = udiv i32 %10, 2
%12 = icmp ult i32 %7, %11
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 4, i32* %4
br label %18
18: ; preds = %17, %3
br label %19
19: ; preds = %23, %18
%20 = load i32, i32* %4
%21 = load i32, i32* %5
%22 = icmp ult i32 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* %4
%25 = mul i32 %24, 2
store i32 %25, i32* %4
br label %19
26: ; preds = %19
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
@UINT_MAX = global i32 0
define i32 @darray_next_alloc(i32 %0, i32 %1, i32 %2) {
%4 = load i32, i32* @UINT_MAX
%5 = udiv i32 %4, %2
%6 = lshr i32 %5, 1
%7 = icmp ugt i32 %6, %1
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp eq i32 %0, 0
%11 = select i1 %10, i32 4, i32 %0
br label %12
12: ; preds = %12, %3
%13 = phi i32 [ %11, %3 ], [ %15, %12 ]
%14 = icmp ult i32 %13, %1
%15 = shl i32 %13, 1
br i1 %14, label %12, label %16
16: ; preds = %12
ret i32 %13
}