#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 */
long least_divisor(long);
long *malloc(int);
long *realloc(long *, int);
long *prime_factors(long n) {
  long *rtrn_list = malloc(sizeof(long));
  int rtrn_list_length = 0;
  long least_div;
  while (n != 1) {
    least_div = least_divisor(n);
    rtrn_list = realloc(rtrn_list, (rtrn_list_length + 2) * sizeof(long));
    rtrn_list[++rtrn_list_length] = least_div;
    if (least_div == n) {
      break;
    }
    while (n % least_div == 0) {
      n /= least_div;
    }
  }
  rtrn_list[0] = rtrn_list_length;
  return rtrn_list;
}


x86 assembly (clang, -O0):
<prime_factors>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x8,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpq   $0x1,-0x8(%rbp)
je     11e3 <prime_factors+0xb3>
mov    -0x8(%rbp),%rdi
call   1030 <least_divisor@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x3,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x14(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    11af <prime_factors+0x7f>
jmp    11e3 <prime_factors+0xb3>
jmp    11b4 <prime_factors+0x84>
mov    -0x8(%rbp),%rax
cqto
idivq  -0x20(%rbp)
cmp    $0x0,%rdx
jne    11de <prime_factors+0xae>
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
cqto
idiv   %rcx
mov    %rax,-0x8(%rbp)
jmp    11b4 <prime_factors+0x84>
jmp    1151 <prime_factors+0x21>
movslq -0x14(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prime_factors>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
mov    $0x8,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
cmp    $0x1,%r15
jne    1155 <prime_factors+0x25>
xor    %eax,%eax
jmp    11f7 <prime_factors+0xc7>
xor    %r12d,%r12d
jmp    116a <prime_factors+0x3a>
nopw   0x0(%rax,%rax,1)
cmp    $0x1,%r15
je     11f4 <prime_factors+0xc4>
mov    %r15,%rdi
call   1030 <least_divisor@plt>
mov    %rax,%rbx
lea    0x10(,%r12,8),%esi
mov    %r14,%rdi
call   1050 <realloc@plt>
mov    %rax,%r14
mov    %rbx,0x8(%rax,%r12,8)
add    $0x1,%r12
cmp    %r15,%rbx
jne    11ab <prime_factors+0x7b>
jmp    11f4 <prime_factors+0xc4>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rax
cqto
idiv   %rbx
mov    %rax,%r15
mov    %r15,%rax
or     %rbx,%rax
shr    $0x20,%rax
je     11d0 <prime_factors+0xa0>
mov    %r15,%rax
cqto
idiv   %rbx
test   %rdx,%rdx
je     11dc <prime_factors+0xac>
jmp    1160 <prime_factors+0x30>
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%eax
xor    %edx,%edx
div    %ebx
test   %rdx,%rdx
jne    1160 <prime_factors+0x30>
mov    %r15,%rax
or     %rbx,%rax
shr    $0x20,%rax
jne    11a0 <prime_factors+0x70>
mov    %r15d,%eax
xor    %edx,%edx
div    %ebx
mov    %eax,%r15d
jmp    11ab <prime_factors+0x7b>
movslq %r12d,%rax
mov    %rax,(%r14)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64* @prime_factors(i64 %0) {
%2 = alloca i64
%3 = alloca i64*
%4 = alloca i32
%5 = alloca i64
store i64 %0, i64* %2
%6 = call i64* @malloc(i32 8)
store i64* %6, i64** %3
store i32 0, i32* %4
br label %7
7: ; preds = %40, %1
%8 = load i64, i64* %2
%9 = icmp ne i64 %8, 1
br i1 %9, label %10, label %41
10: ; preds = %7
%11 = load i64, i64* %2
%12 = call i64 @least_divisor(i64 %11)
store i64 %12, i64* %5
%13 = load i64*, i64** %3
%14 = load i32, i32* %4
%15 = add nsw i32 %14, 2
%16 = sext i32 %15 to i64
%17 = mul i64 %16, 8
%18 = trunc i64 %17 to i32
%19 = call i64* @realloc(i64* %13, i32 %18)
store i64* %19, i64** %3
%20 = load i64, i64* %5
%21 = load i64*, i64** %3
%22 = load i32, i32* %4
%23 = add nsw i32 %22, 1
store i32 %23, i32* %4
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i64, i64* %21, i64 %24
store i64 %20, i64* %25
%26 = load i64, i64* %5
%27 = load i64, i64* %2
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %10
br label %41
30: ; preds = %10
br label %31
31: ; preds = %36, %30
%32 = load i64, i64* %2
%33 = load i64, i64* %5
%34 = srem i64 %32, %33
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %31
%37 = load i64, i64* %5
%38 = load i64, i64* %2
%39 = sdiv i64 %38, %37
store i64 %39, i64* %2
br label %31
40: ; preds = %31
br label %7
41: ; preds = %29, %7
%42 = load i32, i32* %4
%43 = sext i32 %42 to i64
%44 = load i64*, i64** %3
%45 = getelementptr inbounds i64, i64* %44, i64 0
store i64 %43, i64* %45
%46 = load i64*, i64** %3
ret i64* %46
}

LLVM IR (-O2):
define i64* @prime_factors(i64 %0) {
%2 = tail call i64* @malloc(i32 8)
%3 = icmp eq i64 %0, 1
br i1 %3, label %30, label %7
4: ; preds = %22, %19
%5 = phi i64 [ %9, %19 ], [ %24, %22 ]
%6 = icmp eq i64 %5, 1
br i1 %6, label %27, label %7
7: ; preds = %1, %4
%8 = phi i64 [ %16, %4 ], [ 0, %1 ]
%9 = phi i64 [ %5, %4 ], [ %0, %1 ]
%10 = phi i64* [ %15, %4 ], [ %2, %1 ]
%11 = tail call i64 @least_divisor(i64 %9)
%12 = trunc i64 %8 to i32
%13 = shl i32 %12, 3
%14 = add i32 %13, 16
%15 = tail call i64* @realloc(i64* %10, i32 %14)
%16 = add nuw i64 %8, 1
%17 = getelementptr inbounds i64, i64* %15, i64 %16
store i64 %11, i64* %17
%18 = icmp eq i64 %11, %9
br i1 %18, label %27, label %19
19: ; preds = %7
%20 = srem i64 %9, %11
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %4
22: ; preds = %19, %22
%23 = phi i64 [ %24, %22 ], [ %9, %19 ]
%24 = sdiv i64 %23, %11
%25 = srem i64 %24, %11
%26 = icmp eq i64 %25, 0
br i1 %26, label %22, label %4
27: ; preds = %7, %4
%28 = shl i64 %16, 32
%29 = ashr exact i64 %28, 32
br label %30
30: ; preds = %27, %1
%31 = phi i64* [ %2, %1 ], [ %15, %27 ]
%32 = phi i64 [ 0, %1 ], [ %29, %27 ]
store i64 %32, i64* %31
ret i64* %31
}