#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*/ FILE;

/* Variables and functions */
unsigned long crc32_buf(char *, unsigned int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, unsigned int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ lib_free(char *);
char *lib_malloc(unsigned int);
scalar_t__ util_check_null_string(char const *);
scalar_t__ util_file_length(int /*<<< orphan*/ *);
unsigned long crc32_file(const char *filename) {
  FILE *fd;
  char *buffer;
  unsigned int len;
  unsigned long crc = 0;
  if (util_check_null_string(filename) < 0)
    return 0;
  fd = fopen(filename, "r");
  if (fd == NULL)
    return 0;
  len = (unsigned int)util_file_length(fd);
  buffer = lib_malloc(len);
  if (fread(buffer, len, 1, fd) == 1)
    crc = crc32_buf(buffer, len);
  fclose(fd);
  lib_free(buffer);
  return crc;
}


x86 assembly (clang, -O0):
<crc32_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <util_check_null_string@plt>
cmp    $0x0,%rax
jge    11b4 <crc32_file+0x34>
movq   $0x0,-0x8(%rbp)
jmp    1240 <crc32_file+0xc0>
mov    -0x10(%rbp),%rdi
lea    0xe41(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e0 <crc32_file+0x60>
movq   $0x0,-0x8(%rbp)
jmp    1240 <crc32_file+0xc0>
mov    -0x18(%rbp),%rdi
call   1090 <util_file_length@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <lib_malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    1226 <crc32_file+0xa6>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1070 <crc32_buf@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1050 <lib_free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crc32_file>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1080 <util_check_null_string@plt>
test   %rax,%rax
js     11e4 <crc32_file+0x64>
lea    0xe67(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     11e4 <crc32_file+0x64>
mov    %rax,%r15
mov    %rax,%rdi
call   1090 <util_file_length@plt>
mov    %rax,%rbx
mov    %ebx,%edi
call   1030 <lib_malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    11e8 <crc32_file+0x68>
mov    %r14,%rdi
mov    %ebx,%esi
call   1070 <crc32_buf@plt>
mov    %rax,%rbx
jmp    11ea <crc32_file+0x6a>
xor    %ebx,%ebx
jmp    11fa <crc32_file+0x7a>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
call   1050 <lib_free@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i64 @crc32_file(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
store i8* %0, i8** %3
store i64 0, i64* %7
%8 = load i8*, i8** %3
%9 = call i64 @util_check_null_string(i8* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i64 0, i64* %2
br label %39
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %4
%15 = load i32*, i32** %4
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %12
store i64 0, i64* %2
br label %39
18: ; preds = %12
%19 = load i32*, i32** %4
%20 = call i64 @util_file_length(i32* %19)
%21 = trunc i64 %20 to i32
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = call i8* @lib_malloc(i32 %22)
store i8* %23, i8** %5
%24 = load i8*, i8** %5
%25 = load i32, i32* %6
%26 = load i32*, i32** %4
%27 = call i32 @fread(i8* %24, i32 %25, i32 1, i32* %26)
%28 = icmp eq i32 %27, 1
br i1 %28, label %29, label %33
29: ; preds = %18
%30 = load i8*, i8** %5
%31 = load i32, i32* %6
%32 = call i64 @crc32_buf(i8* %30, i32 %31)
store i64 %32, i64* %7
br label %33
33: ; preds = %29, %18
%34 = load i32*, i32** %4
%35 = call i32 @fclose(i32* %34)
%36 = load i8*, i8** %5
%37 = call i32 @lib_free(i8* %36)
%38 = load i64, i64* %7
store i64 %38, i64* %2
br label %39
39: ; preds = %33, %17, %11
%40 = load i64, i64* %2
ret i64 %40
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i64 @crc32_file(i8* %0) {
%2 = tail call i64 @util_check_null_string(i8* %0)
%3 = icmp slt i64 %2, 0
br i1 %3, label %19, label %4
4: ; preds = %1
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %19, label %7
7: ; preds = %4
%8 = tail call i64 @util_file_length(i32* %5)
%9 = trunc i64 %8 to i32
%10 = tail call i8* @lib_malloc(i32 %9)
%11 = tail call i32 @fread(i8* %10, i32 %9, i32 1, i32* %5)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = tail call i64 @crc32_buf(i8* %10, i32 %9)
br label %15
15: ; preds = %13, %7
%16 = phi i64 [ %14, %13 ], [ 0, %7 ]
%17 = tail call i32 @fclose(i32* %5)
%18 = tail call i32 @lib_free(i8* %10)
br label %19
19: ; preds = %4, %1, %15
%20 = phi i64 [ %16, %15 ], [ 0, %1 ], [ 0, %4 ]
ret i64 %20
}