; LLVM IR from gem5/gem5 :: src/systemc/dt/fx/scfx_mant.ll (ZN5sc_dt9scfx_mant10alloc_wordEm)

; 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_dt::word_list" = type { ptr }

@_ZN5sc_dtL10free_wordsE = external hidden global [32 x ptr], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN5sc_dt9scfx_mant10alloc_wordEm(i64 noundef %0) #0 align 2 {
  %2 = alloca i64, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  store i64 %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  store i32 128, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  %9 = load i64, ptr %2, align 8, !tbaa !5
  %10 = call noundef i32 @_ZN5sc_dtL15next_pow2_indexEm(i64 noundef %9)
  store i32 %10, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = zext i32 %11 to i64
  %13 = shl i64 1, %12
  %14 = trunc i64 %13 to i32
  store i32 %14, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %15 = load i32, ptr %4, align 4, !tbaa !9
  %16 = zext i32 %15 to i64
  %17 = getelementptr inbounds [32 x ptr], ptr @_ZN5sc_dtL10free_wordsE, i64 0, i64 %16
  store ptr %17, ptr %6, align 8, !tbaa !11
  %18 = load ptr, ptr %6, align 8, !tbaa !11
  %19 = load ptr, ptr %18, align 8, !tbaa !11
  %20 = icmp ne ptr %19, null
  br i1 %20, label %61, label %21

21:                                               ; preds = %1
  %22 = load i32, ptr %5, align 4, !tbaa !9
  %23 = mul i32 128, %22
  %24 = zext i32 %23 to i64
  %25 = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 %24, i64 8)
  %26 = extractvalue { i64, i1 } %25, 1
  %27 = extractvalue { i64, i1 } %25, 0
  %28 = select i1 %26, i64 -1, i64 %27
  %29 = call noalias noundef nonnull ptr @_Znam(i64 noundef %28) #6
  %30 = load ptr, ptr %6, align 8, !tbaa !11
  store ptr %29, ptr %30, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %31

31:                                               ; preds = %50, %21
  %32 = load i32, ptr %7, align 4, !tbaa !9
  %33 = load i32, ptr %5, align 4, !tbaa !9
  %34 = mul i32 %33, 127
  %35 = icmp ult i32 %32, %34
  br i1 %35, label %36, label %54

36:                                               ; preds = %31
  %37 = load ptr, ptr %6, align 8, !tbaa !11
  %38 = load ptr, ptr %37, align 8, !tbaa !11
  %39 = load i32, ptr %7, align 4, !tbaa !9
  %40 = load i32, ptr %5, align 4, !tbaa !9
  %41 = add i32 %39, %40
  %42 = zext i32 %41 to i64
  %43 = getelementptr inbounds %"class.sc_dt::word_list", ptr %38, i64 %42
  %44 = load ptr, ptr %6, align 8, !tbaa !11
  %45 = load ptr, ptr %44, align 8, !tbaa !11
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = zext i32 %46 to i64
  %48 = getelementptr inbounds %"class.sc_dt::word_list", ptr %45, i64 %47
  %49 = getelementptr inbounds %"class.sc_dt::word_list", ptr %48, i32 0, i32 0
  store ptr %43, ptr %49, align 8, !tbaa !13
  br label %50

50:                                               ; preds = %36
  %51 = load i32, ptr %5, align 4, !tbaa !9
  %52 = load i32, ptr %7, align 4, !tbaa !9
  %53 = add i32 %52, %51
  store i32 %53, ptr %7, align 4, !tbaa !9
  br label %31, !llvm.loop !15

54:                                               ; preds = %31
  %55 = load ptr, ptr %6, align 8, !tbaa !11
  %56 = load ptr, ptr %55, align 8, !tbaa !11
  %57 = load i32, ptr %7, align 4, !tbaa !9
  %58 = zext i32 %57 to i64
  %59 = getelementptr inbounds %"class.sc_dt::word_list", ptr %56, i64 %58
  %60 = getelementptr inbounds %"class.sc_dt::word_list", ptr %59, i32 0, i32 0
  store ptr null, ptr %60, align 8, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  br label %61

61:                                               ; preds = %54, %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %62 = load ptr, ptr %6, align 8, !tbaa !11
  %63 = load ptr, ptr %62, align 8, !tbaa !11
  store ptr %63, ptr %8, align 8, !tbaa !11
  %64 = load ptr, ptr %6, align 8, !tbaa !11
  %65 = load ptr, ptr %64, align 8, !tbaa !11
  %66 = getelementptr inbounds %"class.sc_dt::word_list", ptr %65, i64 0
  %67 = getelementptr inbounds %"class.sc_dt::word_list", ptr %66, i32 0, i32 0
  %68 = load ptr, ptr %67, align 8, !tbaa !13
  %69 = load i32, ptr %4, align 4, !tbaa !9
  %70 = zext i32 %69 to i64
  %71 = getelementptr inbounds [32 x ptr], ptr @_ZN5sc_dtL10free_wordsE, i64 0, i64 %70
  store ptr %68, ptr %71, align 8, !tbaa !11
  %72 = load ptr, ptr %8, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  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: inlinehint mustprogress uwtable
declare hidden noundef i32 @_ZN5sc_dtL15next_pow2_indexEm(i64 noundef) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare { i64, i1 } @llvm.umul.with.overflow.i64(i64, i64) #3

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

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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 #5 = { nounwind }
attributes #6 = { builtin 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 = !{!"long", !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, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"_ZTSN5sc_dt9word_listE", !12, 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"

%"class.sc_dt::word_list" = type { ptr }

@_ZN5sc_dtL10free_wordsE = external hidden local_unnamed_addr global [32 x ptr], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN5sc_dt9scfx_mant10alloc_wordEm(i64 noundef %0) local_unnamed_addr #0 align 2 {
  %2 = tail call noundef i32 @_ZN5sc_dtL15next_pow2_indexEm(i64 noundef %0)
  %3 = zext i32 %2 to i64
  %4 = shl nuw i64 1, %3
  %5 = trunc i64 %4 to i32
  %6 = getelementptr inbounds [32 x ptr], ptr @_ZN5sc_dtL10free_wordsE, i64 0, i64 %3
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %.not = icmp eq ptr %7, null
  br i1 %.not, label %8, label %23

8:                                                ; preds = %1
  %9 = shl i64 1024, %3
  %10 = and i64 %9, 34359737344
  %11 = tail call noalias noundef nonnull ptr @_Znam(i64 noundef %10) #3
  store ptr %11, ptr %6, align 8, !tbaa !5
  %12 = mul i32 %5, 127
  %.not21 = icmp ugt i32 %2, 31
  br i1 %.not21, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %8, %.lr.ph
  %.020 = phi i32 [ %14, %.lr.ph ], [ 0, %8 ]
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = add i32 %.020, %5
  %15 = zext i32 %14 to i64
  %16 = getelementptr inbounds %"class.sc_dt::word_list", ptr %13, i64 %15
  %17 = zext i32 %.020 to i64
  %18 = getelementptr inbounds %"class.sc_dt::word_list", ptr %13, i64 %17
  store ptr %16, ptr %18, align 8, !tbaa !9
  %19 = icmp ult i32 %14, %12
  br i1 %19, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !11

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre = load ptr, ptr %6, align 8, !tbaa !5
  %20 = zext i32 %14 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %8, %._crit_edge.loopexit
  %21 = phi ptr [ %.pre, %._crit_edge.loopexit ], [ %11, %8 ]
  %.0.lcssa = phi i64 [ %20, %._crit_edge.loopexit ], [ 0, %8 ]
  %22 = getelementptr inbounds %"class.sc_dt::word_list", ptr %21, i64 %.0.lcssa
  store ptr null, ptr %22, align 8, !tbaa !9
  %.pre22 = load ptr, ptr %6, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %._crit_edge, %1
  %24 = phi ptr [ %.pre22, %._crit_edge ], [ %7, %1 ]
  %25 = load ptr, ptr %24, align 8, !tbaa !9
  store ptr %25, ptr %6, align 8, !tbaa !5
  ret ptr %24
}

; Function Attrs: inlinehint mustprogress uwtable
declare hidden noundef i32 @_ZN5sc_dtL15next_pow2_indexEm(i64 noundef) local_unnamed_addr #1

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) local_unnamed_addr #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 = { inlinehint 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 = { 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 #3 = { builtin 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, !6, i64 0}
!10 = !{!"_ZTSN5sc_dt9word_listE", !6, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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