#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 scalar_t__ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ memcpy(scalar_t__ *, char const *, size_t);
size_t strlen(char const *);
size_t uint32_pack(size_t, scalar_t__ *);
size_t string_pack(const char *str, uint8_t *out) {
  if (str == NULL) {
    out[0] = 0;
    return 1;
  }
  size_t len = strlen(str);
  size_t rv = uint32_pack(len, out);
  memcpy(out + rv, str, len);
  return rv + len;
}


x86 assembly (clang, -O0):
<string_pack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1163 <string_pack+0x33>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movq   $0x1,-0x8(%rbp)
jmp    11a9 <string_pack+0x79>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <uint32_pack@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_pack>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
test   %rdi,%rdi
je     116d <string_pack+0x3d>
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r14,%rsi
call   1050 <uint32_pack@plt>
mov    %rax,%rbx
lea    (%r14,%rax,8),%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
add    %r12,%rbx
jmp    1179 <string_pack+0x49>
movq   $0x0,(%r14)
mov    $0x1,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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