#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 uint32_t;

/* Variables and functions */
char *calloc(int, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ uint32Unpack(char const *, size_t, int *);
size_t astringUnpack(const char *buffer, size_t size, char **data) {
  uint32_t length;
  if (size < sizeof(uint32_t)) {
    return sizeof(uint32_t);
  }
  uint32Unpack(buffer, size, &length);
  buffer += sizeof(uint32_t);
  if (size < sizeof(uint32_t) + length * sizeof(char)) {
    return sizeof(uint32_t) + length * sizeof(char);
  }
  if (*data != NULL)
    free(*data);
  *data = calloc(length + 1, sizeof(char));
  memcpy(*data, buffer, length * sizeof(char));
  return sizeof(uint32_t) + length * sizeof(char);
}


x86 assembly (clang, -O0):
<astringUnpack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x4,-0x18(%rbp)
jae    116c <astringUnpack+0x2c>
movq   $0x4,-0x8(%rbp)
jmp    1215 <astringUnpack+0xd5>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1060 <uint32Unpack@plt>
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
shl    $0x0,%rcx
add    $0x4,%rcx
cmp    %rcx,%rax
jae    11b7 <astringUnpack+0x77>
movslq -0x24(%rbp),%rax
shl    $0x0,%rax
add    $0x4,%rax
mov    %rax,-0x8(%rbp)
jmp    1215 <astringUnpack+0xd5>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     11d1 <astringUnpack+0x91>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x24(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edx
call   1050 <memcpy@plt>
movslq -0x24(%rbp),%rax
shl    $0x0,%rax
add    $0x4,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<astringUnpack>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0x4,%eax
cmp    $0x4,%rsi
jb     11b2 <astringUnpack+0x72>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r12
lea    0x4(%rsp),%rdx
call   1060 <uint32Unpack@plt>
movslq 0x4(%rsp),%r15
lea    0x4(%r15),%rax
cmp    %rbx,%rax
ja     11b2 <astringUnpack+0x72>
add    $0x4,%r12
mov    (%r14),%rdi
test   %rdi,%rdi
je     118a <astringUnpack+0x4a>
call   1030 <free@plt>
mov    0x4(%rsp),%r15d
lea    0x1(%r15),%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,(%r14)
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1050 <memcpy@plt>
movslq 0x4(%rsp),%rax
add    $0x4,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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