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

; 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_mempool_int" = type <{ ptr, i32, i32, i32, [4 x i8] }>

@_ZL15use_default_new = external hidden global i8, align 1
@_ZN7sc_coreL11the_mempoolE = external hidden global ptr, align 8

; Function Attrs: mustprogress nounwind uwtable
declare hidden noundef zeroext i1 @_ZN7sc_coreL23compute_use_default_newEv() #0

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znwm(i64 noundef) #1

declare i32 @__gxx_personality_v0(...)

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN7sc_core14sc_mempool_int11do_allocateEm(ptr noundef nonnull align 8 dereferenceable(20), i64 noundef) #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN7sc_core10sc_mempool8allocateEm(i64 noundef %0) #3 align 2 personality ptr @__gxx_personality_v0 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  store i64 %0, ptr %3, align 8, !tbaa !5
  %6 = load i8, ptr @_ZL15use_default_new, align 1, !tbaa !9, !range !11, !noundef !12
  %7 = trunc i8 %6 to i1
  br i1 %7, label %8, label %11

8:                                                ; preds = %1
  %9 = load i64, ptr %3, align 8, !tbaa !5
  %10 = call noalias noundef nonnull ptr @_Znwm(i64 noundef %9) #4
  store ptr %10, ptr %2, align 8
  br label %43

11:                                               ; preds = %1
  %12 = load ptr, ptr @_ZN7sc_coreL11the_mempoolE, align 8, !tbaa !13
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %29

14:                                               ; preds = %11
  %15 = call noundef zeroext i1 @_ZN7sc_coreL23compute_use_default_newEv()
  %16 = zext i1 %15 to i8
  store i8 %16, ptr @_ZL15use_default_new, align 1, !tbaa !9
  %17 = load i8, ptr @_ZL15use_default_new, align 1, !tbaa !9, !range !11, !noundef !12
  %18 = trunc i8 %17 to i1
  br i1 %18, label %19, label %22

19:                                               ; preds = %14
  %20 = load i64, ptr %3, align 8, !tbaa !5
  %21 = call noalias noundef nonnull ptr @_Znwm(i64 noundef %20) #4
  store ptr %21, ptr %2, align 8
  br label %43

22:                                               ; preds = %14
  %23 = call noalias noundef nonnull ptr @_Znwm(i64 noundef 24) #5
  invoke void @_ZN7sc_core14sc_mempool_intC1Eiii(ptr noundef nonnull align 8 dereferenceable(20) %23, i32 noundef 1984, i32 noundef 9, i32 noundef 8)
          to label %24 unwind label %25

24:                                               ; preds = %22
  store ptr %23, ptr @_ZN7sc_coreL11the_mempoolE, align 8, !tbaa !13
  br label %29

25:                                               ; preds = %22
  %26 = landingpad { ptr, i32 }
          cleanup
  %27 = extractvalue { ptr, i32 } %26, 0
  store ptr %27, ptr %4, align 8
  %28 = extractvalue { ptr, i32 } %26, 1
  store i32 %28, ptr %5, align 4
  call void @_ZdlPvm(ptr noundef %23, i64 noundef 24) #6
  br label %45

29:                                               ; preds = %24, %11
  %30 = load i64, ptr %3, align 8, !tbaa !5
  %31 = load ptr, ptr @_ZN7sc_coreL11the_mempoolE, align 8, !tbaa !13
  %32 = getelementptr inbounds %"class.sc_core::sc_mempool_int", ptr %31, i32 0, i32 3
  %33 = load i32, ptr %32, align 8, !tbaa !15
  %34 = zext i32 %33 to i64
  %35 = icmp ugt i64 %30, %34
  br i1 %35, label %36, label %39

36:                                               ; preds = %29
  %37 = load i64, ptr %3, align 8, !tbaa !5
  %38 = call noalias noundef nonnull ptr @_Znwm(i64 noundef %37) #4
  store ptr %38, ptr %2, align 8
  br label %43

39:                                               ; preds = %29
  %40 = load ptr, ptr @_ZN7sc_coreL11the_mempoolE, align 8, !tbaa !13
  %41 = load i64, ptr %3, align 8, !tbaa !5
  %42 = call noundef ptr @_ZN7sc_core14sc_mempool_int11do_allocateEm(ptr noundef nonnull align 8 dereferenceable(20) %40, i64 noundef %41)
  store ptr %42, ptr %2, align 8
  br label %43

43:                                               ; preds = %39, %36, %19, %8
  %44 = load ptr, ptr %2, align 8
  ret ptr %44

45:                                               ; preds = %25
  %46 = load ptr, ptr %4, align 8
  %47 = load i32, ptr %5, align 4
  %48 = insertvalue { ptr, i32 } poison, ptr %46, 0
  %49 = insertvalue { ptr, i32 } %48, i32 %47, 1
  resume { ptr, i32 } %49
}

declare void @_ZN7sc_core14sc_mempool_intC1Eiii(ptr, i32, i32, i32)

attributes #0 = { 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 #1 = { nobuiltin allocsize(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 = { 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 #3 = { 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 #4 = { allocsize(0) }
attributes #5 = { builtin allocsize(0) }
attributes #6 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = !{!14, !14, i64 0}
!14 = !{!"any pointer", !7, i64 0}
!15 = !{!16, !17, i64 16}
!16 = !{!"_ZTSN7sc_core14sc_mempool_intE", !14, i64 0, !17, i64 8, !17, i64 12, !17, i64 16}
!17 = !{!"int", !7, i64 0}


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

@_ZL15use_default_new = external hidden local_unnamed_addr global i8, align 1
@_ZN7sc_coreL11the_mempoolE = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nounwind uwtable
declare hidden noundef zeroext i1 @_ZN7sc_coreL23compute_use_default_newEv() local_unnamed_addr #0

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znwm(i64 noundef) local_unnamed_addr #1

declare i32 @__gxx_personality_v0(...)

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN7sc_core14sc_mempool_int11do_allocateEm(ptr noundef nonnull align 8 dereferenceable(20), i64 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN7sc_core10sc_mempool8allocateEm(i64 noundef %0) local_unnamed_addr #3 align 2 personality ptr @__gxx_personality_v0 {
  %2 = load i8, ptr @_ZL15use_default_new, align 1, !tbaa !5, !range !9, !noundef !10
  %3 = trunc nuw i8 %2 to i1
  br i1 %3, label %4, label %6

4:                                                ; preds = %1
  %5 = tail call noalias noundef nonnull ptr @_Znwm(i64 noundef %0) #4
  br label %29

6:                                                ; preds = %1
  %7 = load ptr, ptr @_ZN7sc_coreL11the_mempoolE, align 8, !tbaa !11
  %8 = icmp eq ptr %7, null
  br i1 %8, label %9, label %19

9:                                                ; preds = %6
  %10 = tail call noundef zeroext i1 @_ZN7sc_coreL23compute_use_default_newEv()
  %11 = zext i1 %10 to i8
  store i8 %11, ptr @_ZL15use_default_new, align 1, !tbaa !5
  br i1 %10, label %12, label %14

12:                                               ; preds = %9
  %13 = tail call noalias noundef nonnull ptr @_Znwm(i64 noundef %0) #4
  br label %29

14:                                               ; preds = %9
  %15 = tail call noalias noundef nonnull dereferenceable(24) ptr @_Znwm(i64 noundef 24) #5
  invoke void @_ZN7sc_core14sc_mempool_intC1Eiii(ptr noundef nonnull align 8 dereferenceable(20) %15, i32 noundef 1984, i32 noundef 9, i32 noundef 8)
          to label %16 unwind label %17

16:                                               ; preds = %14
  store ptr %15, ptr @_ZN7sc_coreL11the_mempoolE, align 8, !tbaa !11
  br label %19

17:                                               ; preds = %14
  %18 = landingpad { ptr, i32 }
          cleanup
  tail call void @_ZdlPvm(ptr noundef nonnull %15, i64 noundef 24) #6
  resume { ptr, i32 } %18

19:                                               ; preds = %16, %6
  %20 = phi ptr [ %15, %16 ], [ %7, %6 ]
  %21 = getelementptr inbounds i8, ptr %20, i64 16
  %22 = load i32, ptr %21, align 8, !tbaa !13
  %23 = zext i32 %22 to i64
  %24 = icmp ult i64 %23, %0
  br i1 %24, label %25, label %27

25:                                               ; preds = %19
  %26 = tail call noalias noundef nonnull ptr @_Znwm(i64 noundef %0) #4
  br label %29

27:                                               ; preds = %19
  %28 = tail call noundef ptr @_ZN7sc_core14sc_mempool_int11do_allocateEm(ptr noundef nonnull align 8 dereferenceable(20) %20, i64 noundef %0)
  br label %29

29:                                               ; preds = %27, %25, %12, %4
  %.0 = phi ptr [ %5, %4 ], [ %13, %12 ], [ %26, %25 ], [ %28, %27 ]
  ret ptr %.0
}

declare void @_ZN7sc_core14sc_mempool_intC1Eiii(ptr, i32, i32, i32) local_unnamed_addr

attributes #0 = { 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 #1 = { nobuiltin allocsize(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 = { 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 #3 = { 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 #4 = { allocsize(0) }
attributes #5 = { builtin allocsize(0) }
attributes #6 = { 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 = !{!"bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{i8 0, i8 2}
!10 = !{}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !15, i64 16}
!14 = !{!"_ZTSN7sc_core14sc_mempool_intE", !12, i64 0, !15, i64 8, !15, i64 12, !15, i64 16}
!15 = !{!"int", !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass