#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 /*<<< orphan*/ uint8_t;
typedef scalar_t__ uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *Z_NULL;
scalar_t__ crc32_z(scalar_t__, int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ errno;
scalar_t__ ferror(int /*<<< orphan*/ *);
size_t fread(int /*<<< orphan*/ *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
char *strerror(int /*<<< orphan*/);
int tbz_compute_stream_crc32(FILE *input_f, uint32_t *returned_crc,
                             char **error) {
  size_t chunk_size = 1 << 12;
  uint8_t *buffer;
  uint32_t crc;
  buffer = (uint8_t *)malloc(chunk_size * sizeof(uint8_t));
  if (buffer == NULL) {
    *error = strerror(errno);
    return 1;
  }
  crc = crc32_z(0, Z_NULL, 0);
  while (1) {
    size_t bytes_read = fread(buffer, 1, chunk_size, input_f);
    if (bytes_read > 0) {
      crc = crc32_z(crc, buffer, bytes_read);
    }
    if (bytes_read < chunk_size) {
      if (ferror(input_f)) {
        *error = strerror(errno);
        free(buffer);
        return 1;
      } else {
        break;
      }
    }
  }
  free(buffer);
  *returned_crc = crc;
  return 0;
}


x86 assembly (clang, -O0):
<tbz_compute_stream_crc32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x1000,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
shl    $0x2,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11bc <tbz_compute_stream_crc32+0x5c>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1283 <tbz_compute_stream_crc32+0x123>
mov    0x2e15(%rip),%rax
mov    (%rax),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rdi
call   1030 <crc32_z@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fread@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jbe    1210 <tbz_compute_stream_crc32+0xb0>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1030 <crc32_z@plt>
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    1263 <tbz_compute_stream_crc32+0x103>
mov    -0x10(%rbp),%rdi
call   1050 <ferror@plt>
cmp    $0x0,%rax
je     125e <tbz_compute_stream_crc32+0xfe>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1283 <tbz_compute_stream_crc32+0x123>
jmp    1268 <tbz_compute_stream_crc32+0x108>
jmp    11d6 <tbz_compute_stream_crc32+0x76>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tbz_compute_stream_crc32>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    $0x4000,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1204 <tbz_compute_stream_crc32+0xa4>
mov    %rax,%r13
mov    0x2e47(%rip),%rax
mov    (%rax),%rsi
xor    %edi,%edi
xor    %edx,%edx
call   1030 <crc32_z@plt>
mov    %rax,%rbx
mov    $0x1000,%edx
mov    %r13,%rdi
mov    $0x1,%esi
mov    %r12,%rcx
call   1060 <fread@plt>
test   %rax,%rax
je     11d7 <tbz_compute_stream_crc32+0x77>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %rax,%rdx
call   1030 <crc32_z@plt>
mov    %rax,%rbx
cmp    $0xfff,%rbp
ja     11a0 <tbz_compute_stream_crc32+0x40>
mov    %r12,%rdi
call   1050 <ferror@plt>
test   %rax,%rax
je     121c <tbz_compute_stream_crc32+0xbc>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,(%r14)
mov    %r13,%rdi
call   1040 <free@plt>
mov    $0x1,%eax
jmp    1229 <tbz_compute_stream_crc32+0xc9>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,(%r14)
mov    $0x1,%eax
jmp    1229 <tbz_compute_stream_crc32+0xc9>
mov    %r13,%rdi
call   1040 <free@plt>
mov    %rbx,(%r15)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@Z_NULL = global i32* null
define i32 @tbz_compute_stream_crc32(i32* %0, i64* %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i64*
%7 = alloca i8**
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i64
store i32* %0, i32** %5
store i64* %1, i64** %6
store i8** %2, i8*** %7
store i64 4096, i64* %8
%12 = load i64, i64* %8
%13 = mul i64 %12, 4
%14 = call i64 @malloc(i64 %13)
%15 = inttoptr i64 %14 to i32*
store i32* %15, i32** %9
%16 = load i32*, i32** %9
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %22
18: ; preds = %3
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = load i8**, i8*** %7
store i8* %20, i8** %21
store i32 1, i32* %4
br label %58
22: ; preds = %3
%23 = load i32*, i32** @Z_NULL
%24 = call i64 @crc32_z(i64 0, i32* %23, i64 0)
store i64 %24, i64* %10
br label %25
25: ; preds = %22, %52
%26 = load i32*, i32** %9
%27 = load i64, i64* %8
%28 = load i32*, i32** %5
%29 = call i64 @fread(i32* %26, i32 1, i64 %27, i32* %28)
store i64 %29, i64* %11
%30 = load i64, i64* %11
%31 = icmp ugt i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %25
%33 = load i64, i64* %10
%34 = load i32*, i32** %9
%35 = load i64, i64* %11
%36 = call i64 @crc32_z(i64 %33, i32* %34, i64 %35)
store i64 %36, i64* %10
br label %37
37: ; preds = %32, %25
%38 = load i64, i64* %11
%39 = load i64, i64* %8
%40 = icmp ult i64 %38, %39
br i1 %40, label %41, label %52
41: ; preds = %37
%42 = load i32*, i32** %5
%43 = call i64 @ferror(i32* %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %41
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = load i8**, i8*** %7
store i8* %47, i8** %48
%49 = load i32*, i32** %9
%50 = call i32 @free(i32* %49)
store i32 1, i32* %4
br label %58
51: ; preds = %41
br label %53
52: ; preds = %37
br label %25
53: ; preds = %51
%54 = load i32*, i32** %9
%55 = call i32 @free(i32* %54)
%56 = load i64, i64* %10
%57 = load i64*, i64** %6
store i64 %56, i64* %57
store i32 0, i32* %4
br label %58
58: ; preds = %53, %45, %18
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@errno = global i32 0
@Z_NULL = global i32* null
define i32 @tbz_compute_stream_crc32(i32* nocapture %0, i64* nocapture writeonly %1, i8** nocapture writeonly %2) {
%4 = tail call i64 @malloc(i64 16384)
%5 = inttoptr i64 %4 to i32*
%6 = icmp eq i64 %4, 0
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
store i8* %9, i8** %2
br label %30
10: ; preds = %3
%11 = load i32*, i32** @Z_NULL
%12 = tail call i64 @crc32_z(i64 0, i32* %11, i64 0)
br label %13
13: ; preds = %17, %10
%14 = phi i64 [ %12, %10 ], [ %18, %17 ]
%15 = tail call i64 @fread(i32* %5, i32 1, i64 4096, i32* %0)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %13
%18 = tail call i64 @crc32_z(i64 %14, i32* %5, i64 %15)
%19 = icmp ult i64 %15, 4096
br i1 %19, label %20, label %13
20: ; preds = %13, %17
%21 = phi i64 [ %18, %17 ], [ %14, %13 ]
%22 = tail call i64 @ferror(i32* %0)
%23 = icmp eq i64 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
store i8* %26, i8** %2
%27 = tail call i32 @free(i32* %5)
br label %30
28: ; preds = %20
%29 = tail call i32 @free(i32* %5)
store i64 %21, i64* %1
br label %30
30: ; preds = %24, %28, %7
%31 = phi i32 [ 1, %7 ], [ 0, %28 ], [ 1, %24 ]
ret i32 %31
}