#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*/ free(char *);
char *strdup(char *);
char *strjoin(char *, char const *, ...);
char *to_str(const int *a, int n) {
  if (!a) {
    return strdup("{}");
  }
  char *ret = strdup("{");
  if (!ret) {
    return NULL;
  }
  const char *sep = "";
  int i;
  char *together;
  for (i = 0; i < n; i++) {
    together = strjoin(ret, sep);
    if (!together) {
      free(ret);
      return NULL;
    }
    free(ret);
    ret = together;
    together = strjoin(ret, "%d", a[i]);
    if (!together) {
      free(ret);
      return NULL;
    }
    free(ret);
    ret = together;
    sep = ", ";
  }
  together = strjoin(ret, "}");
  if (!together) {
    free(ret);
    return NULL;
  }
  free(ret);
  ret = together;
  return ret;
}


x86 assembly (clang, -O0):
<to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115f <to_str+0x2f>
lea    0xeaf(%rip),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    12a6 <to_str+0x176>
lea    0xe9d(%rip),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1187 <to_str+0x57>
movq   $0x0,-0x8(%rbp)
jmp    12a6 <to_str+0x176>
lea    0xe7c(%rip),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1256 <to_str+0x126>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x0,%al
call   1040 <strjoin@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11d9 <to_str+0xa9>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12a6 <to_str+0x176>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1040 <strjoin@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    122c <to_str+0xfc>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12a6 <to_str+0x176>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    0xdc4(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1199 <to_str+0x69>
mov    -0x20(%rbp),%rdi
lea    0xda0(%rip),%rsi
mov    $0x0,%al
call   1040 <strjoin@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    128d <to_str+0x15d>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12a6 <to_str+0x176>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_str>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     11fd <to_str+0xcd>
mov    %esi,%ebx
mov    %rdi,%r14
lea    0xeb3(%rip),%rdi
call   1050 <strdup@plt>
test   %rax,%rax
je     11ec <to_str+0xbc>
mov    %rax,%r12
test   %ebx,%ebx
jle    11cb <to_str+0x9b>
mov    %ebx,%r13d
lea    0xe9b(%rip),%rsi
lea    0xe8f(%rip),%r15
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
xor    %eax,%eax
call   1040 <strjoin@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1030 <free@plt>
test   %rbx,%rbx
je     11ec <to_str+0xbc>
mov    (%r14,%rbp,4),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <strjoin@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1030 <free@plt>
test   %r12,%r12
je     11ec <to_str+0xbc>
add    $0x1,%rbp
lea    0xe42(%rip),%rsi
cmp    %rbp,%r13
jne    1180 <to_str+0x50>
lea    0xe2f(%rip),%rsi
mov    %r12,%rdi
xor    %eax,%eax
call   1040 <strjoin@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
jmp    11ee <to_str+0xbe>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfc(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <strdup@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"{}\00"
@.str.1 = [2 x i8] c"{\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [3 x i8] c", \00"
@.str.5 = [2 x i8] c"}\00"
define i8* @to_str(i32* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
store i32* %0, i32** %4
store i32 %1, i32* %5
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %14, label %12
12: ; preds = %2
%13 = call i8* @strdup(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %13, i8** %3
br label %69
14: ; preds = %2
%15 = call i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %14
store i8* null, i8** %3
br label %69
19: ; preds = %14
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8** %7
store i32 0, i32* %8
br label %20
20: ; preds = %53, %19
%21 = load i32, i32* %8
%22 = load i32, i32* %5
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %56
24: ; preds = %20
%25 = load i8*, i8** %6
%26 = load i8*, i8** %7
%27 = call i8* (i8*, i8*, ...) @strjoin(i8* %25, i8* %26)
store i8* %27, i8** %9
%28 = load i8*, i8** %9
%29 = icmp ne i8* %28, null
br i1 %29, label %33, label %30
30: ; preds = %24
%31 = load i8*, i8** %6
%32 = call i32 @free(i8* %31)
store i8* null, i8** %3
br label %69
33: ; preds = %24
%34 = load i8*, i8** %6
%35 = call i32 @free(i8* %34)
%36 = load i8*, i8** %9
store i8* %36, i8** %6
%37 = load i8*, i8** %6
%38 = load i32*, i32** %4
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
%42 = load i32, i32* %41
%43 = call i8* (i8*, i8*, ...) @strjoin(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %42)
store i8* %43, i8** %9
%44 = load i8*, i8** %9
%45 = icmp ne i8* %44, null
br i1 %45, label %49, label %46
46: ; preds = %33
%47 = load i8*, i8** %6
%48 = call i32 @free(i8* %47)
store i8* null, i8** %3
br label %69
49: ; preds = %33
%50 = load i8*, i8** %6
%51 = call i32 @free(i8* %50)
%52 = load i8*, i8** %9
store i8* %52, i8** %6
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8** %7
br label %53
53: ; preds = %49
%54 = load i32, i32* %8
%55 = add nsw i32 %54, 1
store i32 %55, i32* %8
br label %20
56: ; preds = %20
%57 = load i8*, i8** %6
%58 = call i8* (i8*, i8*, ...) @strjoin(i8* %57, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i8* %58, i8** %9
%59 = load i8*, i8** %9
%60 = icmp ne i8* %59, null
br i1 %60, label %64, label %61
61: ; preds = %56
%62 = load i8*, i8** %6
%63 = call i32 @free(i8* %62)
store i8* null, i8** %3
br label %69
64: ; preds = %56
%65 = load i8*, i8** %6
%66 = call i32 @free(i8* %65)
%67 = load i8*, i8** %9
store i8* %67, i8** %6
%68 = load i8*, i8** %6
store i8* %68, i8** %3
br label %69
69: ; preds = %64, %61, %46, %30, %18, %12
%70 = load i8*, i8** %3
ret i8* %70
}

LLVM IR (-O2):
@.str = [3 x i8] c"{}\00"
@.str.1 = [2 x i8] c"{\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [3 x i8] c", \00"
@.str.5 = [2 x i8] c"}\00"
define i8* @to_str(i32* %0, i32 %1) {
%3 = icmp eq i32* %0, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call dereferenceable_or_null(3) i8* @strdup(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %33
6: ; preds = %2
%7 = tail call dereferenceable_or_null(2) i8* @strdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %33, label %9
9: ; preds = %6
%10 = icmp sgt i32 %1, 0
br i1 %10, label %11, label %29
11: ; preds = %9
%12 = zext i32 %1 to i64
br label %13
13: ; preds = %11, %26
%14 = phi i64 [ 0, %11 ], [ %27, %26 ]
%15 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), %11 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), %26 ]
%16 = phi i8* [ %7, %11 ], [ %23, %26 ]
%17 = tail call i8* (i8*, i8*, ...) @strjoin(i8* %16, i8* %15)
%18 = icmp eq i8* %17, null
%19 = tail call i32 @free(i8* %16)
br i1 %18, label %33, label %20
20: ; preds = %13
%21 = getelementptr inbounds i32, i32* %0, i64 %14
%22 = load i32, i32* %21
%23 = tail call i8* (i8*, i8*, ...) @strjoin(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = icmp eq i8* %23, null
%25 = tail call i32 @free(i8* %17)
br i1 %24, label %33, label %26
26: ; preds = %20
%27 = add nuw nsw i64 %14, 1
%28 = icmp eq i64 %27, %12
br i1 %28, label %29, label %13
29: ; preds = %26, %9
%30 = phi i8* [ %7, %9 ], [ %23, %26 ]
%31 = tail call i8* (i8*, i8*, ...) @strjoin(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%32 = tail call i32 @free(i8* %30)
br label %33
33: ; preds = %20, %13, %29, %6, %4
%34 = phi i8* [ %5, %4 ], [ null, %6 ], [ %31, %29 ], [ null, %13 ], [ null, %20 ]
ret i8* %34
}