#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*/ lround(double);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int snprintf(char *, int, char *, int /*<<< orphan*/);
char *doubleArrayToString(int n, double *values) {
  int buflen = 0;
  char *buf;
  buflen = n * 12;
  buf = (char *)malloc(buflen);
  int pos = 0;
  int i = 0;
  for (i = 0; i < n; ++i) {
    int extra = snprintf(buf + pos, buflen - pos, "%ld", lround(values[i]));
    if (extra < 0) {
      perror("snprintf");
      return buf;
    }
    pos += extra;
    if (extra + 1 >= buflen)
      break;
    if (i + 1 < n) {
      buf[pos++] = ',';
      buf[pos] = 0;
    }
  }
  return buf;
}


x86 assembly (clang, -O0):
<doubleArrayToString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
imul   $0xc,-0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1250 <doubleArrayToString+0x110>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x1c(%rbp),%eax
sub    -0x2c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
call   1030 <lround@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%esi
mov    %eax,%ecx
lea    0xe3c(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11ef <doubleArrayToString+0xaf>
lea    0xe27(%rip),%rdi
call   1060 <perror@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1258 <doubleArrayToString+0x118>
mov    -0x34(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
cmp    -0x1c(%rbp),%eax
jl     120c <doubleArrayToString+0xcc>
jmp    1250 <doubleArrayToString+0x110>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
cmp    -0xc(%rbp),%eax
jge    123d <doubleArrayToString+0xfd>
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x2c(%rbp)
movslq %ecx,%rcx
movb   $0x2c,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1242 <doubleArrayToString+0x102>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    117e <doubleArrayToString+0x3e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doubleArrayToString>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
mov    %edi,%r15d
lea    0x0(,%r15,4),%eax
lea    (%rax,%rax,2),%r13d
mov    %r13d,%edi
call   1050 <malloc@plt>
mov    %rax,%r12
test   %r15d,%r15d
jle    1209 <doubleArrayToString+0xc9>
mov    %r15d,%eax
mov    %rax,0x8(%rsp)
neg    %rax
mov    %rax,(%rsp)
xor    %r15d,%r15d
mov    $0x1,%r14d
jmp    11a8 <doubleArrayToString+0x68>
mov    (%rsp),%rcx
add    %r14,%rcx
add    $0x1,%rcx
add    $0x1,%r14
mov    %eax,%r15d
cmp    $0x1,%rcx
je     1209 <doubleArrayToString+0xc9>
movslq %r15d,%rax
lea    (%r12,%rax,1),%rbx
mov    %r13d,%ebp
sub    %eax,%ebp
mov    0x10(%rsp),%rax
movsd  -0x8(%rax,%r14,8),%xmm0
call   1030 <lround@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
lea    0xe2f(%rip),%rdx
mov    %eax,%ecx
call   1040 <snprintf@plt>
test   %eax,%eax
js     11fd <doubleArrayToString+0xbd>
lea    0x1(%rax),%ecx
cmp    %r13d,%ecx
jge    1209 <doubleArrayToString+0xc9>
add    %r15d,%eax
cmp    0x8(%rsp),%r14
jae    1190 <doubleArrayToString+0x50>
movslq %eax,%rcx
lea    0x1(%rcx),%eax
movw   $0x2c,(%r12,%rcx,1)
jmp    1190 <doubleArrayToString+0x50>
lea    0xe00(%rip),%rdi
call   1060 <perror@plt>
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"%ld\00"
@.str.1 = [9 x i8] c"snprintf\00"
define i8* @doubleArrayToString(i32 %0, double* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca double*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store double* %1, double** %5
store i32 0, i32* %6
%11 = load i32, i32* %4
%12 = mul nsw i32 %11, 12
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 0, i32* %9
br label %16
16: ; preds = %65, %2
%17 = load i32, i32* %9
%18 = load i32, i32* %4
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %68
20: ; preds = %16
%21 = load i8*, i8** %7
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i32, i32* %6
%26 = load i32, i32* %8
%27 = sub nsw i32 %25, %26
%28 = load double*, double** %5
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds double, double* %28, i64 %30
%32 = load double, double* %31
%33 = call i32 @lround(double %32)
%34 = call i32 @snprintf(i8* %24, i32 %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %33)
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %20
%38 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%39 = load i8*, i8** %7
store i8* %39, i8** %3
br label %70
40: ; preds = %20
%41 = load i32, i32* %10
%42 = load i32, i32* %8
%43 = add nsw i32 %42, %41
store i32 %43, i32* %8
%44 = load i32, i32* %10
%45 = add nsw i32 %44, 1
%46 = load i32, i32* %6
%47 = icmp sge i32 %45, %46
br i1 %47, label %48, label %49
48: ; preds = %40
br label %68
49: ; preds = %40
%50 = load i32, i32* %9
%51 = add nsw i32 %50, 1
%52 = load i32, i32* %4
%53 = icmp slt i32 %51, %52
br i1 %53, label %54, label %64
54: ; preds = %49
%55 = load i8*, i8** %7
%56 = load i32, i32* %8
%57 = add nsw i32 %56, 1
store i32 %57, i32* %8
%58 = sext i32 %56 to i64
%59 = getelementptr inbounds i8, i8* %55, i64 %58
store i8 44, i8* %59
%60 = load i8*, i8** %7
%61 = load i32, i32* %8
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
store i8 0, i8* %63
br label %64
64: ; preds = %54, %49
br label %65
65: ; preds = %64
%66 = load i32, i32* %9
%67 = add nsw i32 %66, 1
store i32 %67, i32* %9
br label %16
68: ; preds = %48, %16
%69 = load i8*, i8** %7
store i8* %69, i8** %3
br label %70
70: ; preds = %68, %37
%71 = load i8*, i8** %3
ret i8* %71
}

LLVM IR (-O2):
@.str = [4 x i8] c"%ld\00"
@.str.1 = [9 x i8] c"snprintf\00"
define i8* @doubleArrayToString(i32 %0, double* nocapture %1) {
%3 = mul nsw i32 %0, 12
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to i8*
%6 = icmp sgt i32 %0, 0
br i1 %6, label %7, label %39
7: ; preds = %2
%8 = zext i32 %0 to i64
%9 = zext i32 %0 to i64
br label %10
10: ; preds = %7, %36
%11 = phi i64 [ 0, %7 ], [ %28, %36 ]
%12 = phi i32 [ 0, %7 ], [ %37, %36 ]
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %5, i64 %13
%15 = sub nsw i32 %3, %12
%16 = getelementptr inbounds double, double* %1, i64 %11
%17 = load double, double* %16
%18 = tail call i32 @lround(double %17)
%19 = tail call i32 @snprintf(i8* %14, i32 %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %10
%22 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %39
23: ; preds = %10
%24 = add nsw i32 %19, %12
%25 = add nuw nsw i32 %19, 1
%26 = icmp slt i32 %25, %3
br i1 %26, label %27, label %39
27: ; preds = %23
%28 = add nuw nsw i64 %11, 1
%29 = icmp ult i64 %28, %8
br i1 %29, label %30, label %36
30: ; preds = %27
%31 = add nsw i32 %24, 1
%32 = sext i32 %24 to i64
%33 = getelementptr inbounds i8, i8* %5, i64 %32
store i8 44, i8* %33
%34 = sext i32 %31 to i64
%35 = getelementptr inbounds i8, i8* %5, i64 %34
store i8 0, i8* %35
br label %36
36: ; preds = %30, %27
%37 = phi i32 [ %24, %27 ], [ %31, %30 ]
%38 = icmp eq i64 %28, %9
br i1 %38, label %39, label %10
39: ; preds = %23, %36, %2, %21
ret i8* %5
}