#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*/ fwrite(void const *, int, int, int /*<<< orphan*/ *);
void tinywav(FILE *fp, short numChannels, short bitsPerSample, int sampleRateHz,
             const void *data, int size, int is_floating) {
  short bpsamp;
  int length, bpsec;
  fwrite("RIFF", 1, 4, fp);
  length = size + 44 - 8;
  fwrite(&length, 1, 4, fp);
  fwrite(is_floating ? "WAVEfmt \x10\x00\x00\x00\x03\x00"
                     : "WAVEfmt \x10\x00\x00\x00\x01\x00",
         1, 14, fp);
  fwrite(&numChannels, 1, 2, fp);
  fwrite(&sampleRateHz, 1, 4, fp);
  bpsec = numChannels * sampleRateHz * bitsPerSample / 8;
  fwrite(&bpsec, 1, 4, fp);
  bpsamp = numChannels * bitsPerSample / 8;
  fwrite(&bpsamp, 1, 2, fp);
  fwrite(&bitsPerSample, 1, 2, fp);
  fwrite("data", 1, 4, fp);
  fwrite(&size, 1, 4, fp);
  fwrite(data, 1, size, fp);
}


x86 assembly (clang, -O0):
<tinywav>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dx,%ax
mov    %si,%dx
mov    0x10(%rbp),%esi
mov    %rdi,-0x8(%rbp)
mov    %dx,-0xa(%rbp)
mov    %ax,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    %r8,-0x18(%rbp)
mov    %r9d,-0x1c(%rbp)
mov    -0x8(%rbp),%rcx
lea    0xebd(%rip),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fwrite@plt>
mov    -0x1c(%rbp),%eax
add    $0x2c,%eax
sub    $0x8,%eax
mov    %eax,-0x24(%rbp)
lea    -0x24(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fwrite@plt>
mov    0x10(%rbp),%ecx
lea    0xe9a(%rip),%rdi
lea    0xe84(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0xe,%edx
call   1030 <fwrite@plt>
lea    -0xa(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2,%edx
call   1030 <fwrite@plt>
lea    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fwrite@plt>
movswl -0xa(%rbp),%eax
imul   -0x10(%rbp),%eax
movswl -0xc(%rbp),%ecx
imul   %ecx,%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x28(%rbp)
lea    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fwrite@plt>
movswl -0xa(%rbp),%eax
movswl -0xc(%rbp),%ecx
imul   %ecx,%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %ax,-0x1e(%rbp)
lea    -0x1e(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2,%edx
call   1030 <fwrite@plt>
lea    -0xc(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2,%edx
call   1030 <fwrite@plt>
mov    -0x8(%rbp),%rcx
lea    0xdb6(%rip),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fwrite@plt>
lea    -0x1c(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fwrite@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fwrite@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tinywav>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %r9d,%ebp
mov    %r8,%r14
mov    %edx,%r15d
mov    %rdi,%rbx
mov    %si,0x2(%rsp)
mov    %r15w,0x6(%rsp)
mov    %ecx,0xc(%rsp)
mov    %r9d,0x8(%rsp)
lea    0xebf(%rip),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rbx,%rcx
call   1030 <fwrite@plt>
add    $0x24,%ebp
mov    %ebp,0x14(%rsp)
lea    0x14(%rsp),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rbx,%rcx
call   1030 <fwrite@plt>
cmpl   $0x0,0x40(%rsp)
lea    0xe9c(%rip),%rax
lea    0xe86(%rip),%rdi
cmove  %rax,%rdi
mov    $0x1,%esi
mov    $0xe,%edx
mov    %rbx,%rcx
call   1030 <fwrite@plt>
lea    0x2(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %rbx,%rcx
call   1030 <fwrite@plt>
lea    0xc(%rsp),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rbx,%rcx
call   1030 <fwrite@plt>
movswl 0x2(%rsp),%ebp
imul   %r15d,%ebp
mov    0xc(%rsp),%eax
imul   %ebp,%eax
lea    0x7(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x3,%ecx
mov    %ecx,0x10(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rbx,%rcx
call   1030 <fwrite@plt>
lea    0x7(%rbp),%eax
test   %ebp,%ebp
cmovns %ebp,%eax
shr    $0x3,%eax
mov    %ax,0x4(%rsp)
lea    0x4(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %rbx,%rcx
call   1030 <fwrite@plt>
lea    0x6(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %rbx,%rcx
call   1030 <fwrite@plt>
lea    0xdc2(%rip),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rbx,%rcx
call   1030 <fwrite@plt>
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rbx,%rcx
call   1030 <fwrite@plt>
mov    0x8(%rsp),%edx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fwrite@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"RIFF\00"
@.str.1 = [15 x i8] c"WAVEfmt \10\00\00\00\03\00\00"
@.str.2 = [15 x i8] c"WAVEfmt \10\00\00\00\01\00\00"
@.str.3 = [5 x i8] c"data\00"
define void @tinywav(i32* %0, i16 signext %1, i16 signext %2, i32 %3, i8* %4, i32 %5, i32 %6) {
%8 = alloca i32*
%9 = alloca i16
%10 = alloca i16
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i16
%16 = alloca i32
%17 = alloca i32
store i32* %0, i32** %8
store i16 %1, i16* %9
store i16 %2, i16* %10
store i32 %3, i32* %11
store i8* %4, i8** %12
store i32 %5, i32* %13
store i32 %6, i32* %14
%18 = load i32*, i32** %8
%19 = call i32 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 1, i32 4, i32* %18)
%20 = load i32, i32* %13
%21 = add nsw i32 %20, 44
%22 = sub nsw i32 %21, 8
store i32 %22, i32* %16
%23 = bitcast i32* %16 to i8*
%24 = load i32*, i32** %8
%25 = call i32 @fwrite(i8* %23, i32 1, i32 4, i32* %24)
%26 = load i32, i32* %14
%27 = icmp ne i32 %26, 0
%28 = zext i1 %27 to i64
%29 = select i1 %27, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0)
%30 = load i32*, i32** %8
%31 = call i32 @fwrite(i8* %29, i32 1, i32 14, i32* %30)
%32 = bitcast i16* %9 to i8*
%33 = load i32*, i32** %8
%34 = call i32 @fwrite(i8* %32, i32 1, i32 2, i32* %33)
%35 = bitcast i32* %11 to i8*
%36 = load i32*, i32** %8
%37 = call i32 @fwrite(i8* %35, i32 1, i32 4, i32* %36)
%38 = load i16, i16* %9
%39 = sext i16 %38 to i32
%40 = load i32, i32* %11
%41 = mul nsw i32 %39, %40
%42 = load i16, i16* %10
%43 = sext i16 %42 to i32
%44 = mul nsw i32 %41, %43
%45 = sdiv i32 %44, 8
store i32 %45, i32* %17
%46 = bitcast i32* %17 to i8*
%47 = load i32*, i32** %8
%48 = call i32 @fwrite(i8* %46, i32 1, i32 4, i32* %47)
%49 = load i16, i16* %9
%50 = sext i16 %49 to i32
%51 = load i16, i16* %10
%52 = sext i16 %51 to i32
%53 = mul nsw i32 %50, %52
%54 = sdiv i32 %53, 8
%55 = trunc i32 %54 to i16
store i16 %55, i16* %15
%56 = bitcast i16* %15 to i8*
%57 = load i32*, i32** %8
%58 = call i32 @fwrite(i8* %56, i32 1, i32 2, i32* %57)
%59 = bitcast i16* %10 to i8*
%60 = load i32*, i32** %8
%61 = call i32 @fwrite(i8* %59, i32 1, i32 2, i32* %60)
%62 = load i32*, i32** %8
%63 = call i32 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 4, i32* %62)
%64 = bitcast i32* %13 to i8*
%65 = load i32*, i32** %8
%66 = call i32 @fwrite(i8* %64, i32 1, i32 4, i32* %65)
%67 = load i8*, i8** %12
%68 = load i32, i32* %13
%69 = load i32*, i32** %8
%70 = call i32 @fwrite(i8* %67, i32 1, i32 %68, i32* %69)
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"RIFF\00"
@.str.1 = [15 x i8] c"WAVEfmt \10\00\00\00\03\00\00"
@.str.2 = [15 x i8] c"WAVEfmt \10\00\00\00\01\00\00"
@.str.3 = [5 x i8] c"data\00"
define void @tinywav(i32* nocapture %0, i16 signext %1, i16 signext %2, i32 %3, i8* nocapture %4, i32 %5, i32 %6) {
%8 = alloca i16
%9 = alloca i16
%10 = alloca i32
%11 = alloca i32
%12 = alloca i16
%13 = alloca i32
%14 = alloca i32
store i16 %1, i16* %8
store i16 %2, i16* %9
store i32 %3, i32* %10
store i32 %5, i32* %11
%15 = bitcast i16* %12 to i8*
%16 = bitcast i32* %13 to i8*
%17 = bitcast i32* %14 to i8*
%18 = tail call i32 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 1, i32 4, i32* %0)
%19 = add nsw i32 %5, 36
store i32 %19, i32* %13
%20 = call i32 @fwrite(i8* %16, i32 1, i32 4, i32* %0)
%21 = icmp eq i32 %6, 0
%22 = select i1 %21, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0)
%23 = tail call i32 @fwrite(i8* %22, i32 1, i32 14, i32* %0)
%24 = bitcast i16* %8 to i8*
%25 = call i32 @fwrite(i8* %24, i32 1, i32 2, i32* %0)
%26 = bitcast i32* %10 to i8*
%27 = call i32 @fwrite(i8* %26, i32 1, i32 4, i32* %0)
%28 = load i16, i16* %8
%29 = sext i16 %28 to i32
%30 = load i32, i32* %10
%31 = sext i16 %2 to i32
%32 = mul nsw i32 %29, %31
%33 = mul i32 %32, %30
%34 = sdiv i32 %33, 8
store i32 %34, i32* %14
%35 = call i32 @fwrite(i8* %17, i32 1, i32 4, i32* %0)
%36 = sdiv i32 %32, 8
%37 = trunc i32 %36 to i16
store i16 %37, i16* %12
%38 = call i32 @fwrite(i8* %15, i32 1, i32 2, i32* %0)
%39 = bitcast i16* %9 to i8*
%40 = call i32 @fwrite(i8* %39, i32 1, i32 2, i32* %0)
%41 = tail call i32 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 4, i32* %0)
%42 = bitcast i32* %11 to i8*
%43 = call i32 @fwrite(i8* %42, i32 1, i32 4, i32* %0)
%44 = load i32, i32* %11
%45 = tail call i32 @fwrite(i8* %4, i32 1, i32 %44, i32* %0)
ret void
}