#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 */

/* Variables and functions */
int /*<<< orphan*/ putchar(unsigned char);
double onePeriod(int frequency, double inRemainder) {
  int numBytes = 44100 / (2 * frequency);
  double remainder =
      inRemainder +
      (double)(((double)(44100 / (double)(2 * frequency)) - numBytes) / 20);
  unsigned char value = 1;
  int i;
  for (i = 0; i < numBytes; i++) {
    putchar(value);
  }
  if (remainder > 1.0) {
    putchar(value);
  }
  value = -1;
  for (i = 0; i < numBytes; i++) {
    putchar(value);
  }
  if (remainder > 1.0) {
    putchar(value);
    remainder -= 1.0;
  }
  return remainder;
}


x86 assembly (clang, -O0):
<onePeriod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movsd  %xmm0,-0x10(%rbp)
mov    -0x4(%rbp),%ecx
shl    $0x1,%ecx
mov    $0xac44,%eax
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
movsd  -0x10(%rbp),%xmm0
mov    -0x4(%rbp),%eax
shl    $0x1,%eax
cvtsi2sd %eax,%xmm2
movsd  0xec0(%rip),%xmm1
divsd  %xmm2,%xmm1
cvtsi2sdl -0x14(%rbp),%xmm2
subsd  %xmm2,%xmm1
movsd  0xea3(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movb   $0x1,-0x21(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1198 <onePeriod+0x88>
movzbl -0x21(%rbp),%edi
call   1030 <putchar@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1175 <onePeriod+0x65>
movsd  -0x20(%rbp),%xmm0
movsd  0xe6b(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11b8 <onePeriod+0xa8>
movzbl -0x21(%rbp),%edi
call   1030 <putchar@plt>
movb   $0xff,-0x21(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11e6 <onePeriod+0xd6>
movzbl -0x21(%rbp),%edi
call   1030 <putchar@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11c3 <onePeriod+0xb3>
movsd  -0x20(%rbp),%xmm0
movsd  0xe1d(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    121c <onePeriod+0x10c>
movzbl -0x21(%rbp),%edi
call   1030 <putchar@plt>
movsd  -0x20(%rbp),%xmm0
movsd  0xdfd(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<onePeriod>:
push   %rbp
push   %rbx
push   %rax
add    %edi,%edi
mov    $0xac44,%eax
xor    %edx,%edx
idiv   %edi
mov    %eax,%ebx
cvtsi2sd %edi,%xmm1
movsd  0xed4(%rip),%xmm2
divsd  %xmm1,%xmm2
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
subsd  %xmm1,%xmm2
divsd  0xec5(%rip),%xmm2
addsd  %xmm0,%xmm2
movsd  %xmm2,(%rsp)
test   %eax,%eax
jle    116f <onePeriod+0x5f>
mov    %ebx,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x1,%edi
call   1030 <putchar@plt>
add    $0xffffffff,%ebp
jne    1160 <onePeriod+0x50>
movsd  (%rsp),%xmm0
ucomisd 0xe94(%rip),%xmm0
jbe    1188 <onePeriod+0x78>
mov    $0x1,%edi
call   1030 <putchar@plt>
test   %ebx,%ebx
jle    119f <onePeriod+0x8f>
nopl   0x0(%rax)
mov    $0xff,%edi
call   1030 <putchar@plt>
add    $0xffffffff,%ebx
jne    1190 <onePeriod+0x80>
movsd  (%rsp),%xmm0
ucomisd 0xe64(%rip),%xmm0
jbe    11c5 <onePeriod+0xb5>
mov    $0xff,%edi
call   1030 <putchar@plt>
movsd  (%rsp),%xmm0
addsd  0xe53(%rip),%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define double @onePeriod(i32 %0, double %1) {
%3 = alloca i32
%4 = alloca double
%5 = alloca i32
%6 = alloca double
%7 = alloca i8
%8 = alloca i32
store i32 %0, i32* %3
store double %1, double* %4
%9 = load i32, i32* %3
%10 = mul nsw i32 2, %9
%11 = sdiv i32 44100, %10
store i32 %11, i32* %5
%12 = load double, double* %4
%13 = load i32, i32* %3
%14 = mul nsw i32 2, %13
%15 = sitofp i32 %14 to double
%16 = fdiv double 4.410000e+04, %15
%17 = load i32, i32* %5
%18 = sitofp i32 %17 to double
%19 = fsub double %16, %18
%20 = fdiv double %19, 2.000000e+01
%21 = fadd double %12, %20
store double %21, double* %6
store i8 1, i8* %7
store i32 0, i32* %8
br label %22
22: ; preds = %29, %2
%23 = load i32, i32* %8
%24 = load i32, i32* %5
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %32
26: ; preds = %22
%27 = load i8, i8* %7
%28 = call i32 @putchar(i8 zeroext %27)
br label %29
29: ; preds = %26
%30 = load i32, i32* %8
%31 = add nsw i32 %30, 1
store i32 %31, i32* %8
br label %22
32: ; preds = %22
%33 = load double, double* %6
%34 = fcmp ogt double %33, 1.000000e+00
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i8, i8* %7
%37 = call i32 @putchar(i8 zeroext %36)
br label %38
38: ; preds = %35, %32
store i8 -1, i8* %7
store i32 0, i32* %8
br label %39
39: ; preds = %46, %38
%40 = load i32, i32* %8
%41 = load i32, i32* %5
%42 = icmp slt i32 %40, %41
br i1 %42, label %43, label %49
43: ; preds = %39
%44 = load i8, i8* %7
%45 = call i32 @putchar(i8 zeroext %44)
br label %46
46: ; preds = %43
%47 = load i32, i32* %8
%48 = add nsw i32 %47, 1
store i32 %48, i32* %8
br label %39
49: ; preds = %39
%50 = load double, double* %6
%51 = fcmp ogt double %50, 1.000000e+00
br i1 %51, label %52, label %57
52: ; preds = %49
%53 = load i8, i8* %7
%54 = call i32 @putchar(i8 zeroext %53)
%55 = load double, double* %6
%56 = fsub double %55, 1.000000e+00
store double %56, double* %6
br label %57
57: ; preds = %52, %49
%58 = load double, double* %6
ret double %58
}

LLVM IR (-O2):
define double @onePeriod(i32 %0, double %1) {
%3 = shl nsw i32 %0, 1
%4 = sdiv i32 44100, %3
%5 = sitofp i32 %3 to double
%6 = fdiv double 4.410000e+04, %5
%7 = sitofp i32 %4 to double
%8 = fsub double %6, %7
%9 = fdiv double %8, 2.000000e+01
%10 = fadd double %9, %1
%11 = icmp sgt i32 %4, 0
br i1 %11, label %12, label %17
12: ; preds = %2, %12
%13 = phi i32 [ %15, %12 ], [ 0, %2 ]
%14 = tail call i32 @putchar(i8 zeroext 1)
%15 = add nuw nsw i32 %13, 1
%16 = icmp eq i32 %15, %4
br i1 %16, label %17, label %12
17: ; preds = %12, %2
%18 = fcmp ogt double %10, 1.000000e+00
br i1 %18, label %19, label %21
19: ; preds = %17
%20 = tail call i32 @putchar(i8 zeroext 1)
br label %21
21: ; preds = %19, %17
br i1 %11, label %22, label %27
22: ; preds = %21, %22
%23 = phi i32 [ %25, %22 ], [ 0, %21 ]
%24 = tail call i32 @putchar(i8 zeroext -1)
%25 = add nuw nsw i32 %23, 1
%26 = icmp eq i32 %25, %4
br i1 %26, label %27, label %22
27: ; preds = %22, %21
br i1 %18, label %28, label %31
28: ; preds = %27
%29 = tail call i32 @putchar(i8 zeroext -1)
%30 = fadd double %10, -1.000000e+00
br label %31
31: ; preds = %28, %27
%32 = phi double [ %30, %28 ], [ %10, %27 ]
ret double %32
}