#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 */
double cos(double);
double sin(double);
double get_centroid(double xp, double xm, int dim) {
  if (xp == xm)
    return xp;
  if (dim == 1) {
    double x2_3 = xp * xp + xp * xm + xm * xm;
    return 3.0 * (xp * x2_3 + xm * xm * xm) / (4.0 * x2_3);
  } else if (dim == 2) {
    double cp = cos(xp);
    double sp = sin(xp);
    double cm = cos(xm);
    double sm = sin(xm);
    return (xm * cm - xp * cp + sp - sm) / (cm - cp);
  } else
    return 0.5 * (xp + xm);
}


x86 assembly (clang, -O0):
<get_centroid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
mov    %edi,-0x1c(%rbp)
movsd  -0x10(%rbp),%xmm0
ucomisd -0x18(%rbp),%xmm0
jne    115a <get_centroid+0x3a>
jp     115a <get_centroid+0x3a>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1295 <get_centroid+0x175>
cmpl   $0x1,-0x1c(%rbp)
jne    11df <get_centroid+0xbf>
movsd  -0x10(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm2
movsd  -0x10(%rbp),%xmm0
mulsd  -0x18(%rbp),%xmm0
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
movsd  -0x18(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm2
movsd  -0x18(%rbp),%xmm0
mulsd  -0x18(%rbp),%xmm0
mulsd  -0x18(%rbp),%xmm0
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
movsd  0xe50(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  0xe3c(%rip),%xmm1
mulsd  -0x28(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1295 <get_centroid+0x175>
cmpl   $0x2,-0x1c(%rbp)
jne    127a <get_centroid+0x15a>
movsd  -0x10(%rbp),%xmm0
call   1040 <cos@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1030 <sin@plt>
movsd  %xmm0,-0x38(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1040 <cos@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1030 <sin@plt>
movsd  %xmm0,-0x48(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  -0x40(%rbp),%xmm2
movsd  -0x10(%rbp),%xmm1
mulsd  -0x30(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
addsd  -0x38(%rbp),%xmm0
subsd  -0x48(%rbp),%xmm0
movsd  -0x40(%rbp),%xmm1
subsd  -0x30(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1295 <get_centroid+0x175>
movsd  -0x10(%rbp),%xmm1
addsd  -0x18(%rbp),%xmm1
movsd  0xd74(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_centroid>:
sub    $0x28,%rsp
ucomisd %xmm1,%xmm0
jne    1131 <get_centroid+0x11>
jp     1131 <get_centroid+0x11>
add    $0x28,%rsp
ret
cmp    $0x2,%edi
je     1184 <get_centroid+0x64>
cmp    $0x1,%edi
jne    1203 <get_centroid+0xe3>
movapd %xmm0,%xmm2
mulsd  %xmm1,%xmm2
movapd %xmm0,%xmm3
mulsd  %xmm0,%xmm3
addsd  %xmm2,%xmm3
movapd %xmm1,%xmm2
mulsd  %xmm1,%xmm2
addsd  %xmm2,%xmm3
mulsd  %xmm1,%xmm2
mulsd  %xmm3,%xmm0
addsd  %xmm2,%xmm0
mulsd  0xe8d(%rip),%xmm0
mulsd  0xe8d(%rip),%xmm3
divsd  %xmm3,%xmm0
add    $0x28,%rsp
ret
movsd  %xmm0,0x8(%rsp)
movsd  %xmm1,(%rsp)
call   1040 <cos@plt>
movsd  %xmm0,0x18(%rsp)
movsd  0x8(%rsp),%xmm0
call   1030 <sin@plt>
movsd  %xmm0,0x20(%rsp)
movsd  (%rsp),%xmm0
call   1040 <cos@plt>
movsd  %xmm0,0x10(%rsp)
movsd  (%rsp),%xmm0
call   1030 <sin@plt>
movsd  0x18(%rsp),%xmm2
movsd  0x8(%rsp),%xmm3
mulsd  %xmm2,%xmm3
movsd  (%rsp),%xmm1
movsd  0x10(%rsp),%xmm4
mulsd  %xmm4,%xmm1
subsd  %xmm3,%xmm1
addsd  0x20(%rsp),%xmm1
subsd  %xmm0,%xmm1
subsd  %xmm2,%xmm4
divsd  %xmm4,%xmm1
movapd %xmm1,%xmm0
add    $0x28,%rsp
ret
addsd  %xmm1,%xmm0
mulsd  0xe01(%rip),%xmm0
add    $0x28,%rsp
ret

LLVM IR (-O0):
define double @get_centroid(double %0, double %1, i32 %2) {
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca i32
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
store double %0, double* %5
store double %1, double* %6
store i32 %2, i32* %7
%13 = load double, double* %5
%14 = load double, double* %6
%15 = fcmp oeq double %13, %14
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = load double, double* %5
store double %17, double* %4
br label %75
18: ; preds = %3
%19 = load i32, i32* %7
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %43
21: ; preds = %18
%22 = load double, double* %5
%23 = load double, double* %5
%24 = load double, double* %5
%25 = load double, double* %6
%26 = fmul double %24, %25
%27 = call double @llvm.fmuladd.f64(double %22, double %23, double %26)
%28 = load double, double* %6
%29 = load double, double* %6
%30 = call double @llvm.fmuladd.f64(double %28, double %29, double %27)
store double %30, double* %8
%31 = load double, double* %5
%32 = load double, double* %8
%33 = load double, double* %6
%34 = load double, double* %6
%35 = fmul double %33, %34
%36 = load double, double* %6
%37 = fmul double %35, %36
%38 = call double @llvm.fmuladd.f64(double %31, double %32, double %37)
%39 = fmul double 3.000000e+00, %38
%40 = load double, double* %8
%41 = fmul double 4.000000e+00, %40
%42 = fdiv double %39, %41
store double %42, double* %4
br label %75
43: ; preds = %18
%44 = load i32, i32* %7
%45 = icmp eq i32 %44, 2
br i1 %45, label %46, label %70
46: ; preds = %43
%47 = load double, double* %5
%48 = call double @cos(double %47)
store double %48, double* %9
%49 = load double, double* %5
%50 = call double @sin(double %49)
store double %50, double* %10
%51 = load double, double* %6
%52 = call double @cos(double %51)
store double %52, double* %11
%53 = load double, double* %6
%54 = call double @sin(double %53)
store double %54, double* %12
%55 = load double, double* %6
%56 = load double, double* %11
%57 = load double, double* %5
%58 = load double, double* %9
%59 = fmul double %57, %58
%60 = fneg double %59
%61 = call double @llvm.fmuladd.f64(double %55, double %56, double %60)
%62 = load double, double* %10
%63 = fadd double %61, %62
%64 = load double, double* %12
%65 = fsub double %63, %64
%66 = load double, double* %11
%67 = load double, double* %9
%68 = fsub double %66, %67
%69 = fdiv double %65, %68
store double %69, double* %4
br label %75
70: ; preds = %43
%71 = load double, double* %5
%72 = load double, double* %6
%73 = fadd double %71, %72
%74 = fmul double 5.000000e-01, %73
store double %74, double* %4
br label %75
75: ; preds = %70, %46, %21, %16
%76 = load double, double* %4
ret double %76
}

LLVM IR (-O2):
define double @get_centroid(double %0, double %1, i32 %2) {
%4 = fcmp oeq double %0, %1
br i1 %4, label %31, label %5
5: ; preds = %3
switch i32 %2, label %28 [
i32 1, label %6
i32 2, label %16
]
6: ; preds = %5
%7 = fmul double %0, %1
%8 = tail call double @llvm.fmuladd.f64(double %0, double %0, double %7)
%9 = tail call double @llvm.fmuladd.f64(double %1, double %1, double %8)
%10 = fmul double %1, %1
%11 = fmul double %10, %1
%12 = tail call double @llvm.fmuladd.f64(double %0, double %9, double %11)
%13 = fmul double %12, 3.000000e+00
%14 = fmul double %9, 4.000000e+00
%15 = fdiv double %13, %14
br label %31
16: ; preds = %5
%17 = tail call double @cos(double %0)
%18 = tail call double @sin(double %0)
%19 = tail call double @cos(double %1)
%20 = tail call double @sin(double %1)
%21 = fneg double %17
%22 = fmul double %21, %0
%23 = tail call double @llvm.fmuladd.f64(double %1, double %19, double %22)
%24 = fadd double %18, %23
%25 = fsub double %24, %20
%26 = fsub double %19, %17
%27 = fdiv double %25, %26
br label %31
28: ; preds = %5
%29 = fadd double %0, %1
%30 = fmul double %29, 5.000000e-01
br label %31
31: ; preds = %3, %28, %16, %6
%32 = phi double [ %15, %6 ], [ %27, %16 ], [ %30, %28 ], [ %0, %3 ]
ret double %32
}