; LLVM IR from LeetCode-in-C/LeetCode-in-C :: src/main/c/g0201_0300/s0295_find_median_from_data_stream (MedianFinder)

; 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.MedianFinder = type { ptr, i32, i32, ptr, i32, i32 }

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

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

; Function Attrs: nounwind uwtable
define dso_local double @medianFinderFindMedian(ptr noundef %0) #0 {
  %2 = alloca double, align 8
  %3 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.MedianFinder, ptr %4, i32 0, i32 1
  %6 = load i32, ptr %5, align 8, !tbaa !9
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.MedianFinder, ptr %7, i32 0, i32 4
  %9 = load i32, ptr %8, align 8, !tbaa !12
  %10 = icmp sgt i32 %6, %9
  br i1 %10, label %11, label %15

11:                                               ; preds = %1
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = call i32 @maxHeapTop(ptr noundef %12)
  %14 = sitofp i32 %13 to double
  store double %14, ptr %2, align 8
  br label %23

15:                                               ; preds = %1
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = call i32 @maxHeapTop(ptr noundef %16)
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = call i32 @minHeapTop(ptr noundef %18)
  %20 = add nsw i32 %17, %19
  %21 = sitofp i32 %20 to double
  %22 = fdiv double %21, 2.000000e+00
  store double %22, ptr %2, align 8
  br label %23

23:                                               ; preds = %15, %11
  %24 = load double, ptr %2, align 8
  ret double %24
}

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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 8}
!10 = !{!"", !6, i64 0, !11, i64 8, !11, i64 12, !6, i64 16, !11, i64 24, !11, i64 28}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 24}


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

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

; Function Attrs: nounwind uwtable
define dso_local double @medianFinderFindMedian(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = load i32, ptr %4, align 8, !tbaa !11
  %6 = icmp sgt i32 %3, %5
  %7 = tail call i32 @maxHeapTop(ptr noundef nonnull %0)
  br i1 %6, label %8, label %10

8:                                                ; preds = %1
  %9 = sitofp i32 %7 to double
  br label %15

10:                                               ; preds = %1
  %11 = tail call i32 @minHeapTop(ptr noundef nonnull %0)
  %12 = add nsw i32 %11, %7
  %13 = sitofp i32 %12 to double
  %14 = fmul double %13, 5.000000e-01
  br label %15

15:                                               ; preds = %10, %8
  %.0 = phi double [ %9, %8 ], [ %14, %10 ]
  ret double %.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, !10, i64 8}
!6 = !{!"", !7, i64 0, !10, i64 8, !10, i64 12, !7, i64 16, !10, i64 24, !10, i64 28}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 24}


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass