#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__ {
  unsigned int avail_in;
  unsigned char *next_out;
  size_t avail_out;
  int /*<<< orphan*/ *next_in;
  int /*<<< orphan*/ *opaque;
  int /*<<< orphan*/ *zfree;
  int /*<<< orphan*/ *zalloc;
};
typedef TYPE_1__ z_stream;
typedef int /*<<< orphan*/ Bytef;

/* Variables and functions */
int /*<<< orphan*/ Z_DEFAULT_COMPRESSION;
int /*<<< orphan*/ Z_FINISH;
scalar_t__ Z_OK;
int compressBound(unsigned int const);
int /*<<< orphan*/ deflate(TYPE_1__ *, int /*<<< orphan*/);
int /*<<< orphan*/ deflateEnd(TYPE_1__ *);
scalar_t__ deflateInit(TYPE_1__ *, int /*<<< orphan*/);
unsigned int htonl(unsigned int const);
unsigned char *malloc(size_t);
int gzip_compress(const void *src, const unsigned int src_len, void **dst,
                  unsigned int *dst_len) {
  size_t dlen;
  z_stream stream;
  unsigned char *ptr;
  dlen = compressBound(src_len) + sizeof(unsigned int);
  ptr = malloc(dlen);
  if (ptr == NULL)
    return 0;
  *dst = ptr;
  *(unsigned int *)ptr = htonl(src_len);
  ptr += sizeof(unsigned int);
  stream.zalloc = NULL;
  stream.zfree = NULL;
  stream.opaque = NULL;
  if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK)
    return 0;
  stream.next_in = (Bytef *)src;
  stream.avail_in = src_len;
  stream.next_out = ptr;
  stream.avail_out = dlen - sizeof(unsigned int);
  deflate(&stream, Z_FINISH);
  deflateEnd(&stream);
  *dst_len = dlen - sizeof(unsigned int) - stream.avail_out;
  return 1;
}


x86 assembly (clang, -O0):
<gzip_compress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <compressBound@plt>
cltq
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
jne    11ad <gzip_compress+0x4d>
movl   $0x0,-0x4(%rbp)
jmp    1272 <gzip_compress+0x112>
mov    -0x70(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x14(%rbp),%edi
call   1050 <htonl@plt>
mov    %eax,%ecx
mov    -0x70(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x70(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x70(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
lea    -0x68(%rbp),%rdi
call   1060 <deflateInit@plt>
mov    0x2deb(%rip),%rcx
cmp    (%rcx),%rax
je     121a <gzip_compress+0xba>
movl   $0x0,-0x4(%rbp)
jmp    1272 <gzip_compress+0x112>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    -0x70(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x30(%rbp),%rax
sub    $0x4,%rax
mov    %rax,-0x58(%rbp)
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
lea    -0x68(%rbp),%rdi
call   1030 <deflate@plt>
lea    -0x68(%rbp),%rdi
call   1080 <deflateEnd@plt>
mov    -0x30(%rbp),%rax
sub    $0x4,%rax
sub    -0x58(%rbp),%rax
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gzip_compress>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rcx,0x8(%rsp)
mov    %rdx,%r13
mov    %esi,%ebp
mov    %rdi,%r15
mov    %esi,%edi
call   1040 <compressBound@plt>
movslq %eax,%r14
lea    0x4(%r14),%rdi
call   1070 <malloc@plt>
xor    %r12d,%r12d
test   %rax,%rax
je     1224 <gzip_compress+0xc4>
mov    %rax,%rbx
mov    %rax,0x0(%r13)
mov    %ebp,%edi
call   1050 <htonl@plt>
mov    %eax,(%rbx)
xorps  %xmm0,%xmm0
movups %xmm0,0x30(%rsp)
movq   $0x0,0x40(%rsp)
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1060 <deflateInit@plt>
mov    0x2e1b(%rip),%rcx
cmp    (%rcx),%rax
jne    1224 <gzip_compress+0xc4>
add    $0x4,%rbx
mov    %r15,0x28(%rsp)
mov    %ebp,0x10(%rsp)
mov    %rbx,0x18(%rsp)
mov    %r14,0x20(%rsp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <deflate@plt>
mov    %rbx,%rdi
call   1080 <deflateEnd@plt>
mov    0x20(%rsp),%eax
sub    %eax,%r14d
mov    0x8(%rsp),%rax
mov    %r14d,(%rax)
mov    $0x1,%r12d
mov    %r12d,%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, i8*, i64, i32*, i32*, i32*, i32* }
@Z_DEFAULT_COMPRESSION = global i32 0
@Z_OK = global i64 0
@Z_FINISH = global i32 0
define i32 @gzip_compress(i8* %0, i32 %1, i8** %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8**
%9 = alloca i32*
%10 = alloca i64
%11 = alloca %struct.TYPE_5__
%12 = alloca i8*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8** %2, i8*** %8
store i32* %3, i32** %9
%13 = load i32, i32* %7
%14 = call i32 @compressBound(i32 %13)
%15 = sext i32 %14 to i64
%16 = add i64 %15, 4
store i64 %16, i64* %10
%17 = load i64, i64* %10
%18 = call i8* @malloc(i64 %17)
store i8* %18, i8** %12
%19 = load i8*, i8** %12
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %4
store i32 0, i32* %5
br label %60
22: ; preds = %4
%23 = load i8*, i8** %12
%24 = load i8**, i8*** %8
store i8* %23, i8** %24
%25 = load i32, i32* %7
%26 = call i32 @htonl(i32 %25)
%27 = load i8*, i8** %12
%28 = bitcast i8* %27 to i32*
store i32 %26, i32* %28
%29 = load i8*, i8** %12
%30 = getelementptr inbounds i8, i8* %29, i64 4
store i8* %30, i8** %12
%31 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %11, i32 0, i32 6
store i32* null, i32** %31
%32 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %11, i32 0, i32 5
store i32* null, i32** %32
%33 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %11, i32 0, i32 4
store i32* null, i32** %33
%34 = load i32, i32* @Z_DEFAULT_COMPRESSION
%35 = call i64 @deflateInit(%struct.TYPE_5__* %11, i32 %34)
%36 = load i64, i64* @Z_OK
%37 = icmp ne i64 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %22
store i32 0, i32* %5
br label %60
39: ; preds = %22
%40 = load i8*, i8** %6
%41 = bitcast i8* %40 to i32*
%42 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %11, i32 0, i32 3
store i32* %41, i32** %42
%43 = load i32, i32* %7
%44 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %11, i32 0, i32 0
store i32 %43, i32* %44
%45 = load i8*, i8** %12
%46 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %11, i32 0, i32 1
store i8* %45, i8** %46
%47 = load i64, i64* %10
%48 = sub i64 %47, 4
%49 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %11, i32 0, i32 2
store i64 %48, i64* %49
%50 = load i32, i32* @Z_FINISH
%51 = call i32 @deflate(%struct.TYPE_5__* %11, i32 %50)
%52 = call i32 @deflateEnd(%struct.TYPE_5__* %11)
%53 = load i64, i64* %10
%54 = sub i64 %53, 4
%55 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %11, i32 0, i32 2
%56 = load i64, i64* %55
%57 = sub i64 %54, %56
%58 = trunc i64 %57 to i32
%59 = load i32*, i32** %9
store i32 %58, i32* %59
store i32 1, i32* %5
br label %60
60: ; preds = %39, %38, %21
%61 = load i32, i32* %5
ret i32 %61
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i32, i8*, i64, i32*, i32*, i32*, i32* }
@Z_DEFAULT_COMPRESSION = global i32 0
@Z_OK = global i64 0
@Z_FINISH = global i32 0
define i32 @gzip_compress(i8* %0, i32 %1, i8** nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = alloca %struct.TYPE_5__
%6 = bitcast %struct.TYPE_5__* %5 to i8*
%7 = tail call i32 @compressBound(i32 %1)
%8 = sext i32 %7 to i64
%9 = add nsw i64 %8, 4
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %34, label %12
12: ; preds = %4
store i8* %10, i8** %2
%13 = tail call i32 @htonl(i32 %1)
%14 = bitcast i8* %10 to i32*
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %5, i64 0, i32 4
%16 = bitcast i32** %15 to i8*
%17 = load i32, i32* @Z_DEFAULT_COMPRESSION
%18 = call i64 @deflateInit(%struct.TYPE_5__* %5, i32 %17)
%19 = load i64, i64* @Z_OK
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %34
21: ; preds = %12
%22 = getelementptr inbounds i8, i8* %10, i64 4
%23 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %5, i64 0, i32 3
%24 = bitcast i32** %23 to i8**
store i8* %0, i8** %24
%25 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %5, i64 0, i32 0
store i32 %1, i32* %25
%26 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %5, i64 0, i32 1
store i8* %22, i8** %26
%27 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %5, i64 0, i32 2
store i64 %8, i64* %27
%28 = load i32, i32* @Z_FINISH
%29 = call i32 @deflate(%struct.TYPE_5__* %5, i32 %28)
%30 = call i32 @deflateEnd(%struct.TYPE_5__* %5)
%31 = load i64, i64* %27
%32 = trunc i64 %31 to i32
%33 = sub i32 %7, %32
store i32 %33, i32* %3
br label %34
34: ; preds = %12, %4, %21
%35 = phi i32 [ 1, %21 ], [ 0, %4 ], [ 0, %12 ]
ret i32 %35
}