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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ crc32(int /*<<< orphan*/, unsigned char *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int crc32_file(char *fpath, uint32_t *result) {
  int rc = -1;
  long left;
  uint32_t crc_val = 0;
  unsigned char buf[4096];
  FILE *fpsrc = NULL, *fpdst = NULL;
  fpsrc = fopen(fpath, "rb");
  if (!fpsrc) {
    goto cleanup;
  }
  fseek(fpsrc, 0, SEEK_END);
  left = ftell(fpsrc);
  rewind(fpsrc);
  while (left) {
    int chunk = sizeof(buf);
    if (left < sizeof(buf))
      chunk = left;
    if (fread(buf, chunk, 1, fpsrc) != 1) {
      goto cleanup;
    }
    crc_val = crc32(crc_val, buf, chunk);
    left -= chunk;
  }
  *result = crc_val;
  rc = 0;
cleanup:
  if (fpsrc) {
    fclose(fpsrc);
    fpsrc = NULL;
  }
  return rc;
}


x86 assembly (clang, -O0):
<crc32_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x1038(%rbp)
movq   $0x0,-0x1040(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x1038(%rbp)
cmpq   $0x0,-0x1038(%rbp)
jne    11d1 <crc32_file+0x61>
jmp    129c <crc32_file+0x12c>
mov    -0x1038(%rbp),%rdi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x1038(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x1038(%rbp),%rdi
call   1060 <rewind@plt>
cmpq   $0x0,-0x20(%rbp)
je     128c <crc32_file+0x11c>
movl   $0x1000,-0x1044(%rbp)
cmpq   $0x1000,-0x20(%rbp)
jae    1231 <crc32_file+0xc1>
mov    -0x20(%rbp),%rax
mov    %eax,-0x1044(%rbp)
lea    -0x1030(%rbp),%rdi
mov    -0x1044(%rbp),%esi
mov    -0x1038(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     125d <crc32_file+0xed>
jmp    129c <crc32_file+0x12c>
mov    -0x24(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    -0x1044(%rbp),%edx
call   1040 <crc32@plt>
mov    %eax,-0x24(%rbp)
movslq -0x1044(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1204 <crc32_file+0x94>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x14(%rbp)
cmpq   $0x0,-0x1038(%rbp)
je     12c1 <crc32_file+0x151>
mov    -0x1038(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x1038(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crc32_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%rbx
lea    0xe75(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1226 <crc32_file+0xb6>
mov    %rax,%r12
mov    %rbx,0x8(%rsp)
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
xor    %r15d,%r15d
mov    %r12,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r12,%rdi
call   1070 <ftell@plt>
mov    %rax,%rbp
mov    %r12,%rdi
call   1060 <rewind@plt>
mov    $0x0,%ebx
test   %rbp,%rbp
je     121d <crc32_file+0xad>
xor    %ebx,%ebx
lea    0x10(%rsp),%r13
nopl   0x0(%rax,%rax,1)
cmp    $0x1000,%rbp
mov    $0x1000,%r14d
cmovb  %rbp,%r14
mov    %r13,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    122e <crc32_file+0xbe>
mov    %ebx,%edi
mov    %r13,%rsi
mov    %r14d,%edx
call   1040 <crc32@plt>
mov    %eax,%ebx
sub    %r14,%rbp
jne    11e0 <crc32_file+0x70>
mov    0x8(%rsp),%rax
mov    %ebx,(%rax)
jmp    1234 <crc32_file+0xc4>
mov    $0xffffffff,%r15d
jmp    123c <crc32_file+0xcc>
mov    $0xffffffff,%r15d
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r15d,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
define i32 @crc32_file(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca [4096 x i8]
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
store i32 -1, i32* %5
store i32 0, i32* %7
store i32* null, i32** %9
store i32* null, i32** %10
%12 = load i8*, i8** %3
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %9
%14 = load i32*, i32** %9
%15 = icmp ne i32* %14, null
br i1 %15, label %17, label %16
16: ; preds = %2
br label %53
17: ; preds = %2
%18 = load i32*, i32** %9
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %9
%22 = call i64 @ftell(i32* %21)
store i64 %22, i64* %6
%23 = load i32*, i32** %9
%24 = call i32 @rewind(i32* %23)
br label %25
25: ; preds = %41, %17
%26 = load i64, i64* %6
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %50
28: ; preds = %25
store i32 4096, i32* %11
%29 = load i64, i64* %6
%30 = icmp ult i64 %29, 4096
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i64, i64* %6
%33 = trunc i64 %32 to i32
store i32 %33, i32* %11
br label %34
34: ; preds = %31, %28
%35 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%36 = load i32, i32* %11
%37 = load i32*, i32** %9
%38 = call i32 @fread(i8* %35, i32 %36, i32 1, i32* %37)
%39 = icmp ne i32 %38, 1
br i1 %39, label %40, label %41
40: ; preds = %34
br label %53
41: ; preds = %34
%42 = load i32, i32* %7
%43 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%44 = load i32, i32* %11
%45 = call i32 @crc32(i32 %42, i8* %43, i32 %44)
store i32 %45, i32* %7
%46 = load i32, i32* %11
%47 = sext i32 %46 to i64
%48 = load i64, i64* %6
%49 = sub nsw i64 %48, %47
store i64 %49, i64* %6
br label %25
50: ; preds = %25
%51 = load i32, i32* %7
%52 = load i32*, i32** %4
store i32 %51, i32* %52
store i32 0, i32* %5
br label %53
53: ; preds = %50, %40, %16
%54 = load i32*, i32** %9
%55 = icmp ne i32* %54, null
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = load i32*, i32** %9
%58 = call i32 @fclose(i32* %57)
store i32* null, i32** %9
br label %59
59: ; preds = %56, %53
%60 = load i32, i32* %5
ret i32 %60
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
define i32 @crc32_file(i8* nocapture %0, i32* nocapture writeonly %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %30, label %7
7: ; preds = %2
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %5, i32 0, i32 %8)
%10 = tail call i64 @ftell(i32* %5)
%11 = tail call i32 @rewind(i32* %5)
%12 = icmp eq i64 %10, 0
br i1 %12, label %25, label %13
13: ; preds = %7, %21
%14 = phi i32 [ %22, %21 ], [ 0, %7 ]
%15 = phi i64 [ %23, %21 ], [ %10, %7 ]
%16 = icmp ult i64 %15, 4096
%17 = select i1 %16, i64 %15, i64 4096
%18 = trunc i64 %17 to i32
%19 = call i32 @fread(i8* %4, i32 %18, i32 1, i32* %5)
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %27
21: ; preds = %13
%22 = call i32 @crc32(i32 %14, i8* %4, i32 %18)
%23 = sub nsw i64 %15, %17
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %13
25: ; preds = %21, %7
%26 = phi i32 [ 0, %7 ], [ %22, %21 ]
store i32 %26, i32* %1
br label %27
27: ; preds = %13, %25
%28 = phi i32 [ 0, %25 ], [ -1, %13 ]
%29 = call i32 @fclose(i32* %5)
br label %30
30: ; preds = %2, %27
%31 = phi i32 [ %28, %27 ], [ -1, %2 ]
ret i32 %31
}