; LLVM IR from xxks-kkk/algo :: heaps/min-max (mm)

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

%struct.MinMaxHeapStruct = type { i32, i32, ptr }

; Function Attrs: nounwind uwtable
declare dso_local ptr @initializeMMH(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 hidden i32 @getDepth(i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local ptr @buildMMH(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
  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
  %7 = load i32, ptr %4, align 4, !tbaa !9
  %8 = mul nsw i32 2, %7
  %9 = call ptr @initializeMMH(i32 noundef %8)
  store ptr %9, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 1, ptr %6, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %28, %2
  %11 = load i32, ptr %6, align 4, !tbaa !9
  %12 = load i32, ptr %4, align 4, !tbaa !9
  %13 = add nsw i32 %12, 1
  %14 = icmp slt i32 %11, %13
  br i1 %14, label %15, label %31

15:                                               ; preds = %10
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = load i32, ptr %6, align 4, !tbaa !9
  %18 = sub nsw i32 %17, 1
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds i32, ptr %16, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !9
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.MinMaxHeapStruct, ptr %22, i32 0, i32 2
  %24 = load ptr, ptr %23, align 8, !tbaa !11
  %25 = load i32, ptr %6, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i32, ptr %24, i64 %26
  store i32 %21, ptr %27, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %15
  %29 = load i32, ptr %6, align 4, !tbaa !9
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %6, align 4, !tbaa !9
  br label %10, !llvm.loop !13

31:                                               ; preds = %10
  %32 = load i32, ptr %4, align 4, !tbaa !9
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.MinMaxHeapStruct, ptr %33, i32 0, i32 1
  store i32 %32, ptr %34, align 4, !tbaa !15
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.MinMaxHeapStruct, ptr %35, i32 0, i32 1
  %37 = load i32, ptr %36, align 4, !tbaa !15
  %38 = sdiv i32 %37, 2
  store i32 %38, ptr %6, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %54, %31
  %40 = load i32, ptr %6, align 4, !tbaa !9
  %41 = icmp sgt i32 %40, 0
  br i1 %41, label %42, label %57

42:                                               ; preds = %39
  %43 = load i32, ptr %6, align 4, !tbaa !9
  %44 = call i32 @getDepth(i32 noundef %43)
  %45 = srem i32 %44, 2
  %46 = icmp eq i32 %45, 0
  br i1 %46, label %47, label %50

47:                                               ; preds = %42
  %48 = load i32, ptr %6, align 4, !tbaa !9
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  call void @percolateDownMin(i32 noundef %48, ptr noundef %49)
  br label %53

50:                                               ; preds = %42
  %51 = load i32, ptr %6, align 4, !tbaa !9
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  call void @percolateDownMax(i32 noundef %51, ptr noundef %52)
  br label %53

53:                                               ; preds = %50, %47
  br label %54

54:                                               ; preds = %53
  %55 = load i32, ptr %6, align 4, !tbaa !9
  %56 = add nsw i32 %55, -1
  store i32 %56, ptr %6, align 4, !tbaa !9
  br label %39, !llvm.loop !16

57:                                               ; preds = %39
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret ptr %58
}

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

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

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 = !{!12, !6, i64 8}
!12 = !{!"MinMaxHeapStruct", !10, i64 0, !10, i64 4, !6, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!12, !10, i64 4}
!16 = distinct !{!16, !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 ptr @initializeMMH(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden i32 @getDepth(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @buildMMH(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = ptrtoint ptr %0 to i64
  %4 = shl nsw i32 %1, 1
  %5 = tail call ptr @initializeMMH(i32 noundef %4)
  %invariant.gep = getelementptr i8, ptr %0, i64 -4
  %.not18 = icmp slt i32 %1, 1
  br i1 %.not18, label %._crit_edge.thread, label %.lr.ph

._crit_edge.thread:                               ; preds = %2
  %6 = getelementptr inbounds i8, ptr %5, i64 4
  store i32 %1, ptr %6, align 4, !tbaa !5
  br label %._crit_edge23

.lr.ph:                                           ; preds = %2
  %7 = getelementptr inbounds i8, ptr %5, i64 8
  %8 = load ptr, ptr %7, align 8, !tbaa !11
  %9 = add nuw i32 %1, 1
  %wide.trip.count = zext i32 %9 to i64
  %10 = zext nneg i32 %1 to i64
  %min.iters.check = icmp ult i32 %1, 8
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.memcheck

vector.memcheck:                                  ; preds = %.lr.ph
  %11 = ptrtoint ptr %8 to i64
  %12 = add i64 %11, 4
  %13 = sub i64 %12, %3
  %diff.check = icmp ult i64 %13, 32
  br i1 %diff.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %n.vec = and i64 %10, 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
  %14 = getelementptr i32, ptr %0, i64 %index
  %15 = getelementptr i8, ptr %14, i64 16
  %wide.load = load <4 x i32>, ptr %14, align 4, !tbaa !12
  %wide.load25 = load <4 x i32>, ptr %15, align 4, !tbaa !12
  %16 = getelementptr inbounds i32, ptr %8, i64 %offset.idx
  %17 = getelementptr inbounds i8, ptr %16, i64 16
  store <4 x i32> %wide.load, ptr %16, align 4, !tbaa !12
  store <4 x i32> %wide.load25, ptr %17, align 4, !tbaa !12
  %index.next = add nuw i64 %index, 8
  %18 = icmp eq i64 %index.next, %n.vec
  br i1 %18, label %middle.block, label %vector.body, !llvm.loop !13

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %10
  br i1 %cmp.n, label %._crit_edge, label %scalar.ph.preheader

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

scalar.ph.prol:                                   ; preds = %scalar.ph.preheader, %scalar.ph.prol
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %scalar.ph.prol ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %scalar.ph.prol ], [ 0, %scalar.ph.preheader ]
  %gep.prol = getelementptr i32, ptr %invariant.gep, i64 %indvars.iv.prol
  %22 = load i32, ptr %gep.prol, align 4, !tbaa !12
  %23 = getelementptr inbounds i32, ptr %8, i64 %indvars.iv.prol
  store i32 %22, ptr %23, align 4, !tbaa !12
  %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 %scalar.ph.prol.loopexit, label %scalar.ph.prol, !llvm.loop !17

scalar.ph.prol.loopexit:                          ; preds = %scalar.ph.prol, %scalar.ph.preheader
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %scalar.ph.preheader ], [ %indvars.iv.next.prol, %scalar.ph.prol ]
  %24 = icmp ult i64 %21, 3
  br i1 %24, label %._crit_edge, label %scalar.ph.preheader.new

scalar.ph.preheader.new:                          ; preds = %scalar.ph.prol.loopexit
  %invariant.gep26 = getelementptr i8, ptr %8, i64 4
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph, %scalar.ph.preheader.new
  %indvars.iv = phi i64 [ %indvars.iv.unr, %scalar.ph.preheader.new ], [ %indvars.iv.next.3, %scalar.ph ]
  %gep = getelementptr i32, ptr %invariant.gep, i64 %indvars.iv
  %25 = load i32, ptr %gep, align 4, !tbaa !12
  %26 = getelementptr inbounds i32, ptr %8, i64 %indvars.iv
  store i32 %25, ptr %26, align 4, !tbaa !12
  %gep.1 = getelementptr i32, ptr %0, i64 %indvars.iv
  %27 = load i32, ptr %gep.1, align 4, !tbaa !12
  %gep27 = getelementptr i32, ptr %invariant.gep26, i64 %indvars.iv
  store i32 %27, ptr %gep27, align 4, !tbaa !12
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %gep.2 = getelementptr i32, ptr %invariant.gep, i64 %indvars.iv.next.1
  %28 = load i32, ptr %gep.2, align 4, !tbaa !12
  %29 = getelementptr inbounds i32, ptr %8, i64 %indvars.iv.next.1
  store i32 %28, ptr %29, align 4, !tbaa !12
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %gep.3 = getelementptr i32, ptr %invariant.gep, i64 %indvars.iv.next.2
  %30 = load i32, ptr %gep.3, align 4, !tbaa !12
  %31 = getelementptr inbounds i32, ptr %8, i64 %indvars.iv.next.2
  store i32 %30, ptr %31, align 4, !tbaa !12
  %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 %scalar.ph, !llvm.loop !19

._crit_edge:                                      ; preds = %scalar.ph.prol.loopexit, %scalar.ph, %middle.block
  %32 = getelementptr inbounds i8, ptr %5, i64 4
  store i32 %1, ptr %32, align 4, !tbaa !5
  %.not = icmp eq i32 %1, 1
  br i1 %.not, label %._crit_edge23, label %.lr.ph22.preheader

.lr.ph22.preheader:                               ; preds = %._crit_edge
  %33 = lshr i32 %1, 1
  br label %.lr.ph22

.lr.ph22:                                         ; preds = %.lr.ph22.preheader, %39
  %.120 = phi i32 [ %40, %39 ], [ %33, %.lr.ph22.preheader ]
  %34 = tail call i32 @getDepth(i32 noundef %.120)
  %35 = and i32 %34, 1
  %36 = icmp eq i32 %35, 0
  br i1 %36, label %37, label %38

37:                                               ; preds = %.lr.ph22
  tail call void @percolateDownMin(i32 noundef %.120, ptr noundef %5)
  br label %39

38:                                               ; preds = %.lr.ph22
  tail call void @percolateDownMax(i32 noundef %.120, ptr noundef %5)
  br label %39

39:                                               ; preds = %37, %38
  %40 = add nsw i32 %.120, -1
  %41 = icmp sgt i32 %.120, 1
  br i1 %41, label %.lr.ph22, label %._crit_edge23, !llvm.loop !20

._crit_edge23:                                    ; preds = %39, %._crit_edge.thread, %._crit_edge
  ret ptr %5
}

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

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

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, !7, i64 4}
!6 = !{!"MinMaxHeapStruct", !7, i64 0, !7, i64 4, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!7, !7, i64 0}
!13 = distinct !{!13, !14, !15, !16}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!"llvm.loop.isvectorized", i32 1}
!16 = !{!"llvm.loop.unroll.runtime.disable"}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.unroll.disable"}
!19 = distinct !{!19, !14, !15}
!20 = distinct !{!20, !14}


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