; LLVM IR from microsoft/LightAIMD :: src (special_func)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local i64 @double_factorial(i64 noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  %6 = load i64, ptr %3, align 8, !tbaa !5
  %7 = icmp slt i64 %6, -1
  br i1 %7, label %8, label %9

8:                                                ; preds = %1
  store i64 0, ptr %2, align 8
  br label %28

9:                                                ; preds = %1
  %10 = load i64, ptr %3, align 8, !tbaa !5
  %11 = icmp sle i64 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %9
  store i64 1, ptr %2, align 8
  br label %28

13:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %14 = load i64, ptr %3, align 8, !tbaa !5
  store i64 %14, ptr %5, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %23, %13
  %16 = load i64, ptr %5, align 8, !tbaa !5
  %17 = icmp sgt i64 %16, 0
  br i1 %17, label %19, label %18

18:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  br label %26

19:                                               ; preds = %15
  %20 = load i64, ptr %5, align 8, !tbaa !5
  %21 = load i64, ptr %4, align 8, !tbaa !5
  %22 = mul nsw i64 %21, %20
  store i64 %22, ptr %4, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %19
  %24 = load i64, ptr %5, align 8, !tbaa !5
  %25 = sub nsw i64 %24, 2
  store i64 %25, ptr %5, align 8, !tbaa !5
  br label %15, !llvm.loop !9

26:                                               ; preds = %18
  %27 = load i64, ptr %4, align 8, !tbaa !5
  store i64 %27, ptr %2, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %28

28:                                               ; preds = %26, %12, %8
  %29 = load i64, ptr %2, align 8
  ret i64 %29
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nofree norecurse nosync nounwind memory(none) uwtable
define dso_local range(i64 0, -9223372036854775808) i64 @double_factorial(i64 noundef %0) local_unnamed_addr #0 {
  %2 = icmp slt i64 %0, -1
  br i1 %2, label %.loopexit, label %3

3:                                                ; preds = %1
  %4 = icmp slt i64 %0, 1
  br i1 %4, label %.loopexit, label %.preheader.preheader

.preheader.preheader:                             ; preds = %3
  %5 = add nsw i64 %0, -1
  %6 = lshr i64 %5, 1
  %7 = add nuw i64 %6, 1
  %xtraiter = and i64 %7, 7
  %8 = icmp ult i64 %0, 15
  br i1 %8, label %.loopexit.loopexit.unr-lcssa, label %.preheader.preheader.new

.preheader.preheader.new:                         ; preds = %.preheader.preheader
  %unroll_iter = and i64 %7, -8
  br label %.preheader

.preheader:                                       ; preds = %.preheader, %.preheader.preheader.new
  %.013 = phi i64 [ %0, %.preheader.preheader.new ], [ %24, %.preheader ]
  %.0912 = phi i64 [ 1, %.preheader.preheader.new ], [ %23, %.preheader ]
  %niter = phi i64 [ 0, %.preheader.preheader.new ], [ %niter.next.7, %.preheader ]
  %9 = mul nuw nsw i64 %.013, %.0912
  %10 = add nsw i64 %.013, -2
  %11 = mul nuw nsw i64 %10, %9
  %12 = add nsw i64 %.013, -4
  %13 = mul nuw nsw i64 %12, %11
  %14 = add nsw i64 %.013, -6
  %15 = mul nuw nsw i64 %14, %13
  %16 = add nsw i64 %.013, -8
  %17 = mul nuw nsw i64 %16, %15
  %18 = add nsw i64 %.013, -10
  %19 = mul nuw nsw i64 %18, %17
  %20 = add nsw i64 %.013, -12
  %21 = mul nuw nsw i64 %20, %19
  %22 = add nsw i64 %.013, -14
  %23 = mul nuw nsw i64 %22, %21
  %24 = add nsw i64 %.013, -16
  %niter.next.7 = add i64 %niter, 8
  %niter.ncmp.7.not = icmp eq i64 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7.not, label %.loopexit.loopexit.unr-lcssa, label %.preheader, !llvm.loop !5

.loopexit.loopexit.unr-lcssa:                     ; preds = %.preheader, %.preheader.preheader
  %.lcssa.ph = phi i64 [ poison, %.preheader.preheader ], [ %23, %.preheader ]
  %.013.unr = phi i64 [ %0, %.preheader.preheader ], [ %24, %.preheader ]
  %.0912.unr = phi i64 [ 1, %.preheader.preheader ], [ %23, %.preheader ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit, label %.preheader.epil

.preheader.epil:                                  ; preds = %.loopexit.loopexit.unr-lcssa, %.preheader.epil
  %.013.epil = phi i64 [ %26, %.preheader.epil ], [ %.013.unr, %.loopexit.loopexit.unr-lcssa ]
  %.0912.epil = phi i64 [ %25, %.preheader.epil ], [ %.0912.unr, %.loopexit.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.preheader.epil ], [ 0, %.loopexit.loopexit.unr-lcssa ]
  %25 = mul nuw nsw i64 %.013.epil, %.0912.epil
  %26 = add nsw i64 %.013.epil, -2
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %.loopexit, label %.preheader.epil, !llvm.loop !7

.loopexit:                                        ; preds = %.loopexit.loopexit.unr-lcssa, %.preheader.epil, %3, %1
  %.010 = phi i64 [ 0, %1 ], [ 1, %3 ], [ %.lcssa.ph, %.loopexit.loopexit.unr-lcssa ], [ %25, %.preheader.epil ]
  ret i64 %.010
}

attributes #0 = { nofree norecurse nosync nounwind memory(none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !8}
!8 = !{!"llvm.loop.unroll.disable"}


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass