int factor(int n) {
  static int a[] = {0, 4, 1, 2, 0, 2};
  static int m = 0;
  static int d = 0;
  if (n) {
    m = n;
    d = 2;
  }
  while ((d * d) <= m) {
    if (!(m % d)) {
      m /= d;
      return d;
    }
    d += a[d % 6];
  }
  n = m;
  m = 0;
  return n;
}


x86 assembly (clang, -O0):
<factor>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1124 <factor+0x24>
mov    -0x8(%rbp),%eax
mov    %eax,0x2f32(%rip)
movl   $0x2,0x2f2c(%rip)
jmp    1129 <factor+0x29>
mov    0x2f21(%rip),%eax
imul   0x2f1a(%rip),%eax
cmp    0x2f10(%rip),%eax
jg     11a7 <factor+0xa7>
mov    0x2f04(%rip),%eax
cltd
idivl  0x2f01(%rip)
cmp    $0x0,%edx
jne    117b <factor+0x7b>
mov    0x2ef2(%rip),%ecx
mov    0x2ee8(%rip),%eax
cltd
idiv   %ecx
mov    %eax,0x2edf(%rip)
mov    0x2edd(%rip),%eax
mov    %eax,-0x4(%rbp)
jmp    11c0 <factor+0xc0>
mov    0x2ecf(%rip),%eax
mov    $0x6,%ecx
cltd
idiv   %ecx
movslq %edx,%rcx
lea    0x2e9d(%rip),%rax
mov    (%rax,%rcx,4),%eax
add    0x2eb4(%rip),%eax
mov    %eax,0x2eae(%rip)
jmp    1129 <factor+0x29>
mov    0x2e9f(%rip),%eax
mov    %eax,-0x8(%rbp)
movl   $0x0,0x2e92(%rip)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<factor>:
test   %edi,%edi
je     1115 <factor+0x15>
movl   $0x2,0x2f1a(%rip)
mov    $0x2,%ecx
jmp    1121 <factor+0x21>
mov    0x2f09(%rip),%edi
mov    0x2f07(%rip),%ecx
mov    %ecx,%eax
imul   %ecx,%eax
xor    %r8d,%r8d
cmp    %edi,%eax
jg     117e <factor+0x7e>
lea    0xecc(%rip),%rsi
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %edi,%eax
cltd
idiv   %ecx
test   %edx,%edx
je     1182 <factor+0x82>
movslq %ecx,%rax
imul   $0x2aaaaaab,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rax
add    %edx,%eax
add    %eax,%eax
lea    (%rax,%rax,2),%eax
mov    %ecx,%edx
sub    %eax,%edx
movslq %edx,%rax
add    (%rsi,%rax,4),%ecx
mov    %ecx,0x2eb3(%rip)
mov    %ecx,%eax
imul   %ecx,%eax
cmp    %edi,%eax
jle    1140 <factor+0x40>
mov    %edi,%ecx
jmp    1185 <factor+0x85>
mov    %eax,%r8d
mov    %r8d,0x2e98(%rip)
mov    %ecx,%eax
ret

LLVM IR (-O0):
@factor.a = internal global [6 x i32] [i32 0, i32 4, i32 1, i32 2, i32 0, i32 2]
@factor.m = internal global i32 0
@factor.d = internal global i32 0
define i32 @factor(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = icmp ne i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = load i32, i32* %3
store i32 %7, i32* @factor.m
store i32 2, i32* @factor.d
br label %8
8: ; preds = %6, %1
br label %9
9: ; preds = %25, %8
%10 = load i32, i32* @factor.d
%11 = load i32, i32* @factor.d
%12 = mul nsw i32 %10, %11
%13 = load i32, i32* @factor.m
%14 = icmp sle i32 %12, %13
br i1 %14, label %15, label %33
15: ; preds = %9
%16 = load i32, i32* @factor.m
%17 = load i32, i32* @factor.d
%18 = srem i32 %16, %17
%19 = icmp ne i32 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = load i32, i32* @factor.d
%22 = load i32, i32* @factor.m
%23 = sdiv i32 %22, %21
store i32 %23, i32* @factor.m
%24 = load i32, i32* @factor.d
store i32 %24, i32* %2
br label %36
25: ; preds = %15
%26 = load i32, i32* @factor.d
%27 = srem i32 %26, 6
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [6 x i32], [6 x i32]* @factor.a, i64 0, i64 %28
%30 = load i32, i32* %29
%31 = load i32, i32* @factor.d
%32 = add nsw i32 %31, %30
store i32 %32, i32* @factor.d
br label %9
33: ; preds = %9
%34 = load i32, i32* @factor.m
store i32 %34, i32* %3
store i32 0, i32* @factor.m
%35 = load i32, i32* %3
store i32 %35, i32* %2
br label %36
36: ; preds = %33, %20
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@factor.a = internal unnamed_addr constant [6 x i32] [i32 0, i32 4, i32 1, i32 2, i32 0, i32 2]
@factor.m = internal unnamed_addr global i32 0
@factor.d = internal unnamed_addr global i32 0
define i32 @factor(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %3, label %6
3: ; preds = %1
%4 = load i32, i32* @factor.m
%5 = load i32, i32* @factor.d
br label %7
6: ; preds = %1
store i32 2, i32* @factor.d
br label %7
7: ; preds = %3, %6
%8 = phi i32 [ %5, %3 ], [ 2, %6 ]
%9 = phi i32 [ %4, %3 ], [ %0, %6 ]
%10 = mul nsw i32 %8, %8
%11 = icmp sgt i32 %10, %9
br i1 %11, label %25, label %12
12: ; preds = %7, %17
%13 = phi i32 [ %22, %17 ], [ %8, %7 ]
%14 = srem i32 %9, %13
%15 = sdiv i32 %9, %13
%16 = icmp eq i32 %14, 0
br i1 %16, label %25, label %17
17: ; preds = %12
%18 = srem i32 %13, 6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [6 x i32], [6 x i32]* @factor.a, i64 0, i64 %19
%21 = load i32, i32* %20
%22 = add nsw i32 %21, %13
store i32 %22, i32* @factor.d
%23 = mul nsw i32 %22, %22
%24 = icmp sgt i32 %23, %9
br i1 %24, label %25, label %12
25: ; preds = %17, %12, %7
%26 = phi i32 [ 0, %7 ], [ %15, %12 ], [ 0, %17 ]
%27 = phi i32 [ %9, %7 ], [ %13, %12 ], [ %9, %17 ]
store i32 %26, i32* @factor.m
ret i32 %27
}