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

/* Variables and functions */
int /*<<< orphan*/ fwrite(void *, int, int, int /*<<< orphan*/ *);
void write_wave(FILE *wavfile, double dur, double freq) {
  static unsigned char lvl = 254;
  static unsigned long int rpos = 0;
  unsigned long int pos;
  double time, rtime, itv;
  itv = 0.5 / freq;
  pos = 0;
  time = 0.0;
  while (time < dur) {
    fwrite((void *)&lvl, sizeof(lvl), 1, wavfile);
    pos++;
    time = pos / (double)22050;
    rpos++;
    rtime = rpos / (double)22050;
    if (rtime > itv) {
      rpos = 0;
      if (lvl == 254)
        lvl = 2;
      else
        lvl = 254;
    }
  }
}


x86 assembly (clang, -O0):
<write_wave>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  0xed2(%rip),%xmm0
divsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movq   $0x0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    123f <write_wave+0x12f>
mov    -0x8(%rbp),%rcx
lea    0x2ec1(%rip),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fwrite@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
movq   -0x20(%rbp),%xmm0
movaps 0xe85(%rip),%xmm2
punpckldq %xmm2,%xmm0
movapd 0xe89(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm3
unpckhpd %xmm0,%xmm0
addsd  %xmm3,%xmm0
movsd  0xe5a(%rip),%xmm3
divsd  %xmm3,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    0x2e7a(%rip),%rax
add    $0x1,%rax
mov    %rax,0x2e6f(%rip)
movq   0x2e67(%rip),%xmm0
punpckldq %xmm2,%xmm0
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xe1c(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
ucomisd -0x38(%rbp),%xmm0
jbe    123a <write_wave+0x12a>
movq   $0x0,0x2e28(%rip)
movzbl 0x2e11(%rip),%eax
cmp    $0xfe,%eax
jne    122e <write_wave+0x11e>
movb   $0x2,0x2dff(%rip)
jmp    1235 <write_wave+0x125>
movb   $0xfe,0x2df3(%rip)
jmp    123a <write_wave+0x12a>
jmp    1148 <write_wave+0x38>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_wave>:
push   %r14
push   %rbx
sub    $0x18,%rsp
xorpd  %xmm2,%xmm2
movsd  %xmm0,0x10(%rsp)
ucomisd %xmm2,%xmm0
jbe    1208 <write_wave+0xf8>
mov    %rdi,%r14
movsd  0xeca(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,0x8(%rsp)
mov    $0x1,%ebx
jmp    1181 <write_wave+0x71>
nopw   0x0(%rax,%rax,1)
movq   %rbx,%xmm0
punpckldq %xmm2,%xmm0
subpd  %xmm3,%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
addsd  %xmm0,%xmm1
divsd  %xmm4,%xmm1
add    $0x1,%rbx
movsd  0x10(%rsp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1208 <write_wave+0xf8>
movsbl 0x2ea0(%rip),%edi
mov    %r14,%rsi
call   1030 <fputc@plt>
movsd  0xe70(%rip),%xmm4
movapd 0xe80(%rip),%xmm3
movdqa 0xe68(%rip),%xmm2
mov    0x2e89(%rip),%rax
add    $0x1,%rax
mov    %rax,0x2e7e(%rip)
movq   %rax,%xmm0
punpckldq %xmm2,%xmm0
subpd  %xmm3,%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
addsd  %xmm0,%xmm1
divsd  %xmm4,%xmm1
ucomisd 0x8(%rsp),%xmm1
jbe    1150 <write_wave+0x40>
movq   $0x0,0x2e4a(%rip)
cmpb   $0xfe,0x2e33(%rip)
sete   %al
shl    $0x2,%al
add    $0xfe,%al
mov    %al,0x2e25(%rip)
jmp    1150 <write_wave+0x40>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@write_wave.lvl = internal global i8 -2
@write_wave.rpos = internal global i64 0
define void @write_wave(i32* %0, double %1, double %2) {
%4 = alloca i32*
%5 = alloca double
%6 = alloca double
%7 = alloca i64
%8 = alloca double
%9 = alloca double
%10 = alloca double
store i32* %0, i32** %4
store double %1, double* %5
store double %2, double* %6
%11 = load double, double* %6
%12 = fdiv double 5.000000e-01, %11
store double %12, double* %10
store i64 0, i64* %7
store double 0.000000e+00, double* %8
br label %13
13: ; preds = %40, %3
%14 = load double, double* %8
%15 = load double, double* %5
%16 = fcmp olt double %14, %15
br i1 %16, label %17, label %41
17: ; preds = %13
%18 = load i32*, i32** %4
%19 = call i32 @fwrite(i8* @write_wave.lvl, i32 1, i32 1, i32* %18)
%20 = load i64, i64* %7
%21 = add i64 %20, 1
store i64 %21, i64* %7
%22 = load i64, i64* %7
%23 = uitofp i64 %22 to double
%24 = fdiv double %23, 2.205000e+04
store double %24, double* %8
%25 = load i64, i64* @write_wave.rpos
%26 = add i64 %25, 1
store i64 %26, i64* @write_wave.rpos
%27 = load i64, i64* @write_wave.rpos
%28 = uitofp i64 %27 to double
%29 = fdiv double %28, 2.205000e+04
store double %29, double* %9
%30 = load double, double* %9
%31 = load double, double* %10
%32 = fcmp ogt double %30, %31
br i1 %32, label %33, label %40
33: ; preds = %17
store i64 0, i64* @write_wave.rpos
%34 = load i8, i8* @write_wave.lvl
%35 = zext i8 %34 to i32
%36 = icmp eq i32 %35, 254
br i1 %36, label %37, label %38
37: ; preds = %33
store i8 2, i8* @write_wave.lvl
br label %39
38: ; preds = %33
store i8 -2, i8* @write_wave.lvl
br label %39
39: ; preds = %38, %37
br label %40
40: ; preds = %39, %17
br label %13
41: ; preds = %13
ret void
}

LLVM IR (-O2):
@write_wave.lvl = internal unnamed_addr global i8 -2
@write_wave.rpos = internal unnamed_addr global i64 0
define void @write_wave(i32* nocapture %0, double %1, double %2) {
%4 = fdiv double 5.000000e-01, %2
%5 = fcmp ogt double %1, 0.000000e+00
br i1 %5, label %6, label %25
6: ; preds = %3, %23
%7 = phi i64 [ %11, %23 ], [ 0, %3 ]
%8 = load i8, i8* @write_wave.lvl
%9 = sext i8 %8 to i32
%10 = tail call i32 @fputc(i32 %9, i32* %0)
%11 = add i64 %7, 1
%12 = uitofp i64 %11 to double
%13 = fdiv double %12, 2.205000e+04
%14 = load i64, i64* @write_wave.rpos
%15 = add i64 %14, 1
store i64 %15, i64* @write_wave.rpos
%16 = uitofp i64 %15 to double
%17 = fdiv double %16, 2.205000e+04
%18 = fcmp ogt double %17, %4
br i1 %18, label %19, label %23
19: ; preds = %6
store i64 0, i64* @write_wave.rpos
%20 = load i8, i8* @write_wave.lvl
%21 = icmp eq i8 %20, -2
%22 = select i1 %21, i8 2, i8 -2
store i8 %22, i8* @write_wave.lvl
br label %23
23: ; preds = %19, %6
%24 = fcmp olt double %13, %1
br i1 %24, label %6, label %25
25: ; preds = %23, %3
ret void
}