; 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
declare dso_local i32 @isEmpty(ptr noundef) #1

declare void @fatal(ptr noundef) #2

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

; Function Attrs: nounwind uwtable
define dso_local i32 @deleteMaxMHH(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 = call i32 @findMaxMMH(ptr noundef %12)
  store i32 %13, ptr %5, align 4, !tbaa !9
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.MinMaxHeapStruct, ptr %14, i32 0, i32 2
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.MinMaxHeapStruct, ptr %17, i32 0, i32 1
  %19 = load i32, ptr %18, align 4, !tbaa !13
  %20 = add nsw i32 %19, -1
  store i32 %20, ptr %18, align 4, !tbaa !13
  %21 = sext i32 %19 to i64
  %22 = getelementptr inbounds i32, ptr %16, i64 %21
  %23 = load i32, ptr %22, align 4, !tbaa !9
  store i32 %23, ptr %6, align 4, !tbaa !9
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = call i32 @findMaxMMHIdx(ptr noundef %24)
  store i32 %25, ptr %3, align 4, !tbaa !9
  br label %26

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

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

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

75:                                               ; preds = %33
  br label %79

76:                                               ; preds = %61
  br label %77

77:                                               ; preds = %76
  %78 = load i32, ptr %4, align 4, !tbaa !9
  store i32 %78, ptr %3, align 4, !tbaa !9
  br label %26, !llvm.loop !14

79:                                               ; preds = %75, %26
  %80 = load i32, ptr %6, align 4, !tbaa !9
  %81 = load ptr, ptr %2, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.MinMaxHeapStruct, ptr %81, i32 0, i32 2
  %83 = load ptr, ptr %82, align 8, !tbaa !11
  %84 = load i32, ptr %3, align 4, !tbaa !9
  %85 = sext i32 %84 to i64
  %86 = getelementptr inbounds i32, ptr %83, i64 %85
  store i32 %80, ptr %86, align 4, !tbaa !9
  %87 = load i32, ptr %5, align 4, !tbaa !9
  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 %87
}

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

; Function Attrs: nounwind uwtable
declare hidden i32 @findMaxMMHIdx(ptr noundef) #1

declare i32 @findMax(ptr 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, !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 = !{!12, !10, 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
declare dso_local i32 @isEmpty(ptr noundef) local_unnamed_addr #0

declare void @fatal(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @deleteMaxMHH(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 = tail call i32 @findMaxMMH(ptr noundef %0)
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %0, i64 4
  %9 = load i32, ptr %8, align 4, !tbaa !11
  %10 = add nsw i32 %9, -1
  store i32 %10, ptr %8, align 4, !tbaa !11
  %11 = sext i32 %9 to i64
  %12 = getelementptr inbounds i32, ptr %7, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !12
  %14 = tail call i32 @findMaxMMHIdx(ptr noundef %0)
  %15 = shl nsw i32 %14, 2
  %16 = load i32, ptr %8, align 4, !tbaa !11
  %.not2627 = icmp sgt i32 %15, %16
  %.pre31 = load ptr, ptr %6, align 8, !tbaa !5
  br i1 %.not2627, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4, %31
  %17 = phi ptr [ %26, %31 ], [ %.pre31, %4 ]
  %18 = phi i32 [ %35, %31 ], [ %16, %4 ]
  %19 = phi i32 [ %34, %31 ], [ %15, %4 ]
  %.028 = phi i32 [ %25, %31 ], [ %14, %4 ]
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds i32, ptr %17, i64 %20
  %22 = sub nsw i32 %18, %19
  %23 = tail call i32 @min(i32 noundef 4, i32 noundef %22) #2
  %24 = tail call i32 @findMax(ptr noundef %21, i32 noundef %23) #2
  %25 = add nsw i32 %24, %19
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = sext i32 %25 to i64
  %28 = getelementptr inbounds i32, ptr %26, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !12
  %30 = icmp slt i32 %13, %29
  br i1 %30, label %31, label %._crit_edge

31:                                               ; preds = %.lr.ph
  %32 = sext i32 %.028 to i64
  %33 = getelementptr inbounds i32, ptr %26, i64 %32
  store i32 %29, ptr %33, align 4, !tbaa !12
  %34 = shl nsw i32 %25, 2
  %35 = load i32, ptr %8, align 4, !tbaa !11
  %.not26 = icmp sgt i32 %34, %35
  br i1 %.not26, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %31, %.lr.ph, %4
  %36 = phi ptr [ %.pre31, %4 ], [ %26, %.lr.ph ], [ %26, %31 ]
  %.0.lcssa = phi i32 [ %14, %4 ], [ %25, %31 ], [ %.028, %.lr.ph ]
  %37 = sext i32 %.0.lcssa to i64
  %38 = getelementptr inbounds i32, ptr %36, i64 %37
  store i32 %13, ptr %38, align 4, !tbaa !12
  ret i32 %5
}

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

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

declare i32 @findMax(ptr 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 = !{!6, !7, i64 4}
!12 = !{!7, !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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