#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*/ uint8_t;
typedef int uint16_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int crc16_update(int, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ usage(int);
int main(int argc, char **argv) {
  uint16_t crc = 0xffff;
  uint8_t b;
  FILE *f;
  if (argc == 2 && strcmp(argv[1], "--help") == 0)
    usage(0);
  if (argc != 2)
    usage(1);
  if ((f = fopen(argv[1], "r+b")) == NULL) {
    fprintf(stderr, "Error opening %s.\n", argv[1]);
    return 1;
  }
  while (fread(&b, 1, 1, f) == 1) {
    crc = crc16_update(crc, b);
  }
  if (ferror(f)) {
    fprintf(stderr, "Error reading from %s.\n", argv[1]);
    return 1;
  }
  fwrite(&crc, 2, 1, f);
  fclose(f);
  fprintf(stdout, "padding CRC16: %4X\n", crc);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffff,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11dc <main+0x4c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe3a(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    11dc <main+0x4c>
xor    %edi,%edi
call   1090 <usage@plt>
cmpl   $0x2,-0x8(%rbp)
je     11f0 <main+0x60>
mov    $0x1,%edi
call   1090 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe08(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    123d <main+0xad>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xde1(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f1 <main+0x161>
jmp    1242 <main+0xb2>
mov    -0x20(%rbp),%rcx
lea    -0x18(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    1272 <main+0xe2>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1060 <crc16_update@plt>
mov    %eax,-0x14(%rbp)
jmp    1242 <main+0xb2>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     12b0 <main+0x120>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd81(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f1 <main+0x161>
mov    -0x20(%rbp),%rcx
lea    -0x14(%rbp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xd53(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%eax
mov    $0x1,%edi
cmp    $0x2,%eax
jne    11bd <main+0x2d>
mov    0x8(%r14),%rdi
lea    0xe4f(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    11c2 <main+0x32>
xor    %edi,%edi
call   1090 <usage@plt>
mov    0x8(%r14),%rdi
lea    0xe3a(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     125d <main+0xcd>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
mov    $0xffff,%ebp
cmp    $0x1,%eax
jne    1236 <main+0xa6>
mov    $0xffff,%ebp
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rsp),%esi
mov    %ebp,%edi
call   1060 <crc16_update@plt>
mov    %eax,%ebp
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     1210 <main+0x80>
mov    %ebp,0x4(%rsp)
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     127f <main+0xef>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xdc3(%rip),%rsi
jmp    1271 <main+0xe1>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xd9a(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%ebx
jmp    12bb <main+0x12b>
lea    0x4(%rsp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
mov    0x4(%rsp),%edx
lea    0xd84(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"--help\00"
@.str.1 = [4 x i8] c"r+b\00"
@stderr = global i32 0
@.str.2 = [19 x i8] c"Error opening %s.\0A\00"
@.str.3 = [24 x i8] c"Error reading from %s.\0A\00"
@stdout = global i32 0
@.str.4 = [20 x i8] c"padding CRC16: %4X\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 65535, i32* %6
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 2
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 @usage(i32 0)
br label %19
19: ; preds = %17, %11, %2
%20 = load i32, i32* %4
%21 = icmp ne i32 %20, 2
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @usage(i32 1)
br label %24
24: ; preds = %22, %19
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %28, i32** %8
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %36
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %34)
store i32 1, i32* %3
br label %63
36: ; preds = %24
br label %37
37: ; preds = %41, %36
%38 = load i32*, i32** %8
%39 = call i32 @fread(i32* %7, i32 1, i32 1, i32* %38)
%40 = icmp eq i32 %39, 1
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i32, i32* %6
%43 = load i32, i32* %7
%44 = call i32 @crc16_update(i32 %42, i32 %43)
store i32 %44, i32* %6
br label %37
45: ; preds = %37
%46 = load i32*, i32** %8
%47 = call i64 @ferror(i32* %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %55
49: ; preds = %45
%50 = load i32, i32* @stderr
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 1
%53 = load i8*, i8** %52
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %53)
store i32 1, i32* %3
br label %63
55: ; preds = %45
%56 = load i32*, i32** %8
%57 = call i32 @fwrite(i32* %6, i32 2, i32 1, i32* %56)
%58 = load i32*, i32** %8
%59 = call i32 @fclose(i32* %58)
%60 = load i32, i32* @stdout
%61 = load i32, i32* %6
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %61)
store i32 0, i32* %3
br label %63
63: ; preds = %55, %49, %30
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
@.str = [7 x i8] c"--help\00"
@.str.1 = [4 x i8] c"r+b\00"
@stderr = global i32 0
@.str.2 = [19 x i8] c"Error opening %s.\0A\00"
@.str.3 = [24 x i8] c"Error reading from %s.\0A\00"
@stdout = global i32 0
@.str.4 = [20 x i8] c"padding CRC16: %4X\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i64 @strcmp(i8* %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2, %8
%14 = phi i32 [ 0, %8 ], [ 1, %2 ]
%15 = tail call i32 @usage(i32 %14)
br label %16
16: ; preds = %13, %8
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = tail call i32* @fopen(i8* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = call i32 @fread(i32* %4, i32 1, i32 1, i32* %19)
%23 = icmp eq i32 %22, 1
br i1 %23, label %28, label %34
24: ; preds = %16
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %17
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %26)
br label %48
28: ; preds = %21, %28
%29 = phi i32 [ %31, %28 ], [ 65535, %21 ]
%30 = load i32, i32* %4
%31 = tail call i32 @crc16_update(i32 %29, i32 %30)
%32 = call i32 @fread(i32* %4, i32 1, i32 1, i32* %19)
%33 = icmp eq i32 %32, 1
br i1 %33, label %28, label %34
34: ; preds = %28, %21
%35 = phi i32 [ 65535, %21 ], [ %31, %28 ]
store i32 %35, i32* %3
%36 = tail call i64 @ferror(i32* %19)
%37 = icmp eq i64 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %17
%41 = tail call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %40)
br label %48
42: ; preds = %34
%43 = call i32 @fwrite(i32* %3, i32 2, i32 1, i32* %19)
%44 = tail call i32 @fclose(i32* %19)
%45 = load i32, i32* @stdout
%46 = load i32, i32* %3
%47 = tail call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %42, %38, %24
%49 = phi i32 [ 1, %24 ], [ 1, %38 ], [ 0, %42 ]
ret i32 %49
}