#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*/ Bytef;

/* Variables and functions */
int Z_MEM_ERROR;
int Z_OK;
int compress2(int /*<<< orphan*/ *, int *, int /*<<< orphan*/ *, int, int);
int compressBound(int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int zip(char **out, int *outlen, char *in, int inlen, int level) {
  uLong bound;
  uLong u_inlen = inlen;
  char *p;
  int e;
  bound = compressBound(u_inlen);
  p = malloc(bound);
  if (p == NULL)
    return (Z_MEM_ERROR);
  e = compress2((Bytef *)p, &bound, (Bytef *)in, u_inlen, level);
  if (e != Z_OK)
    free(p);
  else {
    *out = p;
    *outlen = bound;
  }
  return (e);
}


x86 assembly (clang, -O0):
<zip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
call   1040 <compressBound@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1194 <zip+0x54>
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11e9 <zip+0xa9>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%ecx
mov    -0x28(%rbp),%r8d
lea    -0x2c(%rbp),%rsi
call   1060 <compress2@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    0x2e37(%rip),%rcx
cmp    (%rcx),%eax
je     11cf <zip+0x8f>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    11e3 <zip+0xa3>
mov    -0x38(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zip>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r12d
mov    %ecx,%ebx
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%r15
mov    %ecx,%edi
call   1040 <compressBound@plt>
mov    %eax,0x4(%rsp)
mov    %eax,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11a1 <zip+0x61>
mov    %rax,%rbp
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
mov    %r13,%rdx
mov    %ebx,%ecx
mov    %r12d,%r8d
call   1060 <compress2@plt>
mov    %eax,%ebx
mov    0x2e5f(%rip),%rax
cmp    (%rax),%ebx
jne    11ac <zip+0x6c>
mov    %rbp,(%r15)
mov    0x4(%rsp),%eax
mov    %eax,(%r14)
jmp    11b4 <zip+0x74>
mov    0x2e38(%rip),%rax
mov    (%rax),%ebx
jmp    11b4 <zip+0x74>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@Z_MEM_ERROR = global i32 0
@Z_OK = global i32 0
define i32 @zip(i8** %0, i32* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i32
store i8** %0, i8*** %7
store i32* %1, i32** %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%16 = load i32, i32* %10
store i32 %16, i32* %13
%17 = load i32, i32* %13
%18 = call i32 @compressBound(i32 %17)
store i32 %18, i32* %12
%19 = load i32, i32* %12
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** %14
%21 = load i8*, i8** %14
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %5
%24 = load i32, i32* @Z_MEM_ERROR
store i32 %24, i32* %6
br label %46
25: ; preds = %5
%26 = load i8*, i8** %14
%27 = bitcast i8* %26 to i32*
%28 = load i8*, i8** %9
%29 = bitcast i8* %28 to i32*
%30 = load i32, i32* %13
%31 = load i32, i32* %11
%32 = call i32 @compress2(i32* %27, i32* %12, i32* %29, i32 %30, i32 %31)
store i32 %32, i32* %15
%33 = load i32, i32* %15
%34 = load i32, i32* @Z_OK
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %39
36: ; preds = %25
%37 = load i8*, i8** %14
%38 = call i32 @free(i8* %37)
br label %44
39: ; preds = %25
%40 = load i8*, i8** %14
%41 = load i8**, i8*** %7
store i8* %40, i8** %41
%42 = load i32, i32* %12
%43 = load i32*, i32** %8
store i32 %42, i32* %43
br label %44
44: ; preds = %39, %36
%45 = load i32, i32* %15
store i32 %45, i32* %6
br label %46
46: ; preds = %44, %23
%47 = load i32, i32* %6
ret i32 %47
}

LLVM IR (-O2):
@Z_MEM_ERROR = global i32 0
@Z_OK = global i32 0
define i32 @zip(i8** nocapture writeonly %0, i32* nocapture writeonly %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = bitcast i32* %6 to i8*
%8 = tail call i32 @compressBound(i32 %3)
store i32 %8, i32* %6
%9 = tail call i8* @malloc(i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %5
%12 = load i32, i32* @Z_MEM_ERROR
br label %23
13: ; preds = %5
%14 = bitcast i8* %9 to i32*
%15 = bitcast i8* %2 to i32*
%16 = call i32 @compress2(i32* %14, i32* %6, i32* %15, i32 %3, i32 %4)
%17 = load i32, i32* @Z_OK
%18 = icmp eq i32 %16, %17
br i1 %18, label %21, label %19
19: ; preds = %13
%20 = call i32 @free(i8* %9)
br label %23
21: ; preds = %13
store i8* %9, i8** %0
%22 = load i32, i32* %6
store i32 %22, i32* %1
br label %23
23: ; preds = %19, %21, %11
%24 = phi i32 [ %12, %11 ], [ %16, %21 ], [ %16, %19 ]
ret i32 %24
}