#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 atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ patchheaders(int /*<<< orphan*/ *, unsigned int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ writedata_header(int /*<<< orphan*/ *);
int /*<<< orphan*/ writefmt_header(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ writeriff_header(int /*<<< orphan*/ *);
unsigned int writewave(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  FILE *samplefp;
  FILE *wavefp;
  int freq;
  unsigned int samples;
  if ((sizeof(unsigned int) != 4) || (sizeof(unsigned short) != 2)) {
    fprintf(stderr, "INTERNAL ERROR\n");
    exit(1);
  }
  if (argc != 4) {
    fprintf(stderr,
            "Usage: convertsamples <samplefile> <wavefile> <frequency>\n");
    fprintf(stderr,
            "       Converts the samples in samplefile to a WAV format file\n"
            "       with the specified sampling frequency\n");
    exit(1);
  }
  freq = atoi(argv[3]);
  samplefp = fopen(argv[1], "r");
  if (!samplefp) {
    perror("Opening sample file\n");
    exit(1);
  }
  wavefp = fopen(argv[2], "w");
  if (!samplefp) {
    perror("Opening wave file\n");
    exit(1);
  }
  writeriff_header(wavefp);
  writefmt_header(wavefp, freq);
  writedata_header(wavefp);
  samples = writewave(wavefp, samplefp);
  patchheaders(wavefp, samples);
  fclose(wavefp);
  fclose(samplefp);
  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   $0x4,-0x8(%rbp)
je     1204 <main+0x54>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe85(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    124d <main+0x9d>
lea    0xe6c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe63(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1286 <main+0xd6>
lea    0xe4a(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1050 <writeriff_header@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1070 <writefmt_header@plt>
mov    -0x20(%rbp),%rdi
call   1060 <writedata_header@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <writewave@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   10d0 <patchheaders@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x4,%edi
jne    1242 <main+0x92>
mov    %rsi,%rbp
mov    0x18(%rsi),%rdi
call   10b0 <atoi@plt>
mov    %eax,%r14d
mov    0x8(%rbp),%rdi
lea    0xed1(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     126f <main+0xbf>
mov    %rax,%rbx
mov    0x10(%rbp),%rdi
lea    0xecc(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <writeriff_header@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1070 <writefmt_header@plt>
mov    %rbp,%rdi
call   1060 <writedata_header@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1030 <writewave@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   10d0 <patchheaders@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d97(%rip),%rbx
mov    (%rbx),%edi
lea    0xdae(%rip),%rsi
call   1080 <fprintf@plt>
mov    (%rbx),%edi
lea    0xddb(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xe34(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [59 x i8] c"Usage: convertsamples <samplefile> <wavefile> <frequency>\0A\00"
@.str.1 = [109 x i8] c" Converts the samples in samplefile to a WAV format file\0A with the specified sampling frequency\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [21 x i8] c"Opening sample file\0A\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [19 x i8] c"Opening wave file\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
%9 = alloca i32
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, 4
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([109 x i8], [109 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 3
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %8
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %26, i32** %6
%27 = load i32*, i32** %6
%28 = icmp ne i32* %27, null
br i1 %28, label %32, label %29
29: ; preds = %18
%30 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %18
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = call i32* @fopen(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %36, i32** %7
%37 = load i32*, i32** %6
%38 = icmp ne i32* %37, null
br i1 %38, label %42, label %39
39: ; preds = %32
%40 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %32
%43 = load i32*, i32** %7
%44 = call i32 @writeriff_header(i32* %43)
%45 = load i32*, i32** %7
%46 = load i32, i32* %8
%47 = call i32 @writefmt_header(i32* %45, i32 %46)
%48 = load i32*, i32** %7
%49 = call i32 @writedata_header(i32* %48)
%50 = load i32*, i32** %7
%51 = load i32*, i32** %6
%52 = call i32 @writewave(i32* %50, i32* %51)
store i32 %52, i32* %9
%53 = load i32*, i32** %7
%54 = load i32, i32* %9
%55 = call i32 @patchheaders(i32* %53, i32 %54)
%56 = load i32*, i32** %7
%57 = call i32 @fclose(i32* %56)
%58 = load i32*, i32** %6
%59 = call i32 @fclose(i32* %58)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [59 x i8] c"Usage: convertsamples <samplefile> <wavefile> <frequency>\0A\00"
@.str.1 = [109 x i8] c" Converts the samples in samplefile to a WAV format file\0A with the specified sampling frequency\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [21 x i8] c"Opening sample file\0A\00"
@.str.4 = [2 x i8] c"w\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 4
br i1 %3, label %10, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([109 x i8], [109 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 3
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %10
%19 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %10
%22 = getelementptr inbounds i8*, i8** %1, i64 2
%23 = load i8*, i8** %22
%24 = tail call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%25 = tail call i32 @writeriff_header(i32* %24)
%26 = tail call i32 @writefmt_header(i32* %24, i32 %13)
%27 = tail call i32 @writedata_header(i32* %24)
%28 = tail call i32 @writewave(i32* %24, i32* %16)
%29 = tail call i32 @patchheaders(i32* %24, i32 %28)
%30 = tail call i32 @fclose(i32* %24)
%31 = tail call i32 @fclose(i32* %16)
ret i32 0
}