#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 *calloc(int, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ sprintf(char *, char *, int const);
int /*<<< orphan*/ strcat(char *, char *);
int array_join(char *str, const int array[], const size_t length) {
  int i;
  char *buffer;
  buffer = calloc(10, sizeof(char));
  for (i = 0; i < length; ++i) {
    sprintf(buffer, "%d", array[i]);
    strcat(str, buffer);
  }
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<array_join>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0xa,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11b2 <array_join+0x72>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe6e(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    116e <array_join+0x2e>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<array_join>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0xa,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%rbx
test   %r12,%r12
je     11a3 <array_join+0x63>
xor    %ebp,%ebp
lea    0xe8c(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r14,%rbp,4),%edx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1060 <sprintf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <strcat@plt>
add    $0x1,%rbp
cmp    %rbp,%r12
jne    1180 <array_join+0x40>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
define i32 @array_join(i8* %0, i32* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
store i64 %2, i64* %6
%9 = call i8* @calloc(i32 10, i32 1)
store i8* %9, i8** %8
store i32 0, i32* %7
br label %10
10: ; preds = %26, %3
%11 = load i32, i32* %7
%12 = sext i32 %11 to i64
%13 = load i64, i64* %6
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %29
15: ; preds = %10
%16 = load i8*, i8** %8
%17 = load i32*, i32** %5
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %17, i64 %19
%21 = load i32, i32* %20
%22 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i8*, i8** %4
%24 = load i8*, i8** %8
%25 = call i32 @strcat(i8* %23, i8* %24)
br label %26
26: ; preds = %15
%27 = load i32, i32* %7
%28 = add nsw i32 %27, 1
store i32 %28, i32* %7
br label %10
29: ; preds = %10
%30 = load i8*, i8** %8
%31 = call i32 @free(i8* %30)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
define i32 @array_join(i8* %0, i32* nocapture %1, i64 %2) {
%4 = tail call dereferenceable_or_null(10) i8* @calloc(i32 10, i32 1)
%5 = icmp eq i64 %2, 0
br i1 %5, label %14, label %6
6: ; preds = %3, %6
%7 = phi i64 [ %12, %6 ], [ 0, %3 ]
%8 = getelementptr inbounds i32, i32* %1, i64 %7
%9 = load i32, i32* %8
%10 = tail call i32 @sprintf(i8* dereferenceable(1) %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = tail call i32 @strcat(i8* %0, i8* %4)
%12 = add nuw i64 %7, 1
%13 = icmp eq i64 %12, %2
br i1 %13, label %14, label %6
14: ; preds = %6, %3
%15 = tail call i32 @free(i8* %4)
ret i32 0
}