#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*/ printf(char *, int, int);
int gen_square_wave(int sample_rate, int frequency, int duration,
                    float amplitude) {
  int samples = sample_rate * duration / 1000;
  int tone_midpoint = sample_rate / frequency / 2;
  int sample = -(1 << (16 - 1)) * amplitude;
  int i;
  for (i = 0; i < samples; i++) {
    if (i % tone_midpoint == 0)
      sample = -sample;
    printf("%c%c", sample & 0xff, (sample >> 8) & 0xff);
  }
  return 0;
}


x86 assembly (clang, -O0):
<gen_square_wave>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
movss  %xmm0,-0x10(%rbp)
mov    -0x4(%rbp),%eax
imul   -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%eax
cltd
idivl  -0x8(%rbp)
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
movss  0xeae(%rip),%xmm0
mulss  -0x10(%rbp),%xmm0
cvttss2si %xmm0,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b8 <gen_square_wave+0xa8>
mov    -0x20(%rbp),%eax
cltd
idivl  -0x18(%rbp)
cmp    $0x0,%edx
jne    1189 <gen_square_wave+0x79>
xor    %eax,%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
and    $0xff,%esi
mov    -0x1c(%rbp),%edx
sar    $0x8,%edx
and    $0xff,%edx
lea    0xe5f(%rip),%rdi
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1165 <gen_square_wave+0x55>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_square_wave>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ecx
mov    %edi,%eax
imul   %edi,%ecx
cltd
idiv   %esi
cmp    $0x3e8,%ecx
jl     1195 <gen_square_wave+0x85>
movslq %ecx,%rcx
imul   $0x10624dd3,%rcx,%r15
mov    %r15,%rcx
shr    $0x3f,%rcx
sar    $0x26,%r15
add    %ecx,%r15d
mov    %eax,%r12d
shr    $0x1f,%r12d
add    %eax,%r12d
sar    %r12d
mulss  0xea9(%rip),%xmm0
cvttss2si %xmm0,%ebx
xor    %ebp,%ebp
lea    0xea0(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%eax
cltd
idiv   %r12d
mov    %ebx,%eax
neg    %eax
test   %edx,%edx
cmove  %eax,%ebx
movzbl %bl,%esi
movzbl %bh,%edx
mov    %r14,%rdi
call   1030 <printf@plt>
add    $0x1,%ebp
cmp    %ebp,%r15d
jne    1170 <gen_square_wave+0x60>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%c%c\00"
define i32 @gen_square_wave(i32 %0, i32 %1, i32 %2, float %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca float
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store float %3, float* %8
%13 = load i32, i32* %5
%14 = load i32, i32* %7
%15 = mul nsw i32 %13, %14
%16 = sdiv i32 %15, 1000
store i32 %16, i32* %9
%17 = load i32, i32* %5
%18 = load i32, i32* %6
%19 = sdiv i32 %17, %18
%20 = sdiv i32 %19, 2
store i32 %20, i32* %10
%21 = load float, float* %8
%22 = fmul float -3.276800e+04, %21
%23 = fptosi float %22 to i32
store i32 %23, i32* %11
store i32 0, i32* %12
br label %24
24: ; preds = %43, %4
%25 = load i32, i32* %12
%26 = load i32, i32* %9
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %46
28: ; preds = %24
%29 = load i32, i32* %12
%30 = load i32, i32* %10
%31 = srem i32 %29, %30
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i32, i32* %11
%35 = sub nsw i32 0, %34
store i32 %35, i32* %11
br label %36
36: ; preds = %33, %28
%37 = load i32, i32* %11
%38 = and i32 %37, 255
%39 = load i32, i32* %11
%40 = ashr i32 %39, 8
%41 = and i32 %40, 255
%42 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %38, i32 %41)
br label %43
43: ; preds = %36
%44 = load i32, i32* %12
%45 = add nsw i32 %44, 1
store i32 %45, i32* %12
br label %24
46: ; preds = %24
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"%c%c\00"
define i32 @gen_square_wave(i32 %0, i32 %1, i32 %2, float %3) {
%5 = mul nsw i32 %2, %0
%6 = sdiv i32 %5, 1000
%7 = sdiv i32 %0, %1
%8 = sdiv i32 %7, 2
%9 = icmp sgt i32 %5, 999
br i1 %9, label %10, label %26
10: ; preds = %4
%11 = fmul float %3, -3.276800e+04
%12 = fptosi float %11 to i32
br label %13
13: ; preds = %10, %13
%14 = phi i32 [ %24, %13 ], [ 0, %10 ]
%15 = phi i32 [ %19, %13 ], [ %12, %10 ]
%16 = srem i32 %14, %8
%17 = icmp eq i32 %16, 0
%18 = sub nsw i32 0, %15
%19 = select i1 %17, i32 %18, i32 %15
%20 = and i32 %19, 255
%21 = lshr i32 %19, 8
%22 = and i32 %21, 255
%23 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %20, i32 %22)
%24 = add nuw nsw i32 %14, 1
%25 = icmp eq i32 %24, %6
br i1 %25, label %26, label %13
26: ; preds = %13, %4
ret i32 0
}