#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_7__ TYPE_1__;

/* Type definitions */
struct TYPE_7__ {
  unsigned long total_out;
  scalar_t__ zfree;
  scalar_t__ zalloc;
  void *avail_out;
  int /*<<< orphan*/ *next_out;
  void *avail_in;
  int /*<<< orphan*/ *next_in;
};
typedef TYPE_1__ z_stream;
typedef unsigned long uLong;
typedef void *uInt;
typedef int /*<<< orphan*/ stream;
typedef scalar_t__ free_func;
typedef scalar_t__ alloc_func;
typedef int /*<<< orphan*/ Bytef;

/* Variables and functions */
int Z_BUF_ERROR;
int /*<<< orphan*/ Z_DEFAULT_COMPRESSION;
int /*<<< orphan*/ Z_FINISH;
int Z_OK;
int Z_STREAM_END;
int deflate(TYPE_1__ *, int /*<<< orphan*/);
int deflateEnd(TYPE_1__ *);
int deflateInit(TYPE_1__ *, int /*<<< orphan*/);
int /*<<< orphan*/ inflateEnd(TYPE_1__ *);
int /*<<< orphan*/ memset(TYPE_1__ *, int /*<<< orphan*/, int);
int encode_zip(unsigned char *dest, unsigned long *destLen,
               const unsigned char *source, unsigned long sourceLen) {
  z_stream stream;
  int err;
  memset(&stream, 0, sizeof(stream));
  stream.next_in = (Bytef *)source;
  stream.avail_in = (uInt)sourceLen;
  if ((uLong)stream.avail_in != sourceLen)
    return Z_BUF_ERROR;
  stream.next_out = (Bytef *)dest;
  stream.avail_out = (uInt)*destLen;
  if ((uLong)stream.avail_out != *destLen)
    return Z_BUF_ERROR;
  stream.zalloc = (alloc_func)0;
  stream.zfree = (free_func)0;
  err = deflateInit(&stream, Z_DEFAULT_COMPRESSION);
  if (err != Z_OK)
    return err;
  err = deflate(&stream, Z_FINISH);
  if (err != Z_STREAM_END) {
    inflateEnd(&stream);
    return err == Z_OK ? Z_BUF_ERROR : err;
  }
  *destLen = stream.total_out;
  err = deflateEnd(&stream);
  return err;
}


x86 assembly (clang, -O0):
<encode_zip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x38,%edx
call   1040 <memset@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     11a7 <encode_zip+0x57>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    129f <encode_zip+0x14f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
je     11dc <encode_zip+0x8c>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    129f <encode_zip+0x14f>
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
lea    -0x60(%rbp),%rdi
call   1060 <deflateInit@plt>
mov    %eax,-0x64(%rbp)
mov    -0x64(%rbp),%eax
mov    0x2de5(%rip),%rcx
cmp    (%rcx),%eax
je     121e <encode_zip+0xce>
mov    -0x64(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129f <encode_zip+0x14f>
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
lea    -0x60(%rbp),%rdi
call   1030 <deflate@plt>
mov    %eax,-0x64(%rbp)
mov    -0x64(%rbp),%eax
mov    0x2da3(%rip),%rcx
cmp    (%rcx),%eax
je     1282 <encode_zip+0x132>
lea    -0x60(%rbp),%rdi
call   1050 <inflateEnd@plt>
mov    -0x64(%rbp),%eax
mov    0x2d98(%rip),%rcx
cmp    (%rcx),%eax
jne    1271 <encode_zip+0x121>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
jmp    1277 <encode_zip+0x127>
mov    -0x64(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    -0x68(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129f <encode_zip+0x14f>
mov    -0x60(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
lea    -0x60(%rbp),%rdi
call   1070 <deflateEnd@plt>
mov    %eax,-0x64(%rbp)
mov    -0x64(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_zip>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x38,%edx
call   1040 <memset@plt>
mov    %rbx,0x38(%rsp)
mov    %r15,0x30(%rsp)
mov    %rbp,0x28(%rsp)
mov    (%r14),%rax
mov    %rax,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1060 <deflateInit@plt>
mov    %eax,%ebp
mov    0x2e3b(%rip),%rbx
cmp    (%rbx),%eax
jne    1206 <encode_zip+0xb6>
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
call   1030 <deflate@plt>
mov    %eax,%ebp
mov    0x2e0b(%rip),%rax
cmp    (%rax),%ebp
jne    11ef <encode_zip+0x9f>
mov    0x8(%rsp),%rax
mov    %rax,(%r14)
lea    0x8(%rsp),%rdi
call   1070 <deflateEnd@plt>
mov    %eax,%ebp
jmp    1206 <encode_zip+0xb6>
lea    0x8(%rsp),%rdi
call   1050 <inflateEnd@plt>
cmp    (%rbx),%ebp
jne    1206 <encode_zip+0xb6>
mov    0x2dd4(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_7__ = type { i64, i64, i64, i8*, i32*, i8*, i32* }
@Z_BUF_ERROR = global i32 0
@Z_DEFAULT_COMPRESSION = global i32 0
@Z_OK = global i32 0
@Z_FINISH = global i32 0
@Z_STREAM_END = global i32 0
define i32 @encode_zip(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 %struct.TYPE_7__
%11 = alloca i32
store i8* %0, i8** %6
store i64* %1, i64** %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%12 = call i32 @memset(%struct.TYPE_7__* %10, i32 0, i32 56)
%13 = load i8*, i8** %8
%14 = bitcast i8* %13 to i32*
%15 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %10, i32 0, i32 6
store i32* %14, i32** %15
%16 = load i64, i64* %9
%17 = inttoptr i64 %16 to i8*
%18 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %10, i32 0, i32 5
store i8* %17, i8** %18
%19 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %10, i32 0, i32 5
%20 = load i8*, i8** %19
%21 = ptrtoint i8* %20 to i64
%22 = load i64, i64* %9
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %4
%25 = load i32, i32* @Z_BUF_ERROR
store i32 %25, i32* %5
br label %75
26: ; preds = %4
%27 = load i8*, i8** %6
%28 = bitcast i8* %27 to i32*
%29 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %10, i32 0, i32 4
store i32* %28, i32** %29
%30 = load i64*, i64** %7
%31 = load i64, i64* %30
%32 = inttoptr i64 %31 to i8*
%33 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %10, i32 0, i32 3
store i8* %32, i8** %33
%34 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %10, i32 0, i32 3
%35 = load i8*, i8** %34
%36 = ptrtoint i8* %35 to i64
%37 = load i64*, i64** %7
%38 = load i64, i64* %37
%39 = icmp ne i64 %36, %38
br i1 %39, label %40, label %42
40: ; preds = %26
%41 = load i32, i32* @Z_BUF_ERROR
store i32 %41, i32* %5
br label %75
42: ; preds = %26
%43 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %10, i32 0, i32 2
store i64 0, i64* %43
%44 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %10, i32 0, i32 1
store i64 0, i64* %44
%45 = load i32, i32* @Z_DEFAULT_COMPRESSION
%46 = call i32 @deflateInit(%struct.TYPE_7__* %10, i32 %45)
store i32 %46, i32* %11
%47 = load i32, i32* %11
%48 = load i32, i32* @Z_OK
%49 = icmp ne i32 %47, %48
br i1 %49, label %50, label %52
50: ; preds = %42
%51 = load i32, i32* %11
store i32 %51, i32* %5
br label %75
52: ; preds = %42
%53 = load i32, i32* @Z_FINISH
%54 = call i32 @deflate(%struct.TYPE_7__* %10, i32 %53)
store i32 %54, i32* %11
%55 = load i32, i32* %11
%56 = load i32, i32* @Z_STREAM_END
%57 = icmp ne i32 %55, %56
br i1 %57, label %58, label %69
58: ; preds = %52
%59 = call i32 @inflateEnd(%struct.TYPE_7__* %10)
%60 = load i32, i32* %11
%61 = load i32, i32* @Z_OK
%62 = icmp eq i32 %60, %61
br i1 %62, label %63, label %65
63: ; preds = %58
%64 = load i32, i32* @Z_BUF_ERROR
br label %67
65: ; preds = %58
%66 = load i32, i32* %11
br label %67
67: ; preds = %65, %63
%68 = phi i32 [ %64, %63 ], [ %66, %65 ]
store i32 %68, i32* %5
br label %75
69: ; preds = %52
%70 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %10, i32 0, i32 0
%71 = load i64, i64* %70
%72 = load i64*, i64** %7
store i64 %71, i64* %72
%73 = call i32 @deflateEnd(%struct.TYPE_7__* %10)
store i32 %73, i32* %11
%74 = load i32, i32* %11
store i32 %74, i32* %5
br label %75
75: ; preds = %69, %67, %50, %40, %24
%76 = load i32, i32* %5
ret i32 %76
}

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