; LLVM IR from gem5/gem5 :: ext/systemc/src/sysc/kernel/sc_event.ll (ZN7sc_core14sc_event_timed8allocateEv)

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

%"union.sc_core::sc_event_timed_u" = type { ptr, [8 x i8] }

@_ZN7sc_coreL9free_listE = external hidden 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: mustprogress nounwind uwtable
define dso_local noundef ptr @_ZN7sc_core14sc_event_timed8allocateEv() #1 align 2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  store i32 64, ptr %1, align 4, !tbaa !5
  %4 = load ptr, ptr @_ZN7sc_coreL9free_listE, align 8, !tbaa !9
  %5 = icmp eq ptr %4, null
  br i1 %5, label %6, label %29

6:                                                ; preds = %0
  %7 = call noalias ptr @malloc(i64 noundef 1024) #4
  store ptr %7, ptr @_ZN7sc_coreL9free_listE, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %21, %6
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = icmp slt i32 %9, 63
  br i1 %10, label %11, label %24

11:                                               ; preds = %8
  %12 = load ptr, ptr @_ZN7sc_coreL9free_listE, align 8, !tbaa !9
  %13 = load i32, ptr %2, align 4, !tbaa !5
  %14 = add nsw i32 %13, 1
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds %"union.sc_core::sc_event_timed_u", ptr %12, i64 %15
  %17 = load ptr, ptr @_ZN7sc_coreL9free_listE, align 8, !tbaa !9
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds %"union.sc_core::sc_event_timed_u", ptr %17, i64 %19
  store ptr %16, ptr %20, align 8, !tbaa !11
  br label %21

21:                                               ; preds = %11
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = add nsw i32 %22, 1
  store i32 %23, ptr %2, align 4, !tbaa !5
  br label %8, !llvm.loop !12

24:                                               ; preds = %8
  %25 = load ptr, ptr @_ZN7sc_coreL9free_listE, align 8, !tbaa !9
  %26 = load i32, ptr %2, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds %"union.sc_core::sc_event_timed_u", ptr %25, i64 %27
  store ptr null, ptr %28, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %29

29:                                               ; preds = %24, %0
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %30 = load ptr, ptr @_ZN7sc_coreL9free_listE, align 8, !tbaa !9
  store ptr %30, ptr %3, align 8, !tbaa !9
  %31 = load ptr, ptr @_ZN7sc_coreL9free_listE, align 8, !tbaa !9
  %32 = load ptr, ptr %31, align 8, !tbaa !11
  store ptr %32, ptr @_ZN7sc_coreL9free_listE, align 8, !tbaa !9
  %33 = load ptr, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret ptr %33
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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" }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

%"union.sc_core::sc_event_timed_u" = type { ptr, [8 x i8] }

@_ZN7sc_coreL9free_listE = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local noundef ptr @_ZN7sc_core14sc_event_timed8allocateEv() local_unnamed_addr #0 align 2 {
  %1 = load ptr, ptr @_ZN7sc_coreL9free_listE, align 8, !tbaa !5
  %2 = icmp eq ptr %1, null
  br i1 %2, label %3, label %18

3:                                                ; preds = %0
  %4 = tail call noalias dereferenceable_or_null(1024) ptr @malloc(i64 noundef 1024) #2
  store ptr %4, ptr @_ZN7sc_coreL9free_listE, align 8, !tbaa !5
  br label %5

5:                                                ; preds = %5, %3
  %indvars.iv = phi i64 [ 0, %3 ], [ %indvars.iv.next.2, %5 ]
  %6 = load ptr, ptr @_ZN7sc_coreL9free_listE, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %7 = getelementptr inbounds %"union.sc_core::sc_event_timed_u", ptr %6, i64 %indvars.iv.next
  %8 = getelementptr inbounds %"union.sc_core::sc_event_timed_u", ptr %6, i64 %indvars.iv
  store ptr %7, ptr %8, align 8, !tbaa !9
  %9 = load ptr, ptr @_ZN7sc_coreL9free_listE, align 8, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %10 = getelementptr inbounds %"union.sc_core::sc_event_timed_u", ptr %9, i64 %indvars.iv.next.1
  %11 = getelementptr inbounds %"union.sc_core::sc_event_timed_u", ptr %9, i64 %indvars.iv.next
  store ptr %10, ptr %11, align 8, !tbaa !9
  %12 = load ptr, ptr @_ZN7sc_coreL9free_listE, align 8, !tbaa !5
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %13 = getelementptr inbounds %"union.sc_core::sc_event_timed_u", ptr %12, i64 %indvars.iv.next.2
  %14 = getelementptr inbounds %"union.sc_core::sc_event_timed_u", ptr %12, i64 %indvars.iv.next.1
  store ptr %13, ptr %14, align 8, !tbaa !9
  %exitcond.not.2 = icmp eq i64 %indvars.iv.next.2, 63
  br i1 %exitcond.not.2, label %15, label %5, !llvm.loop !10

15:                                               ; preds = %5
  %16 = load ptr, ptr @_ZN7sc_coreL9free_listE, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %16, i64 1008
  store ptr null, ptr %17, align 8, !tbaa !9
  br label %18

18:                                               ; preds = %15, %0
  %19 = phi ptr [ %16, %15 ], [ %1, %0 ]
  %20 = load ptr, ptr %19, align 8, !tbaa !9
  store ptr %20, ptr @_ZN7sc_coreL9free_listE, align 8, !tbaa !5
  ret ptr %19
}

; 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 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LoopRotatePass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass