#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*/ assert(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ write_little_endian(unsigned int, unsigned int,
                                       int /*<<< orphan*/ *);
void write_wav(char *filename, unsigned long num_samples, short int *data,
               int s_rate) {
  FILE *wav_file;
  unsigned int sample_rate;
  unsigned int num_channels;
  unsigned int bytes_per_sample;
  unsigned int byte_rate;
  unsigned long i;
  num_channels = 1;
  bytes_per_sample = 2;
  if (s_rate <= 0)
    sample_rate = 44100;
  else
    sample_rate = (unsigned int)s_rate;
  byte_rate = sample_rate * num_channels * bytes_per_sample;
  wav_file = fopen(filename, "w");
  assert(wav_file);
  fwrite("RIFF", 1, 4, wav_file);
  write_little_endian(36 + bytes_per_sample * num_samples * num_channels, 4,
                      wav_file);
  fwrite("WAVE", 1, 4, wav_file);
  fwrite("fmt ", 1, 4, wav_file);
  write_little_endian(16, 4, wav_file);
  write_little_endian(1, 2, wav_file);
  write_little_endian(num_channels, 2, wav_file);
  write_little_endian(sample_rate, 4, wav_file);
  write_little_endian(byte_rate, 4, wav_file);
  write_little_endian(num_channels * bytes_per_sample, 2, wav_file);
  write_little_endian(8 * bytes_per_sample, 2, wav_file);
  fwrite("data", 1, 4, wav_file);
  write_little_endian(bytes_per_sample * num_samples * num_channels, 4,
                      wav_file);
  for (i = 0; i < num_samples; i++) {
    write_little_endian((unsigned int)(data[i]), bytes_per_sample, wav_file);
  }
  fclose(wav_file);
}


x86 assembly (clang, -O0):
<write_wav>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x1,-0x30(%rbp)
movl   $0x2,-0x34(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     118b <write_wav+0x3b>
movl   $0xac44,-0x2c(%rbp)
jmp    1191 <write_wav+0x41>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
imul   -0x30(%rbp),%eax
imul   -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <assert@plt>
mov    -0x28(%rbp),%rcx
lea    0xe3b(%rip),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
call   1070 <fwrite@plt>
mov    -0x34(%rbp),%eax
imul   -0x10(%rbp),%rax
mov    -0x30(%rbp),%ecx
imul   %rcx,%rax
add    $0x24,%rax
mov    %eax,%edi
mov    -0x28(%rbp),%rdx
mov    $0x4,%esi
call   1050 <write_little_endian@plt>
mov    -0x28(%rbp),%rcx
lea    0xe03(%rip),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
call   1070 <fwrite@plt>
mov    -0x28(%rbp),%rcx
lea    0xdee(%rip),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
call   1070 <fwrite@plt>
mov    -0x28(%rbp),%rdx
mov    $0x10,%edi
mov    $0x4,%esi
call   1050 <write_little_endian@plt>
mov    -0x28(%rbp),%rdx
mov    $0x1,%edi
mov    $0x2,%esi
call   1050 <write_little_endian@plt>
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    $0x2,%esi
call   1050 <write_little_endian@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    $0x4,%esi
call   1050 <write_little_endian@plt>
mov    -0x38(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    $0x4,%esi
call   1050 <write_little_endian@plt>
mov    -0x30(%rbp),%edi
imul   -0x34(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    $0x2,%esi
call   1050 <write_little_endian@plt>
mov    -0x34(%rbp),%edi
shl    $0x3,%edi
mov    -0x28(%rbp),%rdx
mov    $0x2,%esi
call   1050 <write_little_endian@plt>
mov    -0x28(%rbp),%rcx
lea    0xd57(%rip),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
call   1070 <fwrite@plt>
mov    -0x34(%rbp),%eax
imul   -0x10(%rbp),%rax
mov    -0x30(%rbp),%ecx
imul   %rcx,%rax
mov    %eax,%edi
mov    -0x28(%rbp),%rdx
mov    $0x4,%esi
call   1050 <write_little_endian@plt>
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1327 <write_wav+0x1d7>
mov    -0x18(%rbp),%rax
mov    -0x40(%rbp),%rcx
movswl (%rax,%rcx,2),%edi
mov    -0x34(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1050 <write_little_endian@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    12f0 <write_wav+0x1a0>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_wav>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
test   %ecx,%ecx
mov    $0xac44,%ebp
cmovg  %ecx,%ebp
lea    0x0(%rbp,%rbp,1),%r13d
lea    0xe89(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <assert@plt>
lea    0xe74(%rip),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
lea    (%r15,%r15,1),%r12d
lea    (%r15,%r15,1),%edi
add    $0x24,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1050 <write_little_endian@plt>
lea    0xe48(%rip),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
lea    0xe34(%rip),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    $0x10,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1050 <write_little_endian@plt>
mov    $0x1,%edi
mov    $0x2,%esi
mov    %rbx,%rdx
call   1050 <write_little_endian@plt>
mov    $0x1,%edi
mov    $0x2,%esi
mov    %rbx,%rdx
call   1050 <write_little_endian@plt>
mov    %ebp,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1050 <write_little_endian@plt>
mov    %r13d,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1050 <write_little_endian@plt>
mov    $0x2,%edi
mov    $0x2,%esi
mov    %rbx,%rdx
call   1050 <write_little_endian@plt>
mov    $0x10,%edi
mov    $0x2,%esi
mov    %rbx,%rdx
call   1050 <write_little_endian@plt>
lea    0xda7(%rip),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %r12d,%edi
mov    $0x4,%esi
mov    %rbx,%rdx
call   1050 <write_little_endian@plt>
test   %r15,%r15
je     12bb <write_wav+0x16b>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movswl (%r14,%rbp,2),%edi
mov    $0x2,%esi
mov    %rbx,%rdx
call   1050 <write_little_endian@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
jne    12a0 <write_wav+0x150>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [5 x i8] c"RIFF\00"
@.str.2 = [5 x i8] c"WAVE\00"
@.str.3 = [5 x i8] c"fmt \00"
@.str.4 = [5 x i8] c"data\00"
define void @write_wav(i8* %0, i64 %1, i16* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i16*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i16* %2, i16** %7
store i32 %3, i32* %8
store i32 1, i32* %11
store i32 2, i32* %12
%15 = load i32, i32* %8
%16 = icmp sle i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 44100, i32* %10
br label %20
18: ; preds = %4
%19 = load i32, i32* %8
store i32 %19, i32* %10
br label %20
20: ; preds = %18, %17
%21 = load i32, i32* %10
%22 = load i32, i32* %11
%23 = mul i32 %21, %22
%24 = load i32, i32* %12
%25 = mul i32 %23, %24
store i32 %25, i32* %13
%26 = load i8*, i8** %5
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %27, i32** %9
%28 = load i32*, i32** %9
%29 = call i32 @assert(i32* %28)
%30 = load i32*, i32** %9
%31 = call i32 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 1, i32 4, i32* %30)
%32 = load i32, i32* %12
%33 = zext i32 %32 to i64
%34 = load i64, i64* %6
%35 = mul i64 %33, %34
%36 = load i32, i32* %11
%37 = zext i32 %36 to i64
%38 = mul i64 %35, %37
%39 = add i64 36, %38
%40 = trunc i64 %39 to i32
%41 = load i32*, i32** %9
%42 = call i32 @write_little_endian(i32 %40, i32 4, i32* %41)
%43 = load i32*, i32** %9
%44 = call i32 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 4, i32* %43)
%45 = load i32*, i32** %9
%46 = call i32 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 4, i32* %45)
%47 = load i32*, i32** %9
%48 = call i32 @write_little_endian(i32 16, i32 4, i32* %47)
%49 = load i32*, i32** %9
%50 = call i32 @write_little_endian(i32 1, i32 2, i32* %49)
%51 = load i32, i32* %11
%52 = load i32*, i32** %9
%53 = call i32 @write_little_endian(i32 %51, i32 2, i32* %52)
%54 = load i32, i32* %10
%55 = load i32*, i32** %9
%56 = call i32 @write_little_endian(i32 %54, i32 4, i32* %55)
%57 = load i32, i32* %13
%58 = load i32*, i32** %9
%59 = call i32 @write_little_endian(i32 %57, i32 4, i32* %58)
%60 = load i32, i32* %11
%61 = load i32, i32* %12
%62 = mul i32 %60, %61
%63 = load i32*, i32** %9
%64 = call i32 @write_little_endian(i32 %62, i32 2, i32* %63)
%65 = load i32, i32* %12
%66 = mul i32 8, %65
%67 = load i32*, i32** %9
%68 = call i32 @write_little_endian(i32 %66, i32 2, i32* %67)
%69 = load i32*, i32** %9
%70 = call i32 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 1, i32 4, i32* %69)
%71 = load i32, i32* %12
%72 = zext i32 %71 to i64
%73 = load i64, i64* %6
%74 = mul i64 %72, %73
%75 = load i32, i32* %11
%76 = zext i32 %75 to i64
%77 = mul i64 %74, %76
%78 = trunc i64 %77 to i32
%79 = load i32*, i32** %9
%80 = call i32 @write_little_endian(i32 %78, i32 4, i32* %79)
store i64 0, i64* %14
br label %81
81: ; preds = %94, %20
%82 = load i64, i64* %14
%83 = load i64, i64* %6
%84 = icmp ult i64 %82, %83
br i1 %84, label %85, label %97
85: ; preds = %81
%86 = load i16*, i16** %7
%87 = load i64, i64* %14
%88 = getelementptr inbounds i16, i16* %86, i64 %87
%89 = load i16, i16* %88
%90 = sext i16 %89 to i32
%91 = load i32, i32* %12
%92 = load i32*, i32** %9
%93 = call i32 @write_little_endian(i32 %90, i32 %91, i32* %92)
br label %94
94: ; preds = %85
%95 = load i64, i64* %14
%96 = add i64 %95, 1
store i64 %96, i64* %14
br label %81
97: ; preds = %81
%98 = load i32*, i32** %9
%99 = call i32 @fclose(i32* %98)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [5 x i8] c"RIFF\00"
@.str.2 = [5 x i8] c"WAVE\00"
@.str.3 = [5 x i8] c"fmt \00"
@.str.4 = [5 x i8] c"data\00"
define void @write_wav(i8* nocapture %0, i64 %1, i16* nocapture %2, i32 %3) {
%5 = icmp slt i32 %3, 1
%6 = select i1 %5, i32 44100, i32 %3
%7 = shl nuw i32 %6, 1
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @assert(i32* %8)
%10 = tail call i32 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 1, i32 4, i32* %8)
%11 = trunc i64 %1 to i32
%12 = shl i32 %11, 1
%13 = add i32 %12, 36
%14 = tail call i32 @write_little_endian(i32 %13, i32 4, i32* %8)
%15 = tail call i32 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 4, i32* %8)
%16 = tail call i32 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 4, i32* %8)
%17 = tail call i32 @write_little_endian(i32 16, i32 4, i32* %8)
%18 = tail call i32 @write_little_endian(i32 1, i32 2, i32* %8)
%19 = tail call i32 @write_little_endian(i32 1, i32 2, i32* %8)
%20 = tail call i32 @write_little_endian(i32 %6, i32 4, i32* %8)
%21 = tail call i32 @write_little_endian(i32 %7, i32 4, i32* %8)
%22 = tail call i32 @write_little_endian(i32 2, i32 2, i32* %8)
%23 = tail call i32 @write_little_endian(i32 16, i32 2, i32* %8)
%24 = tail call i32 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 1, i32 4, i32* %8)
%25 = tail call i32 @write_little_endian(i32 %12, i32 4, i32* %8)
%26 = icmp eq i64 %1, 0
br i1 %26, label %35, label %27
27: ; preds = %4, %27
%28 = phi i64 [ %33, %27 ], [ 0, %4 ]
%29 = getelementptr inbounds i16, i16* %2, i64 %28
%30 = load i16, i16* %29
%31 = sext i16 %30 to i32
%32 = tail call i32 @write_little_endian(i32 %31, i32 2, i32* %8)
%33 = add nuw i64 %28, 1
%34 = icmp eq i64 %33, %1
br i1 %34, label %35, label %27
35: ; preds = %27, %4
%36 = tail call i32 @fclose(i32* %8)
ret void
}