#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__ isnan(double);
int /*<<< orphan*/ printf(char *);
double weighted_angle_average(double a1, double a2, double weight) {
  if (a2 - a1 > 3.14159)
    a1 += 2 * 3.14159;
  if (a1 - a2 > 3.14159)
    a2 += 2 * 3.14159;
  double a3 = weight * a1 + (1 - weight) * a2;
  if (a3 > 3.14159)
    a3 -= 2 * 3.14159;
  if (isnan(a3)) {
    printf("IS NAN!!!\n");
    a3 = 0;
  }
  return a3;
}


x86 assembly (clang, -O0):
<weighted_angle_average>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
movsd  %xmm2,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x8(%rbp),%xmm0
movsd  0xeb7(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1165 <weighted_angle_average+0x45>
movsd  0xead(%rip),%xmm0
addsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  0xe89(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1193 <weighted_angle_average+0x73>
movsd  0xe7f(%rip),%xmm0
addsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm2
movsd  0xe6b(%rip),%xmm1
subsd  -0x18(%rbp),%xmm1
mulsd  -0x10(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  0xe37(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11e9 <weighted_angle_average+0xc9>
movsd  -0x20(%rbp),%xmm0
movsd  0xe28(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1040 <isnan@plt>
cmp    $0x0,%rax
je     1211 <weighted_angle_average+0xf1>
lea    0xe14(%rip),%rdi
call   1030 <printf@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<weighted_angle_average>:
sub    $0x18,%rsp
movapd %xmm1,%xmm4
subsd  %xmm0,%xmm4
movsd  0xecc(%rip),%xmm5
movapd %xmm0,%xmm6
addsd  %xmm5,%xmm6
movsd  0xec4(%rip),%xmm7
movapd %xmm7,%xmm3
cmpltsd %xmm4,%xmm3
andpd  %xmm3,%xmm6
andnpd %xmm0,%xmm3
orpd   %xmm6,%xmm3
movapd %xmm3,%xmm0
subsd  %xmm1,%xmm0
addsd  %xmm1,%xmm5
movapd %xmm7,%xmm4
cmpltsd %xmm0,%xmm4
andpd  %xmm4,%xmm5
andnpd %xmm1,%xmm4
orpd   %xmm5,%xmm4
movsd  0xe8e(%rip),%xmm0
subsd  %xmm2,%xmm0
mulsd  %xmm4,%xmm0
mulsd  %xmm2,%xmm3
addsd  %xmm0,%xmm3
movsd  0xe7e(%rip),%xmm0
addsd  %xmm3,%xmm0
cmpltsd %xmm3,%xmm7
andpd  %xmm7,%xmm0
andnpd %xmm3,%xmm7
orpd   %xmm0,%xmm7
movapd %xmm7,(%rsp)
movapd %xmm7,%xmm0
call   1040 <isnan@plt>
test   %rax,%rax
je     11d7 <weighted_angle_average+0xb7>
lea    0xe57(%rip),%rdi
call   1030 <puts@plt>
xorpd  %xmm0,%xmm0
add    $0x18,%rsp
ret
movaps (%rsp),%xmm0
add    $0x18,%rsp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"IS NAN!!!\0A\00"
define double @weighted_angle_average(double %0, double %1, double %2) {
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca double
store double %0, double* %4
store double %1, double* %5
store double %2, double* %6
%8 = load double, double* %5
%9 = load double, double* %4
%10 = fsub double %8, %9
%11 = fcmp ogt double %10, 3.141590e+00
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load double, double* %4
%14 = fadd double %13, 6.283180e+00
store double %14, double* %4
br label %15
15: ; preds = %12, %3
%16 = load double, double* %4
%17 = load double, double* %5
%18 = fsub double %16, %17
%19 = fcmp ogt double %18, 3.141590e+00
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load double, double* %5
%22 = fadd double %21, 6.283180e+00
store double %22, double* %5
br label %23
23: ; preds = %20, %15
%24 = load double, double* %6
%25 = load double, double* %4
%26 = load double, double* %6
%27 = fsub double 1.000000e+00, %26
%28 = load double, double* %5
%29 = fmul double %27, %28
%30 = call double @llvm.fmuladd.f64(double %24, double %25, double %29)
store double %30, double* %7
%31 = load double, double* %7
%32 = fcmp ogt double %31, 3.141590e+00
br i1 %32, label %33, label %36
33: ; preds = %23
%34 = load double, double* %7
%35 = fsub double %34, 6.283180e+00
store double %35, double* %7
br label %36
36: ; preds = %33, %23
%37 = load double, double* %7
%38 = call i64 @isnan(double %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store double 0.000000e+00, double* %7
br label %42
42: ; preds = %40, %36
%43 = load double, double* %7
ret double %43
}

LLVM IR (-O2):
@str = [10 x i8] c"IS NAN!!!\00"
define double @weighted_angle_average(double %0, double %1, double %2) {
%4 = fsub double %1, %0
%5 = fcmp ogt double %4, 3.141590e+00
%6 = fadd double %0, 6.283180e+00
%7 = select i1 %5, double %6, double %0
%8 = fsub double %7, %1
%9 = fcmp ogt double %8, 3.141590e+00
%10 = fadd double %1, 6.283180e+00
%11 = select i1 %9, double %10, double %1
%12 = fsub double 1.000000e+00, %2
%13 = fmul double %12, %11
%14 = tail call double @llvm.fmuladd.f64(double %2, double %7, double %13)
%15 = fcmp ogt double %14, 3.141590e+00
%16 = fadd double %14, -6.283180e+00
%17 = select i1 %15, double %16, double %14
%18 = tail call i64 @isnan(double %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %3
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %22
22: ; preds = %20, %3
%23 = phi double [ 0.000000e+00, %20 ], [ %17, %3 ]
ret double %23
}