void get_test_sample(long int sample_rate, int sample_size,
                     unsigned char *sample) {
  const static unsigned char waveform[] = {
      128, 127, 126, 125, 124, 122, 120, 118, 116, 113, 111, 108, 105, 102, 99,
      96,  93,  90,  87,  83,  80,  77,  74,  70,  67,  64,  61,  58,  55,  52,
      49,  47,  44,  42,  40,  37,  36,  34,  32,  31,  29,  28,  27,  26,  26,
      25,  25,  25,  25,  25,  25,  26,  26,  27,  28,  29,  30,  31,  33,  34,
      36,  37,  39,  41,  42,  44,  46,  48,  50,  52,  54,  56,  58,  60,  62,
      64,  66,  68,  69,  71,  73,  74,  76,  77,  79,  80,  81,  82,  83,  84,
      85,  86,  86,  87,  87,  88,  88,  88,  88,  88,  88,  88,  87,  87,  86,
      86,  85,  85,  84,  83,  82,  81,  80,  79,  78,  77,  76,  75,  74,  72,
      71,  70,  69,  68,  66,  65,  64,  63,  62,  61,  60,  59,  58,  57,  56,
      55,  54,  54,  53,  52,  52,  51,  51,  50,  50,  50,  50,  50,  50,  50,
      50,  50,  50,  50,  50,  51,  51,  51,  52,  52,  53,  54,  54,  55,  55,
      56,  57,  57,  58,  59,  60,  60,  61,  62,  63,  63,  64,  65,  65,  66,
      67,  67,  68,  68,  69,  69,  70,  70,  71,  71,  71,  72,  72,  72,  72,
      73,  73,  73,  73,  73,  73,  73,  73,  72,  72,  72,  72,  72,  71,  71,
      71,  70,  70,  70,  69,  69,  68,  68,  68,  67,  67,  66,  66,  65,  65,
      64,  64,  64,  63,  63,  62,  62,  62,  61,  61,  61,  60,  60,  60,  60,
      59,  59,  59,  59,  59,  59,  59,  59,  59,  59,  59,  59,  59,  59,  59,
      59,  59,  59,  60,  60,  60,  60,  60,  61,  61,  61,  61,  62,  62,  62,
      62,  63,  63,  63,  63,  64,  64,  64,  65,  65,  65,  65,  65,  66,  66,
      66,  66,  66,  66,  67,  67,  67,  67,  67,  67,  67,  67,  67,  67,  67,
      67,  67,  67,  67,  67,  67,  67,  67,  67,  67,  66,  66,  66,  66,  66,
      66,  66,  65,  65,  65,  65,  65,  65,  64,  64,  64,  64,  64,  64,  64,
      63,  63,  63,  63,  63,  63,  63,  63,  62,  62,  62,  62,  62,  62,  62,
      62,  62,  62,  62,  62,  62,  62,  62,  62,  62,  62,  62,  62,  62,  62,
      63,  63,  63,  63,  63,  63,  63,  63,  63,  63,  63,  64,  64,  64,  64,
      64,  64,  64,  64,  64,  64,  64,  65,  65,  65,  65,  65,  65,  65,  65,
      65,  65,  65,  65,  65,  65,  65,  65,  65,  65,  65,  65,  65,  65,  65,
      65,  65,  65,  65,  65,  65,  65,  65,  65,  65,  65,  65,  65,  64,  64,
      64,  64,  64,  64,  64,  64,  64,  64,  64,  64,  64,  64,  64,  64,  64,
      64,  63,  63,  63,  63,  63,  63,  63,  63,  63,  63,  63,  63,  63,  63,
      63,  63,  63,  63,  63,  63,  63,  63,  63,  63,  63,  64,  64,  64,  64,
      64,  64,  64,  64,  64,  64,  64,  64,  64,  64,  64,  64,  64,  64,  64};
  long int sample_pt = 0;
  int sample_sign = 1;
  while (sample_size-- > 0) {
    if (sample_sign > 0)
      *sample++ = waveform[sample_pt / 20] + 127;
    else
      *sample++ = 128 - waveform[sample_pt / 20];
    sample_pt += (10000000L / sample_rate);
    while (sample_pt >= (20 * (sizeof(waveform) / sizeof(unsigned char)))) {
      sample_pt -= (20 * (sizeof(waveform) / sizeof(unsigned char)));
      sample_sign = -sample_sign;
    }
  }
  return;
}


x86 assembly (clang, -O0):
<get_test_sample>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
cmp    $0x0,%eax
jle    11ea <get_test_sample+0xea>
cmpl   $0x0,-0x24(%rbp)
jle    1173 <get_test_sample+0x73>
mov    -0x20(%rbp),%rax
mov    $0x14,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
lea    0xeac(%rip),%rax
movzbl (%rax,%rcx,1),%eax
add    $0x7f,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    11a9 <get_test_sample+0xa9>
mov    -0x20(%rbp),%rax
mov    $0x14,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
lea    0xe75(%rip),%rax
movzbl (%rax,%rcx,1),%ecx
mov    $0x80,%eax
sub    %ecx,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    $0x989680,%eax
cqto
idivq  -0x8(%rbp)
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x2580,-0x20(%rbp)
jb     11e5 <get_test_sample+0xe5>
mov    -0x20(%rbp),%rax
sub    $0x2580,%rax
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    11bc <get_test_sample+0xbc>
jmp    111e <get_test_sample+0x1e>
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_test_sample>:
push   %r14
push   %rbx
test   %esi,%esi
jle    1205 <get_test_sample+0x105>
mov    %rdx,%r10
mov    %rdi,%rax
shr    $0x20,%rax
je     1126 <get_test_sample+0x26>
mov    $0x989680,%eax
xor    %edx,%edx
idiv   %rdi
mov    %rax,%r8
jmp    1132 <get_test_sample+0x32>
mov    $0x989680,%eax
xor    %edx,%edx
div    %edi
mov    %eax,%r8d
lea    -0x2580(%r8),%r9
mov    $0x1,%ecx
xor    %ebx,%ebx
lea    0xeb9(%rip),%r11
movabs $0x6d3a06d3a06d3a07,%r14
06 3a 6d
jmp    1175 <get_test_sample+0x75>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    -0x1(%rsi),%eax
add    $0x1,%r10
mov    %rdi,%rbx
cmp    $0x2,%esi
mov    %eax,%esi
jl     1205 <get_test_sample+0x105>
movzwl %bx,%eax
imul   $0xcccd,%eax,%eax
shr    $0x14,%eax
mov    (%rax,%r11,1),%al
mov    $0x80,%dl
sub    %al,%dl
add    $0x7f,%al
test   %ecx,%ecx
movzbl %al,%eax
movzbl %dl,%edx
cmovg  %eax,%edx
mov    %dl,(%r10)
lea    (%r8,%rbx,1),%rdi
cmp    $0x2580,%rdi
jb     1160 <get_test_sample+0x60>
add    %r9,%rbx
mov    %rbx,%rax
mul    %r14
shr    $0xc,%edx
add    $0x1,%edx
and    $0x7,%rdx
je     11e0 <get_test_sample+0xe0>
imul   $0x2580,%rdx,%rdx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
neg    %ecx
add    $0x2580,%rax
cmp    %rax,%rdx
jne    11d0 <get_test_sample+0xd0>
sub    %rax,%rdi
cmp    $0x10680,%rbx
jb     1160 <get_test_sample+0x60>
nopl   (%rax)
add    $0xfffffffffffed400,%rdi
cmp    $0x257f,%rdi
ja     11f0 <get_test_sample+0xf0>
jmp    1160 <get_test_sample+0x60>
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@get_test_sample.waveform = internal constant [480 x i8] c"\80\7F~}|zxvtqolifc`]ZWSPMJFC@=:741/,*(%$\22 \1F\1D\1C\1B\1A\1A\19\19\19\19\19\19\1A\1A\1B\1C\1D\1E\1F!\22$%')*,.02468:<>@BDEGIJLMOPQRSTUVVWWXXXXXXXWWVVUUTSRQPONMLKJHGFEDBA@?>=<;:98766544332222222222223334456677899:;<<=>??@AABCCDDEEFFGGGHHHHIIIIIIIIHHHHHGGGFFFEEDDDCCBBAA@@@??>>>===<<<<;;;;;;;;;;;;;;;;;;<<<<<====>>>>????@@@AAAAABBBBBBCCCCCCCCCCCCCCCCCCCCCBBBBBBBAAAAAA@@@@@@@????????>>>>>>>>>>>>>>>>>>>>>>???????????@@@@@@@@@@@AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA@@@@@@@@@@@@@@@@@@?????????????????????????@@@@@@@@@@@@@@@@@@@"
define void @get_test_sample(i64 %0, i32 %1, i8* %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i64 %0, i64* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
store i64 0, i64* %7
store i32 1, i32* %8
br label %9
9: ; preds = %49, %3
%10 = load i32, i32* %5
%11 = add nsw i32 %10, -1
store i32 %11, i32* %5
%12 = icmp sgt i32 %10, 0
br i1 %12, label %13, label %50
13: ; preds = %9
%14 = load i32, i32* %8
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %26
16: ; preds = %13
%17 = load i64, i64* %7
%18 = sdiv i64 %17, 20
%19 = getelementptr inbounds [480 x i8], [480 x i8]* @get_test_sample.waveform, i64 0, i64 %18
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = add nsw i32 %21, 127
%23 = trunc i32 %22 to i8
%24 = load i8*, i8** %6
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %6
store i8 %23, i8* %24
br label %36
26: ; preds = %13
%27 = load i64, i64* %7
%28 = sdiv i64 %27, 20
%29 = getelementptr inbounds [480 x i8], [480 x i8]* @get_test_sample.waveform, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = zext i8 %30 to i32
%32 = sub nsw i32 128, %31
%33 = trunc i32 %32 to i8
%34 = load i8*, i8** %6
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %6
store i8 %33, i8* %34
br label %36
36: ; preds = %26, %16
%37 = load i64, i64* %4
%38 = sdiv i64 10000000, %37
%39 = load i64, i64* %7
%40 = add nsw i64 %39, %38
store i64 %40, i64* %7
br label %41
41: ; preds = %44, %36
%42 = load i64, i64* %7
%43 = icmp uge i64 %42, 9600
br i1 %43, label %44, label %49
44: ; preds = %41
%45 = load i64, i64* %7
%46 = sub i64 %45, 9600
store i64 %46, i64* %7
%47 = load i32, i32* %8
%48 = sub nsw i32 0, %47
store i32 %48, i32* %8
br label %41
49: ; preds = %41
br label %9
50: ; preds = %9
ret void
}

LLVM IR (-O2):
@get_test_sample.waveform = internal unnamed_addr constant [480 x i8] c"\80\7F~}|zxvtqolifc`]ZWSPMJFC@=:741/,*(%$\22 \1F\1D\1C\1B\1A\1A\19\19\19\19\19\19\1A\1A\1B\1C\1D\1E\1F!\22$%')*,.02468:<>@BDEGIJLMOPQRSTUVVWWXXXXXXXWWVVUUTSRQPONMLKJHGFEDBA@?>=<;:98766544332222222222223334456677899:;<<=>??@AABCCDDEEFFGGGHHHHIIIIIIIIHHHHHGGGFFFEEDDDCCBBAA@@@??>>>===<<<<;;;;;;;;;;;;;;;;;;<<<<<====>>>>????@@@AAAAABBBBBBCCCCCCCCCCCCCCCCCCCCCBBBBBBBAAAAAA@@@@@@@????????>>>>>>>>>>>>>>>>>>>>>>???????????@@@@@@@@@@@AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA@@@@@@@@@@@@@@@@@@?????????????????????????@@@@@@@@@@@@@@@@@@@"
define void @get_test_sample(i64 %0, i32 %1, i8* nocapture writeonly %2) {
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %54
5: ; preds = %3
%6 = sdiv i64 10000000, %0
%7 = add nsw i64 %6, -9600
br label %12
8: ; preds = %44, %50, %12
%9 = phi i64 [ %28, %12 ], [ %47, %44 ], [ %52, %50 ]
%10 = phi i32 [ %14, %12 ], [ %48, %44 ], [ %45, %50 ]
%11 = icmp sgt i32 %13, 1
br i1 %11, label %12, label %54
12: ; preds = %5, %8
%13 = phi i32 [ %1, %5 ], [ %17, %8 ]
%14 = phi i32 [ 1, %5 ], [ %10, %8 ]
%15 = phi i64 [ 0, %5 ], [ %9, %8 ]
%16 = phi i8* [ %2, %5 ], [ %27, %8 ]
%17 = add nsw i32 %13, -1
%18 = icmp sgt i32 %14, 0
%19 = trunc i64 %15 to i16
%20 = udiv i16 %19, 20
%21 = zext i16 %20 to i64
%22 = getelementptr inbounds [480 x i8], [480 x i8]* @get_test_sample.waveform, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = sub i8 -128, %23
%25 = add i8 %23, 127
%26 = select i1 %18, i8 %25, i8 %24
%27 = getelementptr inbounds i8, i8* %16, i64 1
store i8 %26, i8* %16
%28 = add nsw i64 %6, %15
%29 = icmp ugt i64 %28, 9599
br i1 %29, label %30, label %8
30: ; preds = %12
%31 = add i64 %7, %15
%32 = udiv i64 %31, 9600
%33 = add nuw nsw i64 %32, 1
%34 = and i64 %33, 7
%35 = icmp eq i64 %34, 0
br i1 %35, label %44, label %36
36: ; preds = %30, %36
%37 = phi i32 [ %41, %36 ], [ %14, %30 ]
%38 = phi i64 [ %40, %36 ], [ %28, %30 ]
%39 = phi i64 [ %42, %36 ], [ 0, %30 ]
%40 = add i64 %38, -9600
%41 = sub nsw i32 0, %37
%42 = add i64 %39, 1
%43 = icmp eq i64 %42, %34
br i1 %43, label %44, label %36
44: ; preds = %36, %30
%45 = phi i32 [ %14, %30 ], [ %41, %36 ]
%46 = phi i64 [ %28, %30 ], [ %40, %36 ]
%47 = phi i64 [ undef, %30 ], [ %40, %36 ]
%48 = phi i32 [ undef, %30 ], [ %41, %36 ]
%49 = icmp ult i64 %31, 67200
br i1 %49, label %8, label %50
50: ; preds = %44, %50
%51 = phi i64 [ %52, %50 ], [ %46, %44 ]
%52 = add i64 %51, -76800
%53 = icmp ugt i64 %52, 9599
br i1 %53, label %50, label %8
54: ; preds = %8, %3
ret void
}