#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*/ log_message(char *, char *);
scalar_t__ realloc(char *, size_t);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
char *helper_serialize_str_array(char **array, size_t len) {
  char *serialized = NULL;
  size_t i, l, s_len;
  l = strlen(array[0]);
  s_len = l + 2;
  serialized = (char *)realloc(serialized, s_len);
  strcpy(serialized, array[0]);
  strcat(serialized, "\31");
  for (i = 1; i < len; ++i) {
    l = strlen(array[i]);
    s_len += l + 1;
    serialized = (char *)realloc(serialized, s_len);
    if (serialized == NULL) {
      log_message("[ERROR][%d-%d-%d %d:%d:%d] ",
                  "Ran out of memory when serializing string\r\n");
      return NULL;
    }
    strcat(serialized, array[i]);
    strcat(serialized, "\31");
  }
  serialized[s_len - 2] = '\0';
  return serialized;
}


x86 assembly (clang, -O0):
<helper_serialize_str_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   1070 <strcat@plt>
movq   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1262 <helper_serialize_str_array+0x112>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    122c <helper_serialize_str_array+0xdc>
lea    0xdef(%rip),%rdi
lea    0xe04(%rip),%rsi
call   1060 <log_message@plt>
movq   $0x0,-0x8(%rbp)
jmp    127a <helper_serialize_str_array+0x12a>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1070 <strcat@plt>
mov    -0x20(%rbp),%rdi
lea    0xdb4(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11bd <helper_serialize_str_array+0x6d>
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    $0x2,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<helper_serialize_str_array>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rdi),%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
add    $0x2,%r12
xor    %edi,%edi
mov    %r12,%rsi
call   1050 <realloc@plt>
mov    %rax,%rbx
mov    (%r15),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
lea    0xe71(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcat@plt>
cmp    $0x2,%r14
jb     11f3 <helper_serialize_str_array+0xa3>
mov    $0x1,%ebp
lea    0xe57(%rip),%r13
nopl   0x0(%rax)
mov    (%r15,%rbp,8),%rdi
call   1040 <strlen@plt>
add    %rax,%r12
add    $0x1,%r12
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <realloc@plt>
test   %rax,%rax
je     11fb <helper_serialize_str_array+0xab>
mov    %rax,%rbx
mov    (%r15,%rbp,8),%rsi
mov    %rax,%rdi
call   1070 <strcat@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1070 <strcat@plt>
add    $0x1,%rbp
cmp    %rbp,%r14
jne    11b0 <helper_serialize_str_array+0x60>
movb   $0x0,-0x2(%r12,%rbx,1)
jmp    1210 <helper_serialize_str_array+0xc0>
lea    0xe00(%rip),%rdi
lea    0xe15(%rip),%rsi
call   1060 <log_message@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"\19\00"
@.str.1 = [28 x i8] c"[ERROR][%d-%d-%d %d:%d:%d] \00"
@.str.2 = [44 x i8] c"Ran out of memory when serializing string\0D\0A\00"
define i8* @helper_serialize_str_array(i8** %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i8** %0, i8*** %4
store i64 %1, i64* %5
store i8* null, i8** %6
%10 = load i8**, i8*** %4
%11 = getelementptr inbounds i8*, i8** %10, i64 0
%12 = load i8*, i8** %11
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %8
%14 = load i64, i64* %8
%15 = add i64 %14, 2
store i64 %15, i64* %9
%16 = load i8*, i8** %6
%17 = load i64, i64* %9
%18 = call i64 @realloc(i8* %16, i64 %17)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = load i8**, i8*** %4
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 @strcpy(i8* %20, i8* %23)
%25 = load i8*, i8** %6
%26 = call i32 @strcat(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i64 1, i64* %7
br label %27
27: ; preds = %58, %2
%28 = load i64, i64* %7
%29 = load i64, i64* %5
%30 = icmp ult i64 %28, %29
br i1 %30, label %31, label %61
31: ; preds = %27
%32 = load i8**, i8*** %4
%33 = load i64, i64* %7
%34 = getelementptr inbounds i8*, i8** %32, i64 %33
%35 = load i8*, i8** %34
%36 = call i64 @strlen(i8* %35)
store i64 %36, i64* %8
%37 = load i64, i64* %8
%38 = add i64 %37, 1
%39 = load i64, i64* %9
%40 = add i64 %39, %38
store i64 %40, i64* %9
%41 = load i8*, i8** %6
%42 = load i64, i64* %9
%43 = call i64 @realloc(i8* %41, i64 %42)
%44 = inttoptr i64 %43 to i8*
store i8* %44, i8** %6
%45 = load i8*, i8** %6
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %49
47: ; preds = %31
%48 = call i32 @log_message(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %3
br label %67
49: ; preds = %31
%50 = load i8*, i8** %6
%51 = load i8**, i8*** %4
%52 = load i64, i64* %7
%53 = getelementptr inbounds i8*, i8** %51, i64 %52
%54 = load i8*, i8** %53
%55 = call i32 @strcat(i8* %50, i8* %54)
%56 = load i8*, i8** %6
%57 = call i32 @strcat(i8* %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %58
58: ; preds = %49
%59 = load i64, i64* %7
%60 = add i64 %59, 1
store i64 %60, i64* %7
br label %27
61: ; preds = %27
%62 = load i8*, i8** %6
%63 = load i64, i64* %9
%64 = sub i64 %63, 2
%65 = getelementptr inbounds i8, i8* %62, i64 %64
store i8 0, i8* %65
%66 = load i8*, i8** %6
store i8* %66, i8** %3
br label %67
67: ; preds = %61, %47
%68 = load i8*, i8** %3
ret i8* %68
}

LLVM IR (-O2):
@.str = [2 x i8] c"\19\00"
@.str.1 = [28 x i8] c"[ERROR][%d-%d-%d %d:%d:%d] \00"
@.str.2 = [44 x i8] c"Ran out of memory when serializing string\0D\0A\00"
define i8* @helper_serialize_str_array(i8** nocapture %0, i64 %1) {
%3 = load i8*, i8** %0
%4 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%5 = add i64 %4, 2
%6 = tail call i64 @realloc(i8* null, i64 %5)
%7 = inttoptr i64 %6 to i8*
%8 = load i8*, i8** %0
%9 = tail call i32 @strcpy(i8* %7, i8* %8)
%10 = tail call i32 @strcat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp ugt i64 %1, 1
br i1 %11, label %12, label %34
12: ; preds = %2, %26
%13 = phi i64 [ %20, %26 ], [ %5, %2 ]
%14 = phi i64 [ %30, %26 ], [ 1, %2 ]
%15 = phi i8* [ %22, %26 ], [ %7, %2 ]
%16 = getelementptr inbounds i8*, i8** %0, i64 %14
%17 = load i8*, i8** %16
%18 = tail call i64 @strlen(i8* dereferenceable(1) %17)
%19 = add i64 %13, 1
%20 = add i64 %19, %18
%21 = tail call i64 @realloc(i8* %15, i64 %20)
%22 = inttoptr i64 %21 to i8*
%23 = icmp eq i64 %21, 0
br i1 %23, label %24, label %26
24: ; preds = %12
%25 = tail call i32 @log_message(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
br label %39
26: ; preds = %12
%27 = load i8*, i8** %16
%28 = tail call i32 @strcat(i8* %22, i8* %27)
%29 = tail call i32 @strcat(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%30 = add nuw i64 %14, 1
%31 = icmp eq i64 %30, %1
br i1 %31, label %32, label %12
32: ; preds = %26
%33 = inttoptr i64 %21 to i8*
br label %34
34: ; preds = %32, %2
%35 = phi i8* [ %7, %2 ], [ %33, %32 ]
%36 = phi i64 [ %5, %2 ], [ %20, %32 ]
%37 = add i64 %36, -2
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 0, i8* %38
br label %39
39: ; preds = %34, %24
%40 = phi i8* [ null, %24 ], [ %35, %34 ]
ret i8* %40
}