#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*/ memset(char *, int /*<<< orphan*/, int);
void *realloc(void *, int);
int array_grow(void *buf, int *capacity, int elem_size) {
  void *new_buf;
  int new_capacity = 8;
  if (*capacity >= new_capacity)
    new_capacity = *capacity << 1;
  new_buf = realloc(*(void **)buf, new_capacity * elem_size);
  if (new_buf == NULL)
    return -1;
  memset(((char *)new_buf) + (*capacity * elem_size), 0,
         (new_capacity - *capacity) * elem_size);
  *(void **)buf = new_buf;
  *capacity = new_capacity;
  return 0;
}


x86 assembly (clang, -O0):
<array_grow>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x8,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
cmp    -0x2c(%rbp),%eax
jl     1155 <array_grow+0x35>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
shl    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x2c(%rbp),%esi
imul   -0x1c(%rbp),%esi
call   1040 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1183 <array_grow+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <array_grow+0xa5>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
imul   -0x1c(%rbp),%eax
cltq
add    %rax,%rdi
mov    -0x2c(%rbp),%edx
mov    -0x18(%rbp),%rax
sub    (%rax),%edx
imul   -0x1c(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<array_grow>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r12d
mov    %rsi,%r15
mov    %rdi,%r14
mov    (%rsi),%eax
lea    (%rax,%rax,1),%ecx
cmp    $0x8,%eax
mov    $0x8,%ebp
cmovge %ecx,%ebp
mov    (%rdi),%rdi
mov    %ebp,%esi
imul   %edx,%esi
call   1040 <realloc@plt>
test   %rax,%rax
je     117d <array_grow+0x5d>
mov    %rax,%rbx
mov    (%r15),%eax
mov    %ebp,%edx
sub    %eax,%edx
imul   %r12d,%eax
movslq %eax,%rdi
add    %rbx,%rdi
imul   %r12d,%edx
xor    %r12d,%r12d
xor    %esi,%esi
call   1030 <memset@plt>
mov    %rbx,(%r14)
mov    %ebp,(%r15)
jmp    1183 <array_grow+0x63>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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