#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 Z_BUF_ERROR;
int Z_OK;
scalar_t__ calloc(int, unsigned long);
int /*<<< orphan*/ free(char *);
int uncompress(unsigned char *, unsigned long *, unsigned char const *,
               unsigned long);
int mem_uncompress(const char *src, unsigned long src_len, char **dst,
                   unsigned long *dst_len) {
  int factor = 1, status;
  do {
    *dst_len = src_len * (1 << factor++);
    *dst = (char *)calloc(sizeof(char), *dst_len + 1);
    status = uncompress((unsigned char *)*dst, dst_len,
                        (unsigned const char *)src, src_len);
  } while (status == Z_BUF_ERROR && factor < 16);
  if (status == Z_OK) {
    return 0;
  }
  free(*dst);
  return status;
}


x86 assembly (clang, -O0):
<mem_uncompress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%ecx
mov    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    $0x1,%eax
shl    %cl,%eax
mov    -0x38(%rbp),%rcx
cltq
imul   %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
add    $0x1,%rsi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1040 <uncompress@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%ecx
xor    %eax,%eax
mov    0x2e20(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x39(%rbp)
jne    11d5 <mem_uncompress+0xa5>
cmpl   $0x10,-0x2c(%rbp)
setl   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    114f <mem_uncompress+0x1f>
mov    -0x30(%rbp),%eax
mov    0x2e06(%rip),%rcx
cmp    (%rcx),%eax
jne    11fe <mem_uncompress+0xce>
movl   $0x0,-0x4(%rbp)
jmp    1210 <mem_uncompress+0xe0>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_uncompress>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%r13
mov    %rdi,%r12
mov    $0x1,%ebx
nopl   0x0(%rax)
mov    $0x1,%esi
mov    %ebx,%ecx
shl    %cl,%esi
imul   %r13,%rsi
mov    %rsi,(%r15)
add    $0x1,%rsi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,(%r14)
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1040 <uncompress@plt>
mov    %eax,%ebp
mov    0x2e55(%rip),%rax
cmp    (%rax),%ebp
jne    1199 <mem_uncompress+0x69>
lea    0x1(%rbx),%eax
cmp    $0xf,%ebx
mov    %eax,%ebx
jb     1150 <mem_uncompress+0x20>
mov    0x2e50(%rip),%rcx
xor    %eax,%eax
cmp    (%rcx),%ebp
je     11b0 <mem_uncompress+0x80>
mov    (%r14),%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@Z_BUF_ERROR = global i32 0
@Z_OK = global i32 0
define i32 @mem_uncompress(i8* %0, i64 %1, i8** %2, i64* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8**
%9 = alloca i64*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8** %2, i8*** %8
store i64* %3, i64** %9
store i32 1, i32* %10
br label %12
12: ; preds = %39, %4
%13 = load i64, i64* %7
%14 = load i32, i32* %10
%15 = add nsw i32 %14, 1
store i32 %15, i32* %10
%16 = shl i32 1, %14
%17 = sext i32 %16 to i64
%18 = mul i64 %13, %17
%19 = load i64*, i64** %9
store i64 %18, i64* %19
%20 = load i64*, i64** %9
%21 = load i64, i64* %20
%22 = add i64 %21, 1
%23 = call i64 @calloc(i32 1, i64 %22)
%24 = inttoptr i64 %23 to i8*
%25 = load i8**, i8*** %8
store i8* %24, i8** %25
%26 = load i8**, i8*** %8
%27 = load i8*, i8** %26
%28 = load i64*, i64** %9
%29 = load i8*, i8** %6
%30 = load i64, i64* %7
%31 = call i32 @uncompress(i8* %27, i64* %28, i8* %29, i64 %30)
store i32 %31, i32* %11
br label %32
32: ; preds = %12
%33 = load i32, i32* %11
%34 = load i32, i32* @Z_BUF_ERROR
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i32, i32* %10
%38 = icmp slt i32 %37, 16
br label %39
39: ; preds = %36, %32
%40 = phi i1 [ false, %32 ], [ %38, %36 ]
br i1 %40, label %12, label %41
41: ; preds = %39
%42 = load i32, i32* %11
%43 = load i32, i32* @Z_OK
%44 = icmp eq i32 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %41
store i32 0, i32* %5
br label %51
46: ; preds = %41
%47 = load i8**, i8*** %8
%48 = load i8*, i8** %47
%49 = call i32 @free(i8* %48)
%50 = load i32, i32* %11
store i32 %50, i32* %5
br label %51
51: ; preds = %46, %45
%52 = load i32, i32* %5
ret i32 %52
}

LLVM IR (-O2):
@Z_BUF_ERROR = global i32 0
@Z_OK = global i32 0
define i32 @mem_uncompress(i8* %0, i64 %1, i8** nocapture %2, i64* %3) {
br label %5
5: ; preds = %5, %4
%6 = phi i32 [ 1, %4 ], [ %7, %5 ]
%7 = add nuw nsw i32 %6, 1
%8 = shl nuw nsw i32 1, %6
%9 = zext i32 %8 to i64
%10 = mul i64 %9, %1
store i64 %10, i64* %3
%11 = add i64 %10, 1
%12 = tail call i64 @calloc(i32 1, i64 %11)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %2
%14 = tail call i32 @uncompress(i8* %13, i64* %3, i8* %0, i64 %1)
%15 = load i32, i32* @Z_BUF_ERROR
%16 = icmp eq i32 %14, %15
%17 = icmp ult i32 %6, 15
%18 = select i1 %16, i1 %17, i1 false
br i1 %18, label %5, label %19
19: ; preds = %5
%20 = load i32, i32* @Z_OK
%21 = icmp eq i32 %14, %20
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = load i8*, i8** %2
%24 = tail call i32 @free(i8* %23)
br label %25
25: ; preds = %19, %22
%26 = phi i32 [ %14, %22 ], [ 0, %19 ]
ret i32 %26
}