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

; 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.sc_core::sc_report_handler::msg_def_items" = type { ptr, i32, i8, ptr }
%"struct.sc_core::sc_msg_def" = type { ptr, i32, [4 x i32], i32, [4 x i32], i32, i32, [4 x i32], ptr, i32 }

@_ZN7sc_core17sc_report_handler8messagesE = external dso_local global ptr, align 8
@_ZN7sc_core17sc_report_handler14msg_terminatorE = external dso_local global %"struct.sc_core::sc_report_handler::msg_def_items", align 8
@_ZN7sc_core17sc_report_handler18last_global_reportE = external dso_local global ptr, align 8

; 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: nobuiltin nounwind
declare void @_ZdlPvm(ptr noundef, i64 noundef) #1

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i1 @_ZN7sc_core27sc_report_close_default_logEv() #2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN7sc_core17sc_report_handler7releaseEv() #2 align 2 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = load ptr, ptr @_ZN7sc_core17sc_report_handler18last_global_reportE, align 8, !tbaa !5
  %6 = icmp eq ptr %5, null
  br i1 %6, label %11, label %7

7:                                                ; preds = %0
  %8 = load ptr, ptr %5, align 8, !tbaa !9
  %9 = getelementptr inbounds ptr, ptr %8, i64 1
  %10 = load ptr, ptr %9, align 8
  call void %10(ptr noundef nonnull align 8 dereferenceable(80) %5) #4
  br label %11

11:                                               ; preds = %7, %0
  store ptr null, ptr @_ZN7sc_core17sc_report_handler18last_global_reportE, align 8, !tbaa !5
  %12 = call noundef zeroext i1 @_ZN7sc_core27sc_report_close_default_logEv()
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #4
  %13 = load ptr, ptr @_ZN7sc_core17sc_report_handler8messagesE, align 8, !tbaa !5
  store ptr %13, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  store ptr @_ZN7sc_core17sc_report_handler14msg_terminatorE, ptr %2, align 8, !tbaa !5
  store ptr @_ZN7sc_core17sc_report_handler14msg_terminatorE, ptr @_ZN7sc_core17sc_report_handler8messagesE, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %81, %11
  %15 = load ptr, ptr %1, align 8, !tbaa !5
  %16 = icmp ne ptr %15, @_ZN7sc_core17sc_report_handler14msg_terminatorE
  br i1 %16, label %17, label %82

17:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  store i32 0, ptr %3, align 4, !tbaa !11
  br label %18

18:                                               ; preds = %53, %17
  %19 = load i32, ptr %3, align 4, !tbaa !11
  %20 = load ptr, ptr %1, align 8, !tbaa !5
  %21 = getelementptr inbounds %"struct.sc_core::sc_report_handler::msg_def_items", ptr %20, i32 0, i32 1
  %22 = load i32, ptr %21, align 8, !tbaa !13
  %23 = icmp slt i32 %19, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  br label %56

25:                                               ; preds = %18
  %26 = load ptr, ptr %1, align 8, !tbaa !5
  %27 = getelementptr inbounds %"struct.sc_core::sc_report_handler::msg_def_items", ptr %26, i32 0, i32 0
  %28 = load ptr, ptr %27, align 8, !tbaa !16
  %29 = load i32, ptr %3, align 4, !tbaa !11
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds %"struct.sc_core::sc_msg_def", ptr %28, i64 %30
  %32 = getelementptr inbounds %"struct.sc_core::sc_msg_def", ptr %31, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !17
  %34 = load ptr, ptr %1, align 8, !tbaa !5
  %35 = getelementptr inbounds %"struct.sc_core::sc_report_handler::msg_def_items", ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !16
  %37 = load i32, ptr %3, align 4, !tbaa !11
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds %"struct.sc_core::sc_msg_def", ptr %36, i64 %38
  %40 = getelementptr inbounds %"struct.sc_core::sc_msg_def", ptr %39, i32 0, i32 8
  %41 = load ptr, ptr %40, align 8, !tbaa !19
  %42 = icmp eq ptr %33, %41
  br i1 %42, label %43, label %52

43:                                               ; preds = %25
  %44 = load ptr, ptr %1, align 8, !tbaa !5
  %45 = getelementptr inbounds %"struct.sc_core::sc_report_handler::msg_def_items", ptr %44, i32 0, i32 0
  %46 = load ptr, ptr %45, align 8, !tbaa !16
  %47 = load i32, ptr %3, align 4, !tbaa !11
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds %"struct.sc_core::sc_msg_def", ptr %46, i64 %48
  %50 = getelementptr inbounds %"struct.sc_core::sc_msg_def", ptr %49, i32 0, i32 8
  %51 = load ptr, ptr %50, align 8, !tbaa !19
  call void @free(ptr noundef %51) #4
  br label %52

52:                                               ; preds = %43, %25
  br label %53

53:                                               ; preds = %52
  %54 = load i32, ptr %3, align 4, !tbaa !11
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %3, align 4, !tbaa !11
  br label %18, !llvm.loop !20

56:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %57 = load ptr, ptr %1, align 8, !tbaa !5
  store ptr %57, ptr %4, align 8, !tbaa !5
  %58 = load ptr, ptr %1, align 8, !tbaa !5
  %59 = getelementptr inbounds %"struct.sc_core::sc_report_handler::msg_def_items", ptr %58, i32 0, i32 3
  %60 = load ptr, ptr %59, align 8, !tbaa !22
  store ptr %60, ptr %1, align 8, !tbaa !5
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  %62 = getelementptr inbounds %"struct.sc_core::sc_report_handler::msg_def_items", ptr %61, i32 0, i32 2
  %63 = load i8, ptr %62, align 4, !tbaa !23, !range !24, !noundef !25
  %64 = trunc i8 %63 to i1
  br i1 %64, label %65, label %76

65:                                               ; preds = %56
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  %67 = getelementptr inbounds %"struct.sc_core::sc_report_handler::msg_def_items", ptr %66, i32 0, i32 0
  %68 = load ptr, ptr %67, align 8, !tbaa !16
  %69 = icmp eq ptr %68, null
  br i1 %69, label %71, label %70

70:                                               ; preds = %65
  call void @_ZdaPv(ptr noundef %68) #5
  br label %71

71:                                               ; preds = %70, %65
  %72 = load ptr, ptr %4, align 8, !tbaa !5
  %73 = icmp eq ptr %72, null
  br i1 %73, label %75, label %74

74:                                               ; preds = %71
  call void @_ZdlPvm(ptr noundef %72, i64 noundef 24) #5
  br label %75

75:                                               ; preds = %74, %71
  br label %81

76:                                               ; preds = %56
  %77 = load ptr, ptr %2, align 8, !tbaa !5
  %78 = load ptr, ptr %4, align 8, !tbaa !5
  %79 = getelementptr inbounds %"struct.sc_core::sc_report_handler::msg_def_items", ptr %78, i32 0, i32 3
  store ptr %77, ptr %79, align 8, !tbaa !22
  %80 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %80, ptr %2, align 8, !tbaa !5
  br label %81

81:                                               ; preds = %76, %75
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %14, !llvm.loop !26

82:                                               ; preds = %14
  %83 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %83, ptr @_ZN7sc_core17sc_report_handler8messagesE, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #4
  ret void
}

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

; Function Attrs: nobuiltin nounwind
declare void @_ZdaPv(ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nobuiltin nounwind "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 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 "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 #4 = { nounwind }
attributes #5 = { builtin 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 = !{!"vtable pointer", !8, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 8}
!14 = !{!"_ZTSN7sc_core17sc_report_handler13msg_def_itemsE", !6, i64 0, !12, i64 8, !15, i64 12, !6, i64 16}
!15 = !{!"bool", !7, i64 0}
!16 = !{!14, !6, i64 0}
!17 = !{!18, !6, i64 0}
!18 = !{!"_ZTSN7sc_core10sc_msg_defE", !6, i64 0, !12, i64 8, !7, i64 12, !12, i64 28, !7, i64 32, !12, i64 48, !12, i64 52, !7, i64 56, !6, i64 72, !12, i64 80}
!19 = !{!18, !6, i64 72}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!14, !6, i64 16}
!23 = !{!14, !15, i64 12}
!24 = !{i8 0, i8 2}
!25 = !{}
!26 = distinct !{!26, !21}


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

%"struct.sc_core::sc_report_handler::msg_def_items" = type { ptr, i32, i8, ptr }
%"struct.sc_core::sc_msg_def" = type { ptr, i32, [4 x i32], i32, [4 x i32], i32, i32, [4 x i32], ptr, i32 }

@_ZN7sc_core17sc_report_handler8messagesE = external dso_local local_unnamed_addr global ptr, align 8
@_ZN7sc_core17sc_report_handler14msg_terminatorE = external dso_local global %"struct.sc_core::sc_report_handler::msg_def_items", align 8
@_ZN7sc_core17sc_report_handler18last_global_reportE = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nobuiltin nounwind
declare void @_ZdlPvm(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i1 @_ZN7sc_core27sc_report_close_default_logEv() local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN7sc_core17sc_report_handler7releaseEv() local_unnamed_addr #1 align 2 {
  %1 = load ptr, ptr @_ZN7sc_core17sc_report_handler18last_global_reportE, align 8, !tbaa !5
  %2 = icmp eq ptr %1, null
  br i1 %2, label %7, label %3

3:                                                ; preds = %0
  %4 = load ptr, ptr %1, align 8, !tbaa !9
  %5 = getelementptr inbounds i8, ptr %4, i64 8
  %6 = load ptr, ptr %5, align 8
  tail call void %6(ptr noundef nonnull align 8 dereferenceable(80) %1) #3
  br label %7

7:                                                ; preds = %3, %0
  store ptr null, ptr @_ZN7sc_core17sc_report_handler18last_global_reportE, align 8, !tbaa !5
  %8 = tail call noundef zeroext i1 @_ZN7sc_core27sc_report_close_default_logEv()
  %9 = load ptr, ptr @_ZN7sc_core17sc_report_handler8messagesE, align 8, !tbaa !5
  store ptr @_ZN7sc_core17sc_report_handler14msg_terminatorE, ptr @_ZN7sc_core17sc_report_handler8messagesE, align 8, !tbaa !5
  %.not24 = icmp eq ptr %9, @_ZN7sc_core17sc_report_handler14msg_terminatorE
  br i1 %.not24, label %._crit_edge27, label %.preheader

.preheader:                                       ; preds = %7, %36
  %.026 = phi ptr [ %14, %36 ], [ %9, %7 ]
  %.01925 = phi ptr [ %.1, %36 ], [ @_ZN7sc_core17sc_report_handler14msg_terminatorE, %7 ]
  %10 = getelementptr inbounds i8, ptr %.026, i64 8
  %11 = load i32, ptr %10, align 8, !tbaa !11
  %12 = icmp sgt i32 %11, 0
  br i1 %12, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %26, %.preheader
  %13 = getelementptr inbounds i8, ptr %.026, i64 16
  %14 = load ptr, ptr %13, align 8, !tbaa !15
  %15 = getelementptr inbounds i8, ptr %.026, i64 12
  %16 = load i8, ptr %15, align 4, !tbaa !16, !range !17, !noundef !18
  %17 = trunc nuw i8 %16 to i1
  br i1 %17, label %30, label %35

.lr.ph:                                           ; preds = %.preheader, %26
  %18 = phi i32 [ %27, %26 ], [ %11, %.preheader ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %26 ], [ 0, %.preheader ]
  %19 = load ptr, ptr %.026, align 8, !tbaa !19
  %20 = getelementptr inbounds %"struct.sc_core::sc_msg_def", ptr %19, i64 %indvars.iv
  %21 = load ptr, ptr %20, align 8, !tbaa !20
  %22 = getelementptr inbounds i8, ptr %20, i64 72
  %23 = load ptr, ptr %22, align 8, !tbaa !22
  %24 = icmp eq ptr %21, %23
  br i1 %24, label %25, label %26

25:                                               ; preds = %.lr.ph
  tail call void @free(ptr noundef %23) #3
  %.pre = load i32, ptr %10, align 8, !tbaa !11
  br label %26

26:                                               ; preds = %.lr.ph, %25
  %27 = phi i32 [ %18, %.lr.ph ], [ %.pre, %25 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %28 = sext i32 %27 to i64
  %29 = icmp slt i64 %indvars.iv.next, %28
  br i1 %29, label %.lr.ph, label %._crit_edge, !llvm.loop !23

30:                                               ; preds = %._crit_edge
  %31 = load ptr, ptr %.026, align 8, !tbaa !19
  %32 = icmp eq ptr %31, null
  br i1 %32, label %34, label %33

33:                                               ; preds = %30
  tail call void @_ZdaPv(ptr noundef nonnull %31) #4
  br label %34

34:                                               ; preds = %30, %33
  tail call void @_ZdlPvm(ptr noundef nonnull %.026, i64 noundef 24) #4
  br label %36

35:                                               ; preds = %._crit_edge
  store ptr %.01925, ptr %13, align 8, !tbaa !15
  br label %36

36:                                               ; preds = %34, %35
  %.1 = phi ptr [ %.01925, %34 ], [ %.026, %35 ]
  %.not = icmp eq ptr %14, @_ZN7sc_core17sc_report_handler14msg_terminatorE
  br i1 %.not, label %._crit_edge27, label %.preheader, !llvm.loop !25

._crit_edge27:                                    ; preds = %36, %7
  %.019.lcssa = phi ptr [ @_ZN7sc_core17sc_report_handler14msg_terminatorE, %7 ], [ %.1, %36 ]
  store ptr %.019.lcssa, ptr @_ZN7sc_core17sc_report_handler8messagesE, align 8, !tbaa !5
  ret void
}

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nobuiltin nounwind
declare void @_ZdaPv(ptr noundef) local_unnamed_addr #0

attributes #0 = { nobuiltin nounwind "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 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 willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 }
attributes #4 = { builtin 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 = !{!"vtable pointer", !8, i64 0}
!11 = !{!12, !13, i64 8}
!12 = !{!"_ZTSN7sc_core17sc_report_handler13msg_def_itemsE", !6, i64 0, !13, i64 8, !14, i64 12, !6, i64 16}
!13 = !{!"int", !7, i64 0}
!14 = !{!"bool", !7, i64 0}
!15 = !{!12, !6, i64 16}
!16 = !{!12, !14, i64 12}
!17 = !{i8 0, i8 2}
!18 = !{}
!19 = !{!12, !6, i64 0}
!20 = !{!21, !6, i64 0}
!21 = !{!"_ZTSN7sc_core10sc_msg_defE", !6, i64 0, !13, i64 8, !7, i64 12, !13, i64 28, !7, i64 32, !13, i64 48, !13, i64 52, !7, i64 56, !6, i64 72, !13, i64 80}
!22 = !{!21, !6, i64 72}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}
!25 = distinct !{!25, !24}


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