#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);
double fmod(double, double);
int /*<<< orphan*/ printf(char *, double, double, double);
double reflect(double x, double a, double b) {
  int side = 0;
  double n, e = 0, small = 1e-100;
  if (b - a < small) {
    printf("\nimproper range x0=%.6g (%.6g, %.6g)\n", x, a, b);
    exit(-1);
  }
  if (x < a) {
    e = a - x;
    side = 0;
  } else if (x > b) {
    e = x - b;
    side = 1;
  }
  if (e) {
    n = floor(e / (b - a));
    if (fmod(n, 2.0) > 0.1)
      side = 1 - side;
    e -= n * (b - a);
    x = (side ? b - e : a + e);
  }
  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)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0xe92(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x18(%rbp),%xmm1
subsd  -0x10(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11b1 <reflect+0x71>
movsd  -0x8(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm2
lea    0xe76(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
movsd  -0x8(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11e0 <reflect+0xa0>
movsd  -0x10(%rbp),%xmm0
subsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x1c(%rbp)
jmp    120b <reflect+0xcb>
movsd  -0x8(%rbp),%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    1206 <reflect+0xc6>
movsd  -0x8(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x1,-0x1c(%rbp)
jmp    120b <reflect+0xcb>
movsd  -0x30(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1228 <reflect+0xe8>
jp     1228 <reflect+0xe8>
jmp    12e3 <reflect+0x1a3>
movsd  -0x30(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
subsd  -0x10(%rbp),%xmm1
divsd  %xmm1,%xmm0
call   1060 <floor@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  0xdbe(%rip),%xmm1
call   1040 <fmod@plt>
movsd  0xda9(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1274 <reflect+0x134>
mov    $0x1,%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm2
subsd  -0x10(%rbp),%xmm2
movsd  -0x30(%rbp),%xmm1
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12ca <reflect+0x18a>
movsd  -0x18(%rbp),%xmm0
subsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
jmp    12d9 <reflect+0x199>
movsd  -0x10(%rbp),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reflect>:
push   %rbx
sub    $0x30,%rsp
movapd %xmm2,%xmm4
subsd  %xmm1,%xmm4
movsd  0xeab(%rip),%xmm3
ucomisd %xmm4,%xmm3
ja     120f <reflect+0xcf>
ucomisd %xmm0,%xmm1
jbe    1171 <reflect+0x31>
movapd %xmm1,%xmm5
subsd  %xmm0,%xmm5
xor    %ebx,%ebx
jmp    1188 <reflect+0x48>
ucomisd %xmm2,%xmm0
jbe    1209 <reflect+0xc9>
movapd %xmm0,%xmm5
subsd  %xmm2,%xmm5
mov    $0x1,%ebx
xorpd  %xmm3,%xmm3
ucomisd %xmm3,%xmm5
jne    1194 <reflect+0x54>
jnp    1209 <reflect+0xc9>
movsd  %xmm1,0x8(%rsp)
movsd  %xmm2,0x10(%rsp)
movapd %xmm5,%xmm0
divsd  %xmm4,%xmm0
movsd  %xmm4,0x20(%rsp)
movsd  %xmm5,0x28(%rsp)
call   1060 <floor@plt>
movsd  %xmm0,0x18(%rsp)
movsd  0xe41(%rip),%xmm1
call   1040 <fmod@plt>
movsd  0x28(%rsp),%xmm1
xor    %eax,%eax
ucomisd 0xe34(%rip),%xmm0
seta   %al
movsd  0x18(%rsp),%xmm0
mulsd  0x20(%rsp),%xmm0
subsd  %xmm0,%xmm1
cmp    %eax,%ebx
je     11ff <reflect+0xbf>
movsd  0x10(%rsp),%xmm0
subsd  %xmm1,%xmm0
jmp    1209 <reflect+0xc9>
addsd  0x8(%rsp),%xmm1
movapd %xmm1,%xmm0
add    $0x30,%rsp
pop    %rbx
ret
lea    0xe02(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1050 <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
store double 0.000000e+00, double* %9
store double 1.000000e-100, double* %10
%11 = load double, double* %6
%12 = load double, double* %5
%13 = fsub double %11, %12
%14 = load double, double* %10
%15 = fcmp olt double %13, %14
br i1 %15, label %16, label %22
16: ; preds = %3
%17 = load double, double* %4
%18 = load double, double* %5
%19 = load double, double* %6
%20 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), double %17, double %18, double %19)
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %3
%23 = load double, double* %4
%24 = load double, double* %5
%25 = fcmp olt double %23, %24
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = load double, double* %5
%28 = load double, double* %4
%29 = fsub double %27, %28
store double %29, double* %9
store i32 0, i32* %7
br label %39
30: ; preds = %22
%31 = load double, double* %4
%32 = load double, double* %6
%33 = fcmp ogt double %31, %32
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load double, double* %4
%36 = load double, double* %6
%37 = fsub double %35, %36
store double %37, double* %9
store i32 1, i32* %7
br label %38
38: ; preds = %34, %30
br label %39
39: ; preds = %38, %26
%40 = load double, double* %9
%41 = fcmp une double %40, 0.000000e+00
br i1 %41, label %42, label %75
42: ; preds = %39
%43 = load double, double* %9
%44 = load double, double* %6
%45 = load double, double* %5
%46 = fsub double %44, %45
%47 = fdiv double %43, %46
%48 = call double @llvm.floor.f64(double %47)
store double %48, double* %8
%49 = load double, double* %8
%50 = call double @fmod(double %49, double 2.000000e+00)
%51 = fcmp ogt double %50, 1.000000e-01
br i1 %51, label %52, label %55
52: ; preds = %42
%53 = load i32, i32* %7
%54 = sub nsw i32 1, %53
store i32 %54, i32* %7
br label %55
55: ; preds = %52, %42
%56 = load double, double* %8
%57 = load double, double* %6
%58 = load double, double* %5
%59 = fsub double %57, %58
%60 = load double, double* %9
%61 = fneg double %56
%62 = call double @llvm.fmuladd.f64(double %61, double %59, double %60)
store double %62, double* %9
%63 = load i32, i32* %7
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %69
65: ; preds = %55
%66 = load double, double* %6
%67 = load double, double* %9
%68 = fsub double %66, %67
br label %73
69: ; preds = %55
%70 = load double, double* %5
%71 = load double, double* %9
%72 = fadd double %70, %71
br label %73
73: ; preds = %69, %65
%74 = phi double [ %68, %65 ], [ %72, %69 ]
store double %74, double* %4
br label %75
75: ; preds = %73, %39
%76 = load double, double* %4
ret double %76
}

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-100
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 = fcmp olt double %0, %1
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = fsub double %1, %0
br label %17
13: ; preds = %9
%14 = fcmp ogt double %0, %2
br i1 %14, label %15, label %33
15: ; preds = %13
%16 = fsub double %0, %2
br label %17
17: ; preds = %15, %11
%18 = phi i32 [ 0, %11 ], [ 1, %15 ]
%19 = phi double [ %12, %11 ], [ %16, %15 ]
%20 = fcmp une double %19, 0.000000e+00
br i1 %20, label %21, label %33
21: ; preds = %17
%22 = fdiv double %19, %4
%23 = tail call double @llvm.floor.f64(double %22)
%24 = tail call double @fmod(double %23, double 2.000000e+00)
%25 = fcmp ogt double %24, 1.000000e-01
%26 = zext i1 %25 to i32
%27 = fneg double %23
%28 = tail call double @llvm.fmuladd.f64(double %27, double %4, double %19)
%29 = icmp eq i32 %18, %26
%30 = fsub double %2, %28
%31 = fadd double %28, %1
%32 = select i1 %29, double %31, double %30
br label %33
33: ; preds = %13, %21, %17
%34 = phi double [ %32, %21 ], [ %0, %17 ], [ %0, %13 ]
ret double %34
}