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

/* Variables and functions */
int /*<<< orphan*/ memcpy(void *, ...);
int /*<<< orphan*/ qlz_decompress_core(unsigned char const *, unsigned char *,
                                       size_t, size_t, unsigned char *,
                                       unsigned char const **);
size_t qlz_size_compressed(char *);
size_t qlz_size_decompressed(char *);
size_t qlz_decompress(const char *source, void *destination, char *scratch) {
  unsigned char *buffer_aligned =
      (unsigned char *)scratch + 8 - (((size_t)scratch) % 8);
  size_t *buffersize = (size_t *)buffer_aligned;
  unsigned int headerlen = 2 * ((((*source) & 2) == 2) ? 4 : 1) + 1;
  unsigned char *streambuffer = buffer_aligned + 8;
  const unsigned char **hashtable =
      (const unsigned char **)(streambuffer + ((960000 >> 3) << 3));
  size_t dsiz = qlz_size_decompressed((char *)source);
  size_t csiz = qlz_size_compressed((char *)source);
  if (*buffersize + qlz_size_decompressed((char *)source) - 1 >=
      ((960000 >> 3) << 3)) {
    if ((*source & 1) == 1)
      qlz_decompress_core((const unsigned char *)source + headerlen,
                          destination, dsiz, csiz, (unsigned char *)destination,
                          hashtable);
    else
      memcpy(destination, source + headerlen, dsiz);
    *buffersize = 0;
  } else {
    if ((*source & 1) == 1)
      qlz_decompress_core((const unsigned char *)source + headerlen,
                          streambuffer + *buffersize, dsiz, csiz, streambuffer,
                          hashtable);
    else
      memcpy(streambuffer + *buffersize, source + headerlen, dsiz);
    memcpy(destination, streambuffer + *buffersize, dsiz);
    *buffersize += dsiz;
  }
  return dsiz;
}


x86 assembly (clang, -O0):
<qlz_decompress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    -0x18(%rbp),%rdx
and    $0x7,%rdx
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%edx
and    $0x2,%edx
mov    $0x1,%eax
mov    $0x4,%ecx
cmp    $0x2,%edx
cmove  %ecx,%eax
shl    $0x1,%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    $0xea600,%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <qlz_size_decompressed@plt>
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <qlz_size_compressed@plt>
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <qlz_size_decompressed@plt>
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
add    %rcx,%rax
sub    $0x1,%rax
cmp    $0xea600,%rax
jb     1261 <qlz_decompress+0x121>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
and    $0x1,%eax
cmp    $0x1,%eax
jne    1238 <qlz_decompress+0xf8>
mov    -0x8(%rbp),%rdi
mov    -0x2c(%rbp),%eax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x50(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    -0x40(%rbp),%r9
call   1050 <qlz_decompress_core@plt>
jmp    1251 <qlz_decompress+0x111>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x2c(%rbp),%eax
add    %rax,%rsi
mov    -0x48(%rbp),%rdx
xor    %eax,%eax
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
jmp    12eb <qlz_decompress+0x1ab>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
and    $0x1,%eax
cmp    $0x1,%eax
jne    12a3 <qlz_decompress+0x163>
mov    -0x8(%rbp),%rdi
mov    -0x2c(%rbp),%eax
add    %rax,%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rax
add    (%rax),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x50(%rbp),%rcx
mov    -0x38(%rbp),%r8
mov    -0x40(%rbp),%r9
call   1050 <qlz_decompress_core@plt>
jmp    12c3 <qlz_decompress+0x183>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rax
add    (%rax),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x2c(%rbp),%eax
add    %rax,%rsi
mov    -0x48(%rbp),%rdx
xor    %eax,%eax
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rax
add    (%rax),%rsi
mov    -0x48(%rbp),%rdx
xor    %eax,%eax
call   1040 <memcpy@plt>
mov    -0x48(%rbp),%rcx
mov    -0x28(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x48(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qlz_decompress>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r12
mov    %rsi,(%rsp)
mov    %rdi,%rbx
and    $0xfffffffffffffff8,%r12
lea    0x8(%r12),%r13
testb  $0x2,(%rdi)
mov    $0x3,%eax
mov    $0x9,%r14d
cmove  %eax,%r14d
lea    0xea610(%r12),%rax
mov    %rax,0x10(%rsp)
call   1030 <qlz_size_decompressed@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1060 <qlz_size_compressed@plt>
mov    %rax,0x8(%rsp)
mov    0x8(%r12),%rbp
mov    %rbx,%rdi
call   1030 <qlz_size_decompressed@plt>
lea    (%rax,%rbp,1),%rcx
add    $0xfffffffffff159ff,%rcx
mov    (%rbx),%al
cmp    $0xfffffffffff159ff,%rcx
ja     11da <qlz_decompress+0x9a>
mov    %r14d,%ecx
add    %rcx,%rbx
test   $0x1,%al
jne    11fe <qlz_decompress+0xbe>
xor    %ebp,%ebp
mov    (%rsp),%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <memcpy@plt>
jmp    125f <qlz_decompress+0x11f>
add    $0x10,%r12
test   $0x1,%al
jne    121e <qlz_decompress+0xde>
mov    0x0(%r13),%rdi
add    %r12,%rdi
mov    %r14d,%eax
add    %rax,%rbx
mov    %rbx,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <memcpy@plt>
jmp    1243 <qlz_decompress+0x103>
mov    %rbx,%rdi
mov    (%rsp),%rsi
mov    %r15,%rdx
mov    0x8(%rsp),%rcx
mov    %rsi,%r8
mov    0x10(%rsp),%r9
call   1050 <qlz_decompress_core@plt>
xor    %ebp,%ebp
jmp    125f <qlz_decompress+0x11f>
mov    %r14d,%eax
add    %rax,%rbx
mov    0x0(%r13),%rsi
add    %r12,%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
mov    0x8(%rsp),%rcx
mov    %r12,%r8
mov    0x10(%rsp),%r9
call   1050 <qlz_decompress_core@plt>
add    0x0(%r13),%r12
mov    (%rsp),%rdi
mov    %r12,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <memcpy@plt>
mov    0x0(%r13),%rbp
add    %r15,%rbp
mov    %rbp,0x0(%r13)
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @qlz_decompress(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8**
%12 = alloca i64
%13 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%14 = load i8*, i8** %6
%15 = getelementptr inbounds i8, i8* %14, i64 8
%16 = load i8*, i8** %6
%17 = ptrtoint i8* %16 to i64
%18 = urem i64 %17, 8
%19 = sub i64 0, %18
%20 = getelementptr inbounds i8, i8* %15, i64 %19
store i8* %20, i8** %7
%21 = load i8*, i8** %7
%22 = bitcast i8* %21 to i64*
store i64* %22, i64** %8
%23 = load i8*, i8** %4
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = and i32 %25, 2
%27 = icmp eq i32 %26, 2
%28 = zext i1 %27 to i64
%29 = select i1 %27, i32 4, i32 1
%30 = mul nsw i32 2, %29
%31 = add nsw i32 %30, 1
store i32 %31, i32* %9
%32 = load i8*, i8** %7
%33 = getelementptr inbounds i8, i8* %32, i64 8
store i8* %33, i8** %10
%34 = load i8*, i8** %10
%35 = getelementptr inbounds i8, i8* %34, i64 960000
%36 = bitcast i8* %35 to i8**
store i8** %36, i8*** %11
%37 = load i8*, i8** %4
%38 = call i64 @qlz_size_decompressed(i8* %37)
store i64 %38, i64* %12
%39 = load i8*, i8** %4
%40 = call i64 @qlz_size_compressed(i8* %39)
store i64 %40, i64* %13
%41 = load i64*, i64** %8
%42 = load i64, i64* %41
%43 = load i8*, i8** %4
%44 = call i64 @qlz_size_decompressed(i8* %43)
%45 = add i64 %42, %44
%46 = sub i64 %45, 1
%47 = icmp uge i64 %46, 960000
br i1 %47, label %48, label %75
48: ; preds = %3
%49 = load i8*, i8** %4
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = and i32 %51, 1
%53 = icmp eq i32 %52, 1
br i1 %53, label %54, label %65
54: ; preds = %48
%55 = load i8*, i8** %4
%56 = load i32, i32* %9
%57 = zext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
%59 = load i8*, i8** %5
%60 = load i64, i64* %12
%61 = load i64, i64* %13
%62 = load i8*, i8** %5
%63 = load i8**, i8*** %11
%64 = call i32 @qlz_decompress_core(i8* %58, i8* %59, i64 %60, i64 %61, i8* %62, i8** %63)
br label %73
65: ; preds = %48
%66 = load i8*, i8** %5
%67 = load i8*, i8** %4
%68 = load i32, i32* %9
%69 = zext i32 %68 to i64
%70 = getelementptr inbounds i8, i8* %67, i64 %69
%71 = load i64, i64* %12
%72 = call i32 (i8*, ...) @memcpy(i8* %66, i8* %70, i64 %71)
br label %73
73: ; preds = %65, %54
%74 = load i64*, i64** %8
store i64 0, i64* %74
br label %118
75: ; preds = %3
%76 = load i8*, i8** %4
%77 = load i8, i8* %76
%78 = sext i8 %77 to i32
%79 = and i32 %78, 1
%80 = icmp eq i32 %79, 1
br i1 %80, label %81, label %95
81: ; preds = %75
%82 = load i8*, i8** %4
%83 = load i32, i32* %9
%84 = zext i32 %83 to i64
%85 = getelementptr inbounds i8, i8* %82, i64 %84
%86 = load i8*, i8** %10
%87 = load i64*, i64** %8
%88 = load i64, i64* %87
%89 = getelementptr inbounds i8, i8* %86, i64 %88
%90 = load i64, i64* %12
%91 = load i64, i64* %13
%92 = load i8*, i8** %10
%93 = load i8**, i8*** %11
%94 = call i32 @qlz_decompress_core(i8* %85, i8* %89, i64 %90, i64 %91, i8* %92, i8** %93)
br label %106
95: ; preds = %75
%96 = load i8*, i8** %10
%97 = load i64*, i64** %8
%98 = load i64, i64* %97
%99 = getelementptr inbounds i8, i8* %96, i64 %98
%100 = load i8*, i8** %4
%101 = load i32, i32* %9
%102 = zext i32 %101 to i64
%103 = getelementptr inbounds i8, i8* %100, i64 %102
%104 = load i64, i64* %12
%105 = call i32 (i8*, ...) @memcpy(i8* %99, i8* %103, i64 %104)
br label %106
106: ; preds = %95, %81
%107 = load i8*, i8** %5
%108 = load i8*, i8** %10
%109 = load i64*, i64** %8
%110 = load i64, i64* %109
%111 = getelementptr inbounds i8, i8* %108, i64 %110
%112 = load i64, i64* %12
%113 = call i32 (i8*, ...) @memcpy(i8* %107, i8* %111, i64 %112)
%114 = load i64, i64* %12
%115 = load i64*, i64** %8
%116 = load i64, i64* %115
%117 = add i64 %116, %114
store i64 %117, i64* %115
br label %118
118: ; preds = %106, %73
%119 = load i64, i64* %12
ret i64 %119
}

LLVM IR (-O2):
define i64 @qlz_decompress(i8* %0, i8* %1, i8* %2) {
%4 = getelementptr inbounds i8, i8* %2, i64 8
%5 = ptrtoint i8* %2 to i64
%6 = and i64 %5, 7
%7 = sub nsw i64 0, %6
%8 = getelementptr inbounds i8, i8* %4, i64 %7
%9 = bitcast i8* %8 to i64*
%10 = load i8, i8* %0
%11 = and i8 %10, 2
%12 = icmp eq i8 %11, 0
%13 = select i1 %12, i32 3, i32 9
%14 = getelementptr inbounds i8, i8* %8, i64 8
%15 = getelementptr inbounds i8, i8* %14, i64 960000
%16 = bitcast i8* %15 to i8**
%17 = tail call i64 @qlz_size_decompressed(i8* %0)
%18 = tail call i64 @qlz_size_compressed(i8* %0)
%19 = load i64, i64* %9
%20 = tail call i64 @qlz_size_decompressed(i8* %0)
%21 = add i64 %19, -960001
%22 = add i64 %21, %20
%23 = icmp ult i64 %22, -960000
%24 = load i8, i8* %0
%25 = and i8 %24, 1
%26 = icmp eq i8 %25, 0
br i1 %23, label %27, label %34
27: ; preds = %3
%28 = zext i32 %13 to i64
%29 = getelementptr inbounds i8, i8* %0, i64 %28
br i1 %26, label %32, label %30
30: ; preds = %27
%31 = tail call i32 @qlz_decompress_core(i8* %29, i8* %1, i64 %17, i64 %18, i8* %1, i8** %16)
br label %53
32: ; preds = %27
%33 = tail call i32 (i8*, ...) @memcpy(i8* %1, i8* %29, i64 %17)
br label %53
34: ; preds = %3
br i1 %26, label %41, label %35
35: ; preds = %34
%36 = zext i32 %13 to i64
%37 = getelementptr inbounds i8, i8* %0, i64 %36
%38 = load i64, i64* %9
%39 = getelementptr inbounds i8, i8* %14, i64 %38
%40 = tail call i32 @qlz_decompress_core(i8* %37, i8* %39, i64 %17, i64 %18, i8* %14, i8** %16)
br label %47
41: ; preds = %34
%42 = load i64, i64* %9
%43 = getelementptr inbounds i8, i8* %14, i64 %42
%44 = zext i32 %13 to i64
%45 = getelementptr inbounds i8, i8* %0, i64 %44
%46 = tail call i32 (i8*, ...) @memcpy(i8* %43, i8* %45, i64 %17)
br label %47
47: ; preds = %41, %35
%48 = load i64, i64* %9
%49 = getelementptr inbounds i8, i8* %14, i64 %48
%50 = tail call i32 (i8*, ...) @memcpy(i8* %1, i8* %49, i64 %17)
%51 = load i64, i64* %9
%52 = add i64 %51, %17
br label %53
53: ; preds = %30, %32, %47
%54 = phi i64 [ %52, %47 ], [ 0, %32 ], [ 0, %30 ]
store i64 %54, i64* %9
ret i64 %17
}