#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 *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
int /*<<< orphan*/ strcat(char *, char *);
char *swirjson_lstSerialize(char *szKey, int nValueCount, char **pszValueList,
                            unsigned long *pulTimestampList) {
  char *szPayload = (char *)malloc(2048);
  memset(szPayload, 0, 2048);
  sprintf(szPayload, "{\"%s\": [", szKey);
  int i = 0;
  for (i = 0; i < nValueCount; i++) {
    if (pulTimestampList == NULL) {
      sprintf(szPayload, "%s {\"timestamp\" : \"\", \"value\" : \"%s\"}",
              szPayload, pszValueList[i]);
    } else if (pulTimestampList[i] == 0) {
      sprintf(szPayload, "%s {\"timestamp\" : \"\", \"value\" : \"%s\"}",
              szPayload, pszValueList[i]);
    } else {
      sprintf(szPayload, "%s {\"timestamp\" : %lu, \"value\" : \"%s\"}",
              szPayload, pulTimestampList[i], pszValueList[i]);
    }
    free(pszValueList[i]);
    if (i < nValueCount - 1) {
      strcat(szPayload, ",");
    }
  }
  strcat(szPayload, "]}");
  return szPayload;
}


x86 assembly (clang, -O0):
<swirjson_lstSerialize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    $0x800,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1040 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe6c(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1299 <swirjson_lstSerialize+0x149>
cmpq   $0x0,-0x20(%rbp)
jne    11e7 <swirjson_lstSerialize+0x97>
mov    -0x28(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
lea    0xe2e(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
jmp    1254 <swirjson_lstSerialize+0x104>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    1221 <swirjson_lstSerialize+0xd1>
mov    -0x28(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
lea    0xdf4(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
jmp    124f <swirjson_lstSerialize+0xff>
mov    -0x28(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rsi
mov    (%rax,%rsi,8),%r8
lea    0xde7(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
jmp    1254 <swirjson_lstSerialize+0x104>
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1286 <swirjson_lstSerialize+0x136>
mov    -0x28(%rbp),%rdi
lea    0xdd5(%rip),%rsi
call   1060 <strcat@plt>
jmp    128b <swirjson_lstSerialize+0x13b>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a9 <swirjson_lstSerialize+0x59>
mov    -0x28(%rbp),%rdi
lea    0xdb4(%rip),%rsi
call   1060 <strcat@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<swirjson_lstSerialize>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%r13d
mov    %rdi,%r12
mov    $0x800,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1040 <memset@plt>
lea    0xe76(%rip),%rsi
mov    %rbx,%rdi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <sprintf@plt>
test   %r13d,%r13d
jle    124a <swirjson_lstSerialize+0xfa>
lea    -0x1(%r13),%ebp
mov    %r13d,%r13d
xor    %r12d,%r12d
jmp    11bd <swirjson_lstSerialize+0x6d>
nopl   0x0(%rax)
add    $0x1,%r12
cmp    %r12,%r13
je     124a <swirjson_lstSerialize+0xfa>
test   %r14,%r14
je     11f0 <swirjson_lstSerialize+0xa0>
mov    (%r14,%r12,8),%rcx
mov    (%r15,%r12,8),%r8
mov    %rbx,%rdi
test   %rcx,%rcx
je     1210 <swirjson_lstSerialize+0xc0>
lea    0xe56(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <sprintf@plt>
jmp    1224 <swirjson_lstSerialize+0xd4>
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r15,%r12,8),%rcx
mov    %rbx,%rdi
lea    0xe0b(%rip),%rsi
mov    %rbx,%rdx
jmp    121d <swirjson_lstSerialize+0xcd>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xdf2(%rip),%rsi
mov    %rbx,%rdx
mov    %r8,%rcx
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    (%r15,%r12,8),%rdi
call   1030 <free@plt>
cmp    %rbp,%r12
jae    11b0 <swirjson_lstSerialize+0x60>
mov    %rbx,%rdi
lea    0xe16(%rip),%rsi
call   1060 <strcat@plt>
jmp    11b0 <swirjson_lstSerialize+0x60>
lea    0xe07(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcat@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"{\22%s\22: [\00"
@.str.1 = [38 x i8] c"%s {\22timestamp\22 : \22\22, \22value\22 : \22%s\22}\00"
@.str.2 = [39 x i8] c"%s {\22timestamp\22 : %lu, \22value\22 : \22%s\22}\00"
@.str.3 = [2 x i8] c",\00"
@.str.4 = [3 x i8] c"]}\00"
define i8* @swirjson_lstSerialize(i8* %0, i32 %1, i8** %2, i64* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i64*
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
store i64* %3, i64** %8
%11 = call i64 @malloc(i32 2048)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %9
%13 = load i8*, i8** %9
%14 = call i32 @memset(i8* %13, i32 0, i32 2048)
%15 = load i8*, i8** %9
%16 = load i8*, i8** %5
%17 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %15, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 0, i32* %10
store i32 0, i32* %10
br label %18
18: ; preds = %80, %4
%19 = load i32, i32* %10
%20 = load i32, i32* %6
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %83
22: ; preds = %18
%23 = load i64*, i64** %8
%24 = icmp eq i64* %23, null
br i1 %24, label %25, label %34
25: ; preds = %22
%26 = load i8*, i8** %9
%27 = load i8*, i8** %9
%28 = load i8**, i8*** %7
%29 = load i32, i32* %10
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8*, i8** %28, i64 %30
%32 = load i8*, i8** %31
%33 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %26, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* %32)
br label %65
34: ; preds = %22
%35 = load i64*, i64** %8
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i64, i64* %35, i64 %37
%39 = load i64, i64* %38
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %50
41: ; preds = %34
%42 = load i8*, i8** %9
%43 = load i8*, i8** %9
%44 = load i8**, i8*** %7
%45 = load i32, i32* %10
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8*, i8** %44, i64 %46
%48 = load i8*, i8** %47
%49 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %42, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %43, i8* %48)
br label %64
50: ; preds = %34
%51 = load i8*, i8** %9
%52 = load i8*, i8** %9
%53 = load i64*, i64** %8
%54 = load i32, i32* %10
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i64, i64* %53, i64 %55
%57 = load i64, i64* %56
%58 = load i8**, i8*** %7
%59 = load i32, i32* %10
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8*, i8** %58, i64 %60
%62 = load i8*, i8** %61
%63 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %51, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %52, i64 %57, i8* %62)
br label %64
64: ; preds = %50, %41
br label %65
65: ; preds = %64, %25
%66 = load i8**, i8*** %7
%67 = load i32, i32* %10
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8*, i8** %66, i64 %68
%70 = load i8*, i8** %69
%71 = call i32 @free(i8* %70)
%72 = load i32, i32* %10
%73 = load i32, i32* %6
%74 = sub nsw i32 %73, 1
%75 = icmp slt i32 %72, %74
br i1 %75, label %76, label %79
76: ; preds = %65
%77 = load i8*, i8** %9
%78 = call i32 @strcat(i8* %77, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %79
79: ; preds = %76, %65
br label %80
80: ; preds = %79
%81 = load i32, i32* %10
%82 = add nsw i32 %81, 1
store i32 %82, i32* %10
br label %18
83: ; preds = %18
%84 = load i8*, i8** %9
%85 = call i32 @strcat(i8* %84, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%86 = load i8*, i8** %9
ret i8* %86
}

LLVM IR (-O2):
@.str = [9 x i8] c"{\22%s\22: [\00"
@.str.1 = [38 x i8] c"%s {\22timestamp\22 : \22\22, \22value\22 : \22%s\22}\00"
@.str.2 = [39 x i8] c"%s {\22timestamp\22 : %lu, \22value\22 : \22%s\22}\00"
@.str.3 = [2 x i8] c",\00"
@.str.4 = [3 x i8] c"]}\00"
define i8* @swirjson_lstSerialize(i8* %0, i32 %1, i8** nocapture %2, i64* %3) {
%5 = tail call i64 @malloc(i32 2048)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i32 @memset(i8* %6, i32 0, i32 2048)
%8 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = icmp sgt i32 %1, 0
br i1 %9, label %10, label %41
10: ; preds = %4
%11 = icmp eq i64* %3, null
%12 = add nsw i32 %1, -1
%13 = zext i32 %12 to i64
%14 = zext i32 %1 to i64
br label %15
15: ; preds = %10, %38
%16 = phi i64 [ 0, %10 ], [ %39, %38 ]
br i1 %11, label %17, label %21
17: ; preds = %15
%18 = getelementptr inbounds i8*, i8** %2, i64 %16
%19 = load i8*, i8** %18
%20 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %6, i8* %19)
br label %31
21: ; preds = %15
%22 = getelementptr inbounds i64, i64* %3, i64 %16
%23 = load i64, i64* %22
%24 = icmp eq i64 %23, 0
%25 = getelementptr inbounds i8*, i8** %2, i64 %16
%26 = load i8*, i8** %25
br i1 %24, label %27, label %29
27: ; preds = %21
%28 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %6, i8* %26)
br label %31
29: ; preds = %21
%30 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %6, i64 %23, i8* %26)
br label %31
31: ; preds = %27, %29, %17
%32 = getelementptr inbounds i8*, i8** %2, i64 %16
%33 = load i8*, i8** %32
%34 = tail call i32 @free(i8* %33)
%35 = icmp ult i64 %16, %13
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = tail call i32 @strcat(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %31, %36
%39 = add nuw nsw i64 %16, 1
%40 = icmp eq i64 %39, %14
br i1 %40, label %41, label %15
41: ; preds = %38, %4
%42 = tail call i32 @strcat(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
ret i8* %6
}