; LLVM IR from AdarshGaur/Competitive-Programming :: Codeforces/Codeforces Round #702 (Div. 3)/d.ll (Z9partitionlll)

; 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"

@a = external dso_local global [102 x i64], align 16
@depth = external dso_local global [102 x i64], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z9partitionlll(i64 noundef %0, i64 noundef %1, i64 noundef %2) #0 {
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !5
  %10 = load i64, ptr %4, align 8, !tbaa !5
  %11 = load i64, ptr %5, align 8, !tbaa !5
  %12 = icmp sgt i64 %10, %11
  br i1 %12, label %13, label %14

13:                                               ; preds = %3
  br label %58

14:                                               ; preds = %3
  %15 = load i64, ptr %4, align 8, !tbaa !5
  %16 = load i64, ptr %5, align 8, !tbaa !5
  %17 = icmp eq i64 %15, %16
  br i1 %17, label %18, label %22

18:                                               ; preds = %14
  %19 = load i64, ptr %6, align 8, !tbaa !5
  %20 = load i64, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds [102 x i64], ptr @depth, i64 0, i64 %20
  store i64 %19, ptr %21, align 8, !tbaa !5
  br label %58

22:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  store i64 0, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  store i64 0, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %23 = load i64, ptr %4, align 8, !tbaa !5
  store i64 %23, ptr %9, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %41, %22
  %25 = load i64, ptr %9, align 8, !tbaa !5
  %26 = load i64, ptr %5, align 8, !tbaa !5
  %27 = icmp sle i64 %25, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  br label %44

29:                                               ; preds = %24
  %30 = load i64, ptr %7, align 8, !tbaa !5
  %31 = load i64, ptr %9, align 8, !tbaa !5
  %32 = getelementptr inbounds [102 x i64], ptr @a, i64 0, i64 %31
  %33 = load i64, ptr %32, align 8, !tbaa !5
  %34 = icmp slt i64 %30, %33
  br i1 %34, label %35, label %40

35:                                               ; preds = %29
  %36 = load i64, ptr %9, align 8, !tbaa !5
  %37 = getelementptr inbounds [102 x i64], ptr @a, i64 0, i64 %36
  %38 = load i64, ptr %37, align 8, !tbaa !5
  store i64 %38, ptr %7, align 8, !tbaa !5
  %39 = load i64, ptr %9, align 8, !tbaa !5
  store i64 %39, ptr %8, align 8, !tbaa !5
  br label %40

40:                                               ; preds = %35, %29
  br label %41

41:                                               ; preds = %40
  %42 = load i64, ptr %9, align 8, !tbaa !5
  %43 = add nsw i64 %42, 1
  store i64 %43, ptr %9, align 8, !tbaa !5
  br label %24, !llvm.loop !9

44:                                               ; preds = %28
  %45 = load i64, ptr %6, align 8, !tbaa !5
  %46 = load i64, ptr %8, align 8, !tbaa !5
  %47 = getelementptr inbounds [102 x i64], ptr @depth, i64 0, i64 %46
  store i64 %45, ptr %47, align 8, !tbaa !5
  %48 = load i64, ptr %4, align 8, !tbaa !5
  %49 = load i64, ptr %8, align 8, !tbaa !5
  %50 = sub nsw i64 %49, 1
  %51 = load i64, ptr %6, align 8, !tbaa !5
  %52 = add nsw i64 %51, 1
  call void @_Z9partitionlll(i64 noundef %48, i64 noundef %50, i64 noundef %52)
  %53 = load i64, ptr %8, align 8, !tbaa !5
  %54 = add nsw i64 %53, 1
  %55 = load i64, ptr %5, align 8, !tbaa !5
  %56 = load i64, ptr %6, align 8, !tbaa !5
  %57 = add nsw i64 %56, 1
  call void @_Z9partitionlll(i64 noundef %54, i64 noundef %55, i64 noundef %57)
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  br label %58

58:                                               ; preds = %44, %18, %13
  ret void
}

; 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 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 = !{!"long", !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"

@a = external dso_local local_unnamed_addr global [102 x i64], align 16
@depth = external dso_local local_unnamed_addr global [102 x i64], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z9partitionlll(i64 noundef %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp sgt i64 %0, %1
  br i1 %4, label %.loopexit, label %.lr.ph40.preheader

.lr.ph40.preheader:                               ; preds = %3
  %5 = add i64 %1, 1
  br label %.lr.ph40

.lr.ph40:                                         ; preds = %.lr.ph40.preheader, %tailrecurse
  %.tr3038 = phi i64 [ %18, %tailrecurse ], [ %2, %.lr.ph40.preheader ]
  %.tr37 = phi i64 [ %19, %tailrecurse ], [ %0, %.lr.ph40.preheader ]
  %6 = icmp eq i64 %.tr37, %1
  br i1 %6, label %10, label %.preheader

.preheader:                                       ; preds = %.lr.ph40
  %.not33 = icmp sgt i64 %.tr37, %1
  br i1 %.not33, label %tailrecurse, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %7 = sub i64 %5, %.tr37
  %8 = sub i64 %1, %.tr37
  %xtraiter = and i64 %7, 3
  %9 = icmp ult i64 %8, 3
  br i1 %9, label %tailrecurse.loopexit.unr-lcssa, label %.lr.ph.preheader.new

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

10:                                               ; preds = %.lr.ph40
  %11 = getelementptr inbounds [102 x i64], ptr @depth, i64 0, i64 %1
  store i64 %.tr3038, ptr %11, align 8, !tbaa !5
  br label %.loopexit

tailrecurse.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %spec.select28.lcssa.ph = phi i64 [ poison, %.lr.ph.preheader ], [ %spec.select28.3, %.lr.ph ]
  %.036.unr = phi i64 [ %.tr37, %.lr.ph.preheader ], [ %35, %.lr.ph ]
  %.02235.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %spec.select28.3, %.lr.ph ]
  %.02334.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %spec.select.3, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %tailrecurse, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %tailrecurse.loopexit.unr-lcssa, %.lr.ph.epil
  %.036.epil = phi i64 [ %15, %.lr.ph.epil ], [ %.036.unr, %tailrecurse.loopexit.unr-lcssa ]
  %.02235.epil = phi i64 [ %spec.select28.epil, %.lr.ph.epil ], [ %.02235.unr, %tailrecurse.loopexit.unr-lcssa ]
  %.02334.epil = phi i64 [ %spec.select.epil, %.lr.ph.epil ], [ %.02334.unr, %tailrecurse.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %tailrecurse.loopexit.unr-lcssa ]
  %12 = getelementptr inbounds [102 x i64], ptr @a, i64 0, i64 %.036.epil
  %13 = load i64, ptr %12, align 8, !tbaa !5
  %14 = icmp slt i64 %.02334.epil, %13
  %spec.select.epil = tail call i64 @llvm.smax.i64(i64 %.02334.epil, i64 %13)
  %spec.select28.epil = select i1 %14, i64 %.036.epil, i64 %.02235.epil
  %15 = add i64 %.036.epil, 1
  %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 %tailrecurse, label %.lr.ph.epil, !llvm.loop !9

tailrecurse:                                      ; preds = %tailrecurse.loopexit.unr-lcssa, %.lr.ph.epil, %.preheader
  %.022.lcssa = phi i64 [ 0, %.preheader ], [ %spec.select28.lcssa.ph, %tailrecurse.loopexit.unr-lcssa ], [ %spec.select28.epil, %.lr.ph.epil ]
  %16 = getelementptr inbounds [102 x i64], ptr @depth, i64 0, i64 %.022.lcssa
  store i64 %.tr3038, ptr %16, align 8, !tbaa !5
  %17 = add nsw i64 %.022.lcssa, -1
  %18 = add nsw i64 %.tr3038, 1
  tail call void @_Z9partitionlll(i64 noundef %.tr37, i64 noundef %17, i64 noundef %18)
  %19 = add nsw i64 %.022.lcssa, 1
  %.not = icmp slt i64 %.022.lcssa, %1
  br i1 %.not, label %.lr.ph40, label %.loopexit

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.036 = phi i64 [ %.tr37, %.lr.ph.preheader.new ], [ %35, %.lr.ph ]
  %.02235 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %spec.select28.3, %.lr.ph ]
  %.02334 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %spec.select.3, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %20 = getelementptr inbounds [102 x i64], ptr @a, i64 0, i64 %.036
  %21 = load i64, ptr %20, align 8, !tbaa !5
  %22 = icmp slt i64 %.02334, %21
  %spec.select = tail call i64 @llvm.smax.i64(i64 %.02334, i64 %21)
  %spec.select28 = select i1 %22, i64 %.036, i64 %.02235
  %23 = add i64 %.036, 1
  %24 = getelementptr inbounds [102 x i64], ptr @a, i64 0, i64 %23
  %25 = load i64, ptr %24, align 8, !tbaa !5
  %26 = icmp slt i64 %spec.select, %25
  %spec.select.1 = tail call i64 @llvm.smax.i64(i64 %spec.select, i64 %25)
  %spec.select28.1 = select i1 %26, i64 %23, i64 %spec.select28
  %27 = add i64 %.036, 2
  %28 = getelementptr inbounds [102 x i64], ptr @a, i64 0, i64 %27
  %29 = load i64, ptr %28, align 8, !tbaa !5
  %30 = icmp slt i64 %spec.select.1, %29
  %spec.select.2 = tail call i64 @llvm.smax.i64(i64 %spec.select.1, i64 %29)
  %spec.select28.2 = select i1 %30, i64 %27, i64 %spec.select28.1
  %31 = add i64 %.036, 3
  %32 = getelementptr inbounds [102 x i64], ptr @a, i64 0, i64 %31
  %33 = load i64, ptr %32, align 8, !tbaa !5
  %34 = icmp slt i64 %spec.select.2, %33
  %spec.select.3 = tail call i64 @llvm.smax.i64(i64 %spec.select.2, i64 %33)
  %spec.select28.3 = select i1 %34, i64 %31, i64 %spec.select28.2
  %35 = add i64 %.036, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %tailrecurse.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !11

.loopexit:                                        ; preds = %tailrecurse, %3, %10
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smax.i64(i64, i64) #1

attributes #0 = { mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: 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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.unroll.disable"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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