#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*/ sprintf(char *, char *, char *, char const *);
int strlen(char const *);
char *umalloc(int);
char *str_join(char **strs, const char *with) {
  const int with_len = strlen(with);
  int len;
  char **itr;
  char *ret, *p;
  len = 1;
  for (itr = strs; *itr; itr++)
    len += strlen(*itr) + with_len;
  p = ret = umalloc(len);
  for (itr = strs; *itr; itr++)
    p += sprintf(p, "%s%s", *itr, itr[1] ? with : "");
  return ret;
}


x86 assembly (clang, -O0):
<str_join>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     118f <str_join+0x5f>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
add    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
jmp    115b <str_join+0x2b>
mov    -0x18(%rbp),%edi
call   1040 <umalloc@plt>
mov    %rax,-0x28(%rbp)
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     122d <str_join+0xfd>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     11e4 <str_join+0xb4>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    11f4 <str_join+0xc4>
lea    0xe19(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    11f4 <str_join+0xc4>
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rcx
lea    0xdf9(%rip),%rsi
call   1050 <sprintf@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
jmp    11a7 <str_join+0x77>
mov    -0x28(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_join>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rsi,%r12
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    (%r14),%rdi
test   %rdi,%rdi
je     1178 <str_join+0x48>
mov    %eax,%r15d
lea    0x8(%r14),%rbx
mov    $0x1,%ebp
nopl   (%rax)
call   1030 <strlen@plt>
add    %r15d,%ebp
add    %eax,%ebp
mov    (%rbx),%rdi
add    $0x8,%rbx
test   %rdi,%rdi
jne    1160 <str_join+0x30>
jmp    117d <str_join+0x4d>
mov    $0x1,%ebp
mov    %ebp,%edi
call   1040 <umalloc@plt>
mov    %rax,%r15
mov    (%r14),%rdx
test   %rdx,%rdx
je     11da <str_join+0xaa>
mov    %r12,%rbx
add    $0x8,%r14
lea    0xe67(%rip),%r13
lea    0xe5c(%rip),%r12
mov    %r15,%rbp
jmp    11cc <str_join+0x9c>
nopl   0x0(%rax)
mov    %rbp,%rdi
mov    %r12,%rsi
call   1050 <sprintf@plt>
cltq
add    %rax,%rbp
mov    (%r14),%rdx
add    $0x8,%r14
test   %rdx,%rdx
je     11da <str_join+0xaa>
cmpq   $0x0,(%r14)
mov    %r13,%rcx
je     11b0 <str_join+0x80>
mov    %rbx,%rcx
jmp    11b0 <str_join+0x80>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [1 x i8] zeroinitializer
define i8* @str_join(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i8*
store i8** %0, i8*** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %4
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %5
store i32 1, i32* %6
%12 = load i8**, i8*** %3
store i8** %12, i8*** %7
br label %13
13: ; preds = %25, %2
%14 = load i8**, i8*** %7
%15 = load i8*, i8** %14
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %28
17: ; preds = %13
%18 = load i8**, i8*** %7
%19 = load i8*, i8** %18
%20 = call i32 @strlen(i8* %19)
%21 = load i32, i32* %5
%22 = add nsw i32 %20, %21
%23 = load i32, i32* %6
%24 = add nsw i32 %23, %22
store i32 %24, i32* %6
br label %25
25: ; preds = %17
%26 = load i8**, i8*** %7
%27 = getelementptr inbounds i8*, i8** %26, i32 1
store i8** %27, i8*** %7
br label %13
28: ; preds = %13
%29 = load i32, i32* %6
%30 = call i8* @umalloc(i32 %29)
store i8* %30, i8** %8
store i8* %30, i8** %9
%31 = load i8**, i8*** %3
store i8** %31, i8*** %7
br label %32
32: ; preds = %53, %28
%33 = load i8**, i8*** %7
%34 = load i8*, i8** %33
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %56
36: ; preds = %32
%37 = load i8*, i8** %9
%38 = load i8**, i8*** %7
%39 = load i8*, i8** %38
%40 = load i8**, i8*** %7
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = icmp ne i8* %42, null
br i1 %43, label %44, label %46
44: ; preds = %36
%45 = load i8*, i8** %4
br label %47
46: ; preds = %36
br label %47
47: ; preds = %46, %44
%48 = phi i8* [ %45, %44 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %46 ]
%49 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %39, i8* %48)
%50 = load i8*, i8** %9
%51 = sext i32 %49 to i64
%52 = getelementptr inbounds i8, i8* %50, i64 %51
store i8* %52, i8** %9
br label %53
53: ; preds = %47
%54 = load i8**, i8*** %7
%55 = getelementptr inbounds i8*, i8** %54, i32 1
store i8** %55, i8*** %7
br label %32
56: ; preds = %32
%57 = load i8*, i8** %8
ret i8* %57
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [1 x i8] zeroinitializer
define i8* @str_join(i8** nocapture %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = load i8*, i8** %0
%5 = icmp eq i8* %4, null
br i1 %5, label %16, label %6
6: ; preds = %2, %6
%7 = phi i8* [ %14, %6 ], [ %4, %2 ]
%8 = phi i32 [ %12, %6 ], [ 1, %2 ]
%9 = phi i8** [ %13, %6 ], [ %0, %2 ]
%10 = tail call i32 @strlen(i8* %7)
%11 = add i32 %8, %3
%12 = add i32 %11, %10
%13 = getelementptr inbounds i8*, i8** %9, i64 1
%14 = load i8*, i8** %13
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %6
16: ; preds = %6, %2
%17 = phi i32 [ 1, %2 ], [ %12, %6 ]
%18 = tail call i8* @umalloc(i32 %17)
%19 = load i8*, i8** %0
%20 = icmp eq i8* %19, null
br i1 %20, label %34, label %21
21: ; preds = %16, %21
%22 = phi i8* [ %32, %21 ], [ %19, %16 ]
%23 = phi i8* [ %31, %21 ], [ %18, %16 ]
%24 = phi i8** [ %25, %21 ], [ %0, %16 ]
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = icmp eq i8* %26, null
%28 = select i1 %27, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* %1
%29 = tail call i32 @sprintf(i8* dereferenceable(1) %23, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %22, i8* %28)
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %23, i64 %30
%32 = load i8*, i8** %25
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %21
34: ; preds = %21, %16
ret i8* %18
}