#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 */
double floorf(int /*<<< orphan*/);
int /*<<< orphan*/ sqrtf(double);
void kf_factor(int n, int *facbuf) {
  int p = 4;
  double floor_sqrt;
  floor_sqrt = floorf(sqrtf((double)n));
  do {
    while (n % p) {
      switch (p) {
      case 4:
        p = 2;
        break;
      case 2:
        p = 3;
        break;
      default:
        p += 2;
        break;
      }
      if (p > floor_sqrt)
        p = n;
    }
    n /= p;
    *facbuf++ = p;
    *facbuf++ = n;
  } while (n > 1);
}


x86 assembly (clang, -O0):
<kf_factor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x4,-0x14(%rbp)
cvtsi2sdl -0x4(%rbp),%xmm0
call   1040 <sqrtf@plt>
mov    %eax,%edi
call   1030 <floorf@plt>
movsd  %xmm0,-0x20(%rbp)
jmp    1151 <kf_factor+0x31>
mov    -0x4(%rbp),%eax
cltd
idivl  -0x14(%rbp)
cmp    $0x0,%edx
je     11c2 <kf_factor+0xa2>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0x2,%eax
je     1192 <kf_factor+0x72>
jmp    1175 <kf_factor+0x55>
mov    -0x24(%rbp),%eax
sub    $0x4,%eax
jne    119e <kf_factor+0x7e>
jmp    1186 <kf_factor+0x66>
movl   $0x2,-0x14(%rbp)
jmp    11a7 <kf_factor+0x87>
movl   $0x3,-0x14(%rbp)
jmp    11a7 <kf_factor+0x87>
mov    -0x14(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x14(%rbp)
cvtsi2sdl -0x14(%rbp),%xmm0
ucomisd -0x20(%rbp),%xmm0
jbe    11bd <kf_factor+0x9d>
mov    -0x4(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1151 <kf_factor+0x31>
mov    -0x14(%rbp),%ecx
mov    -0x4(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x10(%rbp)
mov    %ecx,(%rax)
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x10(%rbp)
mov    %ecx,(%rax)
cmpl   $0x1,-0x4(%rbp)
jg     114c <kf_factor+0x2c>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kf_factor>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
cvtsi2sd %edi,%xmm0
call   1040 <sqrtf@plt>
mov    %eax,%edi
call   1030 <floorf@plt>
mov    $0x4,%ecx
jmp    1160 <kf_factor+0x40>
mov    %ebp,%eax
cltd
idiv   %ecx
mov    %ecx,(%rbx)
mov    %eax,%ebp
mov    %eax,0x4(%rbx)
add    $0x8,%rbx
cmp    $0x1,%eax
jle    11a8 <kf_factor+0x88>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%eax
cltd
idiv   %ecx
test   %edx,%edx
je     113f <kf_factor+0x1f>
cmp    $0x4,%ecx
je     1180 <kf_factor+0x60>
cmp    $0x2,%ecx
jne    1190 <kf_factor+0x70>
mov    $0x3,%eax
jmp    1195 <kf_factor+0x75>
nopw   0x0(%rax,%rax,1)
mov    $0x2,%eax
jmp    1195 <kf_factor+0x75>
nopw   0x0(%rax,%rax,1)
add    $0x2,%ecx
mov    %ecx,%eax
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
ucomisd %xmm0,%xmm1
mov    %ebp,%ecx
ja     1160 <kf_factor+0x40>
mov    %eax,%ecx
jmp    1160 <kf_factor+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define void @kf_factor(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca double
store i32 %0, i32* %3
store i32* %1, i32** %4
store i32 4, i32* %5
%7 = load i32, i32* %3
%8 = sitofp i32 %7 to double
%9 = call i32 @sqrtf(double %8)
%10 = call double @floorf(i32 %9)
store double %10, double* %6
br label %11
11: ; preds = %42, %2
br label %12
12: ; preds = %31, %11
%13 = load i32, i32* %3
%14 = load i32, i32* %5
%15 = srem i32 %13, %14
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %32
17: ; preds = %12
%18 = load i32, i32* %5
switch i32 %18, label %21 [
i32 4, label %19
i32 2, label %20
]
19: ; preds = %17
store i32 2, i32* %5
br label %24
20: ; preds = %17
store i32 3, i32* %5
br label %24
21: ; preds = %17
%22 = load i32, i32* %5
%23 = add nsw i32 %22, 2
store i32 %23, i32* %5
br label %24
24: ; preds = %21, %20, %19
%25 = load i32, i32* %5
%26 = sitofp i32 %25 to double
%27 = load double, double* %6
%28 = fcmp ogt double %26, %27
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = load i32, i32* %3
store i32 %30, i32* %5
br label %31
31: ; preds = %29, %24
br label %12
32: ; preds = %12
%33 = load i32, i32* %5
%34 = load i32, i32* %3
%35 = sdiv i32 %34, %33
store i32 %35, i32* %3
%36 = load i32, i32* %5
%37 = load i32*, i32** %4
%38 = getelementptr inbounds i32, i32* %37, i32 1
store i32* %38, i32** %4
store i32 %36, i32* %37
%39 = load i32, i32* %3
%40 = load i32*, i32** %4
%41 = getelementptr inbounds i32, i32* %40, i32 1
store i32* %41, i32** %4
store i32 %39, i32* %40
br label %42
42: ; preds = %32
%43 = load i32, i32* %3
%44 = icmp sgt i32 %43, 1
br i1 %44, label %11, label %45
45: ; preds = %42
ret void
}

LLVM IR (-O2):
define void @kf_factor(i32 %0, i32* nocapture writeonly %1) {
%3 = sitofp i32 %0 to double
%4 = tail call i32 @sqrtf(double %3)
%5 = tail call double @floorf(i32 %4)
br label %6
6: ; preds = %24, %2
%7 = phi i32* [ %1, %2 ], [ %28, %24 ]
%8 = phi i32 [ 4, %2 ], [ %25, %24 ]
%9 = phi i32 [ %0, %2 ], [ %26, %24 ]
%10 = srem i32 %9, %8
%11 = icmp eq i32 %10, 0
br i1 %11, label %24, label %12
12: ; preds = %6, %17
%13 = phi i32 [ %21, %17 ], [ %8, %6 ]
switch i32 %13, label %15 [
i32 4, label %17
i32 2, label %14
]
14: ; preds = %12
br label %17
15: ; preds = %12
%16 = add nsw i32 %13, 2
br label %17
17: ; preds = %12, %15, %14
%18 = phi i32 [ %16, %15 ], [ 3, %14 ], [ 2, %12 ]
%19 = sitofp i32 %18 to double
%20 = fcmp olt double %5, %19
%21 = select i1 %20, i32 %9, i32 %18
%22 = srem i32 %9, %21
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %12
24: ; preds = %17, %6
%25 = phi i32 [ %8, %6 ], [ %21, %17 ]
%26 = sdiv i32 %9, %25
%27 = getelementptr inbounds i32, i32* %7, i64 1
store i32 %25, i32* %7
%28 = getelementptr inbounds i32, i32* %7, i64 2
store i32 %26, i32* %27
%29 = icmp sgt i32 %26, 1
br i1 %29, label %6, label %30
30: ; preds = %24
ret void
}