#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 int /*<<< orphan*/ wchar_t;
typedef size_t uint32_t;

/* Variables and functions */
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ size_buffer(char **, size_t *, size_t);
int /*<<< orphan*/ uint32Pack(size_t *, char **, size_t *, size_t *);
char *wchar_to_utf8(int /*<<< orphan*/ const *const, int /*<<< orphan*/,
                    size_t *);
int /*<<< orphan*/ wcslen(int /*<<< orphan*/ const *const);
size_t ustringPack(const wchar_t *const *data, char **buffer, size_t *size,
                   size_t *pos) {
  uint32_t utf8_size;
  const char *utf8_text;
  if (data == NULL) {
    utf8_size = 0;
  } else {
    utf8_text = wchar_to_utf8(*data, wcslen(*data), &utf8_size);
  }
  uint32Pack(&utf8_size, buffer, size, pos);
  size_buffer(buffer, size, *pos + utf8_size);
  memcpy(*buffer + *pos, utf8_text, utf8_size);
  *pos += utf8_size;
  return sizeof(uint32_t) + utf8_size;
}


x86 assembly (clang, -O0):
<ustringPack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1180 <ustringPack+0x30>
movq   $0x0,-0x28(%rbp)
jmp    11aa <ustringPack+0x5a>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <wcslen@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
lea    -0x28(%rbp),%rdx
call   1030 <wchar_to_utf8@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x28(%rbp),%rdi
call   1050 <uint32Pack@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
add    -0x28(%rbp),%rdx
call   1070 <size_buffer@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
add    (%rax),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x8,%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ustringPack>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %rdx,%r15
mov    %rsi,%r14
test   %rdi,%rdi
je     1183 <ustringPack+0x33>
mov    (%rdi),%r12
mov    %r12,%rdi
call   1040 <wcslen@plt>
mov    %rsp,%rdx
mov    %r12,%rdi
mov    %eax,%esi
call   1030 <wchar_to_utf8@plt>
mov    %rax,%r12
jmp    118b <ustringPack+0x3b>
movq   $0x0,(%rsp)
mov    %rsp,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1050 <uint32Pack@plt>
mov    (%rsp),%rdx
add    (%rbx),%rdx
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <size_buffer@plt>
mov    (%r14),%rdi
add    (%rbx),%rdi
mov    (%rsp),%rdx
mov    %r12,%rsi
call   1060 <memcpy@plt>
mov    (%rsp),%rax
add    %rax,(%rbx)
add    $0x8,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @ustringPack(i32** %0, i8** %1, i64* %2, i64* %3) {
%5 = alloca i32**
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i64*
%9 = alloca i64
%10 = alloca i8*
store i32** %0, i32*** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
store i64* %3, i64** %8
%11 = load i32**, i32*** %5
%12 = icmp eq i32** %11, null
br i1 %12, label %13, label %14
13: ; preds = %4
store i64 0, i64* %9
br label %21
14: ; preds = %4
%15 = load i32**, i32*** %5
%16 = load i32*, i32** %15
%17 = load i32**, i32*** %5
%18 = load i32*, i32** %17
%19 = call i32 @wcslen(i32* %18)
%20 = call i8* @wchar_to_utf8(i32* %16, i32 %19, i64* %9)
store i8* %20, i8** %10
br label %21
21: ; preds = %14, %13
%22 = load i8**, i8*** %6
%23 = load i64*, i64** %7
%24 = load i64*, i64** %8
%25 = call i32 @uint32Pack(i64* %9, i8** %22, i64* %23, i64* %24)
%26 = load i8**, i8*** %6
%27 = load i64*, i64** %7
%28 = load i64*, i64** %8
%29 = load i64, i64* %28
%30 = load i64, i64* %9
%31 = add i64 %29, %30
%32 = call i32 @size_buffer(i8** %26, i64* %27, i64 %31)
%33 = load i8**, i8*** %6
%34 = load i8*, i8** %33
%35 = load i64*, i64** %8
%36 = load i64, i64* %35
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i8*, i8** %10
%39 = load i64, i64* %9
%40 = call i32 @memcpy(i8* %37, i8* %38, i64 %39)
%41 = load i64, i64* %9
%42 = load i64*, i64** %8
%43 = load i64, i64* %42
%44 = add i64 %43, %41
store i64 %44, i64* %42
%45 = load i64, i64* %9
%46 = add i64 8, %45
ret i64 %46
}

LLVM IR (-O2):
define i64 @ustringPack(i32** %0, i8** %1, i64* %2, i64* %3) {
%5 = alloca i64
%6 = bitcast i64* %5 to i8*
%7 = icmp eq i32** %0, null
br i1 %7, label %8, label %9
8: ; preds = %4
store i64 0, i64* %5
br label %13
9: ; preds = %4
%10 = load i32*, i32** %0
%11 = tail call i32 @wcslen(i32* %10)
%12 = call i8* @wchar_to_utf8(i32* %10, i32 %11, i64* %5)
br label %13
13: ; preds = %9, %8
%14 = phi i8* [ undef, %8 ], [ %12, %9 ]
%15 = call i32 @uint32Pack(i64* %5, i8** %1, i64* %2, i64* %3)
%16 = load i64, i64* %3
%17 = load i64, i64* %5
%18 = add i64 %17, %16
%19 = call i32 @size_buffer(i8** %1, i64* %2, i64 %18)
%20 = load i8*, i8** %1
%21 = load i64, i64* %3
%22 = getelementptr inbounds i8, i8* %20, i64 %21
%23 = load i64, i64* %5
%24 = call i32 @memcpy(i8* %22, i8* %14, i64 %23)
%25 = load i64, i64* %5
%26 = load i64, i64* %3
%27 = add i64 %26, %25
store i64 %27, i64* %3
%28 = add i64 %25, 8
ret i64 %28
}