#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 /*<<< orphan*/ invmod2pow_ul(unsigned int);
unsigned int invpowmod_REDClr(unsigned int, unsigned int, int /*<<< orphan*/,
                              int, unsigned int);
int lg2(unsigned int);
int try_factor(uint64_t K, unsigned int N, int sign, unsigned int p) {
  unsigned int kcalc;
  int bbits;
  unsigned int r0;
  bbits = lg2(N);
  r0 = ((unsigned int)1) << (16 - (N >> (bbits - 3)));
  bbits = bbits - 4;
  kcalc = invpowmod_REDClr(N, p, -invmod2pow_ul(p), bbits, r0);
  if (sign > 0)
    kcalc = p - kcalc;
  return (kcalc == (unsigned int)(K % (uint64_t)p));
}


x86 assembly (clang, -O0):
<try_factor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <lg2@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x3,%ecx
shr    %cl,%eax
mov    $0x10,%ecx
sub    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <invmod2pow_ul@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%esi
xor    %edx,%edx
sub    %eax,%edx
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
call   1030 <invpowmod_REDClr@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    11b4 <try_factor+0x84>
mov    -0x10(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%eax
cltd
idivl  -0x10(%rbp)
mov    -0x28(%rbp),%eax
cmp    %edx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<try_factor>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r12d
mov    %edx,%r15d
mov    %esi,%ebp
mov    %edi,%r14d
mov    %esi,%edi
call   1050 <lg2@plt>
lea    -0x3(%rax),%ecx
mov    %ebp,%edx
shr    %cl,%edx
mov    $0x10,%cl
sub    %dl,%cl
mov    $0x1,%ebx
shl    %cl,%ebx
lea    -0x4(%rax),%r13d
mov    %r12d,%edi
call   1040 <invmod2pow_ul@plt>
neg    %eax
mov    %ebp,%edi
mov    %r12d,%esi
mov    %eax,%edx
mov    %r13d,%ecx
mov    %ebx,%r8d
call   1030 <invpowmod_REDClr@plt>
mov    %r12d,%ecx
sub    %eax,%ecx
test   %r15d,%r15d
cmovle %eax,%ecx
mov    %r14d,%eax
cltd
idiv   %r12d
xor    %eax,%eax
cmp    %edx,%ecx
sete   %al
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @try_factor(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i32, i32* %6
%13 = call i32 @lg2(i32 %12)
store i32 %13, i32* %10
%14 = load i32, i32* %6
%15 = load i32, i32* %10
%16 = sub nsw i32 %15, 3
%17 = lshr i32 %14, %16
%18 = sub i32 16, %17
%19 = shl i32 1, %18
store i32 %19, i32* %11
%20 = load i32, i32* %10
%21 = sub nsw i32 %20, 4
store i32 %21, i32* %10
%22 = load i32, i32* %6
%23 = load i32, i32* %8
%24 = load i32, i32* %8
%25 = call i32 @invmod2pow_ul(i32 %24)
%26 = sub nsw i32 0, %25
%27 = load i32, i32* %10
%28 = load i32, i32* %11
%29 = call i32 @invpowmod_REDClr(i32 %22, i32 %23, i32 %26, i32 %27, i32 %28)
store i32 %29, i32* %9
%30 = load i32, i32* %7
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %4
%33 = load i32, i32* %8
%34 = load i32, i32* %9
%35 = sub i32 %33, %34
store i32 %35, i32* %9
br label %36
36: ; preds = %32, %4
%37 = load i32, i32* %9
%38 = load i32, i32* %5
%39 = load i32, i32* %8
%40 = srem i32 %38, %39
%41 = icmp eq i32 %37, %40
%42 = zext i1 %41 to i32
ret i32 %42
}

LLVM IR (-O2):
define i32 @try_factor(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = tail call i32 @lg2(i32 %1)
%6 = add nsw i32 %5, -3
%7 = lshr i32 %1, %6
%8 = sub i32 16, %7
%9 = shl nuw i32 1, %8
%10 = add nsw i32 %5, -4
%11 = tail call i32 @invmod2pow_ul(i32 %3)
%12 = sub nsw i32 0, %11
%13 = tail call i32 @invpowmod_REDClr(i32 %1, i32 %3, i32 %12, i32 %10, i32 %9)
%14 = icmp sgt i32 %2, 0
%15 = sub i32 %3, %13
%16 = select i1 %14, i32 %15, i32 %13
%17 = srem i32 %0, %3
%18 = icmp eq i32 %16, %17
%19 = zext i1 %18 to i32
ret i32 %19
}