#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*/ encode_isdn(short, char);
int /*<<< orphan*/ fputc(int /*<<< orphan*/, int /*<<< orphan*/ *);
double sin(double);
void write_tone(FILE *fp, double t1, double t2, int length, int fade_in,
                int fade_out, char law) {
  double x, s, t, fade;
  int i;
  i = 0;
  while (i < length) {
    if (i < fade_in)
      fade = (double)i / (double)fade_in;
    else
      fade = 1.0;
    if (length - 1 - i < fade_out)
      fade = ((double)length - 1.0 - (double)i) / (double)fade_out;
    s = (double)i / 8000.0 * (double)t1 * 3.1415927F * 2.0;
    t = (double)i / 8000.0 * (double)t2 * 3.1415927F * 2.0;
    x = sin(s) + sin(t);
    fputc(encode_isdn((short)(x * 6000.0 * fade), law), fp);
    i++;
  }
}


x86 assembly (clang, -O0):
<write_tone>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %r8b,%al
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
mov    %edx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %al,-0x25(%rbp)
movl   $0x0,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    128e <write_tone+0x15e>
mov    -0x4c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    118c <write_tone+0x5c>
cvtsi2sdl -0x4c(%rbp),%xmm0
cvtsi2sdl -0x20(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
jmp    1199 <write_tone+0x69>
movsd  0xe6c(%rip),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
sub    -0x4c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11d3 <write_tone+0xa3>
cvtsi2sdl -0x1c(%rbp),%xmm0
movsd  0xe48(%rip),%xmm1
subsd  %xmm1,%xmm0
cvtsi2sdl -0x4c(%rbp),%xmm1
subsd  %xmm1,%xmm0
cvtsi2sdl -0x24(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
cvtsi2sdl -0x4c(%rbp),%xmm0
movsd  0xe28(%rip),%xmm2
divsd  %xmm2,%xmm0
movsd  -0x10(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xe1b(%rip),%xmm1
mulsd  %xmm1,%xmm0
addsd  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
cvtsi2sdl -0x4c(%rbp),%xmm0
divsd  %xmm2,%xmm0
movsd  -0x18(%rbp),%xmm2
mulsd  %xmm2,%xmm0
mulsd  %xmm1,%xmm0
addsd  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x38(%rbp),%xmm0
call   1050 <sin@plt>
movsd  %xmm0,-0x58(%rbp)
movsd  -0x40(%rbp),%xmm0
call   1050 <sin@plt>
movaps %xmm0,%xmm1
movsd  -0x58(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  0xdc0(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  -0x48(%rbp),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
movswl %ax,%edi
movsbl -0x25(%rbp),%esi
call   1040 <encode_isdn@plt>
mov    %eax,%edi
mov    -0x8(%rbp),%rsi
call   1030 <fputc@plt>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    115c <write_tone+0x2c>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_tone>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
movsd  %xmm1,0x20(%rsp)
movsd  %xmm0,0x18(%rsp)
test   %esi,%esi
jle    1282 <write_tone+0x152>
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %esi,%ebx
mov    %rdi,%r12
xorps  %xmm0,%xmm0
cvtsi2sd %edx,%xmm0
movsd  %xmm0,0x10(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sd %esi,%xmm0
addsd  0xe87(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
movsd  %xmm0,(%rsp)
add    $0xffffffff,%ebx
xor    %ebp,%ebp
xorpd  %xmm0,%xmm0
movsd  0xe6c(%rip),%xmm1
movsbl %r8b,%r13d
jmp    1253 <write_tone+0x123>
cs nopw 0x0(%rax,%rax,1)
nop
movsd  %xmm1,0x38(%rsp)
movapd %xmm0,%xmm2
divsd  0xe4e(%rip),%xmm2
movsd  %xmm0,0x40(%rsp)
movapd %xmm2,%xmm0
mulsd  0x18(%rsp),%xmm0
movsd  0xe3e(%rip),%xmm1
mulsd  %xmm1,%xmm0
addsd  %xmm0,%xmm0
mulsd  0x20(%rsp),%xmm2
mulsd  %xmm1,%xmm2
addsd  %xmm2,%xmm2
movsd  %xmm2,0x28(%rsp)
call   1050 <sin@plt>
movsd  %xmm0,0x30(%rsp)
movsd  0x28(%rsp),%xmm0
call   1050 <sin@plt>
addsd  0x30(%rsp),%xmm0
mulsd  0xe06(%rip),%xmm0
mulsd  0x38(%rsp),%xmm0
cvttsd2si %xmm0,%eax
movswl %ax,%edi
mov    %r13d,%esi
call   1040 <encode_isdn@plt>
mov    %eax,%edi
mov    %r12,%rsi
call   1030 <fputc@plt>
movsd  0x40(%rsp),%xmm0
movsd  0xdc1(%rip),%xmm1
addsd  %xmm1,%xmm0
add    $0x1,%ebp
add    $0xffffffff,%ebx
jae    1282 <write_tone+0x152>
cmp    %r15d,%ebp
jge    1265 <write_tone+0x135>
xorps  %xmm1,%xmm1
cvtsi2sd %ebp,%xmm1
divsd  0x10(%rsp),%xmm1
cmp    %r14d,%ebx
jge    11b0 <write_tone+0x80>
movsd  0x8(%rsp),%xmm1
subsd  %xmm0,%xmm1
divsd  (%rsp),%xmm1
jmp    11b0 <write_tone+0x80>
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @write_tone(i32* %0, double %1, double %2, i32 %3, i32 %4, i32 %5, i8 signext %6) {
%8 = alloca i32*
%9 = alloca double
%10 = alloca double
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8
%15 = alloca double
%16 = alloca double
%17 = alloca double
%18 = alloca double
%19 = alloca i32
store i32* %0, i32** %8
store double %1, double* %9
store double %2, double* %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
store i8 %6, i8* %14
store i32 0, i32* %19
br label %20
20: ; preds = %52, %7
%21 = load i32, i32* %19
%22 = load i32, i32* %11
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %83
24: ; preds = %20
%25 = load i32, i32* %19
%26 = load i32, i32* %12
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %34
28: ; preds = %24
%29 = load i32, i32* %19
%30 = sitofp i32 %29 to double
%31 = load i32, i32* %12
%32 = sitofp i32 %31 to double
%33 = fdiv double %30, %32
store double %33, double* %18
br label %35
34: ; preds = %24
store double 1.000000e+00, double* %18
br label %35
35: ; preds = %34, %28
%36 = load i32, i32* %11
%37 = sub nsw i32 %36, 1
%38 = load i32, i32* %19
%39 = sub nsw i32 %37, %38
%40 = load i32, i32* %13
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %52
42: ; preds = %35
%43 = load i32, i32* %11
%44 = sitofp i32 %43 to double
%45 = fsub double %44, 1.000000e+00
%46 = load i32, i32* %19
%47 = sitofp i32 %46 to double
%48 = fsub double %45, %47
%49 = load i32, i32* %13
%50 = sitofp i32 %49 to double
%51 = fdiv double %48, %50
store double %51, double* %18
br label %52
52: ; preds = %42, %35
%53 = load i32, i32* %19
%54 = sitofp i32 %53 to double
%55 = fdiv double %54, 8.000000e+03
%56 = load double, double* %9
%57 = fmul double %55, %56
%58 = fmul double %57, 0x400921FB60000000
%59 = fmul double %58, 2.000000e+00
store double %59, double* %16
%60 = load i32, i32* %19
%61 = sitofp i32 %60 to double
%62 = fdiv double %61, 8.000000e+03
%63 = load double, double* %10
%64 = fmul double %62, %63
%65 = fmul double %64, 0x400921FB60000000
%66 = fmul double %65, 2.000000e+00
store double %66, double* %17
%67 = load double, double* %16
%68 = call double @sin(double %67)
%69 = load double, double* %17
%70 = call double @sin(double %69)
%71 = fadd double %68, %70
store double %71, double* %15
%72 = load double, double* %15
%73 = fmul double %72, 6.000000e+03
%74 = load double, double* %18
%75 = fmul double %73, %74
%76 = fptosi double %75 to i16
%77 = load i8, i8* %14
%78 = call i32 @encode_isdn(i16 signext %76, i8 signext %77)
%79 = load i32*, i32** %8
%80 = call i32 @fputc(i32 %78, i32* %79)
%81 = load i32, i32* %19
%82 = add nsw i32 %81, 1
store i32 %82, i32* %19
br label %20
83: ; preds = %20
ret void
}

LLVM IR (-O2):
define void @write_tone(i32* nocapture %0, double %1, double %2, i32 %3, i32 %4, i32 %5, i8 signext %6) {
%8 = icmp sgt i32 %3, 0
br i1 %8, label %9, label %48
9: ; preds = %7
%10 = sitofp i32 %4 to double
%11 = sitofp i32 %3 to double
%12 = fadd double %11, -1.000000e+00
%13 = sitofp i32 %5 to double
br label %14
14: ; preds = %9, %29
%15 = phi i32 [ 0, %9 ], [ %46, %29 ]
%16 = icmp slt i32 %15, %4
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = sitofp i32 %15 to double
%19 = fdiv double %18, %10
br label %20
20: ; preds = %14, %17
%21 = phi double [ %19, %17 ], [ 1.000000e+00, %14 ]
%22 = xor i32 %15, -1
%23 = add i32 %22, %3
%24 = icmp slt i32 %23, %5
%25 = sitofp i32 %15 to double
br i1 %24, label %26, label %29
26: ; preds = %20
%27 = fsub double %12, %25
%28 = fdiv double %27, %13
br label %29
29: ; preds = %20, %26
%30 = phi double [ %28, %26 ], [ %21, %20 ]
%31 = fdiv double %25, 8.000000e+03
%32 = fmul double %31, %1
%33 = fmul double %32, 0x400921FB60000000
%34 = fmul double %33, 2.000000e+00
%35 = fmul double %31, %2
%36 = fmul double %35, 0x400921FB60000000
%37 = fmul double %36, 2.000000e+00
%38 = tail call double @sin(double %34)
%39 = tail call double @sin(double %37)
%40 = fadd double %38, %39
%41 = fmul double %40, 6.000000e+03
%42 = fmul double %30, %41
%43 = fptosi double %42 to i16
%44 = tail call i32 @encode_isdn(i16 signext %43, i8 signext %6)
%45 = tail call i32 @fputc(i32 %44, i32* %0)
%46 = add nuw nsw i32 %15, 1
%47 = icmp eq i32 %46, %3
br i1 %47, label %48, label %14
48: ; preds = %29, %7
ret void
}