#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*/ exit(int);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, int const);
char *build_int_string(const int *arr, int len) {
  char *ret;
  char *ptr;
  int i;
  if ((ret = malloc(sizeof(*ret) * (11 * len))) == NULL)
    exit(22);
  ;
  ptr = ret;
  for (i = 0; i < len; i++) {
    sprintf(ptr, "%d,", arr[i]);
    while (*ptr)
      ptr++;
  }
  ptr--;
  *ptr = 0;
  return ret;
}


x86 assembly (clang, -O0):
<build_int_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
imul   $0xb,-0xc(%rbp),%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1168 <build_int_string+0x38>
mov    $0x16,%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11cf <build_int_string+0x9f>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe67(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     11bc <build_int_string+0x8c>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    119e <build_int_string+0x6e>
jmp    11c1 <build_int_string+0x91>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1177 <build_int_string+0x47>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_int_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r12d
mov    %rdi,%r15
lea    (%r12,%r12,4),%eax
lea    (%r12,%rax,2),%edi
call   1030 <malloc@plt>
test   %rax,%rax
je     11b9 <build_int_string+0x89>
mov    %rax,%r14
mov    %rax,%rbx
test   %r12d,%r12d
jle    11a3 <build_int_string+0x73>
mov    %r12d,%r13d
xor    %ebp,%ebp
lea    0xe96(%rip),%r12
mov    %r14,%rbx
nopl   (%rax)
mov    (%r15,%rbp,4),%edx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <sprintf@plt>
add    $0xffffffffffffffff,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmpb   $0x0,0x1(%rbx)
lea    0x1(%rbx),%rbx
jne    1190 <build_int_string+0x60>
add    $0x1,%rbp
cmp    %r13,%rbp
jne    1170 <build_int_string+0x40>
movb   $0x0,-0x1(%rbx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x16,%edi
call   1050 <exit@plt>

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

LLVM IR (-O2):
@.str = [4 x i8] c"%d,\00"
define noalias i8* @build_int_string(i32* nocapture %0, i32 %1) {
%3 = mul nsw i32 %1, 11
%4 = tail call i8* @malloc(i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %26
8: ; preds = %6
%9 = zext i32 %1 to i64
br label %12
10: ; preds = %2
%11 = tail call i32 @exit(i32 22)
unreachable
12: ; preds = %8, %23
%13 = phi i64 [ 0, %8 ], [ %24, %23 ]
%14 = phi i8* [ %4, %8 ], [ %19, %23 ]
%15 = getelementptr inbounds i32, i32* %0, i64 %13
%16 = load i32, i32* %15
%17 = tail call i32 @sprintf(i8* %14, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %18, %12
%19 = phi i8* [ %14, %12 ], [ %22, %18 ]
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 0
%22 = getelementptr inbounds i8, i8* %19, i64 1
br i1 %21, label %23, label %18
23: ; preds = %18
%24 = add nuw nsw i64 %13, 1
%25 = icmp eq i64 %24, %9
br i1 %25, label %26, label %12
26: ; preds = %23, %6
%27 = phi i8* [ %4, %6 ], [ %19, %23 ]
%28 = getelementptr inbounds i8, i8* %27, i64 -1
store i8 0, i8* %28
ret i8* %4
}