#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 *trunnel_reallocarray(void *, size_t, size_t);
void *trunnel_dynarray_expand(size_t *allocated_p, void *ptr,
                              size_t howmanymore, size_t eltsize) {
  size_t newsize = howmanymore + *allocated_p;
  void *newarray = NULL;
  if (newsize < 8)
    newsize = 8;
  if (newsize < *allocated_p * 2)
    newsize = *allocated_p * 2;
  if (newsize <= *allocated_p || newsize < howmanymore)
    return NULL;
  newarray = trunnel_reallocarray(ptr, newsize, eltsize);
  if (newarray == NULL)
    return NULL;
  *allocated_p = newsize;
  return newarray;
}


x86 assembly (clang, -O0):
<trunnel_dynarray_expand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    (%rcx),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x8,-0x30(%rbp)
jae    1152 <trunnel_dynarray_expand+0x42>
movq   $0x8,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%rcx
shl    $0x1,%rcx
cmp    %rcx,%rax
jae    1179 <trunnel_dynarray_expand+0x69>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
shl    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%rax
jbe    1198 <trunnel_dynarray_expand+0x88>
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11a5 <trunnel_dynarray_expand+0x95>
movq   $0x0,-0x8(%rbp)
jmp    11e5 <trunnel_dynarray_expand+0xd5>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <trunnel_reallocarray@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11d2 <trunnel_dynarray_expand+0xc2>
movq   $0x0,-0x8(%rbp)
jmp    11e5 <trunnel_dynarray_expand+0xd5>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trunnel_dynarray_expand>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    (%rdi),%rdi
lea    (%rdi,%rdx,1),%rax
cmp    $0x9,%rax
mov    $0x8,%ebx
cmovae %rax,%rbx
lea    (%rdi,%rdi,1),%rax
cmp    %rax,%rbx
cmovb  %rax,%rbx
xor    %eax,%eax
cmp    %rdi,%rbx
jbe    115c <trunnel_dynarray_expand+0x4c>
cmp    %rdx,%rbx
jb     115c <trunnel_dynarray_expand+0x4c>
mov    %rsi,%rdi
mov    %rbx,%rsi
mov    %rcx,%rdx
call   1030 <trunnel_reallocarray@plt>
test   %rax,%rax
je     115a <trunnel_dynarray_expand+0x4a>
mov    %rbx,(%r14)
jmp    115c <trunnel_dynarray_expand+0x4c>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @trunnel_dynarray_expand(i64* %0, i8* %1, i64 %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
store i64* %0, i64** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
%12 = load i64, i64* %8
%13 = load i64*, i64** %6
%14 = load i64, i64* %13
%15 = add i64 %12, %14
store i64 %15, i64* %10
store i8* null, i8** %11
%16 = load i64, i64* %10
%17 = icmp ult i64 %16, 8
br i1 %17, label %18, label %19
18: ; preds = %4
store i64 8, i64* %10
br label %19
19: ; preds = %18, %4
%20 = load i64, i64* %10
%21 = load i64*, i64** %6
%22 = load i64, i64* %21
%23 = mul i64 %22, 2
%24 = icmp ult i64 %20, %23
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = load i64*, i64** %6
%27 = load i64, i64* %26
%28 = mul i64 %27, 2
store i64 %28, i64* %10
br label %29
29: ; preds = %25, %19
%30 = load i64, i64* %10
%31 = load i64*, i64** %6
%32 = load i64, i64* %31
%33 = icmp ule i64 %30, %32
br i1 %33, label %38, label %34
34: ; preds = %29
%35 = load i64, i64* %10
%36 = load i64, i64* %8
%37 = icmp ult i64 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %34, %29
store i8* null, i8** %5
br label %51
39: ; preds = %34
%40 = load i8*, i8** %7
%41 = load i64, i64* %10
%42 = load i64, i64* %9
%43 = call i8* @trunnel_reallocarray(i8* %40, i64 %41, i64 %42)
store i8* %43, i8** %11
%44 = load i8*, i8** %11
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %47
46: ; preds = %39
store i8* null, i8** %5
br label %51
47: ; preds = %39
%48 = load i64, i64* %10
%49 = load i64*, i64** %6
store i64 %48, i64* %49
%50 = load i8*, i8** %11
store i8* %50, i8** %5
br label %51
51: ; preds = %47, %46, %38
%52 = load i8*, i8** %5
ret i8* %52
}

LLVM IR (-O2):
define i8* @trunnel_dynarray_expand(i64* nocapture %0, i8* %1, i64 %2, i64 %3) {
%5 = load i64, i64* %0
%6 = add i64 %5, %2
%7 = icmp ugt i64 %6, 8
%8 = select i1 %7, i64 %6, i64 8
%9 = shl i64 %5, 1
%10 = icmp ult i64 %8, %9
%11 = select i1 %10, i64 %9, i64 %8
%12 = icmp ule i64 %11, %5
%13 = icmp ult i64 %11, %2
%14 = or i1 %12, %13
br i1 %14, label %19, label %15
15: ; preds = %4
%16 = tail call i8* @trunnel_reallocarray(i8* %1, i64 %11, i64 %3)
%17 = icmp eq i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %15
store i64 %11, i64* %0
br label %19
19: ; preds = %15, %4, %18
%20 = phi i8* [ %16, %18 ], [ null, %4 ], [ null, %15 ]
ret i8* %20
}