#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__ malloc(int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
char *long_array_to_str(unsigned long *array, int len) {
  char *buffer = (char *)malloc(len * sizeof(unsigned long) * 2 + len);
  int i;
  for (i = 0; i < len; i++) {
    sprintf(buffer, "%ld", array[i]);
  }
  sprintf(buffer, "\n");
  printf("bbbbb=%s\n", buffer);
  return buffer;
}


x86 assembly (clang, -O0):
<long_array_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
shl    $0x1,%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    119c <long_array_to_str+0x6c>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe79(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1164 <long_array_to_str+0x34>
mov    -0x18(%rbp),%rdi
lea    0xe65(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe4b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<long_array_to_str>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
mov    %esi,%edi
shl    $0x4,%edi
add    %esi,%edi
call   1040 <malloc@plt>
mov    %rax,%r12
test   %ebp,%ebp
jle    117a <long_array_to_str+0x4a>
mov    %ebp,%ebp
lea    0xea7(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    (%r14,%rbx,8),%rdx
mov    %r12,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <sprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1160 <long_array_to_str+0x30>
movw   $0xa,(%r12)
lea    0xe7c(%rip),%rdi
mov    %r12,%rsi
call   1030 <printf@plt>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%ld\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [10 x i8] c"bbbbb=%s\0A\00"
define i8* @long_array_to_str(i64* %0, i32 %1) {
%3 = alloca i64*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i64* %0, i64** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = sext i32 %7 to i64
%9 = mul i64 %8, 8
%10 = mul i64 %9, 2
%11 = load i32, i32* %4
%12 = sext i32 %11 to i64
%13 = add i64 %10, %12
%14 = trunc i64 %13 to i32
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %5
store i32 0, i32* %6
br label %17
17: ; preds = %29, %2
%18 = load i32, i32* %6
%19 = load i32, i32* %4
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %32
21: ; preds = %17
%22 = load i8*, i8** %5
%23 = load i64*, i64** %3
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i64, i64* %23, i64 %25
%27 = load i64, i64* %26
%28 = call i32 (i8*, i8*, ...) @sprintf(i8* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %27)
br label %29
29: ; preds = %21
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
store i32 %31, i32* %6
br label %17
32: ; preds = %17
%33 = load i8*, i8** %5
%34 = call i32 (i8*, i8*, ...) @sprintf(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%35 = load i8*, i8** %5
%36 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = load i8*, i8** %5
ret i8* %37
}

LLVM IR (-O2):
@.str = [4 x i8] c"%ld\00"
@.str.2 = [10 x i8] c"bbbbb=%s\0A\00"
define i8* @long_array_to_str(i64* nocapture %0, i32 %1) {
%3 = mul i32 %1, 17
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to i8*
%6 = icmp sgt i32 %1, 0
br i1 %6, label %7, label %16
7: ; preds = %2
%8 = zext i32 %1 to i64
br label %9
9: ; preds = %7, %9
%10 = phi i64 [ 0, %7 ], [ %14, %9 ]
%11 = getelementptr inbounds i64, i64* %0, i64 %10
%12 = load i64, i64* %11
%13 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %5, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %12)
%14 = add nuw nsw i64 %10, 1
%15 = icmp eq i64 %14, %8
br i1 %15, label %16, label %9
16: ; preds = %9, %2
%17 = inttoptr i64 %4 to i16*
store i16 10, i16* %17
%18 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %5)
ret i8* %5
}