#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 uLong;
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ Bytef;

/* Variables and functions */
int /*<<< orphan*/ *Z_NULL;
int crc32(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, const char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "Usage: crc32 <filename>\n");
    return 1;
  }
  FILE *file = fopen(argv[1], "rb");
  if (file) {
    uLong crc = crc32(0L, Z_NULL, 0);
    for (;;) {
      Bytef buffer[16384];
      int numread = fread((void *)buffer, 1, sizeof(buffer), file);
      if (numread > 0)
        crc = crc32(crc, buffer, numread);
      else
        break;
    }
    fclose(file);
    printf("%08x\n", crc);
    return 0;
  }
  fprintf(stderr, "Error opening input file: %s\n", strerror(errno));
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11b6 <main+0x46>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a2 <main+0x132>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe54(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1267 <main+0xf7>
mov    0x2df0(%rip),%rax
mov    (%rax),%rsi
xor    %edx,%edx
mov    %edx,%edi
call   1040 <crc32@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x10020(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10000,%edx
call   1030 <fread@plt>
mov    %eax,-0x10024(%rbp)
cmpl   $0x0,-0x10024(%rbp)
jle    1239 <main+0xc9>
mov    -0x1c(%rbp),%edi
lea    -0x10020(%rbp),%rsi
mov    -0x10024(%rbp),%edx
call   1040 <crc32@plt>
mov    %eax,-0x1c(%rbp)
jmp    123e <main+0xce>
jmp    1243 <main+0xd3>
jmp    11ef <main+0x7f>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x1c(%rbp),%esi
lea    0xdc6(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a2 <main+0x132>
mov    0x2d72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10028(%rbp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x10028(%rbp),%edi
mov    %rax,%rdx
lea    0xd8e(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x10008,%rsp
cmp    $0x2,%edi
jne    121f <main+0xaf>
mov    0x8(%rsi),%rdi
lea    0xe88(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1238 <main+0xc8>
mov    %rax,%r15
mov    0x2e27(%rip),%rax
mov    (%rax),%rsi
xor    %r14d,%r14d
xor    %edi,%edi
xor    %edx,%edx
call   1040 <crc32@plt>
mov    %eax,%ebp
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x10000,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    1207 <main+0x97>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <crc32@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x10000,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jg     11e0 <main+0x70>
mov    %r15,%rdi
call   1050 <fclose@plt>
lea    0xe06(%rip),%rdi
mov    %ebp,%esi
call   1060 <printf@plt>
jmp    1268 <main+0xf8>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1262 <main+0xf2>
mov    0x2da1(%rip),%rax
mov    (%rax),%ebx
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdcc(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x10008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [25 x i8] c"Usage: crc32 <filename>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@Z_NULL = global i32* null
@.str.2 = [6 x i8] c"%08x\0A\00"
@.str.3 = [30 x i8] c"Error opening input file: %s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca [16384 x i32]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %49
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %6
%20 = load i32*, i32** %6
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %44
22: ; preds = %15
%23 = load i32*, i32** @Z_NULL
%24 = call i32 @crc32(i32 0, i32* %23, i32 0)
store i32 %24, i32* %7
br label %25
25: ; preds = %38, %22
%26 = getelementptr inbounds [16384 x i32], [16384 x i32]* %8, i64 0, i64 0
%27 = bitcast i32* %26 to i8*
%28 = load i32*, i32** %6
%29 = call i32 @fread(i8* %27, i32 1, i32 65536, i32* %28)
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %25
%33 = load i32, i32* %7
%34 = getelementptr inbounds [16384 x i32], [16384 x i32]* %8, i64 0, i64 0
%35 = load i32, i32* %9
%36 = call i32 @crc32(i32 %33, i32* %34, i32 %35)
store i32 %36, i32* %7
br label %38
37: ; preds = %25
br label %39
38: ; preds = %32
br label %25
39: ; preds = %37
%40 = load i32*, i32** %6
%41 = call i32 @fclose(i32* %40)
%42 = load i32, i32* %7
%43 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %42)
store i32 0, i32* %3
br label %49
44: ; preds = %15
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %47)
store i32 1, i32* %3
br label %49
49: ; preds = %44, %39, %12
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [25 x i8] c"Usage: crc32 <filename>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@Z_NULL = global i32* null
@.str.2 = [6 x i8] c"%08x\0A\00"
@.str.3 = [30 x i8] c"Error opening input file: %s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [16384 x i32]
%4 = icmp eq i32 %0, 2
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %36
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %31, label %13
13: ; preds = %8
%14 = load i32*, i32** @Z_NULL
%15 = tail call i32 @crc32(i32 0, i32* %14, i32 0)
%16 = bitcast [16384 x i32]* %3 to i8*
%17 = call i32 @fread(i8* %16, i32 1, i32 65536, i32* %11)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %13
%20 = getelementptr inbounds [16384 x i32], [16384 x i32]* %3, i64 0, i64 0
br label %21
21: ; preds = %19, %21
%22 = phi i32 [ %17, %19 ], [ %25, %21 ]
%23 = phi i32 [ %15, %19 ], [ %24, %21 ]
%24 = call i32 @crc32(i32 %23, i32* %20, i32 %22)
%25 = call i32 @fread(i8* %16, i32 1, i32 65536, i32* %11)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %21, label %27
27: ; preds = %21, %13
%28 = phi i32 [ %15, %13 ], [ %24, %21 ]
%29 = call i32 @fclose(i32* %11)
%30 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %28)
br label %36
31: ; preds = %8
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = tail call i8* @strerror(i32 %33)
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %27, %31, %5
%37 = phi i32 [ 1, %5 ], [ 0, %27 ], [ 1, %31 ]
ret i32 %37
}