#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 */
scalar_t__ ceil(int /*<<< orphan*/);
int /*<<< orphan*/ log10(double);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ sprintf(char *, char *, unsigned long);
size_t netstring_encode_new(char **netstring, char *data, size_t len) {
  char *ns;
  size_t num_len = 1;
  if (len == 0) {
    ns = malloc(3);
    ns[0] = '0';
    ns[1] = ':';
    ns[2] = ',';
  } else {
    num_len = (size_t)ceil(log10((double)len + 1));
    ns = malloc(num_len + len + 2);
    sprintf(ns, "%lu:", (unsigned long)len);
    memcpy(ns + num_len + 1, data, len);
    ns[num_len + len + 1] = ',';
  }
  *netstring = ns;
  return num_len + len + 2;
}


x86 assembly (clang, -O0):
<netstring_encode_new>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x1,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a1 <netstring_encode_new+0x51>
mov    $0x3,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x30,(%rax)
mov    -0x20(%rbp),%rax
movb   $0x3a,0x1(%rax)
mov    -0x20(%rbp),%rax
movb   $0x2c,0x2(%rax)
jmp    1237 <netstring_encode_new+0xe7>
movaps 0xe68(%rip),%xmm1
movq   -0x18(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xe67(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xe30(%rip),%xmm1
addsd  %xmm1,%xmm0
call   1060 <log10@plt>
mov    %eax,%edi
call   1030 <ceil@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
add    -0x18(%rbp),%rdi
add    $0x2,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe28(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
add    -0x18(%rbp),%rcx
movb   $0x2c,0x1(%rax,%rcx,1)
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
add    -0x18(%rbp),%rax
add    $0x2,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netstring_encode_new>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rdi,%r14
test   %rdx,%rdx
je     11e4 <netstring_encode_new+0x94>
mov    %rsi,%r15
movq   %r12,%xmm1
punpckldq 0xe8a(%rip),%xmm1
subpd  0xe92(%rip),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
addsd  %xmm1,%xmm0
addsd  0xe8e(%rip),%xmm0
call   1060 <log10@plt>
mov    %eax,%edi
call   1030 <ceil@plt>
mov    %rax,%r13
lea    (%rax,%r12,1),%rbp
lea    (%rax,%r12,1),%rdi
add    $0x2,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
lea    0xe6c(%rip),%rsi
mov    %rax,%rdi
mov    %r12,%rdx
call   1070 <sprintf@plt>
lea    (%rbx,%r13,1),%rdi
add    $0x1,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
lea    (%rbx,%rbp,1),%rax
add    $0x1,%rax
jmp    1200 <netstring_encode_new+0xb0>
mov    $0x3,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
movw   $0x3a30,(%rax)
add    $0x2,%rax
mov    $0x1,%r13d
movb   $0x2c,(%rax)
mov    %rbx,(%r14)
lea    (%r12,%r13,1),%rax
add    $0x2,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%lu:\00"
define i64 @netstring_encode_new(i8** nocapture writeonly %0, i8* %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %5, label %9
5: ; preds = %3
%6 = tail call dereferenceable_or_null(3) i8* @malloc(i64 3)
store i8 48, i8* %6
%7 = getelementptr inbounds i8, i8* %6, i64 1
store i8 58, i8* %7
%8 = getelementptr inbounds i8, i8* %6, i64 2
br label %23
9: ; preds = %3
%10 = uitofp i64 %2 to double
%11 = fadd double %10, 1.000000e+00
%12 = tail call i32 @log10(double %11)
%13 = tail call i64 @ceil(i32 %12)
%14 = add i64 %13, %2
%15 = add i64 %14, 2
%16 = tail call i8* @malloc(i64 %15)
%17 = tail call i32 @sprintf(i8* dereferenceable(1) %16, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %2)
%18 = getelementptr inbounds i8, i8* %16, i64 %13
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = tail call i32 @memcpy(i8* %19, i8* %1, i64 %2)
%21 = add i64 %14, 1
%22 = getelementptr inbounds i8, i8* %16, i64 %21
br label %23
23: ; preds = %9, %5
%24 = phi i8* [ %22, %9 ], [ %8, %5 ]
%25 = phi i8* [ %16, %9 ], [ %6, %5 ]
%26 = phi i64 [ %13, %9 ], [ 1, %5 ]
store i8 44, i8* %24
store i8* %25, i8** %0
%27 = add i64 %2, 2
%28 = add i64 %27, %26
ret i64 %28
}