#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*/ memory_error();
char *realloc(char *, size_t);
void darray_grow(void *arr, size_t sz, size_t newsize) {
  struct {
    char *data;
    size_t len;
    size_t size;
  } *p = arr;
  while (newsize > p->size) {
    p->size *= 2;
    if (!p->size || !(p->data = realloc(p->data, sz * p->size)))
      memory_error();
  }
}


x86 assembly (clang, -O0):
<darray_grow>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmp    0x10(%rcx),%rax
jbe    11a3 <darray_grow+0x83>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rcx
shl    $0x1,%rcx
mov    %rcx,0x10(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,0x10(%rax)
je     1197 <darray_grow+0x77>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
imul   0x10(%rax),%rsi
call   1030 <realloc@plt>
mov    -0x20(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    119e <darray_grow+0x7e>
mov    $0x0,%al
call   1040 <memory_error@plt>
jmp    113c <darray_grow+0x1c>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<darray_grow>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    0x10(%rdi),%rsi
cmp    %rdx,%rsi
jae    116f <darray_grow+0x4f>
mov    %rdx,%r15
mov    %rdi,%rbx
jmp    1150 <darray_grow+0x30>
nopl   0x0(%rax)
xor    %eax,%eax
call   1040 <memory_error@plt>
mov    0x10(%rbx),%rsi
cmp    %r15,%rsi
jae    116f <darray_grow+0x4f>
add    %rsi,%rsi
mov    %rsi,0x10(%rbx)
je     1140 <darray_grow+0x20>
mov    (%rbx),%rdi
imul   %r14,%rsi
call   1030 <realloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
jne    1147 <darray_grow+0x27>
jmp    1140 <darray_grow+0x20>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.anon = type { i8*, i64, i64 }
define void @darray_grow(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca %struct.anon*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%8 = load i8*, i8** %4
%9 = bitcast i8* %8 to %struct.anon*
store %struct.anon* %9, %struct.anon** %7
br label %10
10: ; preds = %40, %3
%11 = load i64, i64* %6
%12 = load %struct.anon*, %struct.anon** %7
%13 = getelementptr inbounds %struct.anon, %struct.anon* %12, i32 0, i32 2
%14 = load i64, i64* %13
%15 = icmp ugt i64 %11, %14
br i1 %15, label %16, label %41
16: ; preds = %10
%17 = load %struct.anon*, %struct.anon** %7
%18 = getelementptr inbounds %struct.anon, %struct.anon* %17, i32 0, i32 2
%19 = load i64, i64* %18
%20 = mul i64 %19, 2
store i64 %20, i64* %18
%21 = load %struct.anon*, %struct.anon** %7
%22 = getelementptr inbounds %struct.anon, %struct.anon* %21, i32 0, i32 2
%23 = load i64, i64* %22
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %38
25: ; preds = %16
%26 = load %struct.anon*, %struct.anon** %7
%27 = getelementptr inbounds %struct.anon, %struct.anon* %26, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = load i64, i64* %5
%30 = load %struct.anon*, %struct.anon** %7
%31 = getelementptr inbounds %struct.anon, %struct.anon* %30, i32 0, i32 2
%32 = load i64, i64* %31
%33 = mul i64 %29, %32
%34 = call i8* @realloc(i8* %28, i64 %33)
%35 = load %struct.anon*, %struct.anon** %7
%36 = getelementptr inbounds %struct.anon, %struct.anon* %35, i32 0, i32 0
store i8* %34, i8** %36
%37 = icmp ne i8* %34, null
br i1 %37, label %40, label %38
38: ; preds = %25, %16
%39 = call i32 (...) @memory_error()
br label %40
40: ; preds = %38, %25
br label %10
41: ; preds = %10
ret void
}

LLVM IR (-O2):
define void @darray_grow(i8* nocapture %0, i64 %1, i64 %2) {
%4 = getelementptr inbounds i8, i8* %0, i64 16
%5 = bitcast i8* %4 to i64*
%6 = load i64, i64* %5
%7 = icmp ult i64 %6, %2
br i1 %7, label %8, label %24
8: ; preds = %3
%9 = bitcast i8* %0 to i8**
br label %10
10: ; preds = %8, %21
%11 = phi i64 [ %6, %8 ], [ %22, %21 ]
%12 = shl i64 %11, 1
store i64 %12, i64* %5
%13 = icmp eq i64 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %10
%15 = load i8*, i8** %9
%16 = mul i64 %12, %1
%17 = tail call i8* @realloc(i8* %15, i64 %16)
store i8* %17, i8** %9
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %14, %10
%20 = tail call i32 (...) @memory_error()
br label %21
21: ; preds = %19, %14
%22 = load i64, i64* %5
%23 = icmp ult i64 %22, %2
br i1 %23, label %10, label %24
24: ; preds = %21, %3
ret void
}