#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*/ memcpy(char *, void *, size_t);
size_t cb_new_kv(const char *key, size_t keylen, void *value, size_t len,
                 void *out) {
  char *dst = (char *)out;
  if (dst != key) {
    memcpy(dst, key, keylen);
  }
  dst[keylen] = 0;
  memcpy(dst + keylen + 1, value, len);
  return len + keylen + 1;
}


x86 assembly (clang, -O0):
<cb_new_kv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x8(%rbp),%rax
je     1153 <cb_new_kv+0x43>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
add    -0x10(%rbp),%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    -0x10(%rbp),%rax
add    $0x1,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cb_new_kv>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %r8,%rbx
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
cmp    %rdi,%r8
je     1137 <cb_new_kv+0x27>
mov    %rdi,%rsi
mov    %rbx,%rdi
mov    %r12,%rdx
call   1030 <memcpy@plt>
movb   $0x0,(%rbx,%r12,1)
lea    (%rbx,%r12,1),%rdi
add    $0x1,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <memcpy@plt>
lea    (%r12,%r14,1),%rax
add    $0x1,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @cb_new_kv(i8* %0, i64 %1, i8* %2, i64 %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
store i8* %4, i8** %10
%12 = load i8*, i8** %10
store i8* %12, i8** %11
%13 = load i8*, i8** %11
%14 = load i8*, i8** %6
%15 = icmp ne i8* %13, %14
br i1 %15, label %16, label %21
16: ; preds = %5
%17 = load i8*, i8** %11
%18 = load i8*, i8** %6
%19 = load i64, i64* %7
%20 = call i32 @memcpy(i8* %17, i8* %18, i64 %19)
br label %21
21: ; preds = %16, %5
%22 = load i8*, i8** %11
%23 = load i64, i64* %7
%24 = getelementptr inbounds i8, i8* %22, i64 %23
store i8 0, i8* %24
%25 = load i8*, i8** %11
%26 = load i64, i64* %7
%27 = getelementptr inbounds i8, i8* %25, i64 %26
%28 = getelementptr inbounds i8, i8* %27, i64 1
%29 = load i8*, i8** %8
%30 = load i64, i64* %9
%31 = call i32 @memcpy(i8* %28, i8* %29, i64 %30)
%32 = load i64, i64* %9
%33 = load i64, i64* %7
%34 = add i64 %32, %33
%35 = add i64 %34, 1
ret i64 %35
}

LLVM IR (-O2):
define i64 @cb_new_kv(i8* %0, i64 %1, i8* %2, i64 %3, i8* %4) {
%6 = icmp eq i8* %4, %0
br i1 %6, label %9, label %7
7: ; preds = %5
%8 = tail call i32 @memcpy(i8* %4, i8* %0, i64 %1)
br label %9
9: ; preds = %7, %5
%10 = getelementptr inbounds i8, i8* %4, i64 %1
store i8 0, i8* %10
%11 = getelementptr inbounds i8, i8* %10, i64 1
%12 = tail call i32 @memcpy(i8* %11, i8* %2, i64 %3)
%13 = add i64 %1, 1
%14 = add i64 %13, %3
ret i64 %14
}