#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 */
typedef size_t uint8_t;
typedef size_t uint32_t;

/* Variables and functions */
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, size_t);
int /*<<< orphan*/ strcat(char *, char *);
size_t strlen(char *);
uint32_t json_build(char *buff, char **name, uint32_t *val, uint8_t _size) {
  char temp[20];
  uint32_t i = 0;
  memcpy(&buff[0], "{\0", 2);
  for (i = 0; i < _size; i++) {
    strcat(buff, "\"");
    strcat(buff, name[i]);
    strcat(buff, "\":\"");
    sprintf(temp, "%d", val[i]);
    strcat(buff, temp);
    if (_size == i + 1)
      strcat(buff, "\"");
    else
      strcat(buff, "\",");
  }
  strcat(buff, "}");
  return strlen(buff);
}


x86 assembly (clang, -O0):
<json_build>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe95(%rip),%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
movq   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1239 <json_build+0xf9>
mov    -0x8(%rbp),%rdi
lea    0xe6f(%rip),%rsi
call   1050 <strcat@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1050 <strcat@plt>
mov    -0x8(%rbp),%rdi
lea    0xe48(%rip),%rsi
call   1050 <strcat@plt>
lea    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe30(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x20(%rbp),%rax
mov    -0x48(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
jne    1213 <json_build+0xd3>
mov    -0x8(%rbp),%rdi
lea    0xdfc(%rip),%rsi
call   1050 <strcat@plt>
jmp    1223 <json_build+0xe3>
mov    -0x8(%rbp),%rdi
lea    0xdec(%rip),%rsi
call   1050 <strcat@plt>
jmp    1228 <json_build+0xe8>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    117d <json_build+0x3d>
mov    -0x8(%rbp),%rdi
lea    0xdc9(%rip),%rsi
call   1050 <strcat@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<json_build>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,%r14
mov    %rdx,%rbp
mov    %rsi,0x18(%rsp)
mov    %rdi,%rbx
lea    0xe9d(%rip),%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    %r14,0x8(%rsp)
test   %r14,%r14
je     1211 <json_build+0xd1>
mov    0x8(%rsp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,0x10(%rsp)
xor    %r12d,%r12d
lea    0xe72(%rip),%r13
lea    0x20(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r13,%rsi
call   1050 <strcat@plt>
mov    0x18(%rsp),%rax
mov    (%rax,%r12,8),%rsi
mov    %rbx,%rdi
call   1050 <strcat@plt>
mov    %rbx,%rdi
lea    0xe3d(%rip),%rsi
call   1050 <strcat@plt>
mov    0x0(%rbp,%r12,8),%rdx
mov    %r15,%rdi
lea    0xe2d(%rip),%rsi
call   1060 <sprintf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strcat@plt>
lea    0x1(%r12),%r14
cmp    %r12,0x10(%rsp)
lea    0xe0f(%rip),%rsi
cmove  %r13,%rsi
mov    %rbx,%rdi
call   1050 <strcat@plt>
mov    %r14,%r12
cmp    %r14,0x8(%rsp)
jne    11a0 <json_build+0x60>
lea    0xdf5(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcat@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"{\00\00"
@.str.1 = [2 x i8] c"\22\00"
@.str.2 = [4 x i8] c"\22:\22\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [3 x i8] c"\22,\00"
@.str.5 = [2 x i8] c"}\00"
define i64 @json_build(i8* %0, i8** %1, i64* %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i64
%9 = alloca [20 x i8]
%10 = alloca i64
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
store i64 %3, i64* %8
store i64 0, i64* %10
%11 = load i8*, i8** %5
%12 = getelementptr inbounds i8, i8* %11, i64 0
%13 = call i32 @memcpy(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
store i64 0, i64* %10
br label %14
14: ; preds = %49, %4
%15 = load i64, i64* %10
%16 = load i64, i64* %8
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %52
18: ; preds = %14
%19 = load i8*, i8** %5
%20 = call i32 @strcat(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = load i8*, i8** %5
%22 = load i8**, i8*** %6
%23 = load i64, i64* %10
%24 = getelementptr inbounds i8*, i8** %22, i64 %23
%25 = load i8*, i8** %24
%26 = call i32 @strcat(i8* %21, i8* %25)
%27 = load i8*, i8** %5
%28 = call i32 @strcat(i8* %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%29 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%30 = load i64*, i64** %7
%31 = load i64, i64* %10
%32 = getelementptr inbounds i64, i64* %30, i64 %31
%33 = load i64, i64* %32
%34 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i64 %33)
%35 = load i8*, i8** %5
%36 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%37 = call i32 @strcat(i8* %35, i8* %36)
%38 = load i64, i64* %8
%39 = load i64, i64* %10
%40 = add i64 %39, 1
%41 = icmp eq i64 %38, %40
br i1 %41, label %42, label %45
42: ; preds = %18
%43 = load i8*, i8** %5
%44 = call i32 @strcat(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %48
45: ; preds = %18
%46 = load i8*, i8** %5
%47 = call i32 @strcat(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %48
48: ; preds = %45, %42
br label %49
49: ; preds = %48
%50 = load i64, i64* %10
%51 = add i64 %50, 1
store i64 %51, i64* %10
br label %14
52: ; preds = %14
%53 = load i8*, i8** %5
%54 = call i32 @strcat(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%55 = load i8*, i8** %5
%56 = call i64 @strlen(i8* %55)
ret i64 %56
}

LLVM IR (-O2):
@.str = [3 x i8] c"{\00\00"
@.str.1 = [2 x i8] c"\22\00"
@.str.2 = [4 x i8] c"\22:\22\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [3 x i8] c"\22,\00"
@.str.5 = [2 x i8] c"}\00"
define i64 @json_build(i8* %0, i8** nocapture %1, i64* nocapture %2, i64 %3) {
%5 = alloca [20 x i8]
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%7 = tail call i32 @memcpy(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%8 = icmp eq i64 %3, 0
br i1 %8, label %25, label %9
9: ; preds = %4, %9
%10 = phi i64 [ %20, %9 ], [ 0, %4 ]
%11 = call i32 @strcat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = getelementptr inbounds i8*, i8** %1, i64 %10
%13 = load i8*, i8** %12
%14 = call i32 @strcat(i8* %0, i8* %13)
%15 = call i32 @strcat(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%16 = getelementptr inbounds i64, i64* %2, i64 %10
%17 = load i64, i64* %16
%18 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i64 %17)
%19 = call i32 @strcat(i8* %0, i8* %6)
%20 = add nuw i64 %10, 1
%21 = icmp eq i64 %20, %3
%22 = select i1 %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0)
%23 = call i32 @strcat(i8* %0, i8* %22)
%24 = icmp eq i64 %20, %3
br i1 %24, label %25, label %9
25: ; preds = %9, %4
%26 = call i32 @strcat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%27 = call i64 @strlen(i8* dereferenceable(1) %0)
ret i64 %27
}