#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(unsigned long, void *, unsigned long);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fread(void *, int, unsigned long, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char const *, ...);
int getFileCrc(const char *filenameinzip, void *buf, unsigned long size_buf,
               unsigned long *result_crc) {
  unsigned long calculate_crc = 0;
  int err = (0);
  FILE *fin = fopen(filenameinzip, "rb");
  unsigned long size_read = 0;
  unsigned long total_read = 0;
  if (fin == NULL) {
    err = ((-1));
  }
  if (err == (0))
    do {
      err = (0);
      size_read = (int)fread(buf, 1, size_buf, fin);
      if (size_read < size_buf)
        if (feof(fin) == 0) {
          printf("error in reading %s\n", filenameinzip);
          err = ((-1));
        }
      if (size_read > 0)
        calculate_crc = crc32(calculate_crc, buf, size_read);
      total_read += size_read;
    } while ((err == (0)) && (size_read > 0));
  if (fin)
    fclose(fin);
  *result_crc = calculate_crc;
  printf("file %s crc %x\n", filenameinzip, calculate_crc);
  return err;
}


x86 assembly (clang, -O0):
<getFileCrc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe6e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11bd <getFileCrc+0x5d>
movl   $0xffffffff,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1284 <getFileCrc+0x124>
jmp    11cc <getFileCrc+0x6c>
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cltq
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    122e <getFileCrc+0xce>
mov    -0x38(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
jne    1229 <getFileCrc+0xc9>
mov    -0x8(%rbp),%rsi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x2c(%rbp)
jmp    122e <getFileCrc+0xce>
cmpq   $0x0,-0x40(%rbp)
jbe    124e <getFileCrc+0xee>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1040 <crc32@plt>
mov    %rax,-0x28(%rbp)
mov    -0x40(%rbp),%rax
add    -0x48(%rbp),%rax
mov    %rax,-0x48(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x2c(%rbp)
mov    %al,-0x49(%rbp)
jne    1274 <getFileCrc+0x114>
cmpq   $0x0,-0x40(%rbp)
seta   %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    11cc <getFileCrc+0x6c>
jmp    1284 <getFileCrc+0x124>
cmpq   $0x0,-0x38(%rbp)
je     1298 <getFileCrc+0x138>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xd66(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getFileCrc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r15
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe7f(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11eb <getFileCrc+0x8b>
mov    %rax,%r12
mov    %r15,0x8(%rsp)
mov    %r14,0x10(%rsp)
xor    %r13d,%r13d
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rbp,%rdx
mov    %r12,%rcx
call   1030 <fread@plt>
mov    %rax,%r15
movslq %r15d,%r14
shl    $0x20,%r15
cmp    %rbp,%r14
jae    11cf <getFileCrc+0x6f>
mov    %r12,%rdi
call   1070 <feof@plt>
test   %rax,%rax
je     11f5 <getFileCrc+0x95>
test   %r15,%r15
je     11e7 <getFileCrc+0x87>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <crc32@plt>
mov    %rax,%r13
jmp    11a0 <getFileCrc+0x40>
xor    %ebp,%ebp
jmp    1223 <getFileCrc+0xc3>
mov    $0xffffffff,%ebp
xor    %r13d,%r13d
jmp    1235 <getFileCrc+0xd5>
lea    0xe07(%rip),%rdi
mov    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%ebp
test   %r15,%r15
je     1223 <getFileCrc+0xc3>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <crc32@plt>
mov    %rax,%r13
mov    0x8(%rsp),%r15
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    0x10(%rsp),%r14
mov    %r13,(%r15)
lea    0xdd9(%rip),%rdi
mov    %r14,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [21 x i8] c"error in reading %s\0A\00"
@.str.2 = [16 x i8] c"file %s crc %x\0A\00"
define i32 @getFileCrc(i8* %0, i8* %1, i64 %2, i64* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i64
%13 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64* %3, i64** %8
store i64 0, i64* %9
store i32 0, i32* %10
%14 = load i8*, i8** %5
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %11
store i64 0, i64* %12
store i64 0, i64* %13
%16 = load i32*, i32** %11
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 -1, i32* %10
br label %19
19: ; preds = %18, %4
%20 = load i32, i32* %10
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %62
22: ; preds = %19
br label %23
23: ; preds = %59, %22
store i32 0, i32* %10
%24 = load i8*, i8** %6
%25 = load i64, i64* %7
%26 = load i32*, i32** %11
%27 = call i64 @fread(i8* %24, i32 1, i64 %25, i32* %26)
%28 = trunc i64 %27 to i32
%29 = sext i32 %28 to i64
store i64 %29, i64* %12
%30 = load i64, i64* %12
%31 = load i64, i64* %7
%32 = icmp ult i64 %30, %31
br i1 %32, label %33, label %41
33: ; preds = %23
%34 = load i32*, i32** %11
%35 = call i64 @feof(i32* %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i8*, i8** %5
%39 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %38)
store i32 -1, i32* %10
br label %40
40: ; preds = %37, %33
br label %41
41: ; preds = %40, %23
%42 = load i64, i64* %12
%43 = icmp ugt i64 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %41
%45 = load i64, i64* %9
%46 = load i8*, i8** %6
%47 = load i64, i64* %12
%48 = call i64 @crc32(i64 %45, i8* %46, i64 %47)
store i64 %48, i64* %9
br label %49
49: ; preds = %44, %41
%50 = load i64, i64* %12
%51 = load i64, i64* %13
%52 = add i64 %51, %50
store i64 %52, i64* %13
br label %53
53: ; preds = %49
%54 = load i32, i32* %10
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = load i64, i64* %12
%58 = icmp ugt i64 %57, 0
br label %59
59: ; preds = %56, %53
%60 = phi i1 [ false, %53 ], [ %58, %56 ]
br i1 %60, label %23, label %61
61: ; preds = %59
br label %62
62: ; preds = %61, %19
%63 = load i32*, i32** %11
%64 = icmp ne i32* %63, null
br i1 %64, label %65, label %68
65: ; preds = %62
%66 = load i32*, i32** %11
%67 = call i32 @fclose(i32* %66)
br label %68
68: ; preds = %65, %62
%69 = load i64, i64* %9
%70 = load i64*, i64** %8
store i64 %69, i64* %70
%71 = load i8*, i8** %5
%72 = load i64, i64* %9
%73 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %71, i64 %72)
%74 = load i32, i32* %10
ret i32 %74
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [21 x i8] c"error in reading %s\0A\00"
@.str.2 = [16 x i8] c"file %s crc %x\0A\00"
define i32 @getFileCrc(i8* %0, i8* %1, i64 %2, i64* nocapture writeonly %3) {
%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 %29, label %7
7: ; preds = %4, %23
%8 = phi i64 [ %24, %23 ], [ 0, %4 ]
%9 = tail call i64 @fread(i8* %1, i32 1, i64 %2, i32* %5)
%10 = shl i64 %9, 32
%11 = ashr exact i64 %10, 32
%12 = icmp ult i64 %11, %2
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = tail call i64 @feof(i32* %5)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13, %7
%17 = icmp eq i64 %10, 0
br i1 %17, label %25, label %23
18: ; preds = %13
%19 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%20 = icmp eq i64 %10, 0
br i1 %20, label %25, label %21
21: ; preds = %18
%22 = tail call i64 @crc32(i64 %8, i8* %1, i64 %11)
br label %25
23: ; preds = %16
%24 = tail call i64 @crc32(i64 %8, i8* %1, i64 %11)
br label %7
25: ; preds = %16, %18, %21
%26 = phi i32 [ -1, %18 ], [ -1, %21 ], [ 0, %16 ]
%27 = phi i64 [ %8, %18 ], [ %22, %21 ], [ %8, %16 ]
%28 = tail call i32 @fclose(i32* %5)
br label %29
29: ; preds = %4, %25
%30 = phi i64 [ %27, %25 ], [ 0, %4 ]
%31 = phi i32 [ %26, %25 ], [ -1, %4 ]
store i64 %30, i64* %3
%32 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %0, i64 %30)
ret i32 %31
}