; LLVM IR from Leet-Us-Code/DSA-A-Z-Practice :: Companies/Intuit/Distribute N candies among 3.ll (Z8solutioni)

; 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: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z8solutioni(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %6 = load i32, ptr %2, align 4, !tbaa !5
  %7 = sub nsw i32 %6, 1
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = sub nsw i32 %8, 2
  %10 = mul nsw i32 %7, %9
  %11 = sdiv i32 %10, 2
  store i32 %11, ptr %3, align 4, !tbaa !5
  %12 = load i32, ptr %2, align 4, !tbaa !5
  %13 = srem i32 %12, 3
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %15, label %18

15:                                               ; preds = %1
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = add nsw i32 %16, -1
  store i32 %17, ptr %3, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %15, %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 1, ptr %4, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %39, %18
  %20 = load i32, ptr %4, align 4, !tbaa !5
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = icmp sle i32 %20, %21
  br i1 %22, label %24, label %23

23:                                               ; preds = %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %42

24:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %25 = load i32, ptr %2, align 4, !tbaa !5
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = mul nsw i32 2, %26
  %28 = sub nsw i32 %25, %27
  store i32 %28, ptr %5, align 4, !tbaa !5
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = icmp sgt i32 %29, 0
  br i1 %30, label %31, label %38

31:                                               ; preds = %24
  %32 = load i32, ptr %5, align 4, !tbaa !5
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = icmp slt i32 %32, %33
  br i1 %34, label %35, label %38

35:                                               ; preds = %31
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = sub nsw i32 %36, 3
  store i32 %37, ptr %3, align 4, !tbaa !5
  br label %38

38:                                               ; preds = %35, %31, %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %39

39:                                               ; preds = %38
  %40 = load i32, ptr %4, align 4, !tbaa !5
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %4, align 4, !tbaa !5
  br label %19, !llvm.loop !9

42:                                               ; preds = %23
  %43 = load i32, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %43
}

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

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

attributes #0 = { mustprogress 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple 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: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local noundef i32 @_Z8solutioni(i32 noundef %0) local_unnamed_addr #0 {
  %2 = add nsw i32 %0, -1
  %3 = add nsw i32 %0, -2
  %4 = mul nsw i32 %2, %3
  %5 = sdiv i32 %4, 2
  %6 = srem i32 %0, 3
  %7 = icmp eq i32 %6, 0
  %8 = sext i1 %7 to i32
  %spec.select = add nsw i32 %5, %8
  %.not17 = icmp slt i32 %0, 1
  br i1 %.not17, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %1
  %xtraiter = and i32 %0, 1
  %9 = icmp eq i32 %0, 1
  br i1 %9, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i32 %0, 2147483646
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.2.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %.2.1, %.lr.ph ]
  %.119.unr = phi i32 [ %spec.select, %.lr.ph.preheader ], [ %.2.1, %.lr.ph ]
  %.01418.unr = phi i32 [ 1, %.lr.ph.preheader ], [ %26, %.lr.ph ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %10 = shl nuw nsw i32 %.01418.unr, 1
  %11 = sub nsw i32 %0, %10
  %12 = icmp sgt i32 %11, 0
  %13 = icmp slt i32 %11, %.01418.unr
  %or.cond.epil = and i1 %12, %13
  %14 = add nsw i32 %.119.unr, -3
  %.2.epil = select i1 %or.cond.epil, i32 %14, i32 %.119.unr
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa, %1
  %.1.lcssa = phi i32 [ %spec.select, %1 ], [ %.2.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %.2.epil, %.lr.ph.epil ]
  ret i32 %.1.lcssa

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.119 = phi i32 [ %spec.select, %.lr.ph.preheader.new ], [ %.2.1, %.lr.ph ]
  %.01418 = phi i32 [ 1, %.lr.ph.preheader.new ], [ %26, %.lr.ph ]
  %niter = phi i32 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %15 = shl nuw nsw i32 %.01418, 1
  %16 = sub nsw i32 %0, %15
  %17 = icmp sgt i32 %16, 0
  %18 = icmp slt i32 %16, %.01418
  %or.cond = and i1 %17, %18
  %19 = add nsw i32 %.119, -3
  %.2 = select i1 %or.cond, i32 %19, i32 %.119
  %20 = add nuw i32 %.01418, 1
  %21 = shl nuw nsw i32 %20, 1
  %22 = sub nsw i32 %0, %21
  %23 = icmp sgt i32 %22, 0
  %24 = icmp slt i32 %22, %20
  %or.cond.1 = and i1 %23, %24
  %25 = add nsw i32 %.2, -3
  %.2.1 = select i1 %or.cond.1, i32 %25, i32 %.2
  %26 = add nuw i32 %.01418, 2
  %niter.next.1 = add nuw i32 %niter, 2
  %niter.ncmp.1 = icmp eq i32 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !5
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn 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"}


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