#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*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(unsigned char *);
int hpack_decode_byte_huffman(int /*<<< orphan*/ const *, int *);
unsigned char *malloc(size_t);
char *hpack_decode_str_huffman(const uint8_t *data, size_t length) {
  unsigned char *str = malloc(length * 2 + 1);
  if (str == NULL)
    return NULL;
  size_t len = 0;
  int bit_offset = -8 * length;
  data += length;
  for (;;) {
    int c = hpack_decode_byte_huffman(data, &bit_offset);
    if (c < 0) {
      errno = EINVAL;
      goto error;
    }
    str[len++] = c;
    if (c == 256)
      break;
  }
  return (char *)str;
error:
  free(str);
  return NULL;
}


x86 assembly (clang, -O0):
<hpack_decode_str_huffman>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
shl    $0x1,%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    116d <hpack_decode_str_huffman+0x3d>
movq   $0x0,-0x8(%rbp)
jmp    120e <hpack_decode_str_huffman+0xde>
movq   $0x0,-0x28(%rbp)
imul   $0xfffffffffffffff8,-0x18(%rbp),%rax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
call   1050 <hpack_decode_byte_huffman@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    11be <hpack_decode_str_huffman+0x8e>
mov    0x2e2a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e29(%rip),%rax
mov    %ecx,(%rax)
jmp    11fd <hpack_decode_str_huffman+0xcd>
mov    -0x30(%rbp),%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x28(%rbp)
mov    %dl,(%rax,%rcx,1)
cmpl   $0x100,-0x30(%rbp)
jne    11eb <hpack_decode_str_huffman+0xbb>
jmp    11f0 <hpack_decode_str_huffman+0xc0>
jmp    118d <hpack_decode_str_huffman+0x5d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    120e <hpack_decode_str_huffman+0xde>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hpack_decode_str_huffman>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r15
lea    (%rsi,%rsi,1),%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     11a8 <hpack_decode_str_huffman+0x78>
mov    %rax,%r14
lea    0x0(,%rbx,8),%eax
neg    %eax
mov    %eax,0x4(%rsp)
lea    (%r15,%rbx,4),%r12
lea    0x4(%rsp),%r15
mov    %r14,%rbx
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <hpack_decode_byte_huffman@plt>
test   %eax,%eax
js     118e <hpack_decode_str_huffman+0x5e>
mov    %al,(%rbx)
add    $0x1,%rbx
cmp    $0x100,%eax
jne    1170 <hpack_decode_str_huffman+0x40>
jmp    11ab <hpack_decode_str_huffman+0x7b>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    0x2e42(%rip),%rcx
mov    %eax,(%rcx)
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
define noalias i8* @hpack_decode_str_huffman(i32* %0, i64 %1) {
%3 = alloca i32
%4 = shl i64 %1, 1
%5 = or i64 %4, 1
%6 = tail call i8* @malloc(i64 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %27, label %8
8: ; preds = %2
%9 = bitcast i32* %3 to i8*
%10 = trunc i64 %1 to i32
%11 = mul i32 %10, -8
store i32 %11, i32* %3
%12 = getelementptr inbounds i32, i32* %0, i64 %1
br label %13
13: ; preds = %17, %8
%14 = phi i64 [ 0, %8 ], [ %19, %17 ]
%15 = call i32 @hpack_decode_byte_huffman(i32* %12, i32* %3)
%16 = icmp slt i32 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %13
%18 = trunc i32 %15 to i8
%19 = add i64 %14, 1
%20 = getelementptr inbounds i8, i8* %6, i64 %14
store i8 %18, i8* %20
%21 = icmp eq i32 %15, 256
br i1 %21, label %25, label %13
22: ; preds = %13
%23 = load i32, i32* @EINVAL
store i32 %23, i32* @errno
%24 = call i32 @free(i8* %6)
br label %25
25: ; preds = %17, %22
%26 = phi i8* [ null, %22 ], [ %6, %17 ]
br label %27
27: ; preds = %2, %25
%28 = phi i8* [ %26, %25 ], [ null, %2 ]
ret i8* %28
}