#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 */
unsigned long schrage(unsigned long, unsigned long, unsigned long);
unsigned long int schrage_mult(unsigned long int a, unsigned long int b,
                               unsigned long int m, unsigned long int sqrtm) {
  unsigned long int t0 = schrage(sqrtm, b, m);
  unsigned long int t1 = schrage(a / sqrtm, t0, m);
  unsigned long int t2 = schrage(a % sqrtm, b, m);
  unsigned long int t = t1 + t2;
  return (t >= m) ? (t - m) : t;
}


x86 assembly (clang, -O0):
<schrage_mult>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <schrage@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x20(%rbp)
mov    %rax,%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <schrage@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x20(%rbp)
mov    %rdx,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <schrage@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     11a8 <schrage_mult+0x98>
mov    -0x40(%rbp),%rax
sub    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    11b0 <schrage_mult+0xa0>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<schrage_mult>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
mov    %rcx,%rdi
call   1030 <schrage@plt>
mov    %rax,%rsi
mov    %r12,%rax
or     %rbx,%rax
shr    $0x20,%rax
je     1148 <schrage_mult+0x38>
mov    %r12,%rax
xor    %edx,%edx
div    %rbx
mov    %rdx,%r12
jmp    1152 <schrage_mult+0x42>
mov    %r12d,%eax
xor    %edx,%edx
div    %ebx
mov    %edx,%r12d
mov    %rax,%rdi
mov    %r15,%rdx
call   1030 <schrage@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <schrage@plt>
add    %rbx,%rax
xor    %ecx,%ecx
cmp    %r15,%rax
cmovae %r15,%rcx
sub    %rcx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @schrage_mult(i64 %0, i64 %1, i64 %2, i64 %3) {
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
store i64 %0, i64* %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i64 %3, i64* %8
%13 = load i64, i64* %8
%14 = load i64, i64* %6
%15 = load i64, i64* %7
%16 = call i64 @schrage(i64 %13, i64 %14, i64 %15)
store i64 %16, i64* %9
%17 = load i64, i64* %5
%18 = load i64, i64* %8
%19 = udiv i64 %17, %18
%20 = load i64, i64* %9
%21 = load i64, i64* %7
%22 = call i64 @schrage(i64 %19, i64 %20, i64 %21)
store i64 %22, i64* %10
%23 = load i64, i64* %5
%24 = load i64, i64* %8
%25 = urem i64 %23, %24
%26 = load i64, i64* %6
%27 = load i64, i64* %7
%28 = call i64 @schrage(i64 %25, i64 %26, i64 %27)
store i64 %28, i64* %11
%29 = load i64, i64* %10
%30 = load i64, i64* %11
%31 = add i64 %29, %30
store i64 %31, i64* %12
%32 = load i64, i64* %12
%33 = load i64, i64* %7
%34 = icmp uge i64 %32, %33
br i1 %34, label %35, label %39
35: ; preds = %4
%36 = load i64, i64* %12
%37 = load i64, i64* %7
%38 = sub i64 %36, %37
br label %41
39: ; preds = %4
%40 = load i64, i64* %12
br label %41
41: ; preds = %39, %35
%42 = phi i64 [ %38, %35 ], [ %40, %39 ]
ret i64 %42
}

LLVM IR (-O2):
define i64 @schrage_mult(i64 %0, i64 %1, i64 %2, i64 %3) {
%5 = tail call i64 @schrage(i64 %3, i64 %1, i64 %2)
%6 = udiv i64 %0, %3
%7 = tail call i64 @schrage(i64 %6, i64 %5, i64 %2)
%8 = urem i64 %0, %3
%9 = tail call i64 @schrage(i64 %8, i64 %1, i64 %2)
%10 = add i64 %9, %7
%11 = icmp ult i64 %10, %2
%12 = select i1 %11, i64 0, i64 %2
%13 = sub i64 %10, %12
ret i64 %13
}