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

/* Variables and functions */
int EINVAL;
int ENOMEM;
int /*<<< orphan*/ Z_FINISH;
void *Z_NULL;
int Z_OK;
int /*<<< orphan*/ deflate(TYPE_1__ *, int /*<<< orphan*/);
int /*<<< orphan*/ deflateEnd(TYPE_1__ *);
int deflateInit(TYPE_1__ *, int const);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(size_t const);
int compress_data(const int level, unsigned char **pdata, const size_t size,
                  size_t *newsize) {
  z_stream sout;
  Bytef *buffer;
  int rc;
  if (level == 0) {
    *newsize = size;
    return 0;
  }
  buffer = malloc(size);
  if (buffer == NULL)
    return ENOMEM;
  sout.zalloc = Z_NULL;
  sout.zfree = Z_NULL;
  sout.opaque = NULL;
  rc = deflateInit(&sout, level);
  if (rc != Z_OK) {
    free(buffer);
    return EINVAL;
  }
  sout.next_in = (Bytef *)*pdata;
  sout.avail_in = size;
  sout.next_out = buffer;
  sout.avail_out = size;
  deflate(&sout, Z_FINISH);
  deflateEnd(&sout);
  free(*pdata);
  *pdata = (unsigned char *)buffer;
  *newsize = size - sout.avail_out;
  return 0;
}


x86 assembly (clang, -O0):
<compress_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1188 <compress_data+0x38>
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    127b <compress_data+0x12b>
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
jne    11b1 <compress_data+0x61>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127b <compress_data+0x12b>
mov    0x2e18(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%esi
lea    -0x58(%rbp),%rdi
call   1050 <deflateInit@plt>
mov    %eax,-0x64(%rbp)
mov    -0x64(%rbp),%eax
mov    0x2e02(%rip),%rcx
cmp    (%rcx),%eax
je     1210 <compress_data+0xc0>
mov    -0x60(%rbp),%rdi
call   1030 <free@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127b <compress_data+0x12b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x60(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
lea    -0x58(%rbp),%rdi
call   1040 <deflate@plt>
lea    -0x58(%rbp),%rdi
call   1070 <deflateEnd@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x60(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rcx
sub    -0x40(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compress_data>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rcx,%r14
mov    %rdx,%rbx
test   %edi,%edi
je     11fc <compress_data+0xac>
mov    %rsi,%r15
mov    %edi,%ebp
mov    %rbx,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     1203 <compress_data+0xb3>
mov    %rax,%r12
mov    0x2e46(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x38(%rsp)
mov    %rax,0x30(%rsp)
movq   $0x0,0x28(%rsp)
lea    0x8(%rsp),%rdi
mov    %ebp,%esi
call   1050 <deflateInit@plt>
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%eax
jne    120c <compress_data+0xbc>
mov    (%r15),%rax
mov    %rax,0x8(%rsp)
mov    %rbx,0x10(%rsp)
mov    %r12,0x18(%rsp)
mov    %rbx,0x20(%rsp)
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <deflate@plt>
mov    %rbp,%rdi
call   1070 <deflateEnd@plt>
mov    (%r15),%rdi
call   1030 <free@plt>
mov    %r12,(%r15)
sub    0x20(%rsp),%rbx
mov    %rbx,(%r14)
xor    %eax,%eax
jmp    121d <compress_data+0xcd>
mov    0x2dce(%rip),%rax
jmp    121b <compress_data+0xcb>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_5__ = type { i8*, i64, i8*, i64, i32*, i8*, i8* }
@ENOMEM = global i32 0
@Z_NULL = global i8* null
@Z_OK = global i32 0
@EINVAL = global i32 0
@Z_FINISH = global i32 0
define i32 @compress_data(i32 %0, i8** %1, i64 %2, i64* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i64
%9 = alloca i64*
%10 = alloca %struct.TYPE_5__
%11 = alloca i8*
%12 = alloca i32
store i32 %0, i32* %6
store i8** %1, i8*** %7
store i64 %2, i64* %8
store i64* %3, i64** %9
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %4
%16 = load i64, i64* %8
%17 = load i64*, i64** %9
store i64 %16, i64* %17
store i32 0, i32* %5
br label %63
18: ; preds = %4
%19 = load i64, i64* %8
%20 = call i8* @malloc(i64 %19)
store i8* %20, i8** %11
%21 = load i8*, i8** %11
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = load i32, i32* @ENOMEM
store i32 %24, i32* %5
br label %63
25: ; preds = %18
%26 = load i8*, i8** @Z_NULL
%27 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 6
store i8* %26, i8** %27
%28 = load i8*, i8** @Z_NULL
%29 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 5
store i8* %28, i8** %29
%30 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 4
store i32* null, i32** %30
%31 = load i32, i32* %6
%32 = call i32 @deflateInit(%struct.TYPE_5__* %10, i32 %31)
store i32 %32, i32* %12
%33 = load i32, i32* %12
%34 = load i32, i32* @Z_OK
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %40
36: ; preds = %25
%37 = load i8*, i8** %11
%38 = call i32 @free(i8* %37)
%39 = load i32, i32* @EINVAL
store i32 %39, i32* %5
br label %63
40: ; preds = %25
%41 = load i8**, i8*** %7
%42 = load i8*, i8** %41
%43 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 0
store i8* %42, i8** %43
%44 = load i64, i64* %8
%45 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 1
store i64 %44, i64* %45
%46 = load i8*, i8** %11
%47 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 2
store i8* %46, i8** %47
%48 = load i64, i64* %8
%49 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 3
store i64 %48, i64* %49
%50 = load i32, i32* @Z_FINISH
%51 = call i32 @deflate(%struct.TYPE_5__* %10, i32 %50)
%52 = call i32 @deflateEnd(%struct.TYPE_5__* %10)
%53 = load i8**, i8*** %7
%54 = load i8*, i8** %53
%55 = call i32 @free(i8* %54)
%56 = load i8*, i8** %11
%57 = load i8**, i8*** %7
store i8* %56, i8** %57
%58 = load i64, i64* %8
%59 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 3
%60 = load i64, i64* %59
%61 = sub i64 %58, %60
%62 = load i64*, i64** %9
store i64 %61, i64* %62
store i32 0, i32* %5
br label %63
63: ; preds = %40, %36, %23, %15
%64 = load i32, i32* %5
ret i32 %64
}

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