#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 *astrcat(char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
char *strjoin(char **strings, size_t count, char *joiner) {
  if (count == 0) {
    char *out = malloc(1);
    *out = 0;
    return out;
  }
  char *out1, *out2, *temp;
  temp = astrcat(*strings, joiner);
  out1 = astrcat(temp, *(++strings));
  free(temp);
  while (--count > 1) {
    temp = astrcat(out1, joiner);
    out2 = astrcat(temp, *(++strings));
    free(temp);
    free(out1);
    out1 = out2;
  }
  return out1;
}


x86 assembly (clang, -O0):
<strjoin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1171 <strjoin+0x41>
mov    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    121c <strjoin+0xec>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <astrcat@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rsi
call   1050 <astrcat@plt>
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
cmp    $0x1,%rax
jbe    1214 <strjoin+0xe4>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <astrcat@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rsi
call   1050 <astrcat@plt>
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11ae <strjoin+0x7e>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strjoin>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     11bf <strjoin+0x8f>
mov    %rdx,%r14
mov    %rsi,%r13
mov    %rdi,%r15
mov    (%rdi),%rdi
mov    %rdx,%rsi
call   1050 <astrcat@plt>
mov    %rax,%rbx
mov    0x8(%r15),%rsi
mov    %rax,%rdi
call   1050 <astrcat@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0xffffffffffffffff,%r13
cmp    $0x2,%r13
jb     11d1 <strjoin+0xa1>
add    $0x10,%r15
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r14,%rsi
call   1050 <astrcat@plt>
mov    %rax,%rbp
mov    (%r15),%rsi
mov    %rax,%rdi
call   1050 <astrcat@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
add    $0xffffffffffffffff,%r13
add    $0x8,%r15
mov    %rbx,%r12
cmp    $0x1,%r13
ja     1180 <strjoin+0x50>
jmp    11d4 <strjoin+0xa4>
mov    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
movb   $0x0,(%rax)
jmp    11d4 <strjoin+0xa4>
mov    %r12,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @strjoin(i8** %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i8** %0, i8*** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%12 = load i64, i64* %6
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = call i8* @malloc(i32 1)
store i8* %15, i8** %8
%16 = load i8*, i8** %8
store i8 0, i8* %16
%17 = load i8*, i8** %8
store i8* %17, i8** %4
br label %50
18: ; preds = %3
%19 = load i8**, i8*** %5
%20 = load i8*, i8** %19
%21 = load i8*, i8** %7
%22 = call i8* @astrcat(i8* %20, i8* %21)
store i8* %22, i8** %11
%23 = load i8*, i8** %11
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i32 1
store i8** %25, i8*** %5
%26 = load i8*, i8** %25
%27 = call i8* @astrcat(i8* %23, i8* %26)
store i8* %27, i8** %9
%28 = load i8*, i8** %11
%29 = call i32 @free(i8* %28)
br label %30
30: ; preds = %34, %18
%31 = load i64, i64* %6
%32 = add i64 %31, -1
store i64 %32, i64* %6
%33 = icmp ugt i64 %32, 1
br i1 %33, label %34, label %48
34: ; preds = %30
%35 = load i8*, i8** %9
%36 = load i8*, i8** %7
%37 = call i8* @astrcat(i8* %35, i8* %36)
store i8* %37, i8** %11
%38 = load i8*, i8** %11
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i32 1
store i8** %40, i8*** %5
%41 = load i8*, i8** %40
%42 = call i8* @astrcat(i8* %38, i8* %41)
store i8* %42, i8** %10
%43 = load i8*, i8** %11
%44 = call i32 @free(i8* %43)
%45 = load i8*, i8** %9
%46 = call i32 @free(i8* %45)
%47 = load i8*, i8** %10
store i8* %47, i8** %9
br label %30
48: ; preds = %30
%49 = load i8*, i8** %9
store i8* %49, i8** %4
br label %50
50: ; preds = %48, %14
%51 = load i8*, i8** %4
ret i8* %51
}

LLVM IR (-O2):
define i8* @strjoin(i8** nocapture %0, i64 %1, i8* %2) {
%4 = icmp eq i64 %1, 0
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
store i8 0, i8* %6
br label %28
7: ; preds = %3
%8 = load i8*, i8** %0
%9 = tail call i8* @astrcat(i8* %8, i8* %2)
%10 = getelementptr inbounds i8*, i8** %0, i64 1
%11 = load i8*, i8** %10
%12 = tail call i8* @astrcat(i8* %9, i8* %11)
%13 = tail call i32 @free(i8* %9)
%14 = add i64 %1, -1
%15 = icmp ugt i64 %14, 1
br i1 %15, label %16, label %28
16: ; preds = %7, %16
%17 = phi i64 [ %26, %16 ], [ %14, %7 ]
%18 = phi i8* [ %23, %16 ], [ %12, %7 ]
%19 = phi i8** [ %21, %16 ], [ %10, %7 ]
%20 = tail call i8* @astrcat(i8* %18, i8* %2)
%21 = getelementptr inbounds i8*, i8** %19, i64 1
%22 = load i8*, i8** %21
%23 = tail call i8* @astrcat(i8* %20, i8* %22)
%24 = tail call i32 @free(i8* %20)
%25 = tail call i32 @free(i8* %18)
%26 = add i64 %17, -1
%27 = icmp ugt i64 %26, 1
br i1 %27, label %16, label %28
28: ; preds = %16, %7, %5
%29 = phi i8* [ %6, %5 ], [ %12, %7 ], [ %23, %16 ]
ret i8* %29
}