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

/* Type definitions */
struct TYPE_4__ {
  int avail_out;
  int avail_in;
  int /*<<< orphan*/ *next_in;
  int /*<<< orphan*/ *next_out;
};
typedef TYPE_1__ z_stream;
typedef int /*<<< orphan*/ uint8_t;
typedef int uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int Z_OK;
int /*<<< orphan*/ Z_SYNC_FLUSH;
int deflate(TYPE_1__ *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int fwrite(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int gtg_compress_m2f(z_stream *z, void *in_buf, uint32_t len, FILE *file_out) {
  int status;
  int offset_in = 0;
  int offset_out = 0;
  int total_written = 0;
  int cur_input_size = 0;
  uint8_t out_buf[(128 * 1024)];
  int written = 0;
  while (offset_in < len) {
    z->next_out = out_buf;
    z->avail_out = (128 * 1024);
    if (offset_in + (128 * 1024) > len)
      z->avail_in = len - offset_in;
    else
      z->avail_in = (128 * 1024);
    cur_input_size = z->avail_in;
    z->next_in = &((uint8_t *)in_buf)[offset_in];
    status = deflate(z, Z_SYNC_FLUSH);
    if (Z_OK != status) {
      fprintf(stderr, "error %d in compressing\n", status);
      return -1;
    }
    offset_out = (128 * 1024) - z->avail_out;
    offset_in += cur_input_size;
    written = fwrite(out_buf, sizeof(uint8_t), offset_out, file_out);
    if (written < offset_out) {
      fprintf(stderr, "warning: %d bytes out of %d were written to disk\n",
              written, offset_out);
    }
    total_written += written;
  }
  return total_written;
}


x86 assembly (clang, -O0):
<gtg_compress_m2f>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80050,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x80044(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12a6 <gtg_compress_m2f+0x176>
lea    -0x80040(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x10(%rbp),%rax
movl   $0x20000,(%rax)
mov    -0x30(%rbp),%eax
add    $0x20000,%eax
cmp    -0x1c(%rbp),%eax
jle    11b8 <gtg_compress_m2f+0x88>
mov    -0x1c(%rbp),%ecx
sub    -0x30(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
jmp    11c3 <gtg_compress_m2f+0x93>
mov    -0x10(%rbp),%rax
movl   $0x20000,0x4(%rax)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rcx
movslq -0x30(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rdi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
call   1030 <deflate@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
cmp    -0x2c(%rbp),%eax
je     1231 <gtg_compress_m2f+0x101>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
lea    0xde2(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <gtg_compress_m2f+0x17c>
mov    -0x10(%rbp),%rcx
mov    $0x20000,%eax
sub    (%rcx),%eax
mov    %eax,-0x34(%rbp)
mov    -0x3c(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
lea    -0x80040(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x4,%esi
call   1050 <fwrite@plt>
mov    %eax,-0x80044(%rbp)
mov    -0x80044(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    1295 <gtg_compress_m2f+0x165>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
mov    -0x80044(%rbp),%edx
mov    -0x34(%rbp),%ecx
lea    0xd8b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x80044(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    1170 <gtg_compress_m2f+0x40>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gtg_compress_m2f>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x80028,%rsp
mov    %rcx,0x10(%rsp)
mov    %rsi,0x18(%rsp)
test   %edx,%edx
jle    122d <gtg_compress_m2f+0xfd>
mov    %edx,%ebp
mov    %rdi,%r13
lea    0x20(%rsp),%r12
xor    %r14d,%r14d
xor    %r15d,%r15d
mov    %rdi,0x8(%rsp)
jmp    1186 <gtg_compress_m2f+0x56>
nopw   0x0(%rax,%rax,1)
mov    0x4(%rsp),%r14d
add    %r13d,%r14d
cmp    %ebp,%r15d
mov    0x8(%rsp),%r13
jge    1251 <gtg_compress_m2f+0x121>
mov    %r12,0x10(%r13)
movl   $0x20000,0x0(%r13)
lea    0x20000(%r15),%eax
mov    %ebp,%ebx
sub    %r15d,%ebx
cmp    %ebp,%eax
mov    $0x20000,%eax
cmovle %eax,%ebx
mov    %ebx,0x4(%r13)
movslq %r15d,%rax
mov    0x18(%rsp),%rcx
lea    (%rcx,%rax,4),%rax
mov    %rax,0x8(%r13)
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   1030 <deflate@plt>
mov    0x2e1c(%rip),%rcx
cmp    %eax,(%rcx)
jne    1232 <gtg_compress_m2f+0x102>
mov    %r14d,0x4(%rsp)
mov    $0x20000,%r14d
sub    0x0(%r13),%r14d
add    %ebx,%r15d
mov    %r12,%rdi
mov    $0x4,%esi
mov    %r14d,%edx
mov    0x10(%rsp),%rcx
call   1050 <fwrite@plt>
mov    %eax,%r13d
cmp    %r14d,%eax
jge    1170 <gtg_compress_m2f+0x40>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
mov    %r13d,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1170 <gtg_compress_m2f+0x40>
xor    %r14d,%r14d
jmp    1251 <gtg_compress_m2f+0x121>
mov    0x2d97(%rip),%rcx
mov    (%rcx),%edi
lea    0xdbe(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x80028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32, i32, i32*, i32* }
@Z_SYNC_FLUSH = global i32 0
@Z_OK = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"error %d in compressing\0A\00"
@.str.1 = [50 x i8] c"warning: %d bytes out of %d were written to disk\0A\00"
define i32 @gtg_compress_m2f(%struct.TYPE_4__* %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca %struct.TYPE_4__*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca [131072 x i32]
%16 = alloca i32
store %struct.TYPE_4__* %0, %struct.TYPE_4__** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i32 0, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
store i32 0, i32* %16
br label %17
17: ; preds = %81, %4
%18 = load i32, i32* %11
%19 = load i32, i32* %8
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %85
21: ; preds = %17
%22 = getelementptr inbounds [131072 x i32], [131072 x i32]* %15, i64 0, i64 0
%23 = load %struct.TYPE_4__*, %struct.TYPE_4__** %6
%24 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %23, i32 0, i32 3
store i32* %22, i32** %24
%25 = load %struct.TYPE_4__*, %struct.TYPE_4__** %6
%26 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %25, i32 0, i32 0
store i32 131072, i32* %26
%27 = load i32, i32* %11
%28 = add nsw i32 %27, 131072
%29 = load i32, i32* %8
%30 = icmp sgt i32 %28, %29
br i1 %30, label %31, label %37
31: ; preds = %21
%32 = load i32, i32* %8
%33 = load i32, i32* %11
%34 = sub nsw i32 %32, %33
%35 = load %struct.TYPE_4__*, %struct.TYPE_4__** %6
%36 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %35, i32 0, i32 1
store i32 %34, i32* %36
br label %40
37: ; preds = %21
%38 = load %struct.TYPE_4__*, %struct.TYPE_4__** %6
%39 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %38, i32 0, i32 1
store i32 131072, i32* %39
br label %40
40: ; preds = %37, %31
%41 = load %struct.TYPE_4__*, %struct.TYPE_4__** %6
%42 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %41, i32 0, i32 1
%43 = load i32, i32* %42
store i32 %43, i32* %14
%44 = load i8*, i8** %7
%45 = bitcast i8* %44 to i32*
%46 = load i32, i32* %11
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
%49 = load %struct.TYPE_4__*, %struct.TYPE_4__** %6
%50 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %49, i32 0, i32 2
store i32* %48, i32** %50
%51 = load %struct.TYPE_4__*, %struct.TYPE_4__** %6
%52 = load i32, i32* @Z_SYNC_FLUSH
%53 = call i32 @deflate(%struct.TYPE_4__* %51, i32 %52)
store i32 %53, i32* %10
%54 = load i32, i32* @Z_OK
%55 = load i32, i32* %10
%56 = icmp ne i32 %54, %55
br i1 %56, label %57, label %61
57: ; preds = %40
%58 = load i32, i32* @stderr
%59 = load i32, i32* %10
%60 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %59)
store i32 -1, i32* %5
br label %87
61: ; preds = %40
%62 = load %struct.TYPE_4__*, %struct.TYPE_4__** %6
%63 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %62, i32 0, i32 0
%64 = load i32, i32* %63
%65 = sub nsw i32 131072, %64
store i32 %65, i32* %12
%66 = load i32, i32* %14
%67 = load i32, i32* %11
%68 = add nsw i32 %67, %66
store i32 %68, i32* %11
%69 = getelementptr inbounds [131072 x i32], [131072 x i32]* %15, i64 0, i64 0
%70 = load i32, i32* %12
%71 = load i32*, i32** %9
%72 = call i32 @fwrite(i32* %69, i32 4, i32 %70, i32* %71)
store i32 %72, i32* %16
%73 = load i32, i32* %16
%74 = load i32, i32* %12
%75 = icmp slt i32 %73, %74
br i1 %75, label %76, label %81
76: ; preds = %61
%77 = load i32, i32* @stderr
%78 = load i32, i32* %16
%79 = load i32, i32* %12
%80 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i32 %78, i32 %79)
br label %81
81: ; preds = %76, %61
%82 = load i32, i32* %16
%83 = load i32, i32* %13
%84 = add nsw i32 %83, %82
store i32 %84, i32* %13
br label %17
85: ; preds = %17
%86 = load i32, i32* %13
store i32 %86, i32* %5
br label %87
87: ; preds = %85, %57
%88 = load i32, i32* %5
ret i32 %88
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32, i32, i32*, i32* }
@Z_SYNC_FLUSH = global i32 0
@Z_OK = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"error %d in compressing\0A\00"
@.str.1 = [50 x i8] c"warning: %d bytes out of %d were written to disk\0A\00"
define i32 @gtg_compress_m2f(%struct.TYPE_4__* %0, i8* %1, i32 %2, i32* nocapture %3) {
%5 = alloca [131072 x i32]
%6 = bitcast [131072 x i32]* %5 to i8*
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %43
8: ; preds = %4
%9 = getelementptr inbounds [131072 x i32], [131072 x i32]* %5, i64 0, i64 0
%10 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %0, i64 0, i32 3
%11 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %0, i64 0, i32 0
%12 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %0, i64 0, i32 1
%13 = bitcast i8* %1 to i32*
%14 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %0, i64 0, i32 2
br label %15
15: ; preds = %8, %40
%16 = phi i32 [ 0, %8 ], [ %41, %40 ]
%17 = phi i32 [ 0, %8 ], [ %34, %40 ]
store i32* %9, i32** %10
store i32 131072, i32* %11
%18 = add nsw i32 %17, 131072
%19 = icmp sgt i32 %18, %2
%20 = sub nsw i32 %2, %17
%21 = select i1 %19, i32 %20, i32 131072
store i32 %21, i32* %12
%22 = sext i32 %17 to i64
%23 = getelementptr inbounds i32, i32* %13, i64 %22
store i32* %23, i32** %14
%24 = load i32, i32* @Z_SYNC_FLUSH
%25 = call i32 @deflate(%struct.TYPE_4__* %0, i32 %24)
%26 = load i32, i32* @Z_OK
%27 = icmp eq i32 %26, %25
br i1 %27, label %31, label %28
28: ; preds = %15
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %25)
br label %43
31: ; preds = %15
%32 = load i32, i32* %11
%33 = sub nsw i32 131072, %32
%34 = add nsw i32 %21, %17
%35 = call i32 @fwrite(i32* %9, i32 4, i32 %33, i32* %3)
%36 = icmp slt i32 %35, %33
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i32 %35, i32 %33)
br label %40
40: ; preds = %37, %31
%41 = add nsw i32 %35, %16
%42 = icmp slt i32 %34, %2
br i1 %42, label %15, label %43
43: ; preds = %40, %4, %28
%44 = phi i32 [ -1, %28 ], [ 0, %4 ], [ %41, %40 ]
ret i32 %44
}