#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*/ *calloc(size_t, int);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                          size_t);
int /*<<< orphan*/ uint32Unpack(char const *, size_t, size_t *);
int /*<<< orphan*/ *utf8_to_wchar(char *, size_t, size_t *);
size_t ustringUnpack(const char *buffer, size_t size, wchar_t **data) {
  size_t wchar_count;
  uint32_t length;
  if (size < sizeof(uint32_t)) {
    return sizeof(uint32_t);
  }
  uint32Unpack(buffer, size, &length);
  buffer += sizeof(uint32_t);
  size -= sizeof(uint32_t);
  if (size < length) {
    return sizeof(uint32_t) + length;
  }
  const wchar_t *wchar = utf8_to_wchar((char *)buffer, length, &wchar_count);
  if (*data != NULL)
    free(*data);
  *data = calloc(wchar_count + 1, sizeof(wchar_t));
  memcpy(*data, wchar, wchar_count * sizeof(wchar_t));
  return sizeof(uint32_t) + length;
}


x86 assembly (clang, -O0):
<ustringUnpack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x8,-0x18(%rbp)
jae    117c <ustringUnpack+0x2c>
movq   $0x8,-0x8(%rbp)
jmp    1233 <ustringUnpack+0xe3>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1070 <uint32Unpack@plt>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
sub    $0x8,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    11c4 <ustringUnpack+0x74>
mov    -0x30(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x8(%rbp)
jmp    1233 <ustringUnpack+0xe3>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1040 <utf8_to_wchar@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     11f3 <ustringUnpack+0xa3>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
shl    $0x2,%rdx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ustringUnpack>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0x8,%eax
cmp    $0x8,%rsi
jb     11d8 <ustringUnpack+0x88>
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsp,%rdx
call   1070 <uint32Unpack@plt>
add    $0xfffffffffffffff8,%rbx
mov    (%rsp),%rax
cmp    %rax,%rbx
jb     11d4 <ustringUnpack+0x84>
add    $0x8,%r14
lea    0x8(%rsp),%rdx
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <utf8_to_wchar@plt>
mov    %rax,%r14
mov    (%r15),%rdi
test   %rdi,%rdi
je     11a6 <ustringUnpack+0x56>
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
add    $0x1,%rdi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,(%r15)
mov    0x8(%rsp),%rdx
shl    $0x2,%rdx
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <memcpy@plt>
mov    (%rsp),%rax
add    $0x8,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @ustringUnpack(i8* %0, i64 %1, i32** %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32**
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32** %2, i32*** %7
%11 = load i64, i64* %6
%12 = icmp ult i64 %11, 8
br i1 %12, label %13, label %14
13: ; preds = %3
store i64 8, i64* %4
br label %52
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = load i64, i64* %6
%17 = call i32 @uint32Unpack(i8* %15, i64 %16, i64* %9)
%18 = load i8*, i8** %5
%19 = getelementptr inbounds i8, i8* %18, i64 8
store i8* %19, i8** %5
%20 = load i64, i64* %6
%21 = sub i64 %20, 8
store i64 %21, i64* %6
%22 = load i64, i64* %6
%23 = load i64, i64* %9
%24 = icmp ult i64 %22, %23
br i1 %24, label %25, label %28
25: ; preds = %14
%26 = load i64, i64* %9
%27 = add i64 8, %26
store i64 %27, i64* %4
br label %52
28: ; preds = %14
%29 = load i8*, i8** %5
%30 = load i64, i64* %9
%31 = call i32* @utf8_to_wchar(i8* %29, i64 %30, i64* %8)
store i32* %31, i32** %10
%32 = load i32**, i32*** %7
%33 = load i32*, i32** %32
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %39
35: ; preds = %28
%36 = load i32**, i32*** %7
%37 = load i32*, i32** %36
%38 = call i32 @free(i32* %37)
br label %39
39: ; preds = %35, %28
%40 = load i64, i64* %8
%41 = add i64 %40, 1
%42 = call i32* @calloc(i64 %41, i32 4)
%43 = load i32**, i32*** %7
store i32* %42, i32** %43
%44 = load i32**, i32*** %7
%45 = load i32*, i32** %44
%46 = load i32*, i32** %10
%47 = load i64, i64* %8
%48 = mul i64 %47, 4
%49 = call i32 @memcpy(i32* %45, i32* %46, i64 %48)
%50 = load i64, i64* %9
%51 = add i64 8, %50
store i64 %51, i64* %4
br label %52
52: ; preds = %39, %25, %13
%53 = load i64, i64* %4
ret i64 %53
}

LLVM IR (-O2):
define i64 @ustringUnpack(i8* %0, i64 %1, i32** nocapture %2) {
%4 = alloca i64
%5 = alloca i64
%6 = bitcast i64* %4 to i8*
%7 = bitcast i64* %5 to i8*
%8 = icmp ult i64 %1, 8
br i1 %8, label %32, label %9
9: ; preds = %3
%10 = call i32 @uint32Unpack(i8* %0, i64 %1, i64* %5)
%11 = add i64 %1, -8
%12 = load i64, i64* %5
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = add i64 %12, 8
br label %32
16: ; preds = %9
%17 = getelementptr inbounds i8, i8* %0, i64 8
%18 = call i32* @utf8_to_wchar(i8* %17, i64 %12, i64* %4)
%19 = load i32*, i32** %2
%20 = icmp eq i32* %19, null
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = call i32 @free(i32* %19)
br label %23
23: ; preds = %21, %16
%24 = load i64, i64* %4
%25 = add i64 %24, 1
%26 = call i32* @calloc(i64 %25, i32 4)
store i32* %26, i32** %2
%27 = load i64, i64* %4
%28 = shl i64 %27, 2
%29 = call i32 @memcpy(i32* %26, i32* %18, i64 %28)
%30 = load i64, i64* %5
%31 = add i64 %30, 8
br label %32
32: ; preds = %3, %23, %14
%33 = phi i64 [ %15, %14 ], [ %31, %23 ], [ 8, %3 ]
ret i64 %33
}