#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*/ ularith_addmod_ul_ul(unsigned long *, unsigned long,
                                        unsigned long, unsigned long const);
int /*<<< orphan*/ ularith_mul_ul_ul_2ul(unsigned long *, unsigned long *,
                                         unsigned long, unsigned long const);
void ularith_redc(unsigned long *r, const unsigned long plow,
                  const unsigned long phigh, const unsigned long m,
                  const unsigned long invm) {
  unsigned long t = phigh;
  unsigned long tlow, thigh;
  ;
  tlow = plow * invm;
  ularith_mul_ul_ul_2ul(&tlow, &thigh, tlow, m);
  t += (plow != 0UL) ? 1UL : 0UL;
  ularith_addmod_ul_ul(r, t, thigh, m);
  ;
}


x86 assembly (clang, -O0):
<ularith_redc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
imul   -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x38(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1040 <ularith_mul_ul_ul_2ul@plt>
mov    -0x10(%rbp),%rdx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%rdx
cmovne %rcx,%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1030 <ularith_addmod_ul_ul@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ularith_redc>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
imul   %rsi,%r8
mov    %r8,0x10(%rsp)
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    %r8,%rdx
call   1040 <ularith_mul_ul_ul_2ul@plt>
cmp    $0x1,%r15
sbb    $0xffffffffffffffff,%rbx
mov    0x8(%rsp),%rdx
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r14,%rcx
call   1030 <ularith_addmod_ul_ul@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @ularith_redc(i64* %0, i64 %1, i64 %2, i64 %3, i64 %4) {
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store i64* %0, i64** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i64 %3, i64* %9
store i64 %4, i64* %10
%14 = load i64, i64* %8
store i64 %14, i64* %11
%15 = load i64, i64* %7
%16 = load i64, i64* %10
%17 = mul i64 %15, %16
store i64 %17, i64* %12
%18 = load i64, i64* %12
%19 = load i64, i64* %9
%20 = call i32 @ularith_mul_ul_ul_2ul(i64* %12, i64* %13, i64 %18, i64 %19)
%21 = load i64, i64* %7
%22 = icmp ne i64 %21, 0
%23 = zext i1 %22 to i64
%24 = select i1 %22, i64 1, i64 0
%25 = load i64, i64* %11
%26 = add i64 %25, %24
store i64 %26, i64* %11
%27 = load i64*, i64** %6
%28 = load i64, i64* %11
%29 = load i64, i64* %13
%30 = load i64, i64* %9
%31 = call i32 @ularith_addmod_ul_ul(i64* %27, i64 %28, i64 %29, i64 %30)
ret void
}

LLVM IR (-O2):
define void @ularith_redc(i64* %0, i64 %1, i64 %2, i64 %3, i64 %4) {
%6 = alloca i64
%7 = alloca i64
%8 = bitcast i64* %6 to i8*
%9 = bitcast i64* %7 to i8*
%10 = mul i64 %4, %1
store i64 %10, i64* %6
%11 = call i32 @ularith_mul_ul_ul_2ul(i64* %6, i64* %7, i64 %10, i64 %3)
%12 = icmp ne i64 %1, 0
%13 = zext i1 %12 to i64
%14 = add i64 %13, %2
%15 = load i64, i64* %7
%16 = call i32 @ularith_addmod_ul_ul(i64* %0, i64 %14, i64 %15, i64 %3)
ret void
}