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

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int O_APPEND;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
unsigned int crc32(unsigned char *, unsigned int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
unsigned int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
unsigned char *mmap(int /*<<< orphan*/ *, unsigned int, int /*<<< orphan*/,
                    int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(unsigned char *, unsigned int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, unsigned int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  unsigned char *buffer;
  unsigned int length;
  unsigned int crc;
  if ((argc != 2) && ((argc != 3) && !strcmp(argv[2], "write"))) {
    fprintf(stderr, "Usage: crc32 <filename> [write]\n");
    return 1;
  }
  fd = open(argv[1], O_RDONLY);
  if (fd == -1) {
    perror("open");
    return 1;
  }
  length = lseek(fd, 0, SEEK_END);
  lseek(fd, 0, SEEK_SET);
  buffer = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0);
  if (!buffer) {
    perror("mmap");
    close(fd);
    return 1;
  }
  crc = crc32(buffer, length);
  printf("CRC32 (%s): %08x\n", argv[1], crc);
  munmap(buffer, length);
  close(fd);
  if (argc == 3) {
    char b[4];
    fd = open(argv[1], O_WRONLY | O_APPEND);
    if (fd == -1) {
      perror("open");
      return 1;
    }
    b[0] = (crc & 0xff000000) >> 24;
    b[1] = (crc & 0x00ff0000) >> 16;
    b[2] = (crc & 0x0000ff00) >> 8;
    b[3] = (crc & 0x000000ff) >> 0;
    write(fd, &b[0], 4);
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1218 <main+0x68>
cmpl   $0x3,-0x8(%rbp)
je     1218 <main+0x68>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe17(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    1218 <main+0x68>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xdff(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b0 <main+0x200>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1253 <main+0xa3>
lea    0xde5(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b0 <main+0x200>
mov    -0x14(%rbp),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <lseek@plt>
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d45(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <lseek@plt>
mov    -0x24(%rbp),%esi
mov    0x2d72(%rip),%rax
mov    (%rax),%edx
mov    0x2d59(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12d0 <main+0x120>
lea    0xd75(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b0 <main+0x200>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <crc32@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x28(%rbp),%edx
lea    0xd40(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   10b0 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
cmpl   $0x3,-0x8(%rbp)
jne    13a9 <main+0x1f9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2c85(%rip),%rax
mov    (%rax),%esi
mov    0x2c74(%rip),%rax
or     (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1358 <main+0x1a8>
lea    0xce0(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b0 <main+0x200>
mov    -0x28(%rbp),%eax
and    $0xff000000,%eax
shr    $0x18,%eax
mov    %al,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
and    $0xff0000,%eax
shr    $0x10,%eax
mov    %al,-0x2b(%rbp)
mov    -0x28(%rbp),%eax
and    $0xff00,%eax
shr    $0x8,%eax
mov    %al,-0x2a(%rbp)
mov    -0x28(%rbp),%eax
and    $0xff,%eax
shr    $0x0,%eax
mov    %al,-0x29(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %edi,%r15d
mov    %edi,%eax
and    $0xfffffffe,%eax
cmp    $0x2,%eax
je     11e4 <main+0x34>
mov    0x10(%r12),%rdi
lea    0xe29(%rip),%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
je     12d3 <main+0x123>
mov    0x8(%r12),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     12ea <main+0x13a>
mov    %eax,%ebp
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1070 <lseek@plt>
mov    %eax,%ebx
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1070 <lseek@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
mov    0x2db0(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
test   %rax,%rax
je     12f8 <main+0x148>
mov    %rax,%r14
mov    %rax,%rdi
mov    %ebx,%esi
call   1030 <crc32@plt>
mov    %eax,%r13d
mov    0x8(%r12),%rsi
lea    0xdc3(%rip),%rdi
mov    %eax,%edx
call   1060 <printf@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   10b0 <munmap@plt>
mov    %ebp,%edi
call   1080 <close@plt>
cmp    $0x3,%r15d
jne    12cf <main+0x11f>
mov    0x8(%r12),%rdi
mov    0x2d10(%rip),%rax
mov    0x2d01(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     12ea <main+0x13a>
mov    %eax,%ebx
bswap  %r13d
mov    %r13d,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
jmp    1310 <main+0x160>
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
lea    0xd23(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    130b <main+0x15b>
lea    0xd36(%rip),%rdi
call   10d0 <perror@plt>
jmp    130b <main+0x15b>
lea    0xd2d(%rip),%rdi
call   10d0 <perror@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"Usage: crc32 <filename> [write]\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [18 x i8] c"CRC32 (%s): %08x\0A\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [4 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %25
13: ; preds = %2
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %25
16: ; preds = %13
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 2
%19 = load i8*, i8** %18
%20 = call i32 @strcmp(i8* %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%21 = icmp ne i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %16
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %108
25: ; preds = %16, %13, %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = load i32, i32* @O_RDONLY
%30 = call i32 @open(i8* %28, i32 %29)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %108
35: ; preds = %25
%36 = load i32, i32* %6
%37 = load i32, i32* @SEEK_END
%38 = call i32 @lseek(i32 %36, i32 0, i32 %37)
store i32 %38, i32* %8
%39 = load i32, i32* %6
%40 = load i32, i32* @SEEK_SET
%41 = call i32 @lseek(i32 %39, i32 0, i32 %40)
%42 = load i32, i32* %8
%43 = load i32, i32* @PROT_READ
%44 = load i32, i32* @MAP_SHARED
%45 = load i32, i32* %6
%46 = call i8* @mmap(i32* null, i32 %42, i32 %43, i32 %44, i32 %45, i32 0)
store i8* %46, i8** %7
%47 = load i8*, i8** %7
%48 = icmp ne i8* %47, null
br i1 %48, label %53, label %49
49: ; preds = %35
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%51 = load i32, i32* %6
%52 = call i32 @close(i32 %51)
store i32 1, i32* %3
br label %108
53: ; preds = %35
%54 = load i8*, i8** %7
%55 = load i32, i32* %8
%56 = call i32 @crc32(i8* %54, i32 %55)
store i32 %56, i32* %9
%57 = load i8**, i8*** %5
%58 = getelementptr inbounds i8*, i8** %57, i64 1
%59 = load i8*, i8** %58
%60 = load i32, i32* %9
%61 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %59, i32 %60)
%62 = load i8*, i8** %7
%63 = load i32, i32* %8
%64 = call i32 @munmap(i8* %62, i32 %63)
%65 = load i32, i32* %6
%66 = call i32 @close(i32 %65)
%67 = load i32, i32* %4
%68 = icmp eq i32 %67, 3
br i1 %68, label %69, label %107
69: ; preds = %53
%70 = load i8**, i8*** %5
%71 = getelementptr inbounds i8*, i8** %70, i64 1
%72 = load i8*, i8** %71
%73 = load i32, i32* @O_WRONLY
%74 = load i32, i32* @O_APPEND
%75 = or i32 %73, %74
%76 = call i32 @open(i8* %72, i32 %75)
store i32 %76, i32* %6
%77 = load i32, i32* %6
%78 = icmp eq i32 %77, -1
br i1 %78, label %79, label %81
79: ; preds = %69
%80 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %108
81: ; preds = %69
%82 = load i32, i32* %9
%83 = and i32 %82, -16777216
%84 = lshr i32 %83, 24
%85 = trunc i32 %84 to i8
%86 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
store i8 %85, i8* %86
%87 = load i32, i32* %9
%88 = and i32 %87, 16711680
%89 = lshr i32 %88, 16
%90 = trunc i32 %89 to i8
%91 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 1
store i8 %90, i8* %91
%92 = load i32, i32* %9
%93 = and i32 %92, 65280
%94 = lshr i32 %93, 8
%95 = trunc i32 %94 to i8
%96 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 2
store i8 %95, i8* %96
%97 = load i32, i32* %9
%98 = and i32 %97, 255
%99 = lshr i32 %98, 0
%100 = trunc i32 %99 to i8
%101 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 3
store i8 %100, i8* %101
%102 = load i32, i32* %6
%103 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%104 = call i32 @write(i32 %102, i8* %103, i32 4)
%105 = load i32, i32* %6
%106 = call i32 @close(i32 %105)
br label %107
107: ; preds = %81, %53
store i32 0, i32* %3
br label %108
108: ; preds = %107, %79, %49, %33, %22
%109 = load i32, i32* %3
ret i32 %109
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
@stderr = global i32 0
@.str.1 = [33 x i8] c"Usage: crc32 <filename> [write]\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [18 x i8] c"CRC32 (%s): %08x\0A\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4 x i8]
%4 = and i32 %0, -2
%5 = icmp eq i32 %4, 2
br i1 %5, label %14, label %6
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 2
%8 = load i8*, i8** %7
%9 = tail call i32 @strcmp(i8* dereferenceable(1) %8, i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %64
14: ; preds = %6, %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDONLY
%18 = tail call i32 @open(i8* %16, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %64
22: ; preds = %14
%23 = load i32, i32* @SEEK_END
%24 = tail call i32 @lseek(i32 %18, i32 0, i32 %23)
%25 = load i32, i32* @SEEK_SET
%26 = tail call i32 @lseek(i32 %18, i32 0, i32 %25)
%27 = load i32, i32* @PROT_READ
%28 = load i32, i32* @MAP_SHARED
%29 = tail call i8* @mmap(i32* null, i32 %24, i32 %27, i32 %28, i32 %18, i32 0)
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %34
31: ; preds = %22
%32 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%33 = tail call i32 @close(i32 %18)
br label %64
34: ; preds = %22
%35 = tail call i32 @crc32(i8* %29, i32 %24)
%36 = load i8*, i8** %15
%37 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %36, i32 %35)
%38 = tail call i32 @munmap(i8* %29, i32 %24)
%39 = tail call i32 @close(i32 %18)
%40 = icmp eq i32 %0, 3
br i1 %40, label %41, label %64
41: ; preds = %34
%42 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%43 = load i8*, i8** %15
%44 = load i32, i32* @O_WRONLY
%45 = load i32, i32* @O_APPEND
%46 = or i32 %45, %44
%47 = tail call i32 @open(i8* %43, i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %41
%50 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %64
51: ; preds = %41
%52 = lshr i32 %35, 24
%53 = trunc i32 %52 to i8
store i8 %53, i8* %42
%54 = lshr i32 %35, 16
%55 = trunc i32 %54 to i8
%56 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
store i8 %55, i8* %56
%57 = lshr i32 %35, 8
%58 = trunc i32 %57 to i8
%59 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
store i8 %58, i8* %59
%60 = trunc i32 %35 to i8
%61 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 3
store i8 %60, i8* %61
%62 = call i32 @write(i32 %47, i8* %42, i32 4)
%63 = tail call i32 @close(i32 %47)
br label %64
64: ; preds = %34, %51, %49, %31, %20, %11
%65 = phi i32 [ 1, %20 ], [ 1, %31 ], [ 1, %11 ], [ 1, %49 ], [ 0, %51 ], [ 0, %34 ]
ret i32 %65
}