#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__ {
  char *msg;
  int avail_out;
  scalar_t__ next_out;
};
typedef TYPE_1__ z_stream;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ Z_FINISH;
int Z_OK;
int Z_STREAM_END;
int deflate(TYPE_1__ *, int /*<<< orphan*/);
int deflateEnd(TYPE_1__ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ png_write_bytes(int /*<<< orphan*/ *, scalar_t__, int);
int /*<<< orphan*/ stderr;
int finishzlib(z_stream *zs, FILE *fi) {
  int size = 0;
  int ret;
  while (1) {
    ret = deflate(zs, Z_FINISH);
    if (ret != Z_OK && ret != Z_STREAM_END) {
      fprintf(stderr, "error in deflate(finish): %s\n",
              zs->msg ? zs->msg : "unknown");
      return 0;
    }
    if (zs->avail_out != 16384) {
      int consumed = 16384 - zs->avail_out;
      size += consumed;
      png_write_bytes(fi, zs->next_out - consumed, consumed);
      zs->next_out = zs->next_out - consumed;
      zs->avail_out = 16384;
    }
    if (ret == Z_STREAM_END) {
      break;
    }
  }
  ret = deflateEnd(zs);
  if (ret != Z_OK) {
    fprintf(stderr, "error in deflateEnd(): %s\n",
            zs->msg ? zs->msg : "unknown");
    return 0;
  }
  return size;
}


x86 assembly (clang, -O0):
<finishzlib>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
call   1030 <deflate@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2e7a(%rip),%rcx
cmp    (%rcx),%eax
je     11e9 <finishzlib+0xa9>
mov    -0x20(%rbp),%eax
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%eax
je     11e9 <finishzlib+0xa9>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     11ba <finishzlib+0x7a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    11ca <finishzlib+0x8a>
lea    0xe5d(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11ca <finishzlib+0x8a>
mov    -0x28(%rbp),%edi
mov    -0x30(%rbp),%rdx
lea    0xe28(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e8 <finishzlib+0x1a8>
mov    -0x10(%rbp),%rax
cmpl   $0x4000,0x8(%rax)
je     124f <finishzlib+0x10f>
mov    -0x10(%rbp),%rcx
mov    $0x4000,%eax
sub    0x8(%rcx),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
movslq -0x24(%rbp),%rax
sub    %rax,%rsi
mov    -0x24(%rbp),%edx
call   1060 <png_write_bytes@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
movslq -0x24(%rbp),%rax
sub    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x10(%rbp),%rax
movl   $0x4000,0x8(%rax)
mov    -0x20(%rbp),%eax
mov    0x2d87(%rip),%rcx
cmp    (%rcx),%eax
jne    1266 <finishzlib+0x126>
jmp    126b <finishzlib+0x12b>
jmp    1157 <finishzlib+0x17>
mov    -0x10(%rbp),%rdi
call   1050 <deflateEnd@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2d6f(%rip),%rcx
cmp    (%rcx),%eax
je     12e2 <finishzlib+0x1a2>
mov    0x2d40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     12b3 <finishzlib+0x173>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    12c3 <finishzlib+0x183>
lea    0xd64(%rip),%rax
mov    %rax,-0x40(%rbp)
jmp    12c3 <finishzlib+0x183>
mov    -0x34(%rbp),%edi
mov    -0x40(%rbp),%rdx
lea    0xd55(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e8 <finishzlib+0x1a8>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<finishzlib>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
mov    %rdi,%r12
movl   $0x0,0xc(%rsp)
mov    0x2e63(%rip),%r13
mov    0x2e84(%rip),%r14
mov    0x2e6d(%rip),%r15
jmp    1184 <finishzlib+0x44>
cs nopw 0x0(%rax,%rax,1)
nop
cmp    %eax,%ebp
je     1203 <finishzlib+0xc3>
mov    0x0(%r13),%esi
mov    %r12,%rdi
call   1030 <deflate@plt>
mov    %eax,%ebp
mov    (%r15),%eax
cmp    (%r14),%ebp
je     119e <finishzlib+0x5e>
cmp    %eax,%ebp
jne    11df <finishzlib+0x9f>
movslq 0x8(%r12),%rcx
cmp    $0x4000,%rcx
je     1180 <finishzlib+0x40>
mov    $0x4000,%ebx
sub    %rcx,%rbx
add    %ebx,0xc(%rsp)
mov    0x10(%r12),%rsi
sub    %rbx,%rsi
mov    0x10(%rsp),%rdi
mov    %ebx,%edx
call   1060 <png_write_bytes@plt>
sub    %rbx,0x10(%r12)
movl   $0x4000,0x8(%r12)
mov    (%r15),%eax
jmp    1180 <finishzlib+0x40>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
mov    (%r12),%rax
test   %rax,%rax
lea    0xe28(%rip),%rdx
cmovne %rax,%rdx
lea    0xdff(%rip),%rsi
jmp    1236 <finishzlib+0xf6>
mov    %r12,%rdi
call   1050 <deflateEnd@plt>
cmp    (%r14),%eax
mov    0xc(%rsp),%eax
je     123d <finishzlib+0xfd>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    (%r12),%rax
test   %rax,%rax
lea    0xdf3(%rip),%rdx
cmovne %rax,%rdx
lea    0xdf0(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.TYPE_5__ = type { i8*, i32, i64 }
@Z_FINISH = global i32 0
@Z_OK = global i32 0
@Z_STREAM_END = global i32 0
@stderr = global i32 0
@.str = [30 x i8] c"error in deflate(finish): %s\0A\00"
@.str.1 = [8 x i8] c"unknown\00"
@.str.2 = [27 x i8] c"error in deflateEnd(): %s\0A\00"
define i32 @finishzlib(%struct.TYPE_5__* %0, i32* %1) {
%3 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %0, i64 0, i32 1
%4 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %0, i64 0, i32 2
br label %5
5: ; preds = %34, %2
%6 = phi i32 [ 0, %2 ], [ %36, %34 ]
%7 = load i32, i32* @Z_FINISH
%8 = tail call i32 @deflate(%struct.TYPE_5__* %0, i32 %7)
%9 = load i32, i32* @Z_OK
%10 = icmp eq i32 %8, %9
%11 = load i32, i32* @Z_STREAM_END
%12 = icmp eq i32 %8, %11
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %21, label %14
14: ; preds = %5
%15 = load i32, i32* @stderr
%16 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %0, i64 0, i32 0
%17 = load i8*, i8** %16
%18 = icmp eq i8* %17, null
%19 = select i1 %18, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %17
%20 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %19)
br label %49
21: ; preds = %5
%22 = load i32, i32* %3
%23 = icmp eq i32 %22, 16384
br i1 %23, label %34, label %24
24: ; preds = %21
%25 = sub nsw i32 16384, %22
%26 = add nsw i32 %25, %6
%27 = load i64, i64* %4
%28 = sext i32 %25 to i64
%29 = sub nsw i64 %27, %28
%30 = tail call i32 @png_write_bytes(i32* %1, i64 %29, i32 %25)
%31 = load i64, i64* %4
%32 = sub nsw i64 %31, %28
store i64 %32, i64* %4
store i32 16384, i32* %3
%33 = load i32, i32* @Z_STREAM_END
br label %34
34: ; preds = %24, %21
%35 = phi i32 [ %33, %24 ], [ %11, %21 ]
%36 = phi i32 [ %26, %24 ], [ %6, %21 ]
%37 = icmp eq i32 %8, %35
br i1 %37, label %38, label %5
38: ; preds = %34
%39 = tail call i32 @deflateEnd(%struct.TYPE_5__* %0)
%40 = load i32, i32* @Z_OK
%41 = icmp eq i32 %39, %40
br i1 %41, label %49, label %42
42: ; preds = %38
%43 = load i32, i32* @stderr
%44 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %0, i64 0, i32 0
%45 = load i8*, i8** %44
%46 = icmp eq i8* %45, null
%47 = select i1 %46, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %45
%48 = tail call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %47)
br label %49
49: ; preds = %38, %42, %14
%50 = phi i32 [ 0, %14 ], [ 0, %42 ], [ %36, %38 ]
ret i32 %50
}