#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 */
typedef int uint64_t;

/* Variables and functions */
int UINT64_C(int);
int modadd(int, int, int);
int modshl(int, int, int);
uint64_t modmul(uint64_t a, uint64_t b, uint64_t mod) {
  uint64_t ah, al, bh, bl;
  ah = a >> 32;
  bh = b >> 32;
  al = a & UINT64_C(0x00000000FFFFFFFF);
  bl = b & UINT64_C(0x00000000FFFFFFFF);
  return modadd(
      modadd(modshl((ah * bh) % mod, 64, mod),
             modshl(modadd((ah * bl) % mod, (al * bh) % mod, mod), 32, mod),
             mod),
      (al * bl) % mod, mod);
}


x86 assembly (clang, -O0):
<modmul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x20,%ecx
sar    %cl,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x20,%ecx
sar    %cl,%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    $0xffffffff,%edi
call   1030 <UINT64_C@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
and    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    $0xffffffff,%edi
call   1030 <UINT64_C@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
and    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%eax
imul   -0x18(%rbp),%eax
cltd
idivl  -0xc(%rbp)
mov    %edx,%edi
mov    -0xc(%rbp),%edx
mov    $0x40,%esi
call   1050 <modshl@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%eax
imul   -0x1c(%rbp),%eax
cltd
idivl  -0xc(%rbp)
mov    %edx,%edi
mov    -0x14(%rbp),%eax
imul   -0x18(%rbp),%eax
cltd
idivl  -0xc(%rbp)
mov    %edx,%esi
mov    -0xc(%rbp),%edx
call   1040 <modadd@plt>
mov    %eax,%edi
mov    -0xc(%rbp),%edx
mov    $0x20,%esi
call   1050 <modshl@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%esi
mov    -0xc(%rbp),%edx
call   1040 <modadd@plt>
mov    %eax,%edi
mov    -0x14(%rbp),%eax
imul   -0x1c(%rbp),%eax
cltd
idivl  -0xc(%rbp)
mov    %edx,%esi
mov    -0xc(%rbp),%edx
call   1040 <modadd@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<modmul>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%r14d
mov    %edi,%ebp
mov    $0xffffffff,%edi
call   1030 <UINT64_C@plt>
mov    %eax,%ebx
and    %ebp,%ebx
mov    $0xffffffff,%edi
call   1030 <UINT64_C@plt>
mov    %eax,%ebp
and    %r14d,%ebp
imul   %ebx,%ebp
mov    $0x40,%esi
mov    %r15d,%edx
call   1050 <modshl@plt>
mov    %eax,%ebx
mov    %r15d,%edx
call   1040 <modadd@plt>
mov    %eax,%edi
mov    $0x20,%esi
mov    %r15d,%edx
call   1050 <modshl@plt>
mov    %ebx,%edi
mov    %eax,%esi
mov    %r15d,%edx
call   1040 <modadd@plt>
mov    %eax,%edi
mov    %ebp,%eax
cltd
idiv   %r15d
mov    %edx,%esi
mov    %r15d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <modadd@plt>

LLVM IR (-O0):
define i32 @modmul(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%11 = load i32, i32* %4
%12 = ashr i32 %11, 32
store i32 %12, i32* %7
%13 = load i32, i32* %5
%14 = ashr i32 %13, 32
store i32 %14, i32* %9
%15 = load i32, i32* %4
%16 = call i32 @UINT64_C(i32 -1)
%17 = and i32 %15, %16
store i32 %17, i32* %8
%18 = load i32, i32* %5
%19 = call i32 @UINT64_C(i32 -1)
%20 = and i32 %18, %19
store i32 %20, i32* %10
%21 = load i32, i32* %7
%22 = load i32, i32* %9
%23 = mul nsw i32 %21, %22
%24 = load i32, i32* %6
%25 = srem i32 %23, %24
%26 = load i32, i32* %6
%27 = call i32 @modshl(i32 %25, i32 64, i32 %26)
%28 = load i32, i32* %7
%29 = load i32, i32* %10
%30 = mul nsw i32 %28, %29
%31 = load i32, i32* %6
%32 = srem i32 %30, %31
%33 = load i32, i32* %8
%34 = load i32, i32* %9
%35 = mul nsw i32 %33, %34
%36 = load i32, i32* %6
%37 = srem i32 %35, %36
%38 = load i32, i32* %6
%39 = call i32 @modadd(i32 %32, i32 %37, i32 %38)
%40 = load i32, i32* %6
%41 = call i32 @modshl(i32 %39, i32 32, i32 %40)
%42 = load i32, i32* %6
%43 = call i32 @modadd(i32 %27, i32 %41, i32 %42)
%44 = load i32, i32* %8
%45 = load i32, i32* %10
%46 = mul nsw i32 %44, %45
%47 = load i32, i32* %6
%48 = srem i32 %46, %47
%49 = load i32, i32* %6
%50 = call i32 @modadd(i32 %43, i32 %48, i32 %49)
ret i32 %50
}

LLVM IR (-O2):
define i32 @modmul(i32 %0, i32 %1, i32 %2) {
%4 = tail call i32 @UINT64_C(i32 -1)
%5 = and i32 %4, %0
%6 = tail call i32 @UINT64_C(i32 -1)
%7 = and i32 %6, %1
%8 = tail call i32 @modshl(i32 poison, i32 64, i32 %2)
%9 = tail call i32 @modadd(i32 poison, i32 poison, i32 %2)
%10 = tail call i32 @modshl(i32 %9, i32 32, i32 %2)
%11 = tail call i32 @modadd(i32 %8, i32 %10, i32 %2)
%12 = mul nsw i32 %7, %5
%13 = srem i32 %12, %2
%14 = tail call i32 @modadd(i32 %11, i32 %13, i32 %2)
ret i32 %14
}