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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef ptr @_ZN7sc_core12sc_allocator8allocateEv(ptr noundef nonnull align 8 dereferenceable(44) %0) #0 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %7 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  store ptr null, ptr %4, align 8, !tbaa !5
  %8 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 5
  %9 = load i32, ptr %8, align 8, !tbaa !9
  %10 = add nsw i32 %9, 1
  store i32 %10, ptr %8, align 8, !tbaa !9
  %11 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 3
  %12 = load ptr, ptr %11, align 8, !tbaa !12
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %25

14:                                               ; preds = %1
  %15 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 7
  %16 = load i32, ptr %15, align 8, !tbaa !13
  %17 = add nsw i32 %16, 1
  store i32 %17, ptr %15, align 8, !tbaa !13
  %18 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 3
  %19 = load ptr, ptr %18, align 8, !tbaa !12
  store ptr %19, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 3
  %21 = load ptr, ptr %20, align 8, !tbaa !12
  %22 = load ptr, ptr %21, align 8, !tbaa !14
  %23 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 3
  store ptr %22, ptr %23, align 8, !tbaa !12
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %24, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %71

25:                                               ; preds = %1
  %26 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 4
  %27 = load ptr, ptr %26, align 8, !tbaa !15
  %28 = icmp ne ptr %27, null
  br i1 %28, label %29, label %51

29:                                               ; preds = %25
  %30 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 4
  %31 = load ptr, ptr %30, align 8, !tbaa !15
  store ptr %31, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 1
  %33 = load i32, ptr %32, align 4, !tbaa !16
  %34 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 4
  %35 = load ptr, ptr %34, align 8, !tbaa !15
  %36 = sext i32 %33 to i64
  %37 = getelementptr inbounds i8, ptr %35, i64 %36
  store ptr %37, ptr %34, align 8, !tbaa !15
  %38 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 4
  %39 = load ptr, ptr %38, align 8, !tbaa !15
  %40 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 2
  %41 = load ptr, ptr %40, align 8, !tbaa !17
  %42 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 0
  %43 = load i32, ptr %42, align 8, !tbaa !18
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds i8, ptr %41, i64 %44
  %46 = icmp uge ptr %39, %45
  br i1 %46, label %47, label %49

47:                                               ; preds = %29
  %48 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 4
  store ptr null, ptr %48, align 8, !tbaa !15
  br label %49

49:                                               ; preds = %47, %29
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %50, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %71

51:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %52 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 0
  %53 = load i32, ptr %52, align 8, !tbaa !18
  %54 = sext i32 %53 to i64
  %55 = call noalias ptr @malloc(i64 noundef %54) #4
  store ptr %55, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 2
  %57 = load ptr, ptr %56, align 8, !tbaa !17
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %57, ptr %58, align 8, !tbaa !14
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 2
  store ptr %59, ptr %60, align 8, !tbaa !17
  %61 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 2
  %62 = load ptr, ptr %61, align 8, !tbaa !17
  %63 = getelementptr inbounds i8, ptr %62, i64 8
  store ptr %63, ptr %4, align 8, !tbaa !5
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 1
  %66 = load i32, ptr %65, align 4, !tbaa !16
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds i8, ptr %64, i64 %67
  %69 = getelementptr inbounds %"class.sc_core::sc_allocator", ptr %7, i32 0, i32 4
  store ptr %68, ptr %69, align 8, !tbaa !15
  %70 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %70, ptr %2, align 8
  store i32 1, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %71

71:                                               ; preds = %51, %49, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %72 = load ptr, ptr %2, align 8
  ret ptr %72
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind 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 #3 = { nounwind }
attributes #4 = { nounwind allocsize(0) }

!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, !11, i64 32}
!10 = !{!"_ZTSN7sc_core12sc_allocatorE", !11, i64 0, !11, i64 4, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !11, i64 36, !11, i64 40}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 16}
!13 = !{!10, !11, i64 40}
!14 = !{!7, !7, i64 0}
!15 = !{!10, !6, i64 24}
!16 = !{!10, !11, i64 4}
!17 = !{!10, !6, i64 8}
!18 = !{!10, !11, 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"

; Function Attrs: mustprogress nofree nounwind willreturn uwtable
define dso_local noundef ptr @_ZN7sc_core12sc_allocator8allocateEv(ptr nocapture noundef nonnull align 8 dereferenceable(44) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 32
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = add nsw i32 %3, 1
  store i32 %4, ptr %2, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  %6 = load ptr, ptr %5, align 8, !tbaa !11
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %12, label %7

7:                                                ; preds = %1
  %8 = getelementptr inbounds i8, ptr %0, i64 40
  %9 = load i32, ptr %8, align 8, !tbaa !12
  %10 = add nsw i32 %9, 1
  store i32 %10, ptr %8, align 8, !tbaa !12
  %11 = load ptr, ptr %6, align 8, !tbaa !13
  store ptr %11, ptr %5, align 8, !tbaa !11
  br label %36

12:                                               ; preds = %1
  %13 = getelementptr inbounds i8, ptr %0, i64 24
  %14 = load ptr, ptr %13, align 8, !tbaa !14
  %.not9 = icmp eq ptr %14, null
  br i1 %.not9, label %25, label %15

15:                                               ; preds = %12
  %16 = getelementptr inbounds i8, ptr %0, i64 4
  %17 = load i32, ptr %16, align 4, !tbaa !15
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i8, ptr %14, i64 %18
  %20 = getelementptr inbounds i8, ptr %0, i64 8
  %21 = load ptr, ptr %20, align 8, !tbaa !16
  %22 = load i32, ptr %0, align 8, !tbaa !17
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds i8, ptr %21, i64 %23
  %.not10 = icmp ult ptr %19, %24
  %spec.store.select = select i1 %.not10, ptr %19, ptr null
  store ptr %spec.store.select, ptr %13, align 8
  br label %36

25:                                               ; preds = %12
  %26 = load i32, ptr %0, align 8, !tbaa !17
  %27 = sext i32 %26 to i64
  %28 = tail call noalias ptr @malloc(i64 noundef %27) #2
  %29 = getelementptr inbounds i8, ptr %0, i64 8
  %30 = load ptr, ptr %29, align 8, !tbaa !16
  store ptr %30, ptr %28, align 8, !tbaa !13
  store ptr %28, ptr %29, align 8, !tbaa !16
  %31 = getelementptr inbounds i8, ptr %28, i64 8
  %32 = getelementptr inbounds i8, ptr %0, i64 4
  %33 = load i32, ptr %32, align 4, !tbaa !15
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i8, ptr %31, i64 %34
  store ptr %35, ptr %13, align 8, !tbaa !14
  br label %36

36:                                               ; preds = %25, %15, %7
  %.0 = phi ptr [ %6, %7 ], [ %14, %15 ], [ %31, %25 ]
  ret ptr %.0
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nofree nounwind willreturn 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 nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(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 #2 = { nounwind allocsize(0) }

!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, !7, i64 32}
!6 = !{!"_ZTSN7sc_core12sc_allocatorE", !7, i64 0, !7, i64 4, !10, i64 8, !10, i64 16, !10, i64 24, !7, i64 32, !7, i64 36, !7, i64 40}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 16}
!12 = !{!6, !7, i64 40}
!13 = !{!8, !8, i64 0}
!14 = !{!6, !10, i64 24}
!15 = !{!6, !7, i64 4}
!16 = !{!6, !10, i64 8}
!17 = !{!6, !7, i64 0}


; Effective O3 passes: DSEPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass