#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*/ str_append(char **, char const *);
char *strdup(char *);
char *str_array_implode(const char **array, const char *separator) {
  int i = 0;
  char *out = strdup("");
  while (array[i]) {
    if (i > 0)
      str_append(&out, separator);
    str_append(&out, array[i++]);
  }
  return out;
}


x86 assembly (clang, -O0):
<str_array_implode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xec2(%rip),%rdi
call   1040 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1195 <str_array_implode+0x75>
cmpl   $0x0,-0x14(%rbp)
jle    1171 <str_array_implode+0x51>
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdi
call   1030 <str_append@plt>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x14(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rsi
lea    -0x20(%rbp),%rdi
call   1030 <str_append@plt>
jmp    1147 <str_array_implode+0x27>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_array_implode>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xeca(%rip),%rdi
call   1040 <strdup@plt>
mov    %rax,0x8(%rsp)
mov    (%rbx),%rsi
test   %rsi,%rsi
je     1196 <str_array_implode+0x76>
lea    0x8(%rsp),%rdi
call   1030 <str_append@plt>
cmpq   $0x0,0x8(%rbx)
je     1191 <str_array_implode+0x71>
add    $0x10,%rbx
lea    0x8(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <str_append@plt>
mov    -0x8(%rbx),%rsi
mov    %r15,%rdi
call   1030 <str_append@plt>
cmpq   $0x0,(%rbx)
lea    0x8(%rbx),%rbx
jne    1170 <str_array_implode+0x50>
mov    0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i8* @str_array_implode(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8** %0, i8*** %3
store i8* %1, i8** %4
store i32 0, i32* %5
%7 = call i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %6
br label %8
8: ; preds = %21, %2
%9 = load i8**, i8*** %3
%10 = load i32, i32* %5
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8*, i8** %9, i64 %11
%13 = load i8*, i8** %12
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %29
15: ; preds = %8
%16 = load i32, i32* %5
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i8*, i8** %4
%20 = call i32 @str_append(i8** %6, i8* %19)
br label %21
21: ; preds = %18, %15
%22 = load i8**, i8*** %3
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8*, i8** %22, i64 %25
%27 = load i8*, i8** %26
%28 = call i32 @str_append(i8** %6, i8* %27)
br label %8
29: ; preds = %8
%30 = load i8*, i8** %6
ret i8* %30
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @str_array_implode(i8** nocapture %0, i8* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = tail call dereferenceable_or_null(1) i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %5, i8** %3
%6 = load i8*, i8** %0
%7 = icmp eq i8* %6, null
br i1 %7, label %25, label %8
8: ; preds = %2
%9 = call i32 @str_append(i8** %3, i8* %6)
%10 = getelementptr inbounds i8*, i8** %0, i64 1
%11 = load i8*, i8** %10
%12 = icmp eq i8* %11, null
br i1 %12, label %23, label %13
13: ; preds = %8, %13
%14 = phi i64 [ %18, %13 ], [ 1, %8 ]
%15 = phi i8** [ %20, %13 ], [ %10, %8 ]
%16 = call i32 @str_append(i8** %3, i8* %1)
%17 = load i8*, i8** %15
%18 = add nuw i64 %14, 1
%19 = call i32 @str_append(i8** %3, i8* %17)
%20 = getelementptr inbounds i8*, i8** %0, i64 %18
%21 = load i8*, i8** %20
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %13
23: ; preds = %13, %8
%24 = load i8*, i8** %3
br label %25
25: ; preds = %23, %2
%26 = phi i8* [ %24, %23 ], [ %5, %2 ]
ret i8* %26
}