; LLVM IR from gem5/gem5 :: ext/systemc/src/sysc/datatypes/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 = shl i32 1, %11
  store i32 %12, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %13 = load i32, ptr %4, align 4, !tbaa !9
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [32 x ptr], ptr @_ZN5sc_dtL10free_wordsE, i64 0, i64 %14
  store ptr %15, ptr %6, align 8, !tbaa !11
  %16 = load ptr, ptr %6, align 8, !tbaa !11
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  %18 = icmp ne ptr %17, null
  br i1 %18, label %59, label %19

19:                                               ; preds = %1
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = mul nsw i32 128, %20
  %22 = sext i32 %21 to i64
  %23 = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 %22, i64 8)
  %24 = extractvalue { i64, i1 } %23, 1
  %25 = extractvalue { i64, i1 } %23, 0
  %26 = select i1 %24, i64 -1, i64 %25
  %27 = call noalias noundef nonnull ptr @_Znam(i64 noundef %26) #6
  %28 = load ptr, ptr %6, align 8, !tbaa !11
  store ptr %27, ptr %28, 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 %29

29:                                               ; preds = %48, %19
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = load i32, ptr %5, align 4, !tbaa !9
  %32 = mul nsw i32 %31, 127
  %33 = icmp slt i32 %30, %32
  br i1 %33, label %34, label %52

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

48:                                               ; preds = %34
  %49 = load i32, ptr %5, align 4, !tbaa !9
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = add nsw i32 %50, %49
  store i32 %51, ptr %7, align 4, !tbaa !9
  br label %29, !llvm.loop !15

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

59:                                               ; preds = %52, %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %60 = load ptr, ptr %6, align 8, !tbaa !11
  %61 = load ptr, ptr %60, align 8, !tbaa !11
  store ptr %61, ptr %8, align 8, !tbaa !11
  %62 = load ptr, ptr %6, align 8, !tbaa !11
  %63 = load ptr, ptr %62, align 8, !tbaa !11
  %64 = getelementptr inbounds %"class.sc_dt::word_list", ptr %63, i64 0
  %65 = getelementptr inbounds %"class.sc_dt::word_list", ptr %64, i32 0, i32 0
  %66 = load ptr, ptr %65, align 8, !tbaa !13
  %67 = load i32, ptr %4, align 4, !tbaa !9
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [32 x ptr], ptr @_ZN5sc_dtL10free_wordsE, i64 0, i64 %68
  store ptr %66, ptr %69, align 8, !tbaa !11
  %70 = 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 %70
}

; 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 = shl nuw i32 1, %2
  %4 = sext i32 %2 to i64
  %5 = getelementptr inbounds [32 x ptr], ptr @_ZN5sc_dtL10free_wordsE, i64 0, i64 %4
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %7, label %25

7:                                                ; preds = %1
  %8 = shl i32 128, %2
  %9 = zext nneg i32 %8 to i64
  %10 = icmp slt i32 %8, 0
  %11 = shl nuw nsw i64 %9, 3
  %12 = select i1 %10, i64 -1, i64 %11
  %13 = tail call noalias noundef nonnull ptr @_Znam(i64 noundef %12) #3
  store ptr %13, ptr %5, align 8, !tbaa !5
  %14 = shl i32 127, %2
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %7
  %16 = sext i32 %3 to i64
  %17 = zext nneg i32 %14 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %indvars.iv.next = add nsw i64 %indvars.iv, %16
  %19 = getelementptr inbounds %"class.sc_dt::word_list", ptr %18, i64 %indvars.iv.next
  %20 = getelementptr inbounds %"class.sc_dt::word_list", ptr %18, i64 %indvars.iv
  store ptr %19, ptr %20, align 8, !tbaa !9
  %21 = icmp slt i64 %indvars.iv.next, %17
  br i1 %21, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !11

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre = load ptr, ptr %5, align 8, !tbaa !5
  %sext = shl i64 %indvars.iv.next, 32
  %22 = ashr exact i64 %sext, 32
  br label %._crit_edge

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

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

; 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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass