#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 */
scalar_t__ abs(int);
scalar_t__ ceil(double);
scalar_t__ diatonic(int);
scalar_t__ floor(double);
double round(double);
int nearestdiat(double pitch) {
  int temp1, temp2;
  temp1 = (int)floor(pitch + .5);
  temp2 = (int)ceil(pitch - .5);
  if ((round(pitch) == pitch) && diatonic((int)pitch))
    return (int)pitch;
  while (!diatonic(temp1))
    temp1--;
  while (!diatonic(temp2))
    temp2++;
  if (abs(temp1 - pitch) <= abs(temp2 - pitch))
    return temp1;
  return temp2;
}


x86 assembly (clang, -O0):
<nearestdiat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  0xe9b(%rip),%xmm0
addsd  -0x10(%rbp),%xmm0
call   1070 <floor@plt>
mov    %eax,-0x14(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xe81(%rip),%xmm1
subsd  %xmm1,%xmm0
call   1040 <ceil@plt>
mov    %eax,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1060 <round@plt>
ucomisd -0x10(%rbp),%xmm0
jne    11c7 <nearestdiat+0x77>
jp     11c7 <nearestdiat+0x77>
cvttsd2si -0x10(%rbp),%edi
call   1050 <diatonic@plt>
cmp    $0x0,%rax
je     11c7 <nearestdiat+0x77>
cvttsd2si -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1274 <nearestdiat+0x124>
jmp    11cc <nearestdiat+0x7c>
mov    -0x14(%rbp),%edi
call   1050 <diatonic@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11ea <nearestdiat+0x9a>
jmp    11f8 <nearestdiat+0xa8>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11cc <nearestdiat+0x7c>
jmp    11fd <nearestdiat+0xad>
mov    -0x18(%rbp),%edi
call   1050 <diatonic@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    121b <nearestdiat+0xcb>
jmp    1229 <nearestdiat+0xd9>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11fd <nearestdiat+0xad>
cvtsi2sdl -0x14(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
cvttsd2si %xmm0,%edi
call   1030 <abs@plt>
mov    %rax,-0x20(%rbp)
cvtsi2sdl -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
cvttsd2si %xmm0,%edi
call   1030 <abs@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
jg     126e <nearestdiat+0x11e>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1274 <nearestdiat+0x124>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nearestdiat>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movapd %xmm0,%xmm1
movsd  %xmm0,0x8(%rsp)
movsd  0xe96(%rip),%xmm0
addsd  %xmm1,%xmm0
call   1070 <floor@plt>
mov    %rax,%r14
movsd  0xe8a(%rip),%xmm0
addsd  0x8(%rsp),%xmm0
call   1040 <ceil@plt>
mov    %rax,%rbx
movsd  0x8(%rsp),%xmm0
call   1060 <round@plt>
movsd  0x8(%rsp),%xmm1
ucomisd %xmm1,%xmm0
jne    11c0 <nearestdiat+0x70>
jp     11c0 <nearestdiat+0x70>
cvttsd2si %xmm1,%ebp
mov    %ebp,%edi
call   1050 <diatonic@plt>
test   %rax,%rax
jne    122b <nearestdiat+0xdb>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14d,%edi
call   1050 <diatonic@plt>
add    $0xffffffff,%r14d
test   %rax,%rax
je     11c0 <nearestdiat+0x70>
add    $0x1,%r14d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
call   1050 <diatonic@plt>
add    $0x1,%ebx
test   %rax,%rax
je     11e0 <nearestdiat+0x90>
xorps  %xmm0,%xmm0
cvtsi2sd %r14d,%xmm0
subsd  0x8(%rsp),%xmm0
cvttsd2si %xmm0,%edi
call   1030 <abs@plt>
add    $0xffffffff,%ebx
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
mov    %rax,%rbp
subsd  0x8(%rsp),%xmm0
cvttsd2si %xmm0,%edi
call   1030 <abs@plt>
cmp    %rax,%rbp
cmovle %r14d,%ebx
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @nearestdiat(double %0) {
%2 = alloca i32
%3 = alloca double
%4 = alloca i32
%5 = alloca i32
store double %0, double* %3
%6 = load double, double* %3
%7 = fadd double %6, 5.000000e-01
%8 = call i64 @floor(double %7)
%9 = trunc i64 %8 to i32
store i32 %9, i32* %4
%10 = load double, double* %3
%11 = fsub double %10, 5.000000e-01
%12 = call i64 @ceil(double %11)
%13 = trunc i64 %12 to i32
store i32 %13, i32* %5
%14 = load double, double* %3
%15 = call double @llvm.round.f64(double %14)
%16 = load double, double* %3
%17 = fcmp oeq double %15, %16
br i1 %17, label %18, label %26
18: ; preds = %1
%19 = load double, double* %3
%20 = fptosi double %19 to i32
%21 = call i64 @diatonic(i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load double, double* %3
%25 = fptosi double %24 to i32
store i32 %25, i32* %2
br label %62
26: ; preds = %18, %1
br label %27
27: ; preds = %32, %26
%28 = load i32, i32* %4
%29 = call i64 @diatonic(i32 %28)
%30 = icmp ne i64 %29, 0
%31 = xor i1 %30, true
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = load i32, i32* %4
%34 = add nsw i32 %33, -1
store i32 %34, i32* %4
br label %27
35: ; preds = %27
br label %36
36: ; preds = %41, %35
%37 = load i32, i32* %5
%38 = call i64 @diatonic(i32 %37)
%39 = icmp ne i64 %38, 0
%40 = xor i1 %39, true
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i32, i32* %5
%43 = add nsw i32 %42, 1
store i32 %43, i32* %5
br label %36
44: ; preds = %36
%45 = load i32, i32* %4
%46 = sitofp i32 %45 to double
%47 = load double, double* %3
%48 = fsub double %46, %47
%49 = fptosi double %48 to i32
%50 = call i64 @abs(i32 %49)
%51 = load i32, i32* %5
%52 = sitofp i32 %51 to double
%53 = load double, double* %3
%54 = fsub double %52, %53
%55 = fptosi double %54 to i32
%56 = call i64 @abs(i32 %55)
%57 = icmp sle i64 %50, %56
br i1 %57, label %58, label %60
58: ; preds = %44
%59 = load i32, i32* %4
store i32 %59, i32* %2
br label %62
60: ; preds = %44
%61 = load i32, i32* %5
store i32 %61, i32* %2
br label %62
62: ; preds = %60, %58, %23
%63 = load i32, i32* %2
ret i32 %63
}

LLVM IR (-O2):
define i32 @nearestdiat(double %0) {
%2 = fadd double %0, 5.000000e-01
%3 = tail call i64 @floor(double %2)
%4 = trunc i64 %3 to i32
%5 = fadd double %0, -5.000000e-01
%6 = tail call i64 @ceil(double %5)
%7 = trunc i64 %6 to i32
%8 = tail call double @llvm.round.f64(double %0)
%9 = fcmp oeq double %8, %0
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = fptosi double %0 to i32
%12 = tail call i64 @diatonic(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %36
14: ; preds = %10, %1
br label %15
15: ; preds = %14, %15
%16 = phi i32 [ %19, %15 ], [ %4, %14 ]
%17 = tail call i64 @diatonic(i32 %16)
%18 = icmp eq i64 %17, 0
%19 = add nsw i32 %16, -1
br i1 %18, label %15, label %20
20: ; preds = %15, %20
%21 = phi i32 [ %24, %20 ], [ %7, %15 ]
%22 = tail call i64 @diatonic(i32 %21)
%23 = icmp eq i64 %22, 0
%24 = add nsw i32 %21, 1
br i1 %23, label %20, label %25
25: ; preds = %20
%26 = sitofp i32 %16 to double
%27 = fsub double %26, %0
%28 = fptosi double %27 to i32
%29 = tail call i64 @abs(i32 %28)
%30 = sitofp i32 %21 to double
%31 = fsub double %30, %0
%32 = fptosi double %31 to i32
%33 = tail call i64 @abs(i32 %32)
%34 = icmp sgt i64 %29, %33
%35 = select i1 %34, i32 %21, i32 %16
br label %36
36: ; preds = %25, %10
%37 = phi i32 [ %11, %10 ], [ %35, %25 ]
ret i32 %37
}