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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ encodeByte(unsigned char, double);
unsigned char encodeData(unsigned char *, int, unsigned char);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ leaderTone(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ onePeriod(int, double);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ silence();
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  unsigned char *myFile;
  if (argc != 2) {
    printf("usage: %s <file>\n", argv[0]);
    return 1;
  }
  FILE *fp = fopen(argv[1], "rb");
  if (!fp) {
    perror("fopen");
    return 1;
  }
  fseek(fp, 0, SEEK_END);
  int length = ftell(fp);
  rewind(fp);
  myFile = (unsigned char *)malloc(length);
  if (fread(myFile, 1, length, fp) != length) {
    perror("fread");
    return 1;
  }
  fprintf(stderr, "Length: %d\n", length);
  fprintf(stderr, "Load at: %.4x..%.4x\n", 0x800, 0x800 + length - 1);
  fprintf(stderr, "Press enter when ready...");
  getchar();
  unsigned char checksum = -1;
  leaderTone(3000);
  onePeriod(2200, 0.0);
  checksum = encodeData(myFile, length, checksum);
  encodeByte(checksum, 0.0);
  onePeriod(200, 0.0);
  silence();
}


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     122f <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xde2(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1383 <main+0x193>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdd4(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    126a <main+0x7a>
lea    0xdbc(%rip),%rdi
call   10f0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1383 <main+0x193>
mov    -0x20(%rbp),%rdi
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x24(%rbp),%edi
call   10c0 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x24(%rbp),%eax
je     12d5 <main+0xe5>
lea    0xd57(%rip),%rdi
call   10f0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1383 <main+0x193>
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xd39(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%ecx
add    $0x800,%ecx
sub    $0x1,%ecx
lea    0xd22(%rip),%rsi
mov    $0x800,%edx
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2cba(%rip),%rax
mov    (%rax),%edi
lea    0xd1b(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x0,%al
call   1080 <getchar@plt>
movb   $0xff,-0x25(%rbp)
mov    $0xbb8,%edi
call   1040 <leaderTone@plt>
mov    $0x898,%edi
xorps  %xmm0,%xmm0
call   1070 <onePeriod@plt>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
movzbl -0x25(%rbp),%edx
call   1110 <encodeData@plt>
mov    %al,-0x25(%rbp)
xorps  %xmm0,%xmm0
movzbl -0x25(%rbp),%edi
call   10b0 <encodeByte@plt>
mov    $0xc8,%edi
xorps  %xmm0,%xmm0
call   1070 <onePeriod@plt>
mov    $0x0,%al
call   1100 <silence@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    1307 <main+0x117>
mov    0x8(%rsi),%rdi
lea    0xe07(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     1328 <main+0x138>
mov    %rax,%rbx
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    %rbx,%rdi
call   10a0 <ftell@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %r15d,%edi
call   10c0 <malloc@plt>
mov    %rax,%r14
mov    $0x1,%ebp
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    %r15d,%eax
jne    1336 <main+0x146>
mov    0x2d63(%rip),%rbx
mov    (%rbx),%edi
lea    0xda3(%rip),%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    (%rbx),%edi
mov    %r15d,%ecx
add    $0x7ff,%ecx
lea    0xd93(%rip),%rsi
mov    $0x800,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    (%rbx),%edi
lea    0xd93(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
mov    $0xbb8,%edi
call   1040 <leaderTone@plt>
xorps  %xmm0,%xmm0
mov    $0x898,%edi
call   1070 <onePeriod@plt>
mov    %r14,%rdi
mov    %r15d,%esi
mov    $0xff,%edx
call   1110 <encodeData@plt>
movzbl %al,%edi
xorps  %xmm0,%xmm0
call   10b0 <encodeByte@plt>
xorps  %xmm0,%xmm0
mov    $0xc8,%edi
call   1070 <onePeriod@plt>
xor    %eax,%eax
call   1100 <silence@plt>
xor    %ebp,%ebp
jmp    131b <main+0x12b>
mov    (%rsi),%rsi
lea    0xcef(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xce6(%rip),%rdi
call   10f0 <perror@plt>
jmp    1316 <main+0x126>
lea    0xcde(%rip),%rdi
call   10f0 <perror@plt>
jmp    131b <main+0x12b>

LLVM IR (-O0):
@.str = [18 x i8] c"usage: %s <file>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [6 x i8] c"fopen\00"
@SEEK_END = global i32 0
@.str.3 = [6 x i8] c"fread\00"
@stderr = global i32 0
@.str.4 = [12 x i8] c"Length: %d\0A\00"
@.str.5 = [21 x i8] c"Load at: %.4x..%.4x\0A\00"
@.str.6 = [26 x i8] c"Press enter when ready...\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i8
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 %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %3
br label %67
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %7
%22 = load i32*, i32** %7
%23 = icmp ne i32* %22, null
br i1 %23, label %26, label %24
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %67
26: ; preds = %17
%27 = load i32*, i32** %7
%28 = load i32, i32* @SEEK_END
%29 = call i32 @fseek(i32* %27, i32 0, i32 %28)
%30 = load i32*, i32** %7
%31 = call i32 @ftell(i32* %30)
store i32 %31, i32* %8
%32 = load i32*, i32** %7
%33 = call i32 @rewind(i32* %32)
%34 = load i32, i32* %8
%35 = call i64 @malloc(i32 %34)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %6
%37 = load i8*, i8** %6
%38 = load i32, i32* %8
%39 = load i32*, i32** %7
%40 = call i32 @fread(i8* %37, i32 1, i32 %38, i32* %39)
%41 = load i32, i32* %8
%42 = icmp ne i32 %40, %41
br i1 %42, label %43, label %45
43: ; preds = %26
%44 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %67
45: ; preds = %26
%46 = load i32, i32* @stderr
%47 = load i32, i32* %8
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %47)
%49 = load i32, i32* @stderr
%50 = load i32, i32* %8
%51 = add nsw i32 2048, %50
%52 = sub nsw i32 %51, 1
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 2048, i32 %52)
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%56 = call i32 (...) @getchar()
store i8 -1, i8* %9
%57 = call i32 @leaderTone(i32 3000)
%58 = call i32 @onePeriod(i32 2200, double 0.000000e+00)
%59 = load i8*, i8** %6
%60 = load i32, i32* %8
%61 = load i8, i8* %9
%62 = call zeroext i8 @encodeData(i8* %59, i32 %60, i8 zeroext %61)
store i8 %62, i8* %9
%63 = load i8, i8* %9
%64 = call i32 @encodeByte(i8 zeroext %63, double 0.000000e+00)
%65 = call i32 @onePeriod(i32 200, double 0.000000e+00)
%66 = call i32 (...) @silence()
br label %67
67: ; preds = %45, %43, %24, %12
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@.str = [18 x i8] c"usage: %s <file>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [6 x i8] c"fopen\00"
@SEEK_END = global i32 0
@.str.3 = [6 x i8] c"fread\00"
@stderr = global i32 0
@.str.4 = [12 x i8] c"Length: %d\0A\00"
@.str.5 = [21 x i8] c"Load at: %.4x..%.4x\0A\00"
@.str.6 = [26 x i8] c"Press enter when ready...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %40
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %40
14: ; preds = %7
%15 = load i32, i32* @SEEK_END
%16 = tail call i32 @fseek(i32* %10, i32 0, i32 %15)
%17 = tail call i32 @ftell(i32* %10)
%18 = tail call i32 @rewind(i32* %10)
%19 = tail call i64 @malloc(i32 %17)
%20 = inttoptr i64 %19 to i8*
%21 = tail call i32 @fread(i8* %20, i32 1, i32 %17, i32* %10)
%22 = icmp eq i32 %21, %17
br i1 %22, label %25, label %23
23: ; preds = %14
%24 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %40
25: ; preds = %14
%26 = load i32, i32* @stderr
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %17)
%28 = load i32, i32* @stderr
%29 = add nsw i32 %17, 2047
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 2048, i32 %29)
%31 = load i32, i32* @stderr
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%33 = tail call i32 (...) @getchar()
%34 = tail call i32 @leaderTone(i32 3000)
%35 = tail call i32 @onePeriod(i32 2200, double 0.000000e+00)
%36 = tail call zeroext i8 @encodeData(i8* %20, i32 %17, i8 zeroext -1)
%37 = tail call i32 @encodeByte(i8 zeroext %36, double 0.000000e+00)
%38 = tail call i32 @onePeriod(i32 200, double 0.000000e+00)
%39 = tail call i32 (...) @silence()
br label %40
40: ; preds = %12, %25, %23, %4
%41 = phi i32 [ 1, %4 ], [ 1, %12 ], [ 1, %23 ], [ 0, %25 ]
ret i32 %41
}