#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 */
scalar_t__ calloc(int, scalar_t__);
scalar_t__ realloc(char *, int);
char *strcat(char *, char *);
scalar_t__ strlen(char *);
char *setReference(int count, char **words) {
  int size = 0;
  char *reference = (char *)calloc(1, strlen(words[0]));
  reference = strcat(reference, words[0]);
  for (int i = 1; i < count; i++) {
    size += strlen(words[i] + 1);
    reference = (char *)realloc(reference, size);
    reference = strcat(strcat(reference, " "), words[i]);
  }
  return reference;
}


x86 assembly (clang, -O0):
<setReference>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    %rax,%rsi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1060 <strcat@plt>
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1202 <setReference+0xc2>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
add    $0x1,%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
movslq -0x14(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe29(%rip),%rsi
call   1060 <strcat@plt>
mov    %rax,%rdi
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1060 <strcat@plt>
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    118e <setReference+0x4e>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setReference>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebx
mov    (%rsi),%rdi
call   1030 <strlen@plt>
mov    $0x1,%edi
mov    %rax,%rsi
call   1040 <calloc@plt>
mov    %rax,%r15
mov    (%r14),%rsi
mov    %rax,%rdi
call   1060 <strcat@plt>
cmp    $0x2,%ebx
jl     11d4 <setReference+0x94>
mov    %ebx,%r12d
mov    $0x1,%r13d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r14,%r13,8),%rdi
add    $0x1,%rdi
call   1030 <strlen@plt>
movslq %ebx,%rbx
add    %rax,%rbx
mov    %r15,%rdi
mov    %ebx,%esi
call   1050 <realloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1030 <strlen@plt>
movw   $0x20,(%r15,%rax,1)
mov    (%r14,%r13,8),%rsi
mov    %r15,%rdi
call   1060 <strcat@plt>
add    $0x1,%r13
cmp    %r13,%r12
jne    1190 <setReference+0x50>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @setReference(i32 %0, i8** nocapture %1) {
%3 = load i8*, i8** %1
%4 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%5 = tail call i64 @calloc(i32 1, i64 %4)
%6 = inttoptr i64 %5 to i8*
%7 = load i8*, i8** %1
%8 = tail call i8* @strcat(i8* dereferenceable(1) %6, i8* dereferenceable(1) %7)
%9 = icmp sgt i32 %0, 1
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = zext i32 %0 to i64
br label %16
12: ; preds = %16
%13 = inttoptr i64 %28 to i8*
br label %14
14: ; preds = %12, %2
%15 = phi i8* [ %6, %2 ], [ %13, %12 ]
ret i8* %15
16: ; preds = %10, %16
%17 = phi i64 [ 1, %10 ], [ %35, %16 ]
%18 = phi i8* [ %6, %10 ], [ %29, %16 ]
%19 = phi i64 [ 0, %10 ], [ %26, %16 ]
%20 = getelementptr inbounds i8*, i8** %1, i64 %17
%21 = load i8*, i8** %20
%22 = getelementptr inbounds i8, i8* %21, i64 1
%23 = tail call i64 @strlen(i8* %22)
%24 = shl i64 %19, 32
%25 = ashr exact i64 %24, 32
%26 = add nsw i64 %23, %25
%27 = trunc i64 %26 to i32
%28 = tail call i64 @realloc(i8* %18, i32 %27)
%29 = inttoptr i64 %28 to i8*
%30 = tail call i64 @strlen(i8* dereferenceable(1) %29)
%31 = getelementptr i8, i8* %29, i64 %30
%32 = bitcast i8* %31 to i16*
store i16 32, i16* %32
%33 = load i8*, i8** %20
%34 = tail call i8* @strcat(i8* dereferenceable(1) %29, i8* dereferenceable(1) %33)
%35 = add nuw nsw i64 %17, 1
%36 = icmp eq i64 %35, %11
br i1 %36, label %12, label %16
}