#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 *, double const);
int /*<<< orphan*/ strcat(char *, char *);
void vectorToString(const double *vec, int dim, char *str) {
  str[0] = '(';
  str[1] = '\0';
  char tmp[20] = "";
  for (int i = 0; i < dim; ++i) {
    if (i < dim - 1) {
      sprintf(tmp, "%g,", vec[i]);
    } else if (i == dim - 1) {
      sprintf(tmp, "%g)\n", vec[i]);
    }
    strcat(str, tmp);
  }
}


x86 assembly (clang, -O0):
<vectorToString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x28,(%rax)
mov    -0x18(%rbp),%rax
movb   $0x0,0x1(%rax)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11f6 <vectorToString+0xc6>
mov    -0x34(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11a8 <vectorToString+0x78>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xe62(%rip),%rsi
call   1050 <sprintf@plt>
jmp    11db <vectorToString+0xab>
mov    -0x34(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11d6 <vectorToString+0xa6>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xe33(%rip),%rsi
call   1050 <sprintf@plt>
jmp    11db <vectorToString+0xab>
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <strcat@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1169 <vectorToString+0x39>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vectorToString>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movw   $0x28,(%rdx)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movl   $0x0,0x10(%rsp)
test   %esi,%esi
jle    119f <vectorToString+0x7f>
mov    %rdx,%r15
mov    %rdi,%r14
lea    -0x1(%rsi),%ebx
mov    %esi,%r13d
xor    %ebp,%ebp
mov    %rsp,%r12
jmp    1182 <vectorToString+0x62>
nopl   0x0(%rax)
movsd  (%r14,%rax,8),%xmm0
mov    %r12,%rdi
call   1040 <sprintf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <strcat@plt>
add    $0x1,%rbp
cmp    %rbp,%r13
je     119f <vectorToString+0x7f>
mov    %rbp,%rax
lea    0xe74(%rip),%rsi
cmp    %rbx,%rbp
jb     1160 <vectorToString+0x40>
mov    %rbx,%rax
lea    0xe69(%rip),%rsi
je     1160 <vectorToString+0x40>
jmp    116e <vectorToString+0x4e>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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