; LLVM IR from gem5/gem5 :: ext/systemc/src/sysc/datatypes/int/sc_unsigned.ll (ZNK5sc_dt11sc_unsigned10and_reduceEv)

; 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::sc_unsigned" = type { %"class.sc_dt::sc_value_base", i32, i32, i32, ptr }
%"class.sc_dt::sc_value_base" = type { ptr }

; 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 zeroext i1 @_ZNK5sc_dt11sc_unsigned10and_reduceEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #1 align 2 {
  %2 = alloca i1, align 1
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %6 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %7 = getelementptr inbounds %"class.sc_dt::sc_unsigned", ptr %6, i32 0, i32 1
  %8 = load i32, ptr %7, align 8, !tbaa !9
  %9 = icmp eq i32 %8, 0
  br i1 %9, label %10, label %11

10:                                               ; preds = %1
  store i1 false, ptr %2, align 1
  store i32 1, ptr %5, align 4
  br label %56

11:                                               ; preds = %1
  store i32 0, ptr %4, align 4, !tbaa !13
  br label %12

12:                                               ; preds = %30, %11
  %13 = load i32, ptr %4, align 4, !tbaa !13
  %14 = getelementptr inbounds %"class.sc_dt::sc_unsigned", ptr %6, i32 0, i32 3
  %15 = load i32, ptr %14, align 8, !tbaa !14
  %16 = sub nsw i32 %15, 1
  %17 = icmp slt i32 %13, %16
  br i1 %17, label %18, label %33

18:                                               ; preds = %12
  %19 = getelementptr inbounds %"class.sc_dt::sc_unsigned", ptr %6, i32 0, i32 4
  %20 = load ptr, ptr %19, align 8, !tbaa !15
  %21 = load i32, ptr %4, align 4, !tbaa !13
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds i32, ptr %20, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !13
  %25 = zext i32 %24 to i64
  %26 = and i64 %25, 1073741823
  %27 = icmp ne i64 %26, 1073741823
  br i1 %27, label %28, label %29

28:                                               ; preds = %18
  store i1 false, ptr %2, align 1
  store i32 1, ptr %5, align 4
  br label %56

29:                                               ; preds = %18
  br label %30

30:                                               ; preds = %29
  %31 = load i32, ptr %4, align 4, !tbaa !13
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %4, align 4, !tbaa !13
  br label %12, !llvm.loop !16

33:                                               ; preds = %12
  %34 = getelementptr inbounds %"class.sc_dt::sc_unsigned", ptr %6, i32 0, i32 4
  %35 = load ptr, ptr %34, align 8, !tbaa !15
  %36 = load i32, ptr %4, align 4, !tbaa !13
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds i32, ptr %35, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !13
  %40 = getelementptr inbounds %"class.sc_dt::sc_unsigned", ptr %6, i32 0, i32 2
  %41 = load i32, ptr %40, align 4, !tbaa !18
  %42 = sub nsw i32 %41, 1
  %43 = srem i32 %42, 30
  %44 = shl i32 -1, %43
  %45 = xor i32 %44, -1
  %46 = and i32 %39, %45
  %47 = getelementptr inbounds %"class.sc_dt::sc_unsigned", ptr %6, i32 0, i32 2
  %48 = load i32, ptr %47, align 4, !tbaa !18
  %49 = sub nsw i32 %48, 1
  %50 = srem i32 %49, 30
  %51 = shl i32 -1, %50
  %52 = xor i32 %51, -1
  %53 = icmp eq i32 %46, %52
  br i1 %53, label %54, label %55

54:                                               ; preds = %33
  store i1 true, ptr %2, align 1
  store i32 1, ptr %5, align 4
  br label %56

55:                                               ; preds = %33
  store i1 false, ptr %2, align 1
  store i32 1, ptr %5, align 4
  br label %56

56:                                               ; preds = %55, %54, %28, %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %57 = load i1, ptr %2, align 1
  ret i1 %57
}

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 }

!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, !12, i64 8}
!10 = !{!"_ZTSN5sc_dt11sc_unsignedE", !11, i64 0, !12, i64 8, !12, i64 12, !12, i64 16, !6, i64 24}
!11 = !{!"_ZTSN5sc_dt13sc_value_baseE"}
!12 = !{!"int", !7, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!10, !12, i64 16}
!15 = !{!10, !6, i64 24}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!10, !12, i64 12}


; 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 norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i1 @_ZNK5sc_dt11sc_unsigned10and_reduceEv(ptr nocapture noundef nonnull readonly align 8 dereferenceable(32) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = icmp eq i32 %3, 0
  br i1 %4, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  %6 = load i32, ptr %5, align 8, !tbaa !12
  %7 = icmp sgt i32 %6, 1
  br i1 %7, label %.lr.ph, label %.preheader.._crit_edge_crit_edge

.preheader.._crit_edge_crit_edge:                 ; preds = %.preheader
  %.phi.trans.insert = getelementptr inbounds i8, ptr %0, i64 24
  %.pre = load ptr, ptr %.phi.trans.insert, align 8, !tbaa !13
  br label %._crit_edge

.lr.ph:                                           ; preds = %.preheader
  %8 = add nsw i32 %6, -1
  %9 = getelementptr inbounds i8, ptr %0, i64 24
  %10 = load ptr, ptr %9, align 8, !tbaa !13
  %11 = add nsw i32 %6, -1
  %wide.trip.count = zext nneg i32 %8 to i64
  br label %12

12:                                               ; preds = %.lr.ph, %16
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %16 ]
  %13 = getelementptr inbounds i32, ptr %10, i64 %indvars.iv
  %14 = load i32, ptr %13, align 4, !tbaa !14
  %15 = and i32 %14, 1073741823
  %.not = icmp eq i32 %15, 1073741823
  br i1 %.not, label %16, label %.loopexit

16:                                               ; preds = %12
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge.loopexit, label %12, !llvm.loop !15

._crit_edge.loopexit:                             ; preds = %16
  %17 = zext nneg i32 %11 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %.preheader.._crit_edge_crit_edge, %._crit_edge.loopexit
  %18 = phi ptr [ %.pre, %.preheader.._crit_edge_crit_edge ], [ %10, %._crit_edge.loopexit ]
  %.0.lcssa = phi i64 [ 0, %.preheader.._crit_edge_crit_edge ], [ %17, %._crit_edge.loopexit ]
  %19 = getelementptr inbounds i32, ptr %18, i64 %.0.lcssa
  %20 = load i32, ptr %19, align 4, !tbaa !14
  %21 = getelementptr inbounds i8, ptr %0, i64 12
  %22 = load i32, ptr %21, align 4, !tbaa !17
  %23 = add nsw i32 %22, -1
  %24 = srem i32 %23, 30
  %25 = shl nsw i32 -1, %24
  %26 = or i32 %25, %20
  %27 = icmp eq i32 %26, -1
  br label %.loopexit

.loopexit:                                        ; preds = %12, %._crit_edge, %1
  %.05 = phi i1 [ false, %1 ], [ %27, %._crit_edge ], [ false, %12 ]
  ret i1 %.05
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) 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" }

!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, !8, i64 8}
!6 = !{!"_ZTSN5sc_dt11sc_unsignedE", !7, i64 0, !8, i64 8, !8, i64 12, !8, i64 16, !11, i64 24}
!7 = !{!"_ZTSN5sc_dt13sc_value_baseE"}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = !{!"any pointer", !9, i64 0}
!12 = !{!6, !8, i64 16}
!13 = !{!6, !11, i64 24}
!14 = !{!8, !8, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!6, !8, i64 12}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass