; LLVM IR from Innocentsax/sorting_algorithms :: 104-heap_sort (heap_sort)

; 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 @swap_ints(ptr noundef, ptr 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 @max_heapify(ptr noundef, i64 noundef, i64 noundef, i64 noundef) #0

declare void @print_array(ptr noundef, i64 noundef) #2

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

9:                                                ; preds = %2
  %10 = load i64, ptr %4, align 8, !tbaa !9
  %11 = icmp ult i64 %10, 2
  br i1 %11, label %12, label %13

12:                                               ; preds = %9, %2
  store i32 1, ptr %6, align 4
  br label %53

13:                                               ; preds = %9
  %14 = load i64, ptr %4, align 8, !tbaa !9
  %15 = udiv i64 %14, 2
  %16 = sub i64 %15, 1
  %17 = trunc i64 %16 to i32
  store i32 %17, ptr %5, align 4, !tbaa !11
  br label %18

18:                                               ; preds = %27, %13
  %19 = load i32, ptr %5, align 4, !tbaa !11
  %20 = icmp sge i32 %19, 0
  br i1 %20, label %21, label %30

21:                                               ; preds = %18
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = load i64, ptr %4, align 8, !tbaa !9
  %24 = load i64, ptr %4, align 8, !tbaa !9
  %25 = load i32, ptr %5, align 4, !tbaa !11
  %26 = sext i32 %25 to i64
  call void @max_heapify(ptr noundef %22, i64 noundef %23, i64 noundef %24, i64 noundef %26)
  br label %27

27:                                               ; preds = %21
  %28 = load i32, ptr %5, align 4, !tbaa !11
  %29 = add nsw i32 %28, -1
  store i32 %29, ptr %5, align 4, !tbaa !11
  br label %18, !llvm.loop !13

30:                                               ; preds = %18
  %31 = load i64, ptr %4, align 8, !tbaa !9
  %32 = sub i64 %31, 1
  %33 = trunc i64 %32 to i32
  store i32 %33, ptr %5, align 4, !tbaa !11
  br label %34

34:                                               ; preds = %49, %30
  %35 = load i32, ptr %5, align 4, !tbaa !11
  %36 = icmp sgt i32 %35, 0
  br i1 %36, label %37, label %52

37:                                               ; preds = %34
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = load i32, ptr %5, align 4, !tbaa !11
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds i32, ptr %39, i64 %41
  call void @swap_ints(ptr noundef %38, ptr noundef %42)
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = load i64, ptr %4, align 8, !tbaa !9
  call void @print_array(ptr noundef %43, i64 noundef %44)
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = load i64, ptr %4, align 8, !tbaa !9
  %47 = load i32, ptr %5, align 4, !tbaa !11
  %48 = sext i32 %47 to i64
  call void @max_heapify(ptr noundef %45, i64 noundef %46, i64 noundef %48, i64 noundef 0)
  br label %49

49:                                               ; preds = %37
  %50 = load i32, ptr %5, align 4, !tbaa !11
  %51 = add nsw i32 %50, -1
  store i32 %51, ptr %5, align 4, !tbaa !11
  br label %34, !llvm.loop !15

52:                                               ; preds = %34
  store i32 0, ptr %6, align 4
  br label %53

53:                                               ; preds = %52, %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  %54 = load i32, ptr %6, align 4
  switch i32 %54, label %56 [
    i32 0, label %55
    i32 1, label %55
  ]

55:                                               ; preds = %53, %53
  ret void

56:                                               ; preds = %53
  unreachable
}

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 = { "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 #3 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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 @swap_ints(ptr noundef, ptr noundef) local_unnamed_addr #0

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

declare void @print_array(ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @heap_sort(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  %4 = icmp ult i64 %1, 2
  %or.cond = or i1 %3, %4
  br i1 %or.cond, label %.loopexit, label %5

5:                                                ; preds = %2
  %6 = lshr i64 %1, 1
  %7 = trunc i64 %6 to i32
  %.022 = add i32 %7, -1
  %8 = icmp sgt i32 %.022, -1
  br i1 %8, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %5
  %9 = zext nneg i32 %.022 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ %9, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  tail call void @max_heapify(ptr noundef %0, i64 noundef %1, i64 noundef %1, i64 noundef %indvars.iv)
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %.not = icmp eq i64 %indvars.iv, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !5

._crit_edge:                                      ; preds = %.lr.ph, %5
  %10 = trunc i64 %1 to i32
  %.124 = add i32 %10, -1
  %11 = icmp sgt i32 %.124, 0
  br i1 %11, label %.lr.ph27.preheader, label %.loopexit

.lr.ph27.preheader:                               ; preds = %._crit_edge
  %12 = zext nneg i32 %.124 to i64
  br label %.lr.ph27

.lr.ph27:                                         ; preds = %.lr.ph27.preheader, %.lr.ph27
  %indvars.iv29 = phi i64 [ %12, %.lr.ph27.preheader ], [ %indvars.iv.next30, %.lr.ph27 ]
  %13 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv29
  tail call void @swap_ints(ptr noundef %0, ptr noundef nonnull %13)
  tail call void @print_array(ptr noundef %0, i64 noundef %1) #2
  tail call void @max_heapify(ptr noundef %0, i64 noundef %1, i64 noundef %indvars.iv29, i64 noundef 0)
  %indvars.iv.next30 = add nsw i64 %indvars.iv29, -1
  %14 = icmp ugt i64 %indvars.iv29, 1
  br i1 %14, label %.lr.ph27, label %.loopexit, !llvm.loop !7

.loopexit:                                        ; preds = %.lr.ph27, %._crit_edge, %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 = { "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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}


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