#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 */
typedef struct TYPE_5__ TYPE_1__;

/* Type definitions */
struct TYPE_5__ {
  int avail_in;
  int avail_out;
  int total_out;
  int /*<<< orphan*/ *next_out;
  int /*<<< orphan*/ *next_in;
  scalar_t__ opaque;
  scalar_t__ zfree;
  scalar_t__ zalloc;
};
typedef TYPE_1__ z_stream;
typedef scalar_t__ voidpf;
typedef scalar_t__ free_func;
typedef scalar_t__ alloc_func;
typedef int /*<<< orphan*/ Bytef;

/* Variables and functions */
int /*<<< orphan*/ Z_NO_FLUSH;
int Z_OK;
int Z_STREAM_END;
int inflate(TYPE_1__ *, int /*<<< orphan*/);
int /*<<< orphan*/ inflateEnd(TYPE_1__ *);
int inflateInit(TYPE_1__ *);
int /*<<< orphan*/ log_error(char *, int);
scalar_t__ realloc(unsigned char *, int);
int sap_inflate(unsigned char *psz_src, unsigned char **_dst, int i_len) {
  int i_result, i_dstsize, n;
  unsigned char *psz_dst;
  z_stream d_stream;
  d_stream.zalloc = (alloc_func)0;
  d_stream.zfree = (free_func)0;
  d_stream.opaque = (voidpf)0;
  i_result = inflateInit(&d_stream);
  if (i_result != Z_OK) {
    log_error("inflateInit() failed (%d)", i_result);
    return -1;
  }
  d_stream.next_in = (Bytef *)psz_src;
  d_stream.avail_in = i_len;
  n = 0;
  psz_dst = NULL;
  do {
    n++;
    psz_dst = (unsigned char *)realloc(psz_dst, n * 1000);
    d_stream.next_out = (Bytef *)&psz_dst[(n - 1) * 1000];
    d_stream.avail_out = 1000;
    i_result = inflate(&d_stream, Z_NO_FLUSH);
    if ((i_result != Z_OK) && (i_result != Z_STREAM_END)) {
      log_error("Zlib decompression failed (%d)", i_result);
      return -1;
    }
  } while ((d_stream.avail_out == 0) && (d_stream.avail_in != 0) &&
           (i_result != Z_STREAM_END));
  i_dstsize = d_stream.total_out;
  inflateEnd(&d_stream);
  *_dst = (unsigned char *)realloc(psz_dst, i_dstsize);
  return i_dstsize;
}


x86 assembly (clang, -O0):
<sap_inflate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
lea    -0x68(%rbp),%rdi
call   1050 <inflateInit@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%eax
je     11b4 <sap_inflate+0x64>
mov    -0x20(%rbp),%esi
lea    0xe5d(%rip),%rdi
call   1040 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <sap_inflate+0x17e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x68(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
imul   $0x3e8,-0x28(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
imul   $0x3e8,%ecx,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x58(%rbp)
movl   $0x3e8,-0x64(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
lea    -0x68(%rbp),%rdi
call   1030 <inflate@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%eax
je     1263 <sap_inflate+0x113>
mov    -0x20(%rbp),%eax
mov    0x2da0(%rip),%rcx
cmp    (%rcx),%eax
je     1263 <sap_inflate+0x113>
mov    -0x20(%rbp),%esi
lea    0xdc8(%rip),%rdi
call   1040 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <sap_inflate+0x17e>
jmp    1268 <sap_inflate+0x118>
xor    %eax,%eax
cmpl   $0x0,-0x64(%rbp)
mov    %al,-0x69(%rbp)
jne    1298 <sap_inflate+0x148>
xor    %eax,%eax
cmpl   $0x0,-0x68(%rbp)
mov    %al,-0x69(%rbp)
je     1298 <sap_inflate+0x148>
mov    -0x20(%rbp),%eax
mov    0x2d50(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x69(%rbp)
mov    -0x69(%rbp),%al
test   $0x1,%al
jne    11d1 <sap_inflate+0x81>
mov    -0x60(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    -0x68(%rbp),%rdi
call   1060 <inflateEnd@plt>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sap_inflate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%rbx
xorps  %xmm0,%xmm0
movups %xmm0,0x30(%rsp)
movq   $0x0,0x40(%rsp)
lea    0x10(%rsp),%rdi
call   1050 <inflateInit@plt>
mov    0x2e67(%rip),%r12
cmp    (%r12),%eax
jne    123a <sap_inflate+0xea>
mov    %rbp,0x8(%rsp)
mov    %rbx,0x28(%rsp)
mov    %r15d,0x10(%rsp)
xor    %ebp,%ebp
mov    0x2e25(%rip),%r13
lea    0x10(%rsp),%r15
mov    0x2e29(%rip),%r14
xor    %ebx,%ebx
nopl   0x0(%rax)
lea    0x3e8(%rbp),%esi
mov    %rbx,%rdi
call   1070 <realloc@plt>
mov    %rax,%rbx
mov    %ebp,%eax
and    $0xfffffff8,%eax
add    %rbx,%rax
mov    %rax,0x20(%rsp)
movl   $0x3e8,0x14(%rsp)
mov    0x0(%r13),%esi
mov    %r15,%rdi
call   1030 <inflate@plt>
mov    (%r14),%ecx
cmp    (%r12),%eax
je     11ff <sap_inflate+0xaf>
cmp    %ecx,%eax
jne    1243 <sap_inflate+0xf3>
cmpl   $0x0,0x14(%rsp)
jne    1218 <sap_inflate+0xc8>
cmpl   $0x0,0x10(%rsp)
je     1218 <sap_inflate+0xc8>
add    $0x3e8,%rbp
cmp    %ecx,%eax
jne    11c0 <sap_inflate+0x70>
mov    0x18(%rsp),%ebp
lea    0x10(%rsp),%rdi
call   1060 <inflateEnd@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1070 <realloc@plt>
mov    0x8(%rsp),%rcx
mov    %rax,(%rcx)
jmp    1256 <sap_inflate+0x106>
lea    0xdbf(%rip),%rdi
jmp    124a <sap_inflate+0xfa>
lea    0xdd0(%rip),%rdi
mov    %eax,%esi
call   1040 <log_error@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_5__ = type { i32, i32, i32, i32*, i32*, i64, i64, i64 }
@Z_OK = global i32 0
@.str = [26 x i8] c"inflateInit() failed (%d)\00"
@Z_NO_FLUSH = global i32 0
@Z_STREAM_END = global i32 0
@.str.1 = [31 x i8] c"Zlib decompression failed (%d)\00"
define i32 @sap_inflate(i8* %0, i8** %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca %struct.TYPE_5__
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
%13 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %12, i32 0, i32 7
store i64 0, i64* %13
%14 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %12, i32 0, i32 6
store i64 0, i64* %14
%15 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %12, i32 0, i32 5
store i64 0, i64* %15
%16 = call i32 @inflateInit(%struct.TYPE_5__* %12)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = load i32, i32* @Z_OK
%19 = icmp ne i32 %17, %18
br i1 %19, label %20, label %23
20: ; preds = %3
%21 = load i32, i32* %8
%22 = call i32 @log_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %21)
store i32 -1, i32* %4
br label %83
23: ; preds = %3
%24 = load i8*, i8** %5
%25 = bitcast i8* %24 to i32*
%26 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %12, i32 0, i32 4
store i32* %25, i32** %26
%27 = load i32, i32* %7
%28 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %12, i32 0, i32 0
store i32 %27, i32* %28
store i32 0, i32* %10
store i8* null, i8** %11
br label %29
29: ; preds = %71, %23
%30 = load i32, i32* %10
%31 = add nsw i32 %30, 1
store i32 %31, i32* %10
%32 = load i8*, i8** %11
%33 = load i32, i32* %10
%34 = mul nsw i32 %33, 1000
%35 = call i64 @realloc(i8* %32, i32 %34)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %11
%37 = load i8*, i8** %11
%38 = load i32, i32* %10
%39 = sub nsw i32 %38, 1
%40 = mul nsw i32 %39, 1000
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %37, i64 %41
%43 = bitcast i8* %42 to i32*
%44 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %12, i32 0, i32 3
store i32* %43, i32** %44
%45 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %12, i32 0, i32 1
store i32 1000, i32* %45
%46 = load i32, i32* @Z_NO_FLUSH
%47 = call i32 @inflate(%struct.TYPE_5__* %12, i32 %46)
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = load i32, i32* @Z_OK
%50 = icmp ne i32 %48, %49
br i1 %50, label %51, label %58
51: ; preds = %29
%52 = load i32, i32* %8
%53 = load i32, i32* @Z_STREAM_END
%54 = icmp ne i32 %52, %53
br i1 %54, label %55, label %58
55: ; preds = %51
%56 = load i32, i32* %8
%57 = call i32 @log_error(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %56)
store i32 -1, i32* %4
br label %83
58: ; preds = %51, %29
br label %59
59: ; preds = %58
%60 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %12, i32 0, i32 1
%61 = load i32, i32* %60
%62 = icmp eq i32 %61, 0
br i1 %62, label %63, label %71
63: ; preds = %59
%64 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %12, i32 0, i32 0
%65 = load i32, i32* %64
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %71
67: ; preds = %63
%68 = load i32, i32* %8
%69 = load i32, i32* @Z_STREAM_END
%70 = icmp ne i32 %68, %69
br label %71
71: ; preds = %67, %63, %59
%72 = phi i1 [ false, %63 ], [ false, %59 ], [ %70, %67 ]
br i1 %72, label %29, label %73
73: ; preds = %71
%74 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %12, i32 0, i32 2
%75 = load i32, i32* %74
store i32 %75, i32* %9
%76 = call i32 @inflateEnd(%struct.TYPE_5__* %12)
%77 = load i8*, i8** %11
%78 = load i32, i32* %9
%79 = call i64 @realloc(i8* %77, i32 %78)
%80 = inttoptr i64 %79 to i8*
%81 = load i8**, i8*** %6
store i8* %80, i8** %81
%82 = load i32, i32* %9
store i32 %82, i32* %4
br label %83
83: ; preds = %73, %55, %20
%84 = load i32, i32* %4
ret i32 %84
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i32, i32, i32, i32*, i32*, i64, i64, i64 }
@Z_OK = global i32 0
@.str = [26 x i8] c"inflateInit() failed (%d)\00"
@Z_NO_FLUSH = global i32 0
@Z_STREAM_END = global i32 0
@.str.1 = [31 x i8] c"Zlib decompression failed (%d)\00"
define i32 @sap_inflate(i8* %0, i8** nocapture writeonly %1, i32 %2) {
%4 = alloca %struct.TYPE_5__
%5 = bitcast %struct.TYPE_5__* %4 to i8*
%6 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %4, i64 0, i32 5
%7 = bitcast i64* %6 to i8*
%8 = call i32 @inflateInit(%struct.TYPE_5__* %4)
%9 = load i32, i32* @Z_OK
%10 = icmp eq i32 %8, %9
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = call i32 @log_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %54
13: ; preds = %3
%14 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %4, i64 0, i32 4
%15 = bitcast i32** %14 to i8**
store i8* %0, i8** %15
%16 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %4, i64 0, i32 0
store i32 %2, i32* %16
%17 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %4, i64 0, i32 3
%18 = bitcast i32** %17 to i8**
%19 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %4, i64 0, i32 1
br label %20
20: ; preds = %40, %13
%21 = phi i64 [ %23, %40 ], [ 0, %13 ]
%22 = phi i8* [ %27, %40 ], [ null, %13 ]
%23 = add nuw nsw i64 %21, 1
%24 = trunc i64 %23 to i32
%25 = mul i32 %24, 1000
%26 = call i64 @realloc(i8* %22, i32 %25)
%27 = inttoptr i64 %26 to i8*
%28 = mul i64 %21, 1000
%29 = and i64 %28, 4294967288
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8* %30, i8** %18
store i32 1000, i32* %19
%31 = load i32, i32* @Z_NO_FLUSH
%32 = call i32 @inflate(%struct.TYPE_5__* %4, i32 %31)
%33 = load i32, i32* @Z_OK
%34 = icmp eq i32 %32, %33
%35 = load i32, i32* @Z_STREAM_END
%36 = icmp eq i32 %32, %35
%37 = select i1 %34, i1 true, i1 %36
br i1 %37, label %40, label %38
38: ; preds = %20
%39 = call i32 @log_error(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %32)
br label %54
40: ; preds = %20
%41 = load i32, i32* %19
%42 = icmp ne i32 %41, 0
%43 = load i32, i32* %16
%44 = icmp eq i32 %43, 0
%45 = select i1 %42, i1 true, i1 %44
%46 = select i1 %45, i1 true, i1 %36
br i1 %46, label %47, label %20
47: ; preds = %40
%48 = inttoptr i64 %26 to i8*
%49 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %4, i64 0, i32 2
%50 = load i32, i32* %49
%51 = call i32 @inflateEnd(%struct.TYPE_5__* %4)
%52 = call i64 @realloc(i8* %48, i32 %50)
%53 = inttoptr i64 %52 to i8*
store i8* %53, i8** %1
br label %54
54: ; preds = %47, %38, %11
%55 = phi i32 [ -1, %11 ], [ -1, %38 ], [ %50, %47 ]
ret i32 %55
}