#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 *, int);
int /*<<< orphan*/ strcat(char *, char *);
char *p(char *str, int arr[], int length) {
  for (int i = 0; i < length; ++i) {
    char s[10] = {};
    sprintf(s, "%d,", arr[i]);
    strcat(str, s);
  }
  return str;
}


x86 assembly (clang, -O0):
<p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    119c <p+0x6c>
lea    -0x22(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1030 <memset@plt>
lea    -0x22(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe84(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x22(%rbp),%rsi
call   1040 <strcat@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    114a <p+0x1a>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<p>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r12
test   %edx,%edx
jle    1183 <p+0x63>
mov    %rsi,%r14
mov    %edx,%r13d
xor    %ebp,%ebp
lea    0xebc(%rip),%r15
lea    0x8(%rsp),%rbx
nopl   0x0(%rax)
movw   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
mov    (%r14,%rbp,4),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <sprintf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1030 <strcat@plt>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1150 <p+0x30>
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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