#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 */
char **realloc(char **, int);
int extendarray(char ***buf, int *buflen, int needed) {
  if (needed > *buflen) {
    char **tmp = realloc(*buf, needed * sizeof(char *));
    if (tmp == NULL)
      return -1;
    *buf = tmp;
    *buflen = needed;
  }
  return *buflen;
}


x86 assembly (clang, -O0):
<extendarray>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jle    1177 <extendarray+0x67>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
movslq -0x1c(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1030 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1163 <extendarray+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    1180 <extendarray+0x70>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extendarray>:
push   %r15
push   %r14
push   %rbx
mov    (%rsi),%eax
cmp    %edx,%eax
jge    1146 <extendarray+0x36>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rdi),%rdi
lea    0x0(,%rbx,8),%esi
call   1030 <realloc@plt>
test   %rax,%rax
je     1141 <extendarray+0x31>
mov    %rax,(%r15)
mov    %ebx,(%r14)
mov    %ebx,%eax
jmp    1146 <extendarray+0x36>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32 @extendarray(i8*** nocapture %0, i32* nocapture %1, i32 %2) {
%4 = load i32, i32* %1
%5 = icmp slt i32 %4, %2
br i1 %5, label %6, label %12
6: ; preds = %3
%7 = load i8**, i8*** %0
%8 = shl i32 %2, 3
%9 = tail call i8** @realloc(i8** %7, i32 %8)
%10 = icmp eq i8** %9, null
br i1 %10, label %12, label %11
11: ; preds = %6
store i8** %9, i8*** %0
store i32 %2, i32* %1
br label %12
12: ; preds = %3, %11, %6
%13 = phi i32 [ -1, %6 ], [ %2, %11 ], [ %4, %3 ]
ret i32 %13
}