#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 abs(double);
double ceil(double);
double adjust_to_range(double value, double min, double max) {
  double range = max - min;
  if (value < min) {
    double needed_additions = ceil(abs(min - value) / range);
    return value + (range * needed_additions);
  } else if (value >= max) {
    double needed_substractions = ceil(abs(value - max) / range);
    return value - (range * needed_substractions);
  } else {
    return value;
  }
}


x86 assembly (clang, -O0):
<adjust_to_range>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  %xmm2,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1199 <adjust_to_range+0x79>
movsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
call   1030 <abs@plt>
divsd  -0x28(%rbp),%xmm0
call   1040 <ceil@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x10(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1209 <adjust_to_range+0xe9>
movsd  -0x10(%rbp),%xmm0
ucomisd -0x20(%rbp),%xmm0
jb     11ff <adjust_to_range+0xdf>
movsd  -0x10(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
call   1030 <abs@plt>
divsd  -0x28(%rbp),%xmm0
call   1040 <ceil@plt>
movsd  %xmm0,-0x38(%rbp)
movsd  -0x10(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
movsd  -0x38(%rbp),%xmm2
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1209 <adjust_to_range+0xe9>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<adjust_to_range>:
sub    $0x18,%rsp
movapd %xmm2,%xmm3
subsd  %xmm1,%xmm3
ucomisd %xmm0,%xmm1
jbe    1167 <adjust_to_range+0x47>
subsd  %xmm0,%xmm1
movsd  %xmm0,0x10(%rsp)
movapd %xmm1,%xmm0
movsd  %xmm3,0x8(%rsp)
call   1030 <abs@plt>
divsd  0x8(%rsp),%xmm0
call   1040 <ceil@plt>
mulsd  0x8(%rsp),%xmm0
addsd  0x10(%rsp),%xmm0
add    $0x18,%rsp
ret
ucomisd %xmm2,%xmm0
jb     11a1 <adjust_to_range+0x81>
movsd  %xmm0,0x10(%rsp)
subsd  %xmm2,%xmm0
movsd  %xmm3,0x8(%rsp)
call   1030 <abs@plt>
divsd  0x8(%rsp),%xmm0
call   1040 <ceil@plt>
mulsd  0x8(%rsp),%xmm0
movsd  0x10(%rsp),%xmm1
subsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x18,%rsp
ret

LLVM IR (-O0):
define double @adjust_to_range(double %0, double %1, double %2) {
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca double
store double %0, double* %5
store double %1, double* %6
store double %2, double* %7
%11 = load double, double* %7
%12 = load double, double* %6
%13 = fsub double %11, %12
store double %13, double* %8
%14 = load double, double* %5
%15 = load double, double* %6
%16 = fcmp olt double %14, %15
br i1 %16, label %17, label %29
17: ; preds = %3
%18 = load double, double* %6
%19 = load double, double* %5
%20 = fsub double %18, %19
%21 = call double @abs(double %20)
%22 = load double, double* %8
%23 = fdiv double %21, %22
%24 = call double @llvm.ceil.f64(double %23)
store double %24, double* %9
%25 = load double, double* %5
%26 = load double, double* %8
%27 = load double, double* %9
%28 = call double @llvm.fmuladd.f64(double %26, double %27, double %25)
store double %28, double* %4
br label %48
29: ; preds = %3
%30 = load double, double* %5
%31 = load double, double* %7
%32 = fcmp oge double %30, %31
br i1 %32, label %33, label %46
33: ; preds = %29
%34 = load double, double* %5
%35 = load double, double* %7
%36 = fsub double %34, %35
%37 = call double @abs(double %36)
%38 = load double, double* %8
%39 = fdiv double %37, %38
%40 = call double @llvm.ceil.f64(double %39)
store double %40, double* %10
%41 = load double, double* %5
%42 = load double, double* %8
%43 = load double, double* %10
%44 = fneg double %42
%45 = call double @llvm.fmuladd.f64(double %44, double %43, double %41)
store double %45, double* %4
br label %48
46: ; preds = %29
%47 = load double, double* %5
store double %47, double* %4
br label %48
48: ; preds = %46, %33, %17
%49 = load double, double* %4
ret double %49
}

LLVM IR (-O2):
define double @adjust_to_range(double %0, double %1, double %2) {
%4 = fsub double %2, %1
%5 = fcmp olt double %0, %1
br i1 %5, label %6, label %12
6: ; preds = %3
%7 = fsub double %1, %0
%8 = tail call double @abs(double %7)
%9 = fdiv double %8, %4
%10 = tail call double @llvm.ceil.f64(double %9)
%11 = tail call double @llvm.fmuladd.f64(double %4, double %10, double %0)
br label %21
12: ; preds = %3
%13 = fcmp ult double %0, %2
br i1 %13, label %21, label %14
14: ; preds = %12
%15 = fsub double %0, %2
%16 = tail call double @abs(double %15)
%17 = fdiv double %16, %4
%18 = tail call double @llvm.ceil.f64(double %17)
%19 = fneg double %4
%20 = tail call double @llvm.fmuladd.f64(double %19, double %18, double %0)
br label %21
21: ; preds = %12, %14, %6
%22 = phi double [ %11, %6 ], [ %20, %14 ], [ %0, %12 ]
ret double %22
}