#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;
  int total_in;
  unsigned char *next_out;
  int total_out;
  int avail_out;
  scalar_t__ avail_in;
  void *zfree;
  void *zalloc;
};
typedef TYPE_1__ z_stream;
typedef int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ Z_FINISH;
int Z_OK;
int /*<<< orphan*/ Z_PARTIAL_FLUSH;
int Z_STREAM_END;
int deflate(TYPE_1__ *, int /*<<< orphan*/);
int /*<<< orphan*/ deflateEnd(TYPE_1__ *);
int deflateInit(TYPE_1__ *, int);
scalar_t__ min(unsigned int, int);
int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
                        uint32_t *sourcelen, uint32_t *dstlen) {
  z_stream strm;
  int ret;
  if (*dstlen <= 12)
    return -1;
  strm.zalloc = (void *)0;
  strm.zfree = (void *)0;
  if (Z_OK != deflateInit(&strm, 3)) {
    return -1;
  }
  strm.next_in = data_in;
  strm.total_in = 0;
  strm.next_out = cpage_out;
  strm.total_out = 0;
  while (strm.total_out < *dstlen - 12 && strm.total_in < *sourcelen) {
    strm.avail_out = *dstlen - (strm.total_out + 12);
    strm.avail_in = min((unsigned)(*sourcelen - strm.total_in), strm.avail_out);
    ret = deflate(&strm, Z_PARTIAL_FLUSH);
    if (ret != Z_OK) {
      deflateEnd(&strm);
      return -1;
    }
  }
  strm.avail_out += 12;
  strm.avail_in = 0;
  ret = deflate(&strm, Z_FINISH);
  if (ret != Z_STREAM_END) {
    deflateEnd(&strm);
    return -1;
  }
  deflateEnd(&strm);
  if (strm.total_out >= strm.total_in)
    return -1;
  *dstlen = strm.total_out;
  *sourcelen = strm.total_in;
  return 0;
}


x86 assembly (clang, -O0):
<jffs2_zlib_compress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmpl   $0xc,(%rax)
jg     1171 <jffs2_zlib_compress+0x31>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <jffs2_zlib_compress+0x1b8>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
lea    -0x60(%rbp),%rdi
mov    $0x3,%esi
call   1050 <deflateInit@plt>
mov    %eax,%ecx
mov    -0x68(%rbp),%eax
cmp    %ecx,%eax
je     11b4 <jffs2_zlib_compress+0x74>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <jffs2_zlib_compress+0x1b8>
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
movl   $0x0,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
sub    $0xc,%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x69(%rbp)
jge    11fa <jffs2_zlib_compress+0xba>
mov    -0x58(%rbp),%eax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%eax
setl   %al
mov    %al,-0x69(%rbp)
mov    -0x69(%rbp),%al
test   $0x1,%al
jne    120a <jffs2_zlib_compress+0xca>
jmp    1271 <jffs2_zlib_compress+0x131>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    -0x48(%rbp),%ecx
add    $0xc,%ecx
sub    %ecx,%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
sub    -0x58(%rbp),%edi
mov    -0x44(%rbp),%esi
call   1030 <min@plt>
mov    %rax,-0x40(%rbp)
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
lea    -0x60(%rbp),%rdi
call   1040 <deflate@plt>
mov    %eax,-0x64(%rbp)
mov    -0x64(%rbp),%eax
mov    0x2da1(%rip),%rcx
cmp    (%rcx),%eax
je     126c <jffs2_zlib_compress+0x12c>
lea    -0x60(%rbp),%rdi
call   1060 <deflateEnd@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <jffs2_zlib_compress+0x1b8>
jmp    11d2 <jffs2_zlib_compress+0x92>
mov    -0x44(%rbp),%eax
add    $0xc,%eax
mov    %eax,-0x44(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x2d3f(%rip),%rax
mov    (%rax),%esi
lea    -0x60(%rbp),%rdi
call   1040 <deflate@plt>
mov    %eax,-0x64(%rbp)
mov    -0x64(%rbp),%eax
mov    0x2d37(%rip),%rcx
cmp    (%rcx),%eax
je     12be <jffs2_zlib_compress+0x17e>
lea    -0x60(%rbp),%rdi
call   1060 <deflateEnd@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <jffs2_zlib_compress+0x1b8>
lea    -0x60(%rbp),%rdi
call   1060 <deflateEnd@plt>
mov    -0x48(%rbp),%eax
cmp    -0x58(%rbp),%eax
jl     12df <jffs2_zlib_compress+0x19f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <jffs2_zlib_compress+0x1b8>
mov    -0x48(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x58(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<jffs2_zlib_compress>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    $0xffffffff,%r14d
cmpl   $0xd,(%rcx)
jl     1266 <jffs2_zlib_compress+0x126>
mov    %rcx,%rbp
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rbx
xorps  %xmm0,%xmm0
movups %xmm0,0x28(%rsp)
mov    0x2e78(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%rdi
mov    $0x3,%esi
call   1050 <deflateInit@plt>
cmp    %eax,%r13d
jne    1266 <jffs2_zlib_compress+0x126>
mov    %rbx,(%rsp)
movl   $0x0,0x8(%rsp)
mov    %r15,0x10(%rsp)
movl   $0x0,0x18(%rsp)
mov    0x0(%rbp),%esi
cmp    $0xd,%esi
mov    0x2e39(%rip),%rbx
jl     120b <jffs2_zlib_compress+0xcb>
xor    %eax,%eax
mov    0x2e1e(%rip),%r13
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r12),%edi
sub    0x8(%rsp),%edi
jle    120b <jffs2_zlib_compress+0xcb>
sub    %eax,%esi
add    $0xfffffff4,%esi
mov    %esi,0x1c(%rsp)
call   1030 <min@plt>
mov    %rax,0x20(%rsp)
mov    0x0(%r13),%esi
mov    %r15,%rdi
call   1040 <deflate@plt>
cmp    (%rbx),%eax
jne    125e <jffs2_zlib_compress+0x11e>
mov    0x18(%rsp),%eax
mov    0x0(%rbp),%esi
lea    -0xc(%rsi),%ecx
cmp    %ecx,%eax
jl     11d0 <jffs2_zlib_compress+0x90>
addl   $0xc,0x1c(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%r15
mov    %r15,%rdi
call   1040 <deflate@plt>
mov    %eax,%r13d
mov    0x2da1(%rip),%rax
mov    (%rax),%ebx
mov    %r15,%rdi
call   1060 <deflateEnd@plt>
cmp    %ebx,%r13d
jne    1266 <jffs2_zlib_compress+0x126>
mov    0x8(%rsp),%eax
mov    0x18(%rsp),%ecx
cmp    %eax,%ecx
jge    1266 <jffs2_zlib_compress+0x126>
mov    %ecx,0x0(%rbp)
mov    %eax,(%r12)
xor    %r14d,%r14d
jmp    1266 <jffs2_zlib_compress+0x126>
mov    %rsp,%rdi
call   1060 <deflateEnd@plt>
mov    %r14d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_5__ = type { i8*, i32, i8*, i32, i32, i64, i8*, i8* }
@Z_OK = global i32 0
@Z_PARTIAL_FLUSH = global i32 0
@Z_FINISH = global i32 0
@Z_STREAM_END = global i32 0
define i32 @jffs2_zlib_compress(i8* %0, i8* %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca %struct.TYPE_5__
%11 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%12 = load i32*, i32** %9
%13 = load i32, i32* %12
%14 = icmp sle i32 %13, 12
br i1 %14, label %15, label %16
15: ; preds = %4
store i32 -1, i32* %5
br label %97
16: ; preds = %4
%17 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 7
store i8* null, i8** %17
%18 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 6
store i8* null, i8** %18
%19 = load i32, i32* @Z_OK
%20 = call i32 @deflateInit(%struct.TYPE_5__* %10, i32 3)
%21 = icmp ne i32 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %16
store i32 -1, i32* %5
br label %97
23: ; preds = %16
%24 = load i8*, i8** %6
%25 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 0
store i8* %24, i8** %25
%26 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 1
store i32 0, i32* %26
%27 = load i8*, i8** %7
%28 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 2
store i8* %27, i8** %28
%29 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 3
store i32 0, i32* %29
br label %30
30: ; preds = %69, %23
%31 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 3
%32 = load i32, i32* %31
%33 = load i32*, i32** %9
%34 = load i32, i32* %33
%35 = sub nsw i32 %34, 12
%36 = icmp slt i32 %32, %35
br i1 %36, label %37, label %43
37: ; preds = %30
%38 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 1
%39 = load i32, i32* %38
%40 = load i32*, i32** %8
%41 = load i32, i32* %40
%42 = icmp slt i32 %39, %41
br label %43
43: ; preds = %37, %30
%44 = phi i1 [ false, %30 ], [ %42, %37 ]
br i1 %44, label %45, label %70
45: ; preds = %43
%46 = load i32*, i32** %9
%47 = load i32, i32* %46
%48 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 3
%49 = load i32, i32* %48
%50 = add nsw i32 %49, 12
%51 = sub nsw i32 %47, %50
%52 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 4
store i32 %51, i32* %52
%53 = load i32*, i32** %8
%54 = load i32, i32* %53
%55 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 1
%56 = load i32, i32* %55
%57 = sub nsw i32 %54, %56
%58 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 4
%59 = load i32, i32* %58
%60 = call i64 @min(i32 %57, i32 %59)
%61 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 5
store i64 %60, i64* %61
%62 = load i32, i32* @Z_PARTIAL_FLUSH
%63 = call i32 @deflate(%struct.TYPE_5__* %10, i32 %62)
store i32 %63, i32* %11
%64 = load i32, i32* %11
%65 = load i32, i32* @Z_OK
%66 = icmp ne i32 %64, %65
br i1 %66, label %67, label %69
67: ; preds = %45
%68 = call i32 @deflateEnd(%struct.TYPE_5__* %10)
store i32 -1, i32* %5
br label %97
69: ; preds = %45
br label %30
70: ; preds = %43
%71 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 4
%72 = load i32, i32* %71
%73 = add nsw i32 %72, 12
store i32 %73, i32* %71
%74 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 5
store i64 0, i64* %74
%75 = load i32, i32* @Z_FINISH
%76 = call i32 @deflate(%struct.TYPE_5__* %10, i32 %75)
store i32 %76, i32* %11
%77 = load i32, i32* %11
%78 = load i32, i32* @Z_STREAM_END
%79 = icmp ne i32 %77, %78
br i1 %79, label %80, label %82
80: ; preds = %70
%81 = call i32 @deflateEnd(%struct.TYPE_5__* %10)
store i32 -1, i32* %5
br label %97
82: ; preds = %70
%83 = call i32 @deflateEnd(%struct.TYPE_5__* %10)
%84 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 3
%85 = load i32, i32* %84
%86 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 1
%87 = load i32, i32* %86
%88 = icmp sge i32 %85, %87
br i1 %88, label %89, label %90
89: ; preds = %82
store i32 -1, i32* %5
br label %97
90: ; preds = %82
%91 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 3
%92 = load i32, i32* %91
%93 = load i32*, i32** %9
store i32 %92, i32* %93
%94 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %10, i32 0, i32 1
%95 = load i32, i32* %94
%96 = load i32*, i32** %8
store i32 %95, i32* %96
store i32 0, i32* %5
br label %97
97: ; preds = %90, %89, %80, %67, %22, %15
%98 = load i32, i32* %5
ret i32 %98
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i8*, i32, i8*, i32, i32, i64, i8*, i8* }
@Z_OK = global i32 0
@Z_PARTIAL_FLUSH = global i32 0
@Z_FINISH = global i32 0
@Z_STREAM_END = global i32 0
define i32 @jffs2_zlib_compress(i8* %0, i8* %1, i32* nocapture %2, i32* nocapture %3) {
%5 = alloca %struct.TYPE_5__
%6 = bitcast %struct.TYPE_5__* %5 to i8*
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 13
br i1 %8, label %59, label %9
9: ; preds = %4
%10 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %5, i64 0, i32 6
%11 = bitcast i8** %10 to i8*
%12 = load i32, i32* @Z_OK
%13 = call i32 @deflateInit(%struct.TYPE_5__* %5, i32 3)
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %59
15: ; preds = %9
%16 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %5, i64 0, i32 0
store i8* %0, i8** %16
%17 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %5, i64 0, i32 1
store i32 0, i32* %17
%18 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %5, i64 0, i32 2
store i8* %1, i8** %18
%19 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %5, i64 0, i32 3
store i32 0, i32* %19
%20 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %5, i64 0, i32 4
%21 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %5, i64 0, i32 5
%22 = load i32, i32* %3
%23 = icmp sgt i32 %22, 12
br i1 %23, label %24, label %46
24: ; preds = %15, %39
%25 = phi i32 [ %41, %39 ], [ %22, %15 ]
%26 = phi i32 [ %40, %39 ], [ 0, %15 ]
%27 = load i32, i32* %17
%28 = load i32, i32* %2
%29 = icmp sgt i32 %28, %27
br i1 %29, label %30, label %46
30: ; preds = %24
%31 = sub i32 -12, %26
%32 = add i32 %31, %25
store i32 %32, i32* %20
%33 = sub nsw i32 %28, %27
%34 = call i64 @min(i32 %33, i32 %32)
store i64 %34, i64* %21
%35 = load i32, i32* @Z_PARTIAL_FLUSH
%36 = call i32 @deflate(%struct.TYPE_5__* %5, i32 %35)
%37 = load i32, i32* @Z_OK
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %44
39: ; preds = %30
%40 = load i32, i32* %19
%41 = load i32, i32* %3
%42 = add nsw i32 %41, -12
%43 = icmp slt i32 %40, %42
br i1 %43, label %24, label %46
44: ; preds = %30
%45 = call i32 @deflateEnd(%struct.TYPE_5__* %5)
br label %59
46: ; preds = %24, %39, %15
%47 = load i32, i32* %20
%48 = add nsw i32 %47, 12
store i32 %48, i32* %20
store i64 0, i64* %21
%49 = load i32, i32* @Z_FINISH
%50 = call i32 @deflate(%struct.TYPE_5__* %5, i32 %49)
%51 = load i32, i32* @Z_STREAM_END
%52 = icmp eq i32 %50, %51
%53 = call i32 @deflateEnd(%struct.TYPE_5__* %5)
br i1 %52, label %54, label %59
54: ; preds = %46
%55 = load i32, i32* %19
%56 = load i32, i32* %17
%57 = icmp slt i32 %55, %56
br i1 %57, label %58, label %59
58: ; preds = %54
store i32 %55, i32* %3
store i32 %56, i32* %2
br label %59
59: ; preds = %46, %54, %9, %4, %58, %44
%60 = phi i32 [ -1, %44 ], [ 0, %58 ], [ -1, %4 ], [ -1, %9 ], [ -1, %54 ], [ -1, %46 ]
ret i32 %60
}