#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;
struct hpack_decoder {
  int dummy;
};
typedef scalar_t__ int_fast32_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
scalar_t__ hpack_append_hdr(struct hpack_decoder *, char *, char *);
scalar_t__ hpack_decode_int(int, int /*<<< orphan*/ const **, size_t *);
char *hpack_decode_str(int /*<<< orphan*/ const **, size_t *);
char *hpack_lookup_name(struct hpack_decoder *, scalar_t__);
int hpack_decode_hdr_index(struct hpack_decoder *dec,
                           const uint8_t **restrict datap,
                           size_t *restrict lengthp, char **restrict namep,
                           char **restrict valuep) {
  int_fast32_t idx = hpack_decode_int(6, datap, lengthp);
  if (idx < 0)
    return -1;
  char *name;
  if (idx != 0)
    name = hpack_lookup_name(dec, idx);
  else
    name = hpack_decode_str(datap, lengthp);
  if (name == NULL)
    return -1;
  char *value = hpack_decode_str(datap, lengthp);
  if (value == NULL) {
    free(name);
    return -1;
  }
  if (hpack_append_hdr(dec, name, value)) {
    free(value);
    free(name);
    return -1;
  }
  *namep = name;
  *valuep = value;
  return 0;
}


x86 assembly (clang, -O0):
<hpack_decode_hdr_index>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    $0x6,%edi
call   1060 <hpack_decode_int@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    1199 <hpack_decode_hdr_index+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <hpack_decode_hdr_index+0x119>
cmpq   $0x0,-0x38(%rbp)
je     11ba <hpack_decode_hdr_index+0x6a>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1070 <hpack_lookup_name@plt>
mov    %rax,-0x40(%rbp)
jmp    11cb <hpack_decode_hdr_index+0x7b>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <hpack_decode_str@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11e2 <hpack_decode_hdr_index+0x92>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <hpack_decode_hdr_index+0x119>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <hpack_decode_str@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1213 <hpack_decode_hdr_index+0xc3>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <hpack_decode_hdr_index+0x119>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1040 <hpack_append_hdr@plt>
cmp    $0x0,%rax
je     124c <hpack_decode_hdr_index+0xfc>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <hpack_decode_hdr_index+0x119>
mov    -0x40(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x48(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hpack_decode_hdr_index>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%rbx
mov    %rcx,%r15
mov    %rdx,%rbp
mov    %rsi,%r14
mov    %rdi,%r13
mov    $0x6,%edi
call   1060 <hpack_decode_int@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
js     11ea <hpack_decode_hdr_index+0x9a>
mov    %rbx,(%rsp)
je     1192 <hpack_decode_hdr_index+0x42>
mov    %r13,%rdi
mov    %rax,%rsi
call   1070 <hpack_lookup_name@plt>
jmp    119d <hpack_decode_hdr_index+0x4d>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1050 <hpack_decode_str@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11ea <hpack_decode_hdr_index+0x9a>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1050 <hpack_decode_str@plt>
test   %rax,%rax
je     11d3 <hpack_decode_hdr_index+0x83>
mov    %rax,%rbp
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <hpack_append_hdr@plt>
test   %rax,%rax
je     11dd <hpack_decode_hdr_index+0x8d>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11ea <hpack_decode_hdr_index+0x9a>
mov    %rbx,(%r15)
mov    (%rsp),%rax
mov    %rbp,(%rax)
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hpack_decoder = type { i32 }
define i32 @hpack_decode_hdr_index(%struct.hpack_decoder* %0, i32** noalias %1, i64* noalias %2, i8** noalias %3, i8** noalias %4) {
%6 = alloca i32
%7 = alloca %struct.hpack_decoder*
%8 = alloca i32**
%9 = alloca i64*
%10 = alloca i8**
%11 = alloca i8**
%12 = alloca i64
%13 = alloca i8*
%14 = alloca i8*
store %struct.hpack_decoder* %0, %struct.hpack_decoder** %7
store i32** %1, i32*** %8
store i64* %2, i64** %9
store i8** %3, i8*** %10
store i8** %4, i8*** %11
%15 = load i32**, i32*** %8
%16 = load i64*, i64** %9
%17 = call i64 @hpack_decode_int(i32 6, i32** %15, i64* %16)
store i64 %17, i64* %12
%18 = load i64, i64* %12
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %5
store i32 -1, i32* %6
br label %61
21: ; preds = %5
%22 = load i64, i64* %12
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = load %struct.hpack_decoder*, %struct.hpack_decoder** %7
%26 = load i64, i64* %12
%27 = call i8* @hpack_lookup_name(%struct.hpack_decoder* %25, i64 %26)
store i8* %27, i8** %13
br label %32
28: ; preds = %21
%29 = load i32**, i32*** %8
%30 = load i64*, i64** %9
%31 = call i8* @hpack_decode_str(i32** %29, i64* %30)
store i8* %31, i8** %13
br label %32
32: ; preds = %28, %24
%33 = load i8*, i8** %13
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %36
35: ; preds = %32
store i32 -1, i32* %6
br label %61
36: ; preds = %32
%37 = load i32**, i32*** %8
%38 = load i64*, i64** %9
%39 = call i8* @hpack_decode_str(i32** %37, i64* %38)
store i8* %39, i8** %14
%40 = load i8*, i8** %14
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %45
42: ; preds = %36
%43 = load i8*, i8** %13
%44 = call i32 @free(i8* %43)
store i32 -1, i32* %6
br label %61
45: ; preds = %36
%46 = load %struct.hpack_decoder*, %struct.hpack_decoder** %7
%47 = load i8*, i8** %13
%48 = load i8*, i8** %14
%49 = call i64 @hpack_append_hdr(%struct.hpack_decoder* %46, i8* %47, i8* %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %45
%52 = load i8*, i8** %14
%53 = call i32 @free(i8* %52)
%54 = load i8*, i8** %13
%55 = call i32 @free(i8* %54)
store i32 -1, i32* %6
br label %61
56: ; preds = %45
%57 = load i8*, i8** %13
%58 = load i8**, i8*** %10
store i8* %57, i8** %58
%59 = load i8*, i8** %14
%60 = load i8**, i8*** %11
store i8* %59, i8** %60
store i32 0, i32* %6
br label %61
61: ; preds = %56, %51, %42, %35, %20
%62 = load i32, i32* %6
ret i32 %62
}

LLVM IR (-O2):
%struct.hpack_decoder = type { i32 }
define i32 @hpack_decode_hdr_index(%struct.hpack_decoder* %0, i32** noalias %1, i64* noalias %2, i8** noalias nocapture writeonly %3, i8** noalias nocapture writeonly %4) {
%6 = tail call i64 @hpack_decode_int(i32 6, i32** %1, i64* %2)
%7 = icmp slt i64 %6, 0
br i1 %7, label %29, label %8
8: ; preds = %5
%9 = icmp eq i64 %6, 0
br i1 %9, label %12, label %10
10: ; preds = %8
%11 = tail call i8* @hpack_lookup_name(%struct.hpack_decoder* %0, i64 %6)
br label %14
12: ; preds = %8
%13 = tail call i8* @hpack_decode_str(i32** %1, i64* %2)
br label %14
14: ; preds = %12, %10
%15 = phi i8* [ %11, %10 ], [ %13, %12 ]
%16 = icmp eq i8* %15, null
br i1 %16, label %29, label %17
17: ; preds = %14
%18 = tail call i8* @hpack_decode_str(i32** %1, i64* %2)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @free(i8* %15)
br label %29
22: ; preds = %17
%23 = tail call i64 @hpack_append_hdr(%struct.hpack_decoder* %0, i8* %15, i8* %18)
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = tail call i32 @free(i8* %18)
%27 = tail call i32 @free(i8* %15)
br label %29
28: ; preds = %22
store i8* %15, i8** %3
store i8* %18, i8** %4
br label %29
29: ; preds = %14, %28, %25, %20, %5
%30 = phi i32 [ -1, %5 ], [ -1, %14 ], [ -1, %20 ], [ -1, %25 ], [ 0, %28 ]
ret i32 %30
}