#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 *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
scalar_t__ realloc(char **, int);
int /*<<< orphan*/ stderr;
void add_to_string_array(char ***array, char *string, size_t length,
                         int num_entries) {
  char **elem;
  if (!(num_entries % 10)) {
    *array = (char **)realloc(*array, sizeof(char *) * (num_entries + 10));
    if (!(*array)) {
      fprintf(stderr, "[Error] Couldn't allocate memory for keys/values!\n");
      exit(1);
    }
  }
  elem = (*array) + num_entries;
  *elem = (char *)malloc(sizeof(char) * (length + 1));
  if (!(*elem)) {
    fprintf(stderr, "[Error] Couldn't allocate memory for keys/values!\n");
    exit(1);
  }
  memcpy(*elem, string, length * sizeof(char));
  (*elem)[length] = 0;
}


x86 assembly (clang, -O0):
<add_to_string_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11d1 <add_to_string_array+0x81>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x1c(%rbp),%eax
add    $0xa,%eax
cltq
shl    $0x3,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11cc <add_to_string_array+0x7c>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11d1 <add_to_string_array+0x81>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x1c(%rbp),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1231 <add_to_string_array+0xe1>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    0xdde(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
shl    $0x0,%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_to_string_array>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %ecx,%r12d
mov    %rdx,%r15
mov    %rsi,%r14
imul   $0xcccccccd,%ecx,%eax
add    $0x19999998,%eax
ror    %eax
mov    (%rdi),%rbx
cmp    $0x19999998,%eax
ja     1198 <add_to_string_array+0x48>
mov    %rdi,%r13
lea    0x50(,%r12,8),%esi
mov    %rbx,%rdi
call   1060 <realloc@plt>
mov    %rax,%rbx
mov    %rax,0x0(%r13)
test   %rax,%rax
je     11ce <add_to_string_array+0x7e>
movslq %r12d,%r12
lea    0x1(%r15),%edi
call   1050 <malloc@plt>
mov    %rax,(%rbx,%r12,8)
test   %rax,%rax
je     11ce <add_to_string_array+0x7e>
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
mov    (%rbx,%r12,8),%rax
movb   $0x0,(%rax,%r15,1)
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [51 x i8] c"[Error] Couldn't allocate memory for keys/values!\0A\00"
define void @add_to_string_array(i8*** %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i8***
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8**
store i8*** %0, i8**** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 %3, i32* %8
%10 = load i32, i32* %8
%11 = srem i32 %10, 10
%12 = icmp ne i32 %11, 0
br i1 %12, label %32, label %13
13: ; preds = %4
%14 = load i8***, i8**** %5
%15 = load i8**, i8*** %14
%16 = load i32, i32* %8
%17 = add nsw i32 %16, 10
%18 = sext i32 %17 to i64
%19 = mul i64 8, %18
%20 = trunc i64 %19 to i32
%21 = call i64 @realloc(i8** %15, i32 %20)
%22 = inttoptr i64 %21 to i8**
%23 = load i8***, i8**** %5
store i8** %22, i8*** %23
%24 = load i8***, i8**** %5
%25 = load i8**, i8*** %24
%26 = icmp ne i8** %25, null
br i1 %26, label %31, label %27
27: ; preds = %13
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %13
br label %32
32: ; preds = %31, %4
%33 = load i8***, i8**** %5
%34 = load i8**, i8*** %33
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8*, i8** %34, i64 %36
store i8** %37, i8*** %9
%38 = load i64, i64* %7
%39 = add i64 %38, 1
%40 = mul i64 1, %39
%41 = trunc i64 %40 to i32
%42 = call i64 @malloc(i32 %41)
%43 = inttoptr i64 %42 to i8*
%44 = load i8**, i8*** %9
store i8* %43, i8** %44
%45 = load i8**, i8*** %9
%46 = load i8*, i8** %45
%47 = icmp ne i8* %46, null
br i1 %47, label %52, label %48
48: ; preds = %32
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %32
%53 = load i8**, i8*** %9
%54 = load i8*, i8** %53
%55 = load i8*, i8** %6
%56 = load i64, i64* %7
%57 = mul i64 %56, 1
%58 = call i32 @memcpy(i8* %54, i8* %55, i64 %57)
%59 = load i8**, i8*** %9
%60 = load i8*, i8** %59
%61 = load i64, i64* %7
%62 = getelementptr inbounds i8, i8* %60, i64 %61
store i8 0, i8* %62
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [51 x i8] c"[Error] Couldn't allocate memory for keys/values!\0A\00"
define void @add_to_string_array(i8*** nocapture %0, i8* %1, i64 %2, i32 %3) {
%5 = srem i32 %3, 10
%6 = icmp eq i32 %5, 0
%7 = load i8**, i8*** %0
br i1 %6, label %8, label %18
8: ; preds = %4
%9 = shl i32 %3, 3
%10 = add i32 %9, 80
%11 = tail call i64 @realloc(i8** %7, i32 %10)
%12 = inttoptr i64 %11 to i8**
store i8** %12, i8*** %0
%13 = icmp eq i64 %11, 0
br i1 %13, label %14, label %18
14: ; preds = %8
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %4, %8
%19 = phi i8** [ %12, %8 ], [ %7, %4 ]
%20 = sext i32 %3 to i64
%21 = getelementptr inbounds i8*, i8** %19, i64 %20
%22 = trunc i64 %2 to i32
%23 = add i32 %22, 1
%24 = tail call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %21
%26 = icmp eq i64 %24, 0
br i1 %26, label %27, label %31
27: ; preds = %18
%28 = load i32, i32* @stderr
%29 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %18
%32 = tail call i32 @memcpy(i8* %25, i8* %1, i64 %2)
%33 = load i8*, i8** %21
%34 = getelementptr inbounds i8, i8* %33, i64 %2
store i8 0, i8* %34
ret void
}