#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 atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ gen_crc_table();
char *get_next_packet(int);
int /*<<< orphan*/ packet_size(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
unsigned long update_crc(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  unsigned long crc_accum;
  int i = 0, numpackets;
  char *packet;
  if (argc != 2) {
    fprintf(stderr, "Usage: crc #numpackets");
    exit(0);
  } else
    numpackets = atoi(argv[1]);
  gen_crc_table();
  while (i < numpackets) {
    packet = get_next_packet(i);
    crc_accum = update_crc(0, packet, packet_size(i));
    i++;
  }
  fprintf(stdout, "CRC completed for %d packets \n", numpackets);
  fprintf(stdout, "crc_accum is %u\n", (unsigned)crc_accum);
  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)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11b5 <main+0x45>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <gen_crc_table@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1213 <main+0xa3>
mov    -0x1c(%rbp),%edi
call   1030 <get_next_packet@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <packet_size@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
xor    %edi,%edi
call   1070 <update_crc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11cc <main+0x5c>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xdf1(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
lea    0xdf3(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jne    11ff <main+0x8f>
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <gen_crc_table@plt>
test   %r14d,%r14d
jle    11ca <main+0x5a>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1030 <get_next_packet@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1050 <packet_size@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1070 <update_crc@plt>
add    $0x1,%ebp
cmp    %ebp,%r14d
jne    11a0 <main+0x30>
mov    %rax,%rbx
jmp    11ca <main+0x5a>
mov    0x2e07(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xe3c(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x0(%rbp),%edi
lea    0xe47(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
lea    0xdf1(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [23 x i8] c"Usage: crc #numpackets\00"
@stdout = global i32 0
@.str.1 = [31 x i8] c"CRC completed for %d packets \0A\00"
@.str.2 = [17 x i8] c"crc_accum is %u\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %8
br label %21
21: ; preds = %16
%22 = call i32 (...) @gen_crc_table()
br label %23
23: ; preds = %27, %21
%24 = load i32, i32* %7
%25 = load i32, i32* %8
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %36
27: ; preds = %23
%28 = load i32, i32* %7
%29 = call i8* @get_next_packet(i32 %28)
store i8* %29, i8** %9
%30 = load i8*, i8** %9
%31 = load i32, i32* %7
%32 = call i32 @packet_size(i32 %31)
%33 = call i64 @update_crc(i32 0, i8* %30, i32 %32)
store i64 %33, i64* %6
%34 = load i32, i32* %7
%35 = add nsw i32 %34, 1
store i32 %35, i32* %7
br label %23
36: ; preds = %23
%37 = load i32, i32* @stdout
%38 = load i32, i32* %8
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = load i32, i32* @stdout
%41 = load i64, i64* %6
%42 = trunc i64 %41 to i32
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %42)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [23 x i8] c"Usage: crc #numpackets\00"
@stdout = global i32 0
@.str.1 = [31 x i8] c"CRC completed for %d packets \0A\00"
@.str.2 = [17 x i8] c"crc_accum is %u\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 0)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = tail call i32 (...) @gen_crc_table()
%13 = icmp sgt i32 %11, 0
br i1 %13, label %14, label %23
14: ; preds = %8, %14
%15 = phi i32 [ %19, %14 ], [ 0, %8 ]
%16 = tail call i8* @get_next_packet(i32 %15)
%17 = tail call i32 @packet_size(i32 %15)
%18 = tail call i64 @update_crc(i32 0, i8* %16, i32 %17)
%19 = add nuw nsw i32 %15, 1
%20 = icmp eq i32 %19, %11
br i1 %20, label %21, label %14
21: ; preds = %14
%22 = trunc i64 %18 to i32
br label %23
23: ; preds = %21, %8
%24 = phi i32 [ %22, %21 ], [ undef, %8 ]
%25 = load i32, i32* @stdout
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%27 = load i32, i32* @stdout
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %24)
ret i32 0
}