#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;
unsigned char *realloc(unsigned char *, unsigned long);
int uncompress(unsigned char *, unsigned long *, unsigned char const *,
               unsigned long);
int zlib_uncompress(unsigned char **dst, unsigned long *dstlen,
                    const unsigned char *src, unsigned long srclen) {
  int ret;
  *dst = NULL;
  *dstlen = srclen;
  do {
    *dstlen *= 2;
    *dst = realloc(*dst, *dstlen);
    if (*dst == NULL)
      return -1;
    ret = uncompress(*dst, dstlen, src, srclen);
  } while (ret == Z_BUF_ERROR);
  return ret;
}


x86 assembly (clang, -O0):
<zlib_uncompress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
shl    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1040 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1193 <zlib_uncompress+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <zlib_uncompress+0xa6>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1030 <uncompress@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%eax
je     114e <zlib_uncompress+0x2e>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zlib_uncompress>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r12
movq   $0x0,(%rdi)
lea    (%rcx,%rcx,1),%rdi
mov    %rdi,(%rsi)
call   1040 <malloc@plt>
mov    %rax,(%r12)
mov    $0xffffffff,%r13d
test   %rax,%rax
je     11a6 <zlib_uncompress+0x76>
mov    0x2e78(%rip),%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
call   1030 <uncompress@plt>
cmp    0x0(%rbp),%eax
jne    11a3 <zlib_uncompress+0x73>
mov    (%rbx),%rsi
mov    (%r12),%rdi
add    %rsi,%rsi
mov    %rsi,(%rbx)
call   1050 <realloc@plt>
mov    %rax,(%r12)
test   %rax,%rax
jne    1170 <zlib_uncompress+0x40>
jmp    11a6 <zlib_uncompress+0x76>
mov    %eax,%r13d
mov    %r13d,%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
define i32 @zlib_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
store i8** %0, i8*** %6
store i64* %1, i64** %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%11 = load i8**, i8*** %6
store i8* null, i8** %11
%12 = load i64, i64* %9
%13 = load i64*, i64** %7
store i64 %12, i64* %13
br label %14
14: ; preds = %35, %4
%15 = load i64*, i64** %7
%16 = load i64, i64* %15
%17 = mul i64 %16, 2
store i64 %17, i64* %15
%18 = load i8**, i8*** %6
%19 = load i8*, i8** %18
%20 = load i64*, i64** %7
%21 = load i64, i64* %20
%22 = call i8* @realloc(i8* %19, i64 %21)
%23 = load i8**, i8*** %6
store i8* %22, i8** %23
%24 = load i8**, i8*** %6
%25 = load i8*, i8** %24
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %14
store i32 -1, i32* %5
br label %41
28: ; preds = %14
%29 = load i8**, i8*** %6
%30 = load i8*, i8** %29
%31 = load i64*, i64** %7
%32 = load i8*, i8** %8
%33 = load i64, i64* %9
%34 = call i32 @uncompress(i8* %30, i64* %31, i8* %32, i64 %33)
store i32 %34, i32* %10
br label %35
35: ; preds = %28
%36 = load i32, i32* %10
%37 = load i32, i32* @Z_BUF_ERROR
%38 = icmp eq i32 %36, %37
br i1 %38, label %14, label %39
39: ; preds = %35
%40 = load i32, i32* %10
store i32 %40, i32* %5
br label %41
41: ; preds = %39, %27
%42 = load i32, i32* %5
ret i32 %42
}

LLVM IR (-O2):
@Z_BUF_ERROR = global i32 0
define i32 @zlib_uncompress(i8** nocapture %0, i64* %1, i8* %2, i64 %3) {
store i8* null, i8** %0
%5 = shl i64 %3, 1
store i64 %5, i64* %1
%6 = tail call i8* @malloc(i64 %5)
store i8* %6, i8** %0
%7 = icmp eq i8* %6, null
br i1 %7, label %19, label %8
8: ; preds = %4, %13
%9 = phi i8* [ %17, %13 ], [ %6, %4 ]
%10 = tail call i32 @uncompress(i8* %9, i64* %1, i8* %2, i64 %3)
%11 = load i32, i32* @Z_BUF_ERROR
%12 = icmp eq i32 %10, %11
br i1 %12, label %13, label %19
13: ; preds = %8
%14 = load i64, i64* %1
%15 = load i8*, i8** %0
%16 = shl i64 %14, 1
store i64 %16, i64* %1
%17 = tail call i8* @realloc(i8* %15, i64 %16)
store i8* %17, i8** %0
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %8
19: ; preds = %13, %8, %4
%20 = phi i32 [ -1, %4 ], [ -1, %13 ], [ %10, %8 ]
ret i32 %20
}