#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 */
int /*<<< orphan*/ exit(int);
double floor(double);
int /*<<< orphan*/ printf(char *, double, double, double);
double reflect(double x, double a, double b) {
  int rounds = 0;
  double x0 = x, nroundsVirtual, small = 1e-50;
  if (b - a < small) {
    printf("\nimproper range x0=%.6g (%.6g, %.6g)\n", x0, a, b);
    exit(-1);
  }
  nroundsVirtual = floor((x - a) / (2 * (b - a)));
  x -= nroundsVirtual * 2 * (b - a);
  for (;; rounds++) {
    if (x < a)
      x = a * 2 - x;
    else if (x > b)
      x = b * 2 - x;
    else
      break;
  }
  return (x);
}


x86 assembly (clang, -O0):
<reflect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
movsd  %xmm2,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  0xea0(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x18(%rbp),%xmm1
subsd  -0x10(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11a3 <reflect+0x73>
movsd  -0x28(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm2
lea    0xe7c(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1040 <exit@plt>
movsd  -0x8(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm2
subsd  -0x10(%rbp),%xmm2
movsd  0xe49(%rip),%xmm1
mulsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
call   1050 <floor@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  0xe2f(%rip),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm2
subsd  -0x10(%rbp),%xmm2
movsd  -0x8(%rbp),%xmm1
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1258 <reflect+0x128>
movsd  -0x10(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
addsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12a5 <reflect+0x175>
movsd  -0x8(%rbp),%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    129b <reflect+0x16b>
movsd  -0x18(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
addsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12a0 <reflect+0x170>
jmp    12b8 <reflect+0x188>
jmp    12a5 <reflect+0x175>
jmp    12aa <reflect+0x17a>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1211 <reflect+0xe1>
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reflect>:
sub    $0x28,%rsp
movapd %xmm0,%xmm3
movsd  %xmm2,0x8(%rsp)
subsd  %xmm1,%xmm2
movsd  0xeb6(%rip),%xmm0
ucomisd %xmm2,%xmm0
ja     11c5 <reflect+0x95>
movapd %xmm3,%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm2,0x18(%rsp)
addsd  %xmm2,%xmm2
divsd  %xmm2,%xmm0
movsd  %xmm1,0x20(%rsp)
movsd  %xmm3,0x10(%rsp)
call   1050 <floor@plt>
movsd  0x20(%rsp),%xmm3
mulsd  0xe83(%rip),%xmm0
mulsd  0x18(%rsp),%xmm0
addsd  0x10(%rsp),%xmm0
movsd  0x8(%rsp),%xmm2
jmp    11ac <reflect+0x7c>
nopl   0x0(%rax)
addsd  %xmm1,%xmm1
subsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
ucomisd %xmm0,%xmm3
movapd %xmm3,%xmm1
ja     11a0 <reflect+0x70>
ucomisd %xmm2,%xmm0
movapd %xmm2,%xmm1
ja     11a0 <reflect+0x70>
add    $0x28,%rsp
ret
lea    0xe44(%rip),%rdi
movapd %xmm3,%xmm0
movsd  0x8(%rsp),%xmm2
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@.str = [38 x i8] c"\0Aimproper range x0=%.6g (%.6g, %.6g)\0A\00"
define double @reflect(double %0, double %1, double %2) {
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca i32
%8 = alloca double
%9 = alloca double
%10 = alloca double
store double %0, double* %4
store double %1, double* %5
store double %2, double* %6
store i32 0, i32* %7
%11 = load double, double* %4
store double %11, double* %8
store double 1.000000e-50, double* %10
%12 = load double, double* %6
%13 = load double, double* %5
%14 = fsub double %12, %13
%15 = load double, double* %10
%16 = fcmp olt double %14, %15
br i1 %16, label %17, label %23
17: ; preds = %3
%18 = load double, double* %8
%19 = load double, double* %5
%20 = load double, double* %6
%21 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), double %18, double %19, double %20)
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %3
%24 = load double, double* %4
%25 = load double, double* %5
%26 = fsub double %24, %25
%27 = load double, double* %6
%28 = load double, double* %5
%29 = fsub double %27, %28
%30 = fmul double 2.000000e+00, %29
%31 = fdiv double %26, %30
%32 = call double @llvm.floor.f64(double %31)
store double %32, double* %9
%33 = load double, double* %9
%34 = fmul double %33, 2.000000e+00
%35 = load double, double* %6
%36 = load double, double* %5
%37 = fsub double %35, %36
%38 = load double, double* %4
%39 = fneg double %34
%40 = call double @llvm.fmuladd.f64(double %39, double %37, double %38)
store double %40, double* %4
br label %41
41: ; preds = %62, %23
%42 = load double, double* %4
%43 = load double, double* %5
%44 = fcmp olt double %42, %43
br i1 %44, label %45, label %50
45: ; preds = %41
%46 = load double, double* %5
%47 = load double, double* %4
%48 = fneg double %47
%49 = call double @llvm.fmuladd.f64(double %46, double 2.000000e+00, double %48)
store double %49, double* %4
br label %61
50: ; preds = %41
%51 = load double, double* %4
%52 = load double, double* %6
%53 = fcmp ogt double %51, %52
br i1 %53, label %54, label %59
54: ; preds = %50
%55 = load double, double* %6
%56 = load double, double* %4
%57 = fneg double %56
%58 = call double @llvm.fmuladd.f64(double %55, double 2.000000e+00, double %57)
store double %58, double* %4
br label %60
59: ; preds = %50
br label %65
60: ; preds = %54
br label %61
61: ; preds = %60, %45
br label %62
62: ; preds = %61
%63 = load i32, i32* %7
%64 = add nsw i32 %63, 1
store i32 %64, i32* %7
br label %41
65: ; preds = %59
%66 = load double, double* %4
ret double %66
}

LLVM IR (-O2):
@.str = [38 x i8] c"\0Aimproper range x0=%.6g (%.6g, %.6g)\0A\00"
define double @reflect(double %0, double %1, double %2) {
%4 = fsub double %2, %1
%5 = fcmp olt double %4, 1.000000e-50
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), double %0, double %1, double %2)
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %3
%10 = fsub double %0, %1
%11 = fmul double %4, 2.000000e+00
%12 = fdiv double %10, %11
%13 = tail call double @llvm.floor.f64(double %12)
%14 = fmul double %13, -2.000000e+00
%15 = tail call double @llvm.fmuladd.f64(double %14, double %4, double %0)
br label %16
16: ; preds = %21, %9
%17 = phi double [ %15, %9 ], [ %24, %21 ]
%18 = fcmp olt double %17, %1
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = fcmp ogt double %17, %2
br i1 %20, label %21, label %25
21: ; preds = %19, %16
%22 = phi double [ %1, %16 ], [ %2, %19 ]
%23 = fneg double %17
%24 = tail call double @llvm.fmuladd.f64(double %22, double 2.000000e+00, double %23)
br label %16
25: ; preds = %19
ret double %17
}