#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 */
float sine(float);
float triangle(float input) {
  ;
  float dec = input - (float)((int)input);
  float output;
  if (sine(input) > 0.0f) {
    if (dec <= 0.50f)
      output = 0.0f + dec;
    else
      output = 1.0f - dec;
  } else if (sine(input) < 0.0f) {
    if (dec <= 0.50f)
      output = 0.0f - dec;
    else
      output = -1.0f + dec;
  } else {
    return 0.0f;
  }
  return 2.0f * output;
}


x86 assembly (clang, -O0):
<triangle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movss  %xmm0,-0x8(%rbp)
movss  -0x8(%rbp),%xmm0
cvttss2si -0x8(%rbp),%eax
cvtsi2ss %eax,%xmm1
subss  %xmm1,%xmm0
movss  %xmm0,-0xc(%rbp)
movss  -0x8(%rbp),%xmm0
call   1030 <sine@plt>
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jbe    1185 <triangle+0x75>
movss  0xeae(%rip),%xmm0
ucomiss -0xc(%rbp),%xmm0
jb     116e <triangle+0x5e>
xorps  %xmm0,%xmm0
addss  -0xc(%rbp),%xmm0
movss  %xmm0,-0x10(%rbp)
jmp    1180 <triangle+0x70>
movss  0xe92(%rip),%xmm0
subss  -0xc(%rbp),%xmm0
movss  %xmm0,-0x10(%rbp)
jmp    11eb <triangle+0xdb>
movss  -0x8(%rbp),%xmm0
call   1030 <sine@plt>
movaps %xmm0,%xmm1
xorps  %xmm0,%xmm0
ucomiss %xmm1,%xmm0
jbe    11d9 <triangle+0xc9>
movss  0xe5a(%rip),%xmm0
ucomiss -0xc(%rbp),%xmm0
jb     11c2 <triangle+0xb2>
xorps  %xmm0,%xmm0
subss  -0xc(%rbp),%xmm0
movss  %xmm0,-0x10(%rbp)
jmp    11d4 <triangle+0xc4>
movss  0xe3a(%rip),%xmm0
addss  -0xc(%rbp),%xmm0
movss  %xmm0,-0x10(%rbp)
jmp    11e6 <triangle+0xd6>
xorps  %xmm0,%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    11fd <triangle+0xed>
jmp    11eb <triangle+0xdb>
movss  0xe19(%rip),%xmm0
mulss  -0x10(%rbp),%xmm0
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<triangle>:
sub    $0x28,%rsp
cvttps2dq %xmm0,%xmm1
cvtdq2ps %xmm1,%xmm1
movaps %xmm0,%xmm2
subss  %xmm1,%xmm2
movaps %xmm2,(%rsp)
movaps %xmm0,0x10(%rsp)
call   1030 <sine@plt>
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jbe    1153 <triangle+0x43>
movss  0xec0(%rip),%xmm0
movaps (%rsp),%xmm1
ucomiss %xmm1,%xmm0
jae    1180 <triangle+0x70>
movss  0xeb7(%rip),%xmm2
jmp    118f <triangle+0x7f>
movaps 0x10(%rsp),%xmm0
call   1030 <sine@plt>
xorps  %xmm2,%xmm2
ucomiss %xmm0,%xmm2
jbe    1197 <triangle+0x87>
movss  0xe93(%rip),%xmm0
movaps (%rsp),%xmm1
ucomiss %xmm1,%xmm0
jae    118c <triangle+0x7c>
addss  0xe86(%rip),%xmm1
jmp    1187 <triangle+0x77>
xorps  %xmm0,%xmm0
addss  %xmm0,%xmm1
movaps %xmm1,%xmm2
jmp    1193 <triangle+0x83>
xorps  %xmm2,%xmm2
subss  %xmm1,%xmm2
addss  %xmm2,%xmm2
movaps %xmm2,%xmm0
add    $0x28,%rsp
ret

LLVM IR (-O0):
define float @triangle(float %0) {
%2 = alloca float
%3 = alloca float
%4 = alloca float
%5 = alloca float
store float %0, float* %3
%6 = load float, float* %3
%7 = load float, float* %3
%8 = fptosi float %7 to i32
%9 = sitofp i32 %8 to float
%10 = fsub float %6, %9
store float %10, float* %4
%11 = load float, float* %3
%12 = call float @sine(float %11)
%13 = fcmp ogt float %12, 0.000000e+00
br i1 %13, label %14, label %24
14: ; preds = %1
%15 = load float, float* %4
%16 = fcmp ole float %15, 5.000000e-01
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load float, float* %4
%19 = fadd float 0.000000e+00, %18
store float %19, float* %5
br label %23
20: ; preds = %14
%21 = load float, float* %4
%22 = fsub float 1.000000e+00, %21
store float %22, float* %5
br label %23
23: ; preds = %20, %17
br label %40
24: ; preds = %1
%25 = load float, float* %3
%26 = call float @sine(float %25)
%27 = fcmp olt float %26, 0.000000e+00
br i1 %27, label %28, label %38
28: ; preds = %24
%29 = load float, float* %4
%30 = fcmp ole float %29, 5.000000e-01
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load float, float* %4
%33 = fsub float 0.000000e+00, %32
store float %33, float* %5
br label %37
34: ; preds = %28
%35 = load float, float* %4
%36 = fadd float -1.000000e+00, %35
store float %36, float* %5
br label %37
37: ; preds = %34, %31
br label %39
38: ; preds = %24
store float 0.000000e+00, float* %2
br label %43
39: ; preds = %37
br label %40
40: ; preds = %39, %23
%41 = load float, float* %5
%42 = fmul float 2.000000e+00, %41
store float %42, float* %2
br label %43
43: ; preds = %40, %38
%44 = load float, float* %2
ret float %44
}

LLVM IR (-O2):
define float @triangle(float %0) {
%2 = fptosi float %0 to i32
%3 = sitofp i32 %2 to float
%4 = fsub float %0, %3
%5 = tail call float @sine(float %0)
%6 = fcmp ogt float %5, 0.000000e+00
br i1 %6, label %7, label %13
7: ; preds = %1
%8 = fcmp ugt float %4, 5.000000e-01
br i1 %8, label %11, label %9
9: ; preds = %7
%10 = fadd float %4, 0.000000e+00
br label %22
11: ; preds = %7
%12 = fsub float 1.000000e+00, %4
br label %22
13: ; preds = %1
%14 = tail call float @sine(float %0)
%15 = fcmp olt float %14, 0.000000e+00
br i1 %15, label %16, label %25
16: ; preds = %13
%17 = fcmp ugt float %4, 5.000000e-01
br i1 %17, label %20, label %18
18: ; preds = %16
%19 = fsub float 0.000000e+00, %4
br label %22
20: ; preds = %16
%21 = fadd float %4, -1.000000e+00
br label %22
22: ; preds = %20, %18, %9, %11
%23 = phi float [ %10, %9 ], [ %12, %11 ], [ %19, %18 ], [ %21, %20 ]
%24 = fmul float %23, 2.000000e+00
br label %25
25: ; preds = %13, %22
%26 = phi float [ %24, %22 ], [ 0.000000e+00, %13 ]
ret float %26
}