double MultModM(double a, double s, double c, double m) {
  double v;
  long a1;
  v = a * s + c;
  if ((v >= 9007199254740992.0) || (v <= -9007199254740992.0)) {
    a1 = (long)(a / 131072.0);
    a -= a1 * 131072.0;
    v = a1 * s;
    a1 = (long)(v / m);
    v -= a1 * m;
    v = v * 131072.0 + a * s + c;
  }
  a1 = (long)(v / m);
  if ((v -= a1 * m) < 0.0)
    return v += m;
  else
    return v;
}


x86 assembly (clang, -O0):
<MultModM>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  %xmm2,-0x20(%rbp)
movsd  %xmm3,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  0xebf(%rip),%xmm1
ucomisd %xmm1,%xmm0
jae    115e <MultModM+0x5e>
movsd  0xeb5(%rip),%xmm0
ucomisd -0x30(%rbp),%xmm0
jb     122f <MultModM+0x12f>
movsd  -0x10(%rbp),%xmm0
movsd  0xea5(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x38(%rbp)
cvtsi2sdq -0x38(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  0xe6e(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
cvtsi2sdq -0x38(%rbp),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
divsd  -0x28(%rbp),%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x38(%rbp)
cvtsi2sdq -0x38(%rbp),%xmm0
movsd  -0x28(%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)
movsd  -0x30(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
mulsd  -0x18(%rbp),%xmm1
movsd  0xdf3(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
divsd  -0x28(%rbp),%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x38(%rbp)
cvtsi2sdq -0x38(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm2
movsd  -0x30(%rbp),%xmm0
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,-0x30(%rbp)
xorps  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jbe    129c <MultModM+0x19c>
movsd  -0x28(%rbp),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  %xmm0,-0x8(%rbp)
jmp    12a6 <MultModM+0x1a6>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<MultModM>:
movapd %xmm0,%xmm4
mulsd  %xmm1,%xmm4
addsd  %xmm2,%xmm4
ucomisd 0xeec(%rip),%xmm4
jae    1124 <MultModM+0x24>
movsd  0xeea(%rip),%xmm5
ucomisd %xmm4,%xmm5
jb     1182 <MultModM+0x82>
movsd  0xee4(%rip),%xmm4
mulsd  %xmm0,%xmm4
cvttsd2si %xmm4,%rax
xorps  %xmm4,%xmm4
cvtsi2sd %rax,%xmm4
movsd  0xed3(%rip),%xmm5
movapd %xmm4,%xmm6
mulsd  %xmm5,%xmm6
subsd  %xmm6,%xmm0
mulsd  %xmm1,%xmm4
movapd %xmm4,%xmm6
divsd  %xmm3,%xmm6
cvttsd2si %xmm6,%rax
xorps  %xmm6,%xmm6
cvtsi2sd %rax,%xmm6
mulsd  %xmm3,%xmm6
subsd  %xmm6,%xmm4
mulsd  %xmm1,%xmm0
mulsd  %xmm5,%xmm4
addsd  %xmm0,%xmm4
addsd  %xmm2,%xmm4
movapd %xmm4,%xmm0
divsd  %xmm3,%xmm0
cvttsd2si %xmm0,%rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
mulsd  %xmm3,%xmm0
subsd  %xmm0,%xmm4
xorpd  %xmm1,%xmm1
movapd %xmm4,%xmm0
cmpltsd %xmm1,%xmm0
andpd  %xmm0,%xmm3
movsd  0xe68(%rip),%xmm1
andnpd %xmm1,%xmm0
orpd   %xmm3,%xmm0
addsd  %xmm4,%xmm0
ret

LLVM IR (-O0):
define double @MultModM(double %0, double %1, double %2, double %3) {
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca i64
store double %0, double* %6
store double %1, double* %7
store double %2, double* %8
store double %3, double* %9
%12 = load double, double* %6
%13 = load double, double* %7
%14 = load double, double* %8
%15 = call double @llvm.fmuladd.f64(double %12, double %13, double %14)
store double %15, double* %10
%16 = load double, double* %10
%17 = fcmp oge double %16, 0x4340000000000000
br i1 %17, label %21, label %18
18: ; preds = %4
%19 = load double, double* %10
%20 = fcmp ole double %19, 0xC340000000000000
br i1 %20, label %21, label %51
21: ; preds = %18, %4
%22 = load double, double* %6
%23 = fdiv double %22, 1.310720e+05
%24 = fptosi double %23 to i64
store i64 %24, i64* %11
%25 = load i64, i64* %11
%26 = sitofp i64 %25 to double
%27 = load double, double* %6
%28 = fneg double %26
%29 = call double @llvm.fmuladd.f64(double %28, double 1.310720e+05, double %27)
store double %29, double* %6
%30 = load i64, i64* %11
%31 = sitofp i64 %30 to double
%32 = load double, double* %7
%33 = fmul double %31, %32
store double %33, double* %10
%34 = load double, double* %10
%35 = load double, double* %9
%36 = fdiv double %34, %35
%37 = fptosi double %36 to i64
store i64 %37, i64* %11
%38 = load i64, i64* %11
%39 = sitofp i64 %38 to double
%40 = load double, double* %9
%41 = load double, double* %10
%42 = fneg double %39
%43 = call double @llvm.fmuladd.f64(double %42, double %40, double %41)
store double %43, double* %10
%44 = load double, double* %10
%45 = load double, double* %6
%46 = load double, double* %7
%47 = fmul double %45, %46
%48 = call double @llvm.fmuladd.f64(double %44, double 1.310720e+05, double %47)
%49 = load double, double* %8
%50 = fadd double %48, %49
store double %50, double* %10
br label %51
51: ; preds = %21, %18
%52 = load double, double* %10
%53 = load double, double* %9
%54 = fdiv double %52, %53
%55 = fptosi double %54 to i64
store i64 %55, i64* %11
%56 = load i64, i64* %11
%57 = sitofp i64 %56 to double
%58 = load double, double* %9
%59 = load double, double* %10
%60 = fneg double %57
%61 = call double @llvm.fmuladd.f64(double %60, double %58, double %59)
store double %61, double* %10
%62 = fcmp olt double %61, 0.000000e+00
br i1 %62, label %63, label %67
63: ; preds = %51
%64 = load double, double* %9
%65 = load double, double* %10
%66 = fadd double %65, %64
store double %66, double* %10
store double %66, double* %5
br label %69
67: ; preds = %51
%68 = load double, double* %10
store double %68, double* %5
br label %69
69: ; preds = %67, %63
%70 = load double, double* %5
ret double %70
}

LLVM IR (-O2):
define double @MultModM(double %0, double %1, double %2, double %3) {
%5 = tail call double @llvm.fmuladd.f64(double %0, double %1, double %2)
%6 = fcmp oge double %5, 0x4340000000000000
%7 = fcmp ole double %5, 0xC340000000000000
%8 = or i1 %6, %7
br i1 %8, label %9, label %24
9: ; preds = %4
%10 = fmul double %0, 0x3EE0000000000000
%11 = fptosi double %10 to i64
%12 = sitofp i64 %11 to double
%13 = fneg double %12
%14 = tail call double @llvm.fmuladd.f64(double %13, double 1.310720e+05, double %0)
%15 = fmul double %12, %1
%16 = fdiv double %15, %3
%17 = fptosi double %16 to i64
%18 = sitofp i64 %17 to double
%19 = fneg double %18
%20 = tail call double @llvm.fmuladd.f64(double %19, double %3, double %15)
%21 = fmul double %14, %1
%22 = tail call double @llvm.fmuladd.f64(double %20, double 1.310720e+05, double %21)
%23 = fadd double %22, %2
br label %24
24: ; preds = %4, %9
%25 = phi double [ %23, %9 ], [ %5, %4 ]
%26 = fdiv double %25, %3
%27 = fptosi double %26 to i64
%28 = sitofp i64 %27 to double
%29 = fneg double %28
%30 = tail call double @llvm.fmuladd.f64(double %29, double %3, double %25)
%31 = fcmp olt double %30, 0.000000e+00
%32 = select i1 %31, double %3, double -0.000000e+00
%33 = fadd double %30, %32
ret double %33
}