; LLVM IR from sbshah97/DS-Algo-Handbook :: Algorithms/Sort_Algorithms/Heap_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"

@ar = external dso_local global [100 x i32], align 16
@sz = external dso_local global i32, align 4

; Function Attrs: nounwind uwtable
declare dso_local void @heapify(i32 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
define dso_local void @heapSort() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  %4 = load i32, ptr @sz, align 4, !tbaa !5
  %5 = sdiv i32 %4, 2
  %6 = sub nsw i32 %5, 1
  store i32 %6, ptr %2, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %14, %0
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = icmp sge i32 %8, 0
  br i1 %9, label %11, label %10

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

11:                                               ; preds = %7
  %12 = load i32, ptr @sz, align 4, !tbaa !5
  %13 = load i32, ptr %2, align 4, !tbaa !5
  call void @heapify(i32 noundef %12, i32 noundef %13)
  br label %14

14:                                               ; preds = %11
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = add nsw i32 %15, -1
  store i32 %16, ptr %2, align 4, !tbaa !5
  br label %7, !llvm.loop !9

17:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %18 = load i32, ptr @sz, align 4, !tbaa !5
  %19 = sub nsw i32 %18, 1
  store i32 %19, ptr %3, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %35, %17
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = icmp sge i32 %21, 0
  br i1 %22, label %24, label %23

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

24:                                               ; preds = %20
  %25 = load i32, ptr @ar, align 16, !tbaa !5
  store i32 %25, ptr %1, align 4, !tbaa !5
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [100 x i32], ptr @ar, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  store i32 %29, ptr @ar, align 16, !tbaa !5
  %30 = load i32, ptr %1, align 4, !tbaa !5
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [100 x i32], ptr @ar, i64 0, i64 %32
  store i32 %30, ptr %33, align 4, !tbaa !5
  %34 = load i32, ptr %3, align 4, !tbaa !5
  call void @heapify(i32 noundef %34, i32 noundef 0)
  br label %35

35:                                               ; preds = %24
  %36 = load i32, ptr %3, align 4, !tbaa !5
  %37 = add nsw i32 %36, -1
  store i32 %37, ptr %3, align 4, !tbaa !5
  br label %20, !llvm.loop !11

38:                                               ; preds = %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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

@ar = external dso_local local_unnamed_addr global [100 x i32], align 16
@sz = external dso_local local_unnamed_addr global i32, align 4

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

; Function Attrs: nounwind uwtable
define dso_local void @heapSort() local_unnamed_addr #0 {
  %1 = load i32, ptr @sz, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 1
  br i1 %2, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %0
  %3 = lshr i32 %1, 1
  br label %.lr.ph

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre = load i32, ptr @sz, align 4, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %0
  %4 = phi i32 [ %.pre, %._crit_edge.loopexit ], [ %1, %0 ]
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.lr.ph13.preheader, label %._crit_edge14

.lr.ph13.preheader:                               ; preds = %._crit_edge
  %6 = zext nneg i32 %4 to i64
  br label %.lr.ph13

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.09.in10 = phi i32 [ %.09, %.lr.ph ], [ %3, %.lr.ph.preheader ]
  %.09 = add nsw i32 %.09.in10, -1
  %7 = load i32, ptr @sz, align 4, !tbaa !5
  tail call void @heapify(i32 noundef %7, i32 noundef %.09)
  %8 = icmp ugt i32 %.09.in10, 1
  br i1 %8, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !9

._crit_edge14:                                    ; preds = %.lr.ph13, %._crit_edge
  ret void

.lr.ph13:                                         ; preds = %.lr.ph13.preheader, %.lr.ph13
  %indvars.iv = phi i64 [ %6, %.lr.ph13.preheader ], [ %indvars.iv.next, %.lr.ph13 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %9 = load i32, ptr @ar, align 16, !tbaa !5
  %10 = getelementptr inbounds [100 x i32], ptr @ar, i64 0, i64 %indvars.iv.next
  %11 = load i32, ptr %10, align 4, !tbaa !5
  store i32 %11, ptr @ar, align 16, !tbaa !5
  store i32 %9, ptr %10, align 4, !tbaa !5
  %12 = trunc nuw nsw i64 %indvars.iv.next to i32
  tail call void @heapify(i32 noundef %12, i32 noundef 0)
  %13 = icmp ugt i64 %indvars.iv, 1
  br i1 %13, label %.lr.ph13, label %._crit_edge14, !llvm.loop !11
}

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.mustprogress"}
!11 = distinct !{!11, !10}


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