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


x86 assembly (clang, -O0):
<tostr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x124(%rbp)
mov    -0x124(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    118d <tostr+0x6d>
mov    -0x8(%rbp),%rax
movslq -0x124(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
movslq -0x124(%rbp),%rax
lea    -0x120(%rbp),%rdi
shl    $0x6,%rax
add    %rax,%rdi
call   1030 <SerializeFloat@plt>
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    1140 <tostr+0x20>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x130(%rbp)
sub    $0x3,%eax
ja     127a <tostr+0x15a>
mov    -0x130(%rbp),%rax
lea    0xe4d(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x18(%rbp),%rdi
lea    -0x120(%rbp),%rdx
lea    0xe42(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
jmp    127a <tostr+0x15a>
mov    -0x18(%rbp),%rdi
lea    -0x120(%rbp),%rdx
lea    -0x120(%rbp),%rcx
add    $0x40,%rcx
lea    0xe1e(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
jmp    127a <tostr+0x15a>
mov    -0x18(%rbp),%rdi
lea    -0x120(%rbp),%rdx
lea    -0x120(%rbp),%rcx
add    $0x40,%rcx
lea    -0x120(%rbp),%r8
add    $0x80,%r8
lea    0xdef(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
jmp    127a <tostr+0x15a>
mov    -0x18(%rbp),%rdi
lea    -0x120(%rbp),%rdx
lea    -0x120(%rbp),%rcx
add    $0x40,%rcx
lea    -0x120(%rbp),%r8
add    $0x80,%r8
lea    -0x120(%rbp),%r9
add    $0xc0,%r9
lea    0xdb5(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tostr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
test   %esi,%esi
jle    116b <tostr+0x4b>
mov    %rdi,%r12
mov    %r15d,%r13d
mov    %rsp,%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
movss  (%r12,%rbp,4),%xmm0
mov    %rbx,%rdi
call   1030 <SerializeFloat@plt>
add    $0x1,%rbp
add    $0x40,%rbx
cmp    %rbp,%r13
jne    1150 <tostr+0x30>
add    $0xffffffff,%r15d
cmp    $0x3,%r15d
ja     1206 <tostr+0xe6>
lea    0xe80(%rip),%rax
movslq (%rax,%r15,4),%rcx
add    %rax,%rcx
jmp    *%rcx
lea    0xe80(%rip),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <sprintf@plt>
jmp    1206 <tostr+0xe6>
lea    0x40(%rsp),%rcx
lea    0xe6a(%rip),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <sprintf@plt>
jmp    1206 <tostr+0xe6>
lea    0x40(%rsp),%rcx
lea    0x80(%rsp),%r8
lea    0xe4f(%rip),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <sprintf@plt>
jmp    1206 <tostr+0xe6>
lea    0x40(%rsp),%rcx
lea    0x80(%rsp),%r8
lea    0xc0(%rsp),%r9
lea    0xe2f(%rip),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <sprintf@plt>
add    $0x108,%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"
@.str.2 = [11 x i8] c"{%s,%s,%s}\00"
@.str.3 = [14 x i8] c"{%s,%s,%s,%s}\00"
define void @tostr(float* %0, i32 %1, i8* %2) {
%4 = alloca float*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [4 x [64 x i8]]
%8 = alloca i32
store float* %0, float** %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 float*, float** %4
%15 = load i32, i32* %8
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds float, float* %14, i64 %16
%18 = load float, float* %17
%19 = load i32, i32* %8
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [4 x [64 x i8]], [4 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 @SerializeFloat(float %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 %61 [
i32 1, label %29
i32 2, label %34
i32 3, label %41
i32 4, label %50
]
29: ; preds = %27
%30 = load i8*, i8** %6
%31 = getelementptr inbounds [4 x [64 x i8]], [4 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 %61
34: ; preds = %27
%35 = load i8*, i8** %6
%36 = getelementptr inbounds [4 x [64 x i8]], [4 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 [4 x [64 x i8]], [4 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 %61
41: ; preds = %27
%42 = load i8*, i8** %6
%43 = getelementptr inbounds [4 x [64 x i8]], [4 x [64 x i8]]* %7, i64 0, i64 0
%44 = getelementptr inbounds [64 x i8], [64 x i8]* %43, i64 0, i64 0
%45 = getelementptr inbounds [4 x [64 x i8]], [4 x [64 x i8]]* %7, i64 0, i64 1
%46 = getelementptr inbounds [64 x i8], [64 x i8]* %45, i64 0, i64 0
%47 = getelementptr inbounds [4 x [64 x i8]], [4 x [64 x i8]]* %7, i64 0, i64 2
%48 = getelementptr inbounds [64 x i8], [64 x i8]* %47, i64 0, i64 0
%49 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %42, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %44, i8* %46, i8* %48)
br label %61
50: ; preds = %27
%51 = load i8*, i8** %6
%52 = getelementptr inbounds [4 x [64 x i8]], [4 x [64 x i8]]* %7, i64 0, i64 0
%53 = getelementptr inbounds [64 x i8], [64 x i8]* %52, i64 0, i64 0
%54 = getelementptr inbounds [4 x [64 x i8]], [4 x [64 x i8]]* %7, i64 0, i64 1
%55 = getelementptr inbounds [64 x i8], [64 x i8]* %54, i64 0, i64 0
%56 = getelementptr inbounds [4 x [64 x i8]], [4 x [64 x i8]]* %7, i64 0, i64 2
%57 = getelementptr inbounds [64 x i8], [64 x i8]* %56, i64 0, i64 0
%58 = getelementptr inbounds [4 x [64 x i8]], [4 x [64 x i8]]* %7, i64 0, i64 3
%59 = getelementptr inbounds [64 x i8], [64 x i8]* %58, i64 0, i64 0
%60 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %51, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %53, i8* %55, i8* %57, i8* %59)
br label %61
61: ; preds = %27, %50, %41, %34, %29
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"{%s}\00"
@.str.1 = [8 x i8] c"{%s,%s}\00"
@.str.2 = [11 x i8] c"{%s,%s,%s}\00"
@.str.3 = [14 x i8] c"{%s,%s,%s,%s}\00"
define void @tostr(float* nocapture %0, i32 %1, i8* nocapture writeonly %2) {
%4 = alloca [4 x [64 x i8]]
%5 = getelementptr inbounds [4 x [64 x i8]], [4 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 %32 [
i32 1, label %18
i32 2, label %20
i32 3, label %23
i32 4, label %27
]
10: ; preds = %7, %10
%11 = phi i64 [ 0, %7 ], [ %16, %10 ]
%12 = getelementptr inbounds float, float* %0, i64 %11
%13 = load float, float* %12
%14 = getelementptr inbounds [4 x [64 x i8]], [4 x [64 x i8]]* %4, i64 0, i64 %11, i64 0
%15 = call i32 @SerializeFloat(float %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 %32
20: ; preds = %9
%21 = getelementptr inbounds [4 x [64 x i8]], [4 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 %32
23: ; preds = %9
%24 = getelementptr inbounds [4 x [64 x i8]], [4 x [64 x i8]]* %4, i64 0, i64 1, i64 0
%25 = getelementptr inbounds [4 x [64 x i8]], [4 x [64 x i8]]* %4, i64 0, i64 2, i64 0
%26 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %2, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %5, i8* %24, i8* %25)
br label %32
27: ; preds = %9
%28 = getelementptr inbounds [4 x [64 x i8]], [4 x [64 x i8]]* %4, i64 0, i64 1, i64 0
%29 = getelementptr inbounds [4 x [64 x i8]], [4 x [64 x i8]]* %4, i64 0, i64 2, i64 0
%30 = getelementptr inbounds [4 x [64 x i8]], [4 x [64 x i8]]* %4, i64 0, i64 3, i64 0
%31 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %2, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %5, i8* %28, i8* %29, i8* %30)
br label %32
32: ; preds = %9, %27, %23, %20, %18
ret void
}