#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 */
scalar_t__ is_prime(int);
int next_prime(int);
int n_distinct_prime_factors(int i) {
  int count = 0;
  int n;
  if (is_prime(i)) {
    return 0;
  }
  for (n = 2; i > 1; n = next_prime(n)) {
    if (i % n == 0) {
      count++;
      while (i % n == 0) {
        i = i / n;
      }
    }
  }
  return count;
}


x86 assembly (clang, -O0):
<n_distinct_prime_factors>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <is_prime@plt>
cmp    $0x0,%rax
je     1150 <n_distinct_prime_factors+0x30>
movl   $0x0,-0x4(%rbp)
jmp    11b9 <n_distinct_prime_factors+0x99>
movl   $0x2,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11b3 <n_distinct_prime_factors+0x93>
mov    -0x8(%rbp),%eax
cltd
idivl  -0x10(%rbp)
cmp    $0x0,%edx
jne    119e <n_distinct_prime_factors+0x7e>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
cltd
idivl  -0x10(%rbp)
cmp    $0x0,%edx
jne    1199 <n_distinct_prime_factors+0x79>
mov    -0x8(%rbp),%eax
cltd
idivl  -0x10(%rbp)
mov    %eax,-0x8(%rbp)
jmp    117a <n_distinct_prime_factors+0x5a>
jmp    119e <n_distinct_prime_factors+0x7e>
jmp    11a3 <n_distinct_prime_factors+0x83>
mov    -0x10(%rbp),%edi
call   1030 <next_prime@plt>
mov    %eax,-0x10(%rbp)
jmp    1157 <n_distinct_prime_factors+0x37>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<n_distinct_prime_factors>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
call   1040 <is_prime@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    1170 <n_distinct_prime_factors+0x50>
cmp    $0x2,%ebp
jl     1170 <n_distinct_prime_factors+0x50>
xor    %ebx,%ebx
mov    $0x2,%edi
jmp    114c <n_distinct_prime_factors+0x2c>
nop
call   1030 <next_prime@plt>
mov    %eax,%edi
cmp    $0x1,%ebp
jle    1170 <n_distinct_prime_factors+0x50>
mov    %ebp,%eax
cltd
idiv   %edi
test   %edx,%edx
jne    1140 <n_distinct_prime_factors+0x20>
add    $0x1,%ebx
nopl   0x0(%rax,%rax,1)
mov    %ebp,%eax
cltd
idiv   %edi
mov    %eax,%ebp
cltd
idiv   %edi
test   %edx,%edx
je     1160 <n_distinct_prime_factors+0x40>
jmp    1140 <n_distinct_prime_factors+0x20>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @n_distinct_prime_factors(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %4
%6 = load i32, i32* %3
%7 = call i64 @is_prime(i32 %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 0, i32* %2
br label %38
10: ; preds = %1
store i32 2, i32* %5
br label %11
11: ; preds = %33, %10
%12 = load i32, i32* %3
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %36
14: ; preds = %11
%15 = load i32, i32* %3
%16 = load i32, i32* %5
%17 = srem i32 %15, %16
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %32
19: ; preds = %14
%20 = load i32, i32* %4
%21 = add nsw i32 %20, 1
store i32 %21, i32* %4
br label %22
22: ; preds = %27, %19
%23 = load i32, i32* %3
%24 = load i32, i32* %5
%25 = srem i32 %23, %24
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = load i32, i32* %3
%29 = load i32, i32* %5
%30 = sdiv i32 %28, %29
store i32 %30, i32* %3
br label %22
31: ; preds = %22
br label %32
32: ; preds = %31, %14
br label %33
33: ; preds = %32
%34 = load i32, i32* %5
%35 = call i32 @next_prime(i32 %34)
store i32 %35, i32* %5
br label %11
36: ; preds = %11
%37 = load i32, i32* %4
store i32 %37, i32* %2
br label %38
38: ; preds = %36, %9
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
define i32 @n_distinct_prime_factors(i32 %0) {
%2 = tail call i64 @is_prime(i32 %0)
%3 = icmp eq i64 %2, 0
%4 = icmp sgt i32 %0, 1
%5 = and i1 %3, %4
br i1 %5, label %6, label %24
6: ; preds = %1, %19
%7 = phi i32 [ %22, %19 ], [ 2, %1 ]
%8 = phi i32 [ %21, %19 ], [ 0, %1 ]
%9 = phi i32 [ %20, %19 ], [ %0, %1 ]
%10 = srem i32 %9, %7
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %6
%13 = add nsw i32 %8, 1
br label %14
14: ; preds = %12, %14
%15 = phi i32 [ %16, %14 ], [ %9, %12 ]
%16 = sdiv i32 %15, %7
%17 = srem i32 %16, %7
%18 = icmp eq i32 %17, 0
br i1 %18, label %14, label %19
19: ; preds = %14, %6
%20 = phi i32 [ %9, %6 ], [ %16, %14 ]
%21 = phi i32 [ %8, %6 ], [ %13, %14 ]
%22 = tail call i32 @next_prime(i32 %7)
%23 = icmp sgt i32 %20, 1
br i1 %23, label %6, label %24
24: ; preds = %19, %1
%25 = phi i32 [ 0, %1 ], [ %21, %19 ]
ret i32 %25
}