#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char **realloc(char **, size_t);
int /*<<< orphan*/ stderr;
char **grow(char **result, size_t count, char *ptr) {
  result = realloc(result, (count + 1) * sizeof(char *));
  if (!result) {
    fprintf(stderr, "Memory allocation error.\n");
    exit(1);
  }
  result[count - 1] = ptr;
  result[count] = NULL;
  return result;
}


x86 assembly (clang, -O0):
<grow>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
shl    $0x3,%rsi
call   1040 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1187 <grow+0x57>
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%rdx
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grow>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
lea    0x8(,%rsi,8),%rsi
call   1040 <realloc@plt>
test   %rax,%rax
je     1161 <grow+0x31>
mov    %r14,-0x8(%rax,%rbx,8)
movq   $0x0,(%rax,%rbx,8)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
lea    0xe8f(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"Memory allocation error.\0A\00"
define i8** @grow(i8** %0, i64 %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i8*
store i8** %0, i8*** %4
store i64 %1, i64* %5
store i8* %2, i8** %6
%7 = load i8**, i8*** %4
%8 = load i64, i64* %5
%9 = add i64 %8, 1
%10 = mul i64 %9, 8
%11 = call i8** @realloc(i8** %7, i64 %10)
store i8** %11, i8*** %4
%12 = load i8**, i8*** %4
%13 = icmp ne i8** %12, null
br i1 %13, label %18, label %14
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %3
%19 = load i8*, i8** %6
%20 = load i8**, i8*** %4
%21 = load i64, i64* %5
%22 = sub i64 %21, 1
%23 = getelementptr inbounds i8*, i8** %20, i64 %22
store i8* %19, i8** %23
%24 = load i8**, i8*** %4
%25 = load i64, i64* %5
%26 = getelementptr inbounds i8*, i8** %24, i64 %25
store i8* null, i8** %26
%27 = load i8**, i8*** %4
ret i8** %27
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"Memory allocation error.\0A\00"
define noalias i8** @grow(i8** nocapture %0, i64 %1, i8* %2) {
%4 = shl i64 %1, 3
%5 = add i64 %4, 8
%6 = tail call i8** @realloc(i8** %0, i64 %5)
%7 = icmp eq i8** %6, null
br i1 %7, label %8, label %12
8: ; preds = %3
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %3
%13 = add i64 %1, -1
%14 = getelementptr inbounds i8*, i8** %6, i64 %13
store i8* %2, i8** %14
%15 = getelementptr inbounds i8*, i8** %6, i64 %1
store i8* null, i8** %15
ret i8** %6
}