; LLVM IR from gem5/gem5 :: ext/systemc/src/sysc/utils/sc_pq.ll (ZN7sc_core11sc_ppq_base7heapifyEi)

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

%"class.sc_core::sc_ppq_base" = type { ptr, i32, i32, ptr }

; 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: mustprogress uwtable
define dso_local void @_ZN7sc_core11sc_ppq_base7heapifyEi(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %1) #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %10 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  br label %11

11:                                               ; preds = %99, %2
  %12 = load i32, ptr %4, align 4, !tbaa !9
  %13 = call noundef i32 @_ZNK7sc_core11sc_ppq_base4leftEi(ptr noundef nonnull align 8 dereferenceable(24) %10, i32 noundef %12)
  store i32 %13, ptr %5, align 4, !tbaa !9
  %14 = load i32, ptr %5, align 4, !tbaa !9
  %15 = getelementptr inbounds %"class.sc_core::sc_ppq_base", ptr %10, i32 0, i32 2
  %16 = load i32, ptr %15, align 4, !tbaa !11
  %17 = icmp sle i32 %14, %16
  br i1 %17, label %18, label %100

18:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %19 = getelementptr inbounds %"class.sc_core::sc_ppq_base", ptr %10, i32 0, i32 3
  %20 = load ptr, ptr %19, align 8, !tbaa !13
  %21 = getelementptr inbounds %"class.sc_core::sc_ppq_base", ptr %10, i32 0, i32 0
  %22 = load ptr, ptr %21, align 8, !tbaa !14
  %23 = load i32, ptr %5, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds ptr, ptr %22, i64 %24
  %26 = load ptr, ptr %25, align 8, !tbaa !5
  %27 = getelementptr inbounds %"class.sc_core::sc_ppq_base", ptr %10, i32 0, i32 0
  %28 = load ptr, ptr %27, align 8, !tbaa !14
  %29 = load i32, ptr %4, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds ptr, ptr %28, i64 %30
  %32 = load ptr, ptr %31, align 8, !tbaa !5
  %33 = call noundef i32 %20(ptr noundef %26, ptr noundef %32)
  %34 = icmp sgt i32 %33, 0
  br i1 %34, label %35, label %37

35:                                               ; preds = %18
  %36 = load i32, ptr %5, align 4, !tbaa !9
  br label %39

37:                                               ; preds = %18
  %38 = load i32, ptr %4, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %37, %35
  %40 = phi i32 [ %36, %35 ], [ %38, %37 ]
  store i32 %40, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %41 = load i32, ptr %4, align 4, !tbaa !9
  %42 = call noundef i32 @_ZNK7sc_core11sc_ppq_base5rightEi(ptr noundef nonnull align 8 dereferenceable(24) %10, i32 noundef %41)
  store i32 %42, ptr %7, align 4, !tbaa !9
  %43 = load i32, ptr %7, align 4, !tbaa !9
  %44 = getelementptr inbounds %"class.sc_core::sc_ppq_base", ptr %10, i32 0, i32 2
  %45 = load i32, ptr %44, align 4, !tbaa !11
  %46 = icmp sle i32 %43, %45
  br i1 %46, label %47, label %66

47:                                               ; preds = %39
  %48 = getelementptr inbounds %"class.sc_core::sc_ppq_base", ptr %10, i32 0, i32 3
  %49 = load ptr, ptr %48, align 8, !tbaa !13
  %50 = getelementptr inbounds %"class.sc_core::sc_ppq_base", ptr %10, i32 0, i32 0
  %51 = load ptr, ptr %50, align 8, !tbaa !14
  %52 = load i32, ptr %7, align 4, !tbaa !9
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds ptr, ptr %51, i64 %53
  %55 = load ptr, ptr %54, align 8, !tbaa !5
  %56 = getelementptr inbounds %"class.sc_core::sc_ppq_base", ptr %10, i32 0, i32 0
  %57 = load ptr, ptr %56, align 8, !tbaa !14
  %58 = load i32, ptr %6, align 4, !tbaa !9
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds ptr, ptr %57, i64 %59
  %61 = load ptr, ptr %60, align 8, !tbaa !5
  %62 = call noundef i32 %49(ptr noundef %55, ptr noundef %61)
  %63 = icmp sgt i32 %62, 0
  br i1 %63, label %64, label %66

64:                                               ; preds = %47
  %65 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %65, ptr %6, align 4, !tbaa !9
  br label %66

66:                                               ; preds = %64, %47, %39
  %67 = load i32, ptr %6, align 4, !tbaa !9
  %68 = load i32, ptr %4, align 4, !tbaa !9
  %69 = icmp ne i32 %67, %68
  br i1 %69, label %70, label %95

70:                                               ; preds = %66
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %71 = getelementptr inbounds %"class.sc_core::sc_ppq_base", ptr %10, i32 0, i32 0
  %72 = load ptr, ptr %71, align 8, !tbaa !14
  %73 = load i32, ptr %4, align 4, !tbaa !9
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds ptr, ptr %72, i64 %74
  %76 = load ptr, ptr %75, align 8, !tbaa !5
  store ptr %76, ptr %8, align 8, !tbaa !5
  %77 = getelementptr inbounds %"class.sc_core::sc_ppq_base", ptr %10, i32 0, i32 0
  %78 = load ptr, ptr %77, align 8, !tbaa !14
  %79 = load i32, ptr %6, align 4, !tbaa !9
  %80 = sext i32 %79 to i64
  %81 = getelementptr inbounds ptr, ptr %78, i64 %80
  %82 = load ptr, ptr %81, align 8, !tbaa !5
  %83 = getelementptr inbounds %"class.sc_core::sc_ppq_base", ptr %10, i32 0, i32 0
  %84 = load ptr, ptr %83, align 8, !tbaa !14
  %85 = load i32, ptr %4, align 4, !tbaa !9
  %86 = sext i32 %85 to i64
  %87 = getelementptr inbounds ptr, ptr %84, i64 %86
  store ptr %82, ptr %87, align 8, !tbaa !5
  %88 = load ptr, ptr %8, align 8, !tbaa !5
  %89 = getelementptr inbounds %"class.sc_core::sc_ppq_base", ptr %10, i32 0, i32 0
  %90 = load ptr, ptr %89, align 8, !tbaa !14
  %91 = load i32, ptr %6, align 4, !tbaa !9
  %92 = sext i32 %91 to i64
  %93 = getelementptr inbounds ptr, ptr %90, i64 %92
  store ptr %88, ptr %93, align 8, !tbaa !5
  %94 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %94, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %96

95:                                               ; preds = %66
  store i32 3, ptr %9, align 4
  br label %97

96:                                               ; preds = %70
  store i32 0, ptr %9, align 4
  br label %97

97:                                               ; preds = %96, %95
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %98 = load i32, ptr %9, align 4
  switch i32 %98, label %101 [
    i32 0, label %99
    i32 3, label %100
  ]

99:                                               ; preds = %97
  br label %11, !llvm.loop !15

100:                                              ; preds = %97, %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret void

101:                                              ; preds = %97
  unreachable
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK7sc_core11sc_ppq_base4leftEi(ptr noundef nonnull align 8 dereferenceable(24), i32 noundef) #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK7sc_core11sc_ppq_base5rightEi(ptr noundef nonnull align 8 dereferenceable(24), i32 noundef) #2 align 2

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

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN7sc_core11sc_ppq_base7heapifyEi(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %1) local_unnamed_addr #0 align 2 {
  %3 = tail call noundef i32 @_ZNK7sc_core11sc_ppq_base4leftEi(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %1)
  %4 = getelementptr inbounds i8, ptr %0, i64 12
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %.not29 = icmp sgt i32 %3, %5
  br i1 %.not29, label %.thread, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  br label %7

7:                                                ; preds = %.lr.ph, %34
  %8 = phi i32 [ %3, %.lr.ph ], [ %43, %34 ]
  %.02030 = phi i32 [ %1, %.lr.ph ], [ %.021, %34 ]
  %9 = load ptr, ptr %6, align 8, !tbaa !11
  %10 = load ptr, ptr %0, align 8, !tbaa !12
  %11 = sext i32 %8 to i64
  %12 = getelementptr inbounds ptr, ptr %10, i64 %11
  %13 = load ptr, ptr %12, align 8, !tbaa !13
  %14 = sext i32 %.02030 to i64
  %15 = getelementptr inbounds ptr, ptr %10, i64 %14
  %16 = load ptr, ptr %15, align 8, !tbaa !13
  %17 = tail call noundef i32 %9(ptr noundef %13, ptr noundef %16)
  %18 = icmp sgt i32 %17, 0
  %19 = select i1 %18, i32 %8, i32 %.02030
  %20 = tail call noundef i32 @_ZNK7sc_core11sc_ppq_base5rightEi(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %.02030)
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %.not24 = icmp sgt i32 %20, %21
  br i1 %.not24, label %33, label %22

22:                                               ; preds = %7
  %23 = load ptr, ptr %6, align 8, !tbaa !11
  %24 = load ptr, ptr %0, align 8, !tbaa !12
  %25 = sext i32 %20 to i64
  %26 = getelementptr inbounds ptr, ptr %24, i64 %25
  %27 = load ptr, ptr %26, align 8, !tbaa !13
  %28 = sext i32 %19 to i64
  %29 = getelementptr inbounds ptr, ptr %24, i64 %28
  %30 = load ptr, ptr %29, align 8, !tbaa !13
  %31 = tail call noundef i32 %23(ptr noundef %27, ptr noundef %30)
  %32 = icmp sgt i32 %31, 0
  %spec.select = select i1 %32, i32 %20, i32 %19
  br label %33

33:                                               ; preds = %22, %7
  %.021 = phi i32 [ %19, %7 ], [ %spec.select, %22 ]
  %.not25 = icmp eq i32 %.021, %.02030
  br i1 %.not25, label %.thread, label %34

34:                                               ; preds = %33
  %35 = load ptr, ptr %0, align 8, !tbaa !12
  %36 = getelementptr inbounds ptr, ptr %35, i64 %14
  %37 = load ptr, ptr %36, align 8, !tbaa !13
  %38 = sext i32 %.021 to i64
  %39 = getelementptr inbounds ptr, ptr %35, i64 %38
  %40 = load ptr, ptr %39, align 8, !tbaa !13
  store ptr %40, ptr %36, align 8, !tbaa !13
  %41 = load ptr, ptr %0, align 8, !tbaa !12
  %42 = getelementptr inbounds ptr, ptr %41, i64 %38
  store ptr %37, ptr %42, align 8, !tbaa !13
  %43 = tail call noundef i32 @_ZNK7sc_core11sc_ppq_base4leftEi(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %.021)
  %44 = load i32, ptr %4, align 4, !tbaa !5
  %.not = icmp sgt i32 %43, %44
  br i1 %.not, label %.thread, label %7

.thread:                                          ; preds = %34, %33, %2
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK7sc_core11sc_ppq_base4leftEi(ptr noundef nonnull align 8 dereferenceable(24), i32 noundef) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZNK7sc_core11sc_ppq_base5rightEi(ptr noundef nonnull align 8 dereferenceable(24), i32 noundef) local_unnamed_addr #1 align 2

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


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