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

@.str.2 = external hidden unnamed_addr constant [15 x i8], align 1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

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

10:                                               ; preds = %1
  call void @fatal(ptr noundef @.str.2)
  br label %11

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

27:                                               ; preds = %78, %11
  %28 = load i32, ptr %3, align 4, !tbaa !12
  %29 = mul nsw i32 %28, 4
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.MinMaxHeapStruct, ptr %30, i32 0, i32 1
  %32 = load i32, ptr %31, align 4, !tbaa !13
  %33 = icmp sle i32 %29, %32
  br i1 %33, label %34, label %80

34:                                               ; preds = %27
  %35 = load i32, ptr %3, align 4, !tbaa !12
  %36 = mul nsw i32 %35, 4
  %37 = load ptr, ptr %2, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.MinMaxHeapStruct, ptr %37, i32 0, i32 2
  %39 = load ptr, ptr %38, align 8, !tbaa !9
  %40 = load i32, ptr %3, align 4, !tbaa !12
  %41 = mul nsw i32 %40, 4
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds i32, ptr %39, i64 %42
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.MinMaxHeapStruct, ptr %44, i32 0, i32 1
  %46 = load i32, ptr %45, align 4, !tbaa !13
  %47 = load i32, ptr %3, align 4, !tbaa !12
  %48 = mul nsw i32 %47, 4
  %49 = sub nsw i32 %46, %48
  %50 = call i32 @min(i32 noundef 4, i32 noundef %49)
  %51 = call i32 @findMin(ptr noundef %43, i32 noundef %50)
  %52 = add nsw i32 %36, %51
  store i32 %52, ptr %4, align 4, !tbaa !12
  %53 = load i32, ptr %6, align 4, !tbaa !12
  %54 = load ptr, ptr %2, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.MinMaxHeapStruct, ptr %54, i32 0, i32 2
  %56 = load ptr, ptr %55, align 8, !tbaa !9
  %57 = load i32, ptr %4, align 4, !tbaa !12
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds i32, ptr %56, i64 %58
  %60 = load i32, ptr %59, align 4, !tbaa !12
  %61 = icmp sgt i32 %53, %60
  br i1 %61, label %62, label %76

62:                                               ; preds = %34
  %63 = load ptr, ptr %2, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.MinMaxHeapStruct, ptr %63, i32 0, i32 2
  %65 = load ptr, ptr %64, align 8, !tbaa !9
  %66 = load i32, ptr %4, align 4, !tbaa !12
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds i32, ptr %65, i64 %67
  %69 = load i32, ptr %68, align 4, !tbaa !12
  %70 = load ptr, ptr %2, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.MinMaxHeapStruct, ptr %70, i32 0, i32 2
  %72 = load ptr, ptr %71, align 8, !tbaa !9
  %73 = load i32, ptr %3, align 4, !tbaa !12
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds i32, ptr %72, i64 %74
  store i32 %69, ptr %75, align 4, !tbaa !12
  br label %77

76:                                               ; preds = %34
  br label %80

77:                                               ; preds = %62
  br label %78

78:                                               ; preds = %77
  %79 = load i32, ptr %4, align 4, !tbaa !12
  store i32 %79, ptr %3, align 4, !tbaa !12
  br label %27, !llvm.loop !14

80:                                               ; preds = %76, %27
  %81 = load i32, ptr %6, align 4, !tbaa !12
  %82 = load ptr, ptr %2, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.MinMaxHeapStruct, ptr %82, i32 0, i32 2
  %84 = load ptr, ptr %83, align 8, !tbaa !9
  %85 = load i32, ptr %3, align 4, !tbaa !12
  %86 = sext i32 %85 to i64
  %87 = getelementptr inbounds i32, ptr %84, i64 %86
  store i32 %81, ptr %87, align 4, !tbaa !12
  %88 = load i32, ptr %5, align 4, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret i32 %88
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @isEmpty(ptr noundef) #1

declare void @fatal(ptr noundef) #2

declare i32 @findMin(ptr noundef, i32 noundef) #2

declare i32 @min(i32 noundef, i32 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !6, i64 8}
!10 = !{!"MinMaxHeapStruct", !11, i64 0, !11, i64 4, !6, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!10, !11, i64 4}
!14 = distinct !{!14, !15}
!15 = !{!"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"

@.str.2 = external hidden unnamed_addr constant [15 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @deleteMinMHH(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call i32 @isEmpty(ptr noundef %0)
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %4, label %3

3:                                                ; preds = %1
  tail call void @fatal(ptr noundef nonnull @.str.2) #2
  br label %4

4:                                                ; preds = %3, %1
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %6, i64 4
  %8 = load i32, ptr %7, align 4, !tbaa !11
  %9 = getelementptr inbounds i8, ptr %0, i64 4
  %10 = load i32, ptr %9, align 4, !tbaa !12
  %11 = add nsw i32 %10, -1
  store i32 %11, ptr %9, align 4, !tbaa !12
  %12 = sext i32 %10 to i64
  %13 = getelementptr inbounds i32, ptr %6, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !11
  %.not2526 = icmp slt i32 %10, 5
  br i1 %.not2526, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4, %29
  %15 = phi ptr [ %24, %29 ], [ %6, %4 ]
  %16 = phi i32 [ %33, %29 ], [ %11, %4 ]
  %17 = phi i32 [ %32, %29 ], [ 4, %4 ]
  %.027 = phi i32 [ %23, %29 ], [ 1, %4 ]
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i32, ptr %15, i64 %18
  %20 = sub nsw i32 %16, %17
  %21 = tail call i32 @min(i32 noundef 4, i32 noundef %20) #2
  %22 = tail call i32 @findMin(ptr noundef %19, i32 noundef %21) #2
  %23 = add nsw i32 %22, %17
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = sext i32 %23 to i64
  %26 = getelementptr inbounds i32, ptr %24, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !11
  %28 = icmp sgt i32 %14, %27
  br i1 %28, label %29, label %._crit_edge.loopexit

29:                                               ; preds = %.lr.ph
  %30 = sext i32 %.027 to i64
  %31 = getelementptr inbounds i32, ptr %24, i64 %30
  store i32 %27, ptr %31, align 4, !tbaa !11
  %32 = shl nsw i32 %23, 2
  %33 = load i32, ptr %9, align 4, !tbaa !12
  %.not25 = icmp sgt i32 %32, %33
  br i1 %.not25, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !13

._crit_edge.loopexit:                             ; preds = %.lr.ph, %29
  %.0.lcssa.ph = phi i32 [ %23, %29 ], [ %.027, %.lr.ph ]
  %34 = sext i32 %.0.lcssa.ph to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %4
  %35 = phi ptr [ %6, %4 ], [ %24, %._crit_edge.loopexit ]
  %.0.lcssa = phi i64 [ 1, %4 ], [ %34, %._crit_edge.loopexit ]
  %36 = getelementptr inbounds i32, ptr %35, i64 %.0.lcssa
  store i32 %14, ptr %36, align 4, !tbaa !11
  ret i32 %8
}

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

declare void @fatal(ptr noundef) local_unnamed_addr #1

declare i32 @findMin(ptr noundef, i32 noundef) local_unnamed_addr #1

declare i32 @min(i32 noundef, i32 noundef) local_unnamed_addr #1

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 = !{!6, !10, i64 8}
!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 = !{!7, !7, i64 0}
!12 = !{!6, !7, i64 4}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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