#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 *malloc(int);
char *realloc(char *, size_t);
int /*<<< orphan*/ sprintf(char *, char *, size_t, unsigned char const *);
size_t strlen(char const *);
int /*<<< orphan*/ strncat(char *, char *, size_t);
char *addField(char **dst, const unsigned char *field) {
  size_t dlen = (*dst == NULL) ? 0 : strlen(*dst);
  size_t flen = strlen((const char *)field);
  char *buf = malloc(11 + 1 + flen + 1);
  if (buf != NULL) {
    char *tmp;
    sprintf(buf, "%u:%s", flen, field);
    tmp = realloc(*dst, dlen + strlen(buf) + 1);
    if (tmp != NULL) {
      if (dlen == 0) {
        *tmp = '\0';
      }
      *dst = tmp;
      strncat(*dst, buf, strlen(buf) + 1);
    }
    free(buf);
  }
  return *dst;
}


x86 assembly (clang, -O0):
<addField>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1189 <addField+0x29>
xor    %eax,%eax
mov    %rax,-0x38(%rbp)
jmp    1199 <addField+0x39>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0xc,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     127d <addField+0x11d>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe1d(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%rdi
add    %rax,%rsi
add    $0x1,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1274 <addField+0x114>
cmpq   $0x0,-0x18(%rbp)
jne    1239 <addField+0xd9>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1050 <strncat@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addField>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r14
mov    (%rdi),%rbx
test   %rbx,%rbx
je     1186 <addField+0x26>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
jmp    1189 <addField+0x29>
xor    %r15d,%r15d
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
lea    0xd(%rax),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1209 <addField+0xa9>
mov    %rax,%r13
lea    0xe55(%rip),%rsi
mov    %rax,%rdi
mov    %rbp,%rdx
mov    %r12,%rcx
call   1080 <sprintf@plt>
mov    (%r14),%rbx
mov    %r13,%rdi
call   1040 <strlen@plt>
lea    (%r15,%rax,1),%rsi
add    $0x1,%rsi
mov    %rbx,%rdi
call   1070 <realloc@plt>
test   %rax,%rax
je     11fe <addField+0x9e>
mov    %rax,%rbx
test   %r15,%r15
jne    11e4 <addField+0x84>
movb   $0x0,(%rbx)
mov    %rbx,(%r14)
mov    %r13,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1050 <strncat@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    (%r14),%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"%u:%s\00"
define i8* @addField(i8** nocapture %0, i8* %1) {
%3 = load i8*, i8** %0
%4 = icmp eq i8* %3, null
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i64 @strlen(i8* %3)
br label %7
7: ; preds = %2, %5
%8 = phi i64 [ %6, %5 ], [ 0, %2 ]
%9 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%10 = trunc i64 %9 to i32
%11 = add i32 %10, 13
%12 = tail call i8* @malloc(i32 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %32, label %14
14: ; preds = %7
%15 = tail call i32 @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %9, i8* %1)
%16 = load i8*, i8** %0
%17 = tail call i64 @strlen(i8* %12)
%18 = add i64 %8, 1
%19 = add i64 %18, %17
%20 = tail call i8* @realloc(i8* %16, i64 %19)
%21 = icmp eq i8* %20, null
br i1 %21, label %29, label %22
22: ; preds = %14
%23 = icmp eq i64 %8, 0
br i1 %23, label %24, label %25
24: ; preds = %22
store i8 0, i8* %20
br label %25
25: ; preds = %24, %22
store i8* %20, i8** %0
%26 = tail call i64 @strlen(i8* %12)
%27 = add i64 %26, 1
%28 = tail call i32 @strncat(i8* %20, i8* %12, i64 %27)
br label %29
29: ; preds = %25, %14
%30 = tail call i32 @free(i8* %12)
%31 = load i8*, i8** %0
br label %32
32: ; preds = %29, %7
%33 = phi i8* [ %31, %29 ], [ %3, %7 ]
ret i8* %33
}