#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*/ SerializeDouble(double, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
void tostr(double *m, int numElems, char *outstr) {
  char s[2][64];
  for (int i = 0; i < numElems; ++i)
    SerializeDouble(m[i], s[i]);
  switch (numElems) {
  case 1:
    sprintf(outstr, "{%s}", s[0]);
    break;
  case 2:
    sprintf(outstr, "{%s,%s}", s[0], s[1]);
    break;
  }
}


x86 assembly (clang, -O0):
<tostr>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0xa4(%rbp)
mov    -0xa4(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    118d <tostr+0x6d>
mov    -0x8(%rbp),%rax
movslq -0xa4(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movslq -0xa4(%rbp),%rax
lea    -0xa0(%rbp),%rdi
shl    $0x6,%rax
add    %rax,%rdi
call   1030 <SerializeDouble@plt>
mov    -0xa4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa4(%rbp)
jmp    1140 <tostr+0x20>
mov    -0xc(%rbp),%eax
mov    %eax,-0xa8(%rbp)
sub    $0x1,%eax
je     11b8 <tostr+0x98>
jmp    11a4 <tostr+0x84>
mov    -0xa8(%rbp),%eax
sub    $0x2,%eax
je     11d6 <tostr+0xb6>
jmp    11fa <tostr+0xda>
mov    -0x18(%rbp),%rdi
lea    -0xa0(%rbp),%rdx
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
jmp    11fa <tostr+0xda>
mov    -0x18(%rbp),%rdi
lea    -0xa0(%rbp),%rdx
lea    -0xa0(%rbp),%rcx
add    $0x40,%rcx
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tostr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
test   %esi,%esi
jle    116b <tostr+0x4b>
mov    %rdi,%r12
mov    %r15d,%r13d
mov    %rsp,%rbp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movsd  (%r12,%rbx,8),%xmm0
mov    %rbp,%rdi
call   1030 <SerializeDouble@plt>
add    $0x1,%rbx
add    $0x40,%rbp
cmp    %rbx,%r13
jne    1150 <tostr+0x30>
cmp    $0x1,%r15d
je     1192 <tostr+0x72>
cmp    $0x2,%r15d
jne    11a6 <tostr+0x86>
lea    0x40(%rsp),%rcx
lea    0xe82(%rip),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <sprintf@plt>
jmp    11a6 <tostr+0x86>
lea    0xe67(%rip),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <sprintf@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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