double __frem_for_triangle(double x, int *i) {
  double pi4;
  *i = 0;
  pi4 = 0.7853981;
  while (x - (*i) * pi4 >= 0)
    (*i) += 1;
  *i -= 1;
  return (x - (*i) * pi4);
}


x86 assembly (clang, -O0):
<__frem_for_triangle>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
movsd  0xee1(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x8(%rbp),%xmm1
mov    -0x10(%rbp),%rax
cvtsi2sdl (%rax),%xmm0
movsd  -0x18(%rbp),%xmm2
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     1172 <__frem_for_triangle+0x72>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    1124 <__frem_for_triangle+0x24>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
sub    $0x1,%ecx
mov    %ecx,(%rax)
movsd  -0x8(%rbp),%xmm1
mov    -0x10(%rbp),%rax
cvtsi2sdl (%rax),%xmm0
movsd  -0x18(%rbp),%xmm2
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<__frem_for_triangle>:
mov    $0xfffffffe,%eax
movsd  0xef3(%rip),%xmm1
xorpd  %xmm2,%xmm2
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0x2(%rax),%ecx
xorps  %xmm3,%xmm3
cvtsi2sd %ecx,%xmm3
mulsd  %xmm1,%xmm3
addsd  %xmm0,%xmm3
add    $0x1,%eax
ucomisd %xmm2,%xmm3
jae    1120 <__frem_for_triangle+0x20>
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
mov    %eax,(%rdi)
mulsd  0xeb4(%rip),%xmm1
addsd  %xmm1,%xmm0
ret

LLVM IR (-O0):
define double @__frem_for_triangle(double %0, i32* %1) {
%3 = alloca double
%4 = alloca i32*
%5 = alloca double
store double %0, double* %3
store i32* %1, i32** %4
%6 = load i32*, i32** %4
store i32 0, i32* %6
store double 0x3FE921FB323AE5AF, double* %5
br label %7
7: ; preds = %16, %2
%8 = load double, double* %3
%9 = load i32*, i32** %4
%10 = load i32, i32* %9
%11 = sitofp i32 %10 to double
%12 = load double, double* %5
%13 = fneg double %11
%14 = call double @llvm.fmuladd.f64(double %13, double %12, double %8)
%15 = fcmp oge double %14, 0.000000e+00
br i1 %15, label %16, label %20
16: ; preds = %7
%17 = load i32*, i32** %4
%18 = load i32, i32* %17
%19 = add nsw i32 %18, 1
store i32 %19, i32* %17
br label %7
20: ; preds = %7
%21 = load i32*, i32** %4
%22 = load i32, i32* %21
%23 = sub nsw i32 %22, 1
store i32 %23, i32* %21
%24 = load double, double* %3
%25 = load i32*, i32** %4
%26 = load i32, i32* %25
%27 = sitofp i32 %26 to double
%28 = load double, double* %5
%29 = fneg double %27
%30 = call double @llvm.fmuladd.f64(double %29, double %28, double %24)
ret double %30
}

LLVM IR (-O2):
define double @__frem_for_triangle(double %0, i32* nocapture writeonly %1) {
br label %3
3: ; preds = %3, %2
%4 = phi i32 [ 0, %2 ], [ %9, %3 ]
%5 = sitofp i32 %4 to double
%6 = fneg double %5
%7 = tail call double @llvm.fmuladd.f64(double %6, double 0x3FE921FB323AE5AF, double %0)
%8 = fcmp ult double %7, 0.000000e+00
%9 = add nuw nsw i32 %4, 1
br i1 %8, label %10, label %3
10: ; preds = %3
%11 = add nsw i32 %4, -1
store i32 %11, i32* %1
%12 = sitofp i32 %11 to double
%13 = fneg double %12
%14 = tail call double @llvm.fmuladd.f64(double %13, double 0x3FE921FB323AE5AF, double %0)
ret double %14
}