; LLVM IR from HarshCasper/NeoAlgo :: C/sort (HeapSort)

; 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: nounwind uwtable
declare dso_local void @removeMax(ptr noundef, i32 noundef) #0

; 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

; Function Attrs: nounwind uwtable
declare dso_local void @reBuildHeap(ptr noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local ptr @createHeap(ptr noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @heap_sort(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = load i32, ptr %4, align 4, !tbaa !9
  %10 = call ptr @createHeap(ptr noundef %8, i32 noundef %9)
  store ptr %10, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %11 = load i32, ptr %4, align 4, !tbaa !9
  store i32 %11, ptr %6, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %22, %2
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = icmp sgt i32 %13, 1
  br i1 %14, label %16, label %15

15:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %25

16:                                               ; preds = %12
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = load i32, ptr %6, align 4, !tbaa !9
  call void @removeMax(ptr noundef %17, i32 noundef %18)
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = load i32, ptr %6, align 4, !tbaa !9
  %21 = sub nsw i32 %20, 1
  call void @reBuildHeap(ptr noundef %19, i32 noundef %21)
  br label %22

22:                                               ; preds = %16
  %23 = load i32, ptr %6, align 4, !tbaa !9
  %24 = add nsw i32 %23, -1
  store i32 %24, ptr %6, align 4, !tbaa !9
  br label %12, !llvm.loop !11

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

26:                                               ; preds = %41, %25
  %27 = load i32, ptr %7, align 4, !tbaa !9
  %28 = load i32, ptr %4, align 4, !tbaa !9
  %29 = icmp sle i32 %27, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %44

31:                                               ; preds = %26
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = load i32, ptr %7, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i32, ptr %32, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !9
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = load i32, ptr %7, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds i32, ptr %37, i64 %39
  store i32 %36, ptr %40, align 4, !tbaa !9
  br label %41

41:                                               ; preds = %31
  %42 = load i32, ptr %7, align 4, !tbaa !9
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %7, align 4, !tbaa !9
  br label %26, !llvm.loop !13

44:                                               ; preds = %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret void
}

attributes #0 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; 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: nounwind uwtable
declare dso_local void @removeMax(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @reBuildHeap(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local ptr @createHeap(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @heap_sort(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = ptrtoint ptr %0 to i64
  %4 = tail call ptr @createHeap(ptr noundef %0, i32 noundef %1)
  %5 = ptrtoint ptr %4 to i64
  %6 = icmp sgt i32 %1, 1
  br i1 %6, label %.lr.ph, label %.preheader

.preheader:                                       ; preds = %.lr.ph, %2
  %.not17 = icmp slt i32 %1, 1
  br i1 %.not17, label %._crit_edge, label %.lr.ph19.preheader

.lr.ph19.preheader:                               ; preds = %.preheader
  %7 = add nuw i32 %1, 1
  %wide.trip.count = zext i32 %7 to i64
  %8 = zext nneg i32 %1 to i64
  %min.iters.check = icmp ult i32 %1, 8
  %9 = sub i64 %3, %5
  %diff.check = icmp ult i64 %9, 32
  %or.cond = select i1 %min.iters.check, i1 true, i1 %diff.check
  br i1 %or.cond, label %.lr.ph19.preheader23, label %vector.ph

.lr.ph19.preheader23:                             ; preds = %middle.block, %.lr.ph19.preheader
  %indvars.iv.ph = phi i64 [ 1, %.lr.ph19.preheader ], [ %ind.end, %middle.block ]
  %10 = sub nsw i64 %wide.trip.count, %indvars.iv.ph
  %11 = zext nneg i32 %1 to i64
  %12 = sub nsw i64 %11, %indvars.iv.ph
  %xtraiter = and i64 %10, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph19.prol.loopexit, label %.lr.ph19.prol

.lr.ph19.prol:                                    ; preds = %.lr.ph19.preheader23, %.lr.ph19.prol
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %.lr.ph19.prol ], [ %indvars.iv.ph, %.lr.ph19.preheader23 ]
  %prol.iter = phi i64 [ %prol.iter.next, %.lr.ph19.prol ], [ 0, %.lr.ph19.preheader23 ]
  %13 = getelementptr inbounds i32, ptr %4, i64 %indvars.iv.prol
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.prol
  store i32 %14, ptr %15, align 4, !tbaa !5
  %indvars.iv.next.prol = add nuw nsw i64 %indvars.iv.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph19.prol.loopexit, label %.lr.ph19.prol, !llvm.loop !9

.lr.ph19.prol.loopexit:                           ; preds = %.lr.ph19.prol, %.lr.ph19.preheader23
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %.lr.ph19.preheader23 ], [ %indvars.iv.next.prol, %.lr.ph19.prol ]
  %16 = icmp ult i64 %12, 3
  br i1 %16, label %._crit_edge, label %.lr.ph19

vector.ph:                                        ; preds = %.lr.ph19.preheader
  %n.vec = and i64 %8, 2147483640
  %ind.end = or disjoint i64 %n.vec, 1
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = or disjoint i64 %index, 1
  %17 = getelementptr inbounds i32, ptr %4, i64 %offset.idx
  %18 = getelementptr inbounds i8, ptr %17, i64 16
  %wide.load = load <4 x i32>, ptr %17, align 4, !tbaa !5
  %wide.load22 = load <4 x i32>, ptr %18, align 4, !tbaa !5
  %19 = getelementptr inbounds i32, ptr %0, i64 %offset.idx
  %20 = getelementptr inbounds i8, ptr %19, i64 16
  store <4 x i32> %wide.load, ptr %19, align 4, !tbaa !5
  store <4 x i32> %wide.load22, ptr %20, align 4, !tbaa !5
  %index.next = add nuw i64 %index, 8
  %21 = icmp eq i64 %index.next, %n.vec
  br i1 %21, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %8
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph19.preheader23

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.01516 = phi i32 [ %22, %.lr.ph ], [ %1, %2 ]
  tail call void @removeMax(ptr noundef %4, i32 noundef %.01516)
  %22 = add nsw i32 %.01516, -1
  tail call void @reBuildHeap(ptr noundef %4, i32 noundef %22)
  %23 = icmp ugt i32 %.01516, 2
  br i1 %23, label %.lr.ph, label %.preheader, !llvm.loop !15

._crit_edge:                                      ; preds = %.lr.ph19.prol.loopexit, %.lr.ph19, %middle.block, %.preheader
  ret void

.lr.ph19:                                         ; preds = %.lr.ph19.prol.loopexit, %.lr.ph19
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %.lr.ph19 ], [ %indvars.iv.unr, %.lr.ph19.prol.loopexit ]
  %24 = getelementptr inbounds i32, ptr %4, i64 %indvars.iv
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  store i32 %25, ptr %26, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %27 = getelementptr inbounds i32, ptr %4, i64 %indvars.iv.next
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  store i32 %28, ptr %29, align 4, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %30 = getelementptr inbounds i32, ptr %4, i64 %indvars.iv.next.1
  %31 = load i32, ptr %30, align 4, !tbaa !5
  %32 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next.1
  store i32 %31, ptr %32, align 4, !tbaa !5
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %33 = getelementptr inbounds i32, ptr %4, i64 %indvars.iv.next.2
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next.2
  store i32 %34, ptr %35, align 4, !tbaa !5
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %exitcond.not.3 = icmp eq i64 %indvars.iv.next.3, %wide.trip.count
  br i1 %exitcond.not.3, label %._crit_edge, label %.lr.ph19, !llvm.loop !16
}

attributes #0 = { 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" }

!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/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.unroll.disable"}
!11 = distinct !{!11, !12, !13, !14}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!"llvm.loop.isvectorized", i32 1}
!14 = !{!"llvm.loop.unroll.runtime.disable"}
!15 = distinct !{!15, !12}
!16 = distinct !{!16, !12, !13}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,SROAPass,SimplifyCFGPass,TailCallElimPass