#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 const *, char *);
int strlen(char const *);
char *umalloc(int);
char *join(const char *sep, char **vec, int n) {
  const int len_sep = strlen(sep);
  int len = 1;
  for (int i = 0; i < n; i++)
    len += len_sep + strlen(vec[i]);
  char *p, *r = p = umalloc(len);
  const char *sep_actual = "";
  for (int i = 0; i < n; i++) {
    p += sprintf(p, "%s%s", sep_actual, vec[i]);
    sep_actual = sep;
  }
  return r;
}


x86 assembly (clang, -O0):
<join>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    119b <join+0x6b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
add    %ecx,%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    115d <join+0x2d>
mov    -0x1c(%rbp),%edi
call   1040 <umalloc@plt>
mov    %rax,-0x28(%rbp)
mov    %rax,-0x30(%rbp)
lea    0xe52(%rip),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    120f <join+0xdf>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    -0x10(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
lea    0xe1c(%rip),%rsi
call   1050 <sprintf@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11bd <join+0x8d>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<join>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebx
mov    %rsi,%r13
mov    %rdi,%r14
call   1030 <strlen@plt>
test   %ebx,%ebx
jle    1215 <join+0xe5>
mov    %eax,%ebp
mov    %ebx,0x14(%rsp)
mov    %ebx,%r15d
mov    $0x1,%ebx
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x0(%r13,%r12,8),%rdi
call   1030 <strlen@plt>
add    %ebp,%ebx
add    %eax,%ebx
add    $0x1,%r12
cmp    %r12,%r15
jne    1170 <join+0x40>
mov    %ebx,%edi
call   1040 <umalloc@plt>
mov    %rax,0x8(%rsp)
mov    0x14(%rsp),%ebx
test   %ebx,%ebx
jle    1201 <join+0xd1>
mov    0x0(%r13),%rcx
lea    0xe5a(%rip),%rsi
lea    0xe57(%rip),%rdx
mov    0x8(%rsp),%rdi
call   1050 <sprintf@plt>
cmp    $0x1,%ebx
je     1201 <join+0xd1>
cltq
mov    0x8(%rsp),%rbp
add    %rax,%rbp
mov    $0x1,%ebx
lea    0xe2e(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x0(%r13,%rbx,8),%rcx
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1050 <sprintf@plt>
cltq
add    %rax,%rbp
add    $0x1,%rbx
cmp    %rbx,%r15
jne    11e0 <join+0xb0>
mov    0x8(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%edi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <umalloc@plt>

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

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