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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ crc32(int /*<<< orphan*/, int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ ferror(int /*<<< orphan*/ *);
size_t fread(int /*<<< orphan*/ *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/);
uint32_t compute_crc32(uint32_t crc, FILE *binfile, size_t compute_start,
                       size_t compute_len) {
  uint8_t readbuf[1024];
  size_t read;
  fseek(binfile, compute_start, SEEK_SET);
  while (binfile && !feof(binfile) && !ferror(binfile) &&
         (compute_len >= sizeof(readbuf))) {
    read = fread(readbuf, sizeof(uint8_t), sizeof(readbuf), binfile);
    crc = crc32(crc, readbuf, read);
    compute_len = compute_len - read;
  }
  if (binfile && !feof(binfile) && !ferror(binfile) && (compute_len > 0)) {
    read = fread(readbuf, sizeof(uint8_t), compute_len, binfile);
    crc = crc32(crc, readbuf, read);
  }
  return crc;
}


x86 assembly (clang, -O0):
<compute_crc32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e67(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x1029(%rbp)
je     11dc <compute_crc32+0x8c>
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1029(%rbp)
jne    11dc <compute_crc32+0x8c>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1029(%rbp)
jne    11dc <compute_crc32+0x8c>
cmpq   $0x1000,-0x20(%rbp)
setae  %al
mov    %al,-0x1029(%rbp)
mov    -0x1029(%rbp),%al
test   $0x1,%al
jne    11ef <compute_crc32+0x9f>
jmp    123d <compute_crc32+0xed>
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
mov    %rax,-0x1028(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1028(%rbp),%rdx
call   1050 <crc32@plt>
mov    %eax,-0x4(%rbp)
mov    -0x20(%rbp),%rax
sub    -0x1028(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1180 <compute_crc32+0x30>
cmpq   $0x0,-0x10(%rbp)
je     12b0 <compute_crc32+0x160>
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
jne    12b0 <compute_crc32+0x160>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%eax
jne    12b0 <compute_crc32+0x160>
cmpq   $0x0,-0x20(%rbp)
jbe    12b0 <compute_crc32+0x160>
lea    -0x1020(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x4,%esi
call   1040 <fread@plt>
mov    %rax,-0x1028(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1028(%rbp),%rdx
call   1050 <crc32@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compute_crc32>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %rcx,%rbx
mov    %rsi,%r12
mov    %edi,%r14d
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %rsi,%rdi
mov    %rdx,%rsi
mov    %eax,%edx
call   1070 <fseek@plt>
test   %r12,%r12
je     1231 <compute_crc32+0xe1>
mov    %r12,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11ed <compute_crc32+0x9d>
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1030 <ferror@plt>
test   %eax,%eax
jne    11ed <compute_crc32+0x9d>
cmp    $0x1000,%rbx
jb     11ed <compute_crc32+0x9d>
mov    $0x1000,%edx
mov    %r15,%rdi
mov    $0x4,%esi
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %rax,%rbp
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rax,%rdx
call   1050 <crc32@plt>
mov    %eax,%r14d
sub    %rbp,%rbx
mov    %r12,%rdi
call   1060 <feof@plt>
test   %eax,%eax
je     11a0 <compute_crc32+0x50>
mov    %r12,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    1231 <compute_crc32+0xe1>
mov    %r12,%rdi
call   1030 <ferror@plt>
test   %eax,%eax
jne    1231 <compute_crc32+0xe1>
test   %rbx,%rbx
je     1231 <compute_crc32+0xe1>
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x4,%esi
mov    %rbx,%rdx
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1050 <crc32@plt>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
define i32 @compute_crc32(i32 %0, i32* %1, i64 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca [1024 x i32]
%10 = alloca i64
store i32 %0, i32* %5
store i32* %1, i32** %6
store i64 %2, i64* %7
store i64 %3, i64* %8
%11 = load i32*, i32** %6
%12 = load i64, i64* %7
%13 = load i32, i32* @SEEK_SET
%14 = call i32 @fseek(i32* %11, i64 %12, i32 %13)
br label %15
15: ; preds = %31, %4
%16 = load i32*, i32** %6
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %29
18: ; preds = %15
%19 = load i32*, i32** %6
%20 = call i32 @feof(i32* %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %29, label %22
22: ; preds = %18
%23 = load i32*, i32** %6
%24 = call i32 @ferror(i32* %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = load i64, i64* %8
%28 = icmp uge i64 %27, 4096
br label %29
29: ; preds = %26, %22, %18, %15
%30 = phi i1 [ false, %22 ], [ false, %18 ], [ false, %15 ], [ %28, %26 ]
br i1 %30, label %31, label %42
31: ; preds = %29
%32 = getelementptr inbounds [1024 x i32], [1024 x i32]* %9, i64 0, i64 0
%33 = load i32*, i32** %6
%34 = call i64 @fread(i32* %32, i32 4, i64 4096, i32* %33)
store i64 %34, i64* %10
%35 = load i32, i32* %5
%36 = getelementptr inbounds [1024 x i32], [1024 x i32]* %9, i64 0, i64 0
%37 = load i64, i64* %10
%38 = call i32 @crc32(i32 %35, i32* %36, i64 %37)
store i32 %38, i32* %5
%39 = load i64, i64* %8
%40 = load i64, i64* %10
%41 = sub i64 %39, %40
store i64 %41, i64* %8
br label %15
42: ; preds = %29
%43 = load i32*, i32** %6
%44 = icmp ne i32* %43, null
br i1 %44, label %45, label %65
45: ; preds = %42
%46 = load i32*, i32** %6
%47 = call i32 @feof(i32* %46)
%48 = icmp ne i32 %47, 0
br i1 %48, label %65, label %49
49: ; preds = %45
%50 = load i32*, i32** %6
%51 = call i32 @ferror(i32* %50)
%52 = icmp ne i32 %51, 0
br i1 %52, label %65, label %53
53: ; preds = %49
%54 = load i64, i64* %8
%55 = icmp ugt i64 %54, 0
br i1 %55, label %56, label %65
56: ; preds = %53
%57 = getelementptr inbounds [1024 x i32], [1024 x i32]* %9, i64 0, i64 0
%58 = load i64, i64* %8
%59 = load i32*, i32** %6
%60 = call i64 @fread(i32* %57, i32 4, i64 %58, i32* %59)
store i64 %60, i64* %10
%61 = load i32, i32* %5
%62 = getelementptr inbounds [1024 x i32], [1024 x i32]* %9, i64 0, i64 0
%63 = load i64, i64* %10
%64 = call i32 @crc32(i32 %61, i32* %62, i64 %63)
store i32 %64, i32* %5
br label %65
65: ; preds = %56, %53, %49, %45, %42
%66 = load i32, i32* %5
ret i32 %66
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define i32 @compute_crc32(i32 %0, i32* %1, i64 %2, i64 %3) {
%5 = alloca [1024 x i32]
%6 = bitcast [1024 x i32]* %5 to i8*
%7 = load i32, i32* @SEEK_SET
%8 = tail call i32 @fseek(i32* %1, i64 %2, i32 %7)
%9 = icmp eq i32* %1, null
br i1 %9, label %40, label %10
10: ; preds = %4
%11 = getelementptr inbounds [1024 x i32], [1024 x i32]* %5, i64 0, i64 0
%12 = call i32 @feof(i32* %1)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %10, %21
%15 = phi i32 [ %23, %21 ], [ %0, %10 ]
%16 = phi i64 [ %24, %21 ], [ %3, %10 ]
%17 = call i32 @ferror(i32* %1)
%18 = icmp eq i32 %17, 0
%19 = icmp ugt i64 %16, 4095
%20 = select i1 %18, i1 %19, i1 false
br i1 %20, label %21, label %27
21: ; preds = %14
%22 = call i64 @fread(i32* %11, i32 4, i64 4096, i32* %1)
%23 = call i32 @crc32(i32 %15, i32* %11, i64 %22)
%24 = sub i64 %16, %22
%25 = call i32 @feof(i32* %1)
%26 = icmp eq i32 %25, 0
br i1 %26, label %14, label %27
27: ; preds = %21, %14, %10
%28 = phi i64 [ %3, %10 ], [ %24, %21 ], [ %16, %14 ]
%29 = phi i32 [ %0, %10 ], [ %23, %21 ], [ %15, %14 ]
%30 = call i32 @feof(i32* %1)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %40
32: ; preds = %27
%33 = call i32 @ferror(i32* %1)
%34 = icmp eq i32 %33, 0
%35 = icmp ne i64 %28, 0
%36 = select i1 %34, i1 %35, i1 false
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = call i64 @fread(i32* %11, i32 4, i64 %28, i32* %1)
%39 = call i32 @crc32(i32 %29, i32* %11, i64 %38)
br label %40
40: ; preds = %4, %37, %32, %27
%41 = phi i32 [ %29, %27 ], [ %39, %37 ], [ %29, %32 ], [ %0, %4 ]
ret i32 %41
}