#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 uLong;
typedef int /*<<< orphan*/ uInt;
typedef int /*<<< orphan*/ Bytef;
typedef int /*<<< orphan*/ Byte;

/* Variables and functions */
int Z_BUF_ERROR;
int /*<<< orphan*/ *Z_NULL;
int Z_OK;
scalar_t__ calloc(int /*<<< orphan*/, int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
int uncompress(int /*<<< orphan*/ *, int *, int /*<<< orphan*/ const *,
               unsigned long);
char *buffer_zlib_uncompress(char *buf, unsigned long long buflen,
                             unsigned long long *uncomprlen) {
  int err;
  Byte *uncompr = NULL;
  uLong uncomprLen = 5 * (unsigned int)buflen;
  if ((uncompr = (Byte *)calloc((uInt)uncomprLen, 1)) == Z_NULL) {
    printf("out of memory\n");
    fflush(stdout);
    return NULL;
  }
  err = uncompress(uncompr, &uncomprLen, (const Bytef *)buf,
                   (unsigned long)buflen);
  if (err != Z_OK) {
    if (err == Z_BUF_ERROR) {
      free(uncompr);
      uncomprLen = 2 * uncomprLen;
      if ((uncompr = (Byte *)calloc((uInt)uncomprLen, 1)) == Z_NULL) {
        printf("out of memory\n");
        fflush(stdout);
        return NULL;
      }
      err = uncompress(uncompr, &uncomprLen, (const Bytef *)buf,
                       (unsigned long)buflen);
      if (err != Z_OK) {
        printf("Error: %d in buffer_uncompress\n", err);
        return NULL;
      }
    } else {
      printf("Error: %d in buffer_uncompress\n", err);
      return NULL;
    }
  }
  *uncomprlen = uncomprLen;
  return (char *)uncompr;
}


x86 assembly (clang, -O0):
<buffer_zlib_uncompress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
imul   $0x5,%eax,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%rax
jne    11c0 <buffer_zlib_uncompress+0x70>
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
movq   $0x0,-0x8(%rbp)
jmp    12db <buffer_zlib_uncompress+0x18b>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x34(%rbp),%rsi
call   1040 <uncompress@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%eax
je     12c8 <buffer_zlib_uncompress+0x178>
mov    -0x24(%rbp),%eax
mov    0x2dec(%rip),%rcx
cmp    (%rcx),%eax
jne    12a5 <buffer_zlib_uncompress+0x155>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    0x2daa(%rip),%rcx
cmp    (%rcx),%rax
jne    1258 <buffer_zlib_uncompress+0x108>
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
movq   $0x0,-0x8(%rbp)
jmp    12db <buffer_zlib_uncompress+0x18b>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x34(%rbp),%rsi
call   1040 <uncompress@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2d76(%rip),%rcx
cmp    (%rcx),%eax
je     12a0 <buffer_zlib_uncompress+0x150>
mov    -0x24(%rbp),%esi
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12db <buffer_zlib_uncompress+0x18b>
jmp    12c3 <buffer_zlib_uncompress+0x173>
mov    -0x24(%rbp),%esi
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12db <buffer_zlib_uncompress+0x18b>
jmp    12c8 <buffer_zlib_uncompress+0x178>
movslq -0x34(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buffer_zlib_uncompress>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    (%rsi,%rsi,4),%edi
mov    %edi,0x4(%rsp)
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    0x2e44(%rip),%r13
cmp    %rax,0x0(%r13)
je     121e <buffer_zlib_uncompress+0xbe>
mov    %rax,%rbx
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
mov    %r12,%rdx
mov    %r15,%rcx
call   1050 <uncompress@plt>
mov    0x2e3d(%rip),%rbp
cmp    0x0(%rbp),%eax
jne    11c2 <buffer_zlib_uncompress+0x62>
movslq 0x4(%rsp),%rax
mov    %rax,(%r14)
jmp    123a <buffer_zlib_uncompress+0xda>
mov    0x2e17(%rip),%rcx
cmp    (%rcx),%eax
jne    120a <buffer_zlib_uncompress+0xaa>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x4(%rsp),%edi
add    %edi,%edi
mov    %edi,0x4(%rsp)
mov    $0x1,%esi
call   1070 <calloc@plt>
cmp    %rax,0x0(%r13)
je     121e <buffer_zlib_uncompress+0xbe>
mov    %rax,%rbx
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
mov    %r12,%rdx
mov    %r15,%rcx
call   1050 <uncompress@plt>
cmp    0x0(%rbp),%eax
je     11b8 <buffer_zlib_uncompress+0x58>
lea    0xdef(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    123a <buffer_zlib_uncompress+0xda>
lea    0xdfb(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@Z_NULL = global i32* null
@.str = [15 x i8] c"out of memory\0A\00"
@stdout = global i32 0
@Z_OK = global i32 0
@Z_BUF_ERROR = global i32 0
@.str.1 = [32 x i8] c"Error: %d in buffer_uncompress\0A\00"
define i8* @buffer_zlib_uncompress(i8* %0, i64 %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64* %2, i64** %7
store i32* null, i32** %9
%11 = load i64, i64* %6
%12 = trunc i64 %11 to i32
%13 = mul i32 5, %12
store i32 %13, i32* %10
%14 = load i32, i32* %10
%15 = call i64 @calloc(i32 %14, i32 1)
%16 = inttoptr i64 %15 to i32*
store i32* %16, i32** %9
%17 = load i32*, i32** @Z_NULL
%18 = icmp eq i32* %16, %17
br i1 %18, label %19, label %23
19: ; preds = %3
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @stdout
%22 = call i32 @fflush(i32 %21)
store i8* null, i8** %4
br label %73
23: ; preds = %3
%24 = load i32*, i32** %9
%25 = load i8*, i8** %5
%26 = bitcast i8* %25 to i32*
%27 = load i64, i64* %6
%28 = call i32 @uncompress(i32* %24, i32* %10, i32* %26, i64 %27)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = load i32, i32* @Z_OK
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %67
32: ; preds = %23
%33 = load i32, i32* %8
%34 = load i32, i32* @Z_BUF_ERROR
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %63
36: ; preds = %32
%37 = load i32*, i32** %9
%38 = call i32 @free(i32* %37)
%39 = load i32, i32* %10
%40 = mul nsw i32 2, %39
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = call i64 @calloc(i32 %41, i32 1)
%43 = inttoptr i64 %42 to i32*
store i32* %43, i32** %9
%44 = load i32*, i32** @Z_NULL
%45 = icmp eq i32* %43, %44
br i1 %45, label %46, label %50
46: ; preds = %36
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%48 = load i32, i32* @stdout
%49 = call i32 @fflush(i32 %48)
store i8* null, i8** %4
br label %73
50: ; preds = %36
%51 = load i32*, i32** %9
%52 = load i8*, i8** %5
%53 = bitcast i8* %52 to i32*
%54 = load i64, i64* %6
%55 = call i32 @uncompress(i32* %51, i32* %10, i32* %53, i64 %54)
store i32 %55, i32* %8
%56 = load i32, i32* %8
%57 = load i32, i32* @Z_OK
%58 = icmp ne i32 %56, %57
br i1 %58, label %59, label %62
59: ; preds = %50
%60 = load i32, i32* %8
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %60)
store i8* null, i8** %4
br label %73
62: ; preds = %50
br label %66
63: ; preds = %32
%64 = load i32, i32* %8
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %64)
store i8* null, i8** %4
br label %73
66: ; preds = %62
br label %67
67: ; preds = %66, %23
%68 = load i32, i32* %10
%69 = sext i32 %68 to i64
%70 = load i64*, i64** %7
store i64 %69, i64* %70
%71 = load i32*, i32** %9
%72 = bitcast i32* %71 to i8*
store i8* %72, i8** %4
br label %73
73: ; preds = %67, %63, %59, %46, %19
%74 = load i8*, i8** %4
ret i8* %74
}

LLVM IR (-O2):
@Z_NULL = global i32* null
@stdout = global i32 0
@Z_OK = global i32 0
@Z_BUF_ERROR = global i32 0
@.str.1 = [32 x i8] c"Error: %d in buffer_uncompress\0A\00"
@str.2 = [14 x i8] c"out of memory\00"
define i8* @buffer_zlib_uncompress(i8* %0, i64 %1, i64* nocapture writeonly %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = trunc i64 %1 to i32
%7 = mul i32 %6, 5
store i32 %7, i32* %4
%8 = tail call i64 @calloc(i32 %7, i32 1)
%9 = inttoptr i64 %8 to i32*
%10 = load i32*, i32** @Z_NULL
%11 = icmp eq i32* %10, %9
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.2, i64 0, i64 0))
%14 = load i32, i32* @stdout
%15 = tail call i32 @fflush(i32 %14)
br label %49
16: ; preds = %3
%17 = bitcast i8* %0 to i32*
%18 = call i32 @uncompress(i32* %9, i32* %4, i32* %17, i64 %1)
%19 = load i32, i32* @Z_OK
%20 = icmp eq i32 %18, %19
br i1 %20, label %44, label %21
21: ; preds = %16
%22 = load i32, i32* @Z_BUF_ERROR
%23 = icmp eq i32 %18, %22
br i1 %23, label %24, label %42
24: ; preds = %21
%25 = call i32 @free(i32* %9)
%26 = load i32, i32* %4
%27 = shl nsw i32 %26, 1
store i32 %27, i32* %4
%28 = call i64 @calloc(i32 %27, i32 1)
%29 = inttoptr i64 %28 to i32*
%30 = load i32*, i32** @Z_NULL
%31 = icmp eq i32* %30, %29
br i1 %31, label %32, label %36
32: ; preds = %24
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.2, i64 0, i64 0))
%34 = load i32, i32* @stdout
%35 = call i32 @fflush(i32 %34)
br label %49
36: ; preds = %24
%37 = call i32 @uncompress(i32* %29, i32* %4, i32* %17, i64 %1)
%38 = load i32, i32* @Z_OK
%39 = icmp eq i32 %37, %38
br i1 %39, label %44, label %40
40: ; preds = %36
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %37)
br label %49
42: ; preds = %21
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %49
44: ; preds = %36, %16
%45 = phi i32* [ %29, %36 ], [ %9, %16 ]
%46 = load i32, i32* %4
%47 = sext i32 %46 to i64
store i64 %47, i64* %2
%48 = bitcast i32* %45 to i8*
br label %49
49: ; preds = %44, %42, %40, %32, %12
%50 = phi i8* [ null, %12 ], [ null, %32 ], [ null, %40 ], [ %48, %44 ], [ null, %42 ]
ret i8* %50
}