#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
unsigned int crc32(unsigned int, char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, unsigned int);
int read(int, char *, int);
void main() {
  int fd;
  char buf[512];
  int c;
  unsigned int checksum;
  fd = open("firmware.bin", O_RDONLY);
  checksum = 0;
  while (1) {
    c = read(fd, buf, 512);
    if (c <= 0)
      break;
    checksum = crc32(checksum, buf, c);
    printf("%x\n", checksum);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
movl   $0x0,-0x218(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,-0x214(%rbp)
cmpl   $0x0,-0x214(%rbp)
jg     11a9 <main+0x59>
jmp    11de <main+0x8e>
mov    -0x218(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    -0x214(%rbp),%edx
call   1030 <crc32@plt>
mov    %eax,-0x218(%rbp)
mov    -0x218(%rbp),%esi
lea    0xe39(%rip),%rdi
call   1040 <printf@plt>
jmp    117d <main+0x2d>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
lea    0xe93(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x200,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    11cc <main+0x7c>
xor    %ebp,%ebp
mov    %rsp,%rbx
lea    0xe79(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <crc32@plt>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1060 <read@plt>
test   %eax,%eax
jg     11a0 <main+0x50>
mov    %r15d,%edi
call   1050 <close@plt>
add    $0x208,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"firmware.bin\00"
@O_RDONLY = global i32 0
@.str.1 = [4 x i8] c"%x\0A\00"
define void @main() {
%1 = alloca i32
%2 = alloca [512 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %1
store i32 0, i32* %4
br label %7
7: ; preds = %0, %14
%8 = load i32, i32* %1
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%10 = call i32 @read(i32 %8, i8* %9, i32 512)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp sle i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %7
br label %21
14: ; preds = %7
%15 = load i32, i32* %4
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%17 = load i32, i32* %3
%18 = call i32 @crc32(i32 %15, i8* %16, i32 %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %7
21: ; preds = %13
%22 = load i32, i32* %1
%23 = call i32 @close(i32 %22)
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"firmware.bin\00"
@O_RDONLY = global i32 0
@.str.1 = [4 x i8] c"%x\0A\00"
define void @main() {
%1 = alloca [512 x i8]
%2 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i32 @read(i32 %4, i8* %2, i32 512)
%6 = icmp slt i32 %5, 1
br i1 %6, label %14, label %7
7: ; preds = %0, %7
%8 = phi i32 [ %12, %7 ], [ %5, %0 ]
%9 = phi i32 [ %10, %7 ], [ 0, %0 ]
%10 = call i32 @crc32(i32 %9, i8* %2, i32 %8)
%11 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = call i32 @read(i32 %4, i8* %2, i32 512)
%13 = icmp slt i32 %12, 1
br i1 %13, label %14, label %7
14: ; preds = %7, %0
%15 = call i32 @close(i32 %4)
ret void
}