; LLVM IR from gem5/gem5 :: ext/systemc/src/sysc/datatypes/int/sc_signed.ll (ZNK5sc_dt9sc_signed10and_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_signed" = 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_dt9sc_signed10and_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
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %7 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %8 = getelementptr inbounds %"class.sc_dt::sc_signed", ptr %7, i32 0, i32 1
  %9 = load i32, ptr %8, align 8, !tbaa !9
  %10 = icmp eq i32 %9, -1
  br i1 %10, label %11, label %70

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

12:                                               ; preds = %38, %11
  %13 = load i32, ptr %5, align 4, !tbaa !13
  %14 = getelementptr inbounds %"class.sc_dt::sc_signed", ptr %7, 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 %41

18:                                               ; preds = %12
  %19 = load i32, ptr %4, align 4, !tbaa !13
  %20 = lshr i32 %19, 30
  %21 = zext i32 %20 to i64
  %22 = getelementptr inbounds %"class.sc_dt::sc_signed", ptr %7, i32 0, i32 4
  %23 = load ptr, ptr %22, align 8, !tbaa !15
  %24 = load i32, ptr %5, align 4, !tbaa !13
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i32, ptr %23, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !13
  %28 = zext i32 %27 to i64
  %29 = xor i64 %28, 1073741823
  %30 = add i64 %21, %29
  %31 = trunc i64 %30 to i32
  store i32 %31, ptr %4, align 4, !tbaa !13
  %32 = load i32, ptr %4, align 4, !tbaa !13
  %33 = zext i32 %32 to i64
  %34 = and i64 %33, 1073741823
  %35 = icmp ne i64 %34, 1073741823
  br i1 %35, label %36, label %37

36:                                               ; preds = %18
  store i1 false, ptr %2, align 1
  store i32 1, ptr %6, align 4
  br label %71

37:                                               ; preds = %18
  br label %38

38:                                               ; preds = %37
  %39 = load i32, ptr %5, align 4, !tbaa !13
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %5, align 4, !tbaa !13
  br label %12, !llvm.loop !16

41:                                               ; preds = %12
  %42 = load i32, ptr %4, align 4, !tbaa !13
  %43 = lshr i32 %42, 30
  %44 = zext i32 %43 to i64
  %45 = getelementptr inbounds %"class.sc_dt::sc_signed", ptr %7, i32 0, i32 4
  %46 = load ptr, ptr %45, align 8, !tbaa !15
  %47 = load i32, ptr %5, align 4, !tbaa !13
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds i32, ptr %46, i64 %48
  %50 = load i32, ptr %49, align 4, !tbaa !13
  %51 = zext i32 %50 to i64
  %52 = xor i64 %51, 1073741823
  %53 = add i64 %44, %52
  %54 = trunc i64 %53 to i32
  store i32 %54, ptr %4, align 4, !tbaa !13
  %55 = load i32, ptr %4, align 4, !tbaa !13
  %56 = getelementptr inbounds %"class.sc_dt::sc_signed", ptr %7, i32 0, i32 2
  %57 = load i32, ptr %56, align 4, !tbaa !18
  %58 = srem i32 %57, 30
  %59 = shl i32 -1, %58
  %60 = xor i32 %59, -1
  %61 = and i32 %55, %60
  %62 = getelementptr inbounds %"class.sc_dt::sc_signed", ptr %7, i32 0, i32 2
  %63 = load i32, ptr %62, align 4, !tbaa !18
  %64 = srem i32 %63, 30
  %65 = shl i32 -1, %64
  %66 = xor i32 %65, -1
  %67 = icmp eq i32 %61, %66
  br i1 %67, label %68, label %69

68:                                               ; preds = %41
  store i1 true, ptr %2, align 1
  store i32 1, ptr %6, align 4
  br label %71

69:                                               ; preds = %41
  br label %70

70:                                               ; preds = %69, %1
  store i1 false, ptr %2, align 1
  store i32 1, ptr %6, align 4
  br label %71

71:                                               ; preds = %70, %68, %36
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %72 = load i1, ptr %2, align 1
  ret i1 %72
}

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_dt9sc_signedE", !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_dt9sc_signed10and_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, -1
  br i1 %4, label %.preheader, label %33

.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, %19
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %19 ]
  %.0913 = phi i32 [ 1073741824, %.lr.ph ], [ %17, %19 ]
  %13 = lshr i32 %.0913, 30
  %14 = getelementptr inbounds i32, ptr %10, i64 %indvars.iv
  %15 = load i32, ptr %14, align 4, !tbaa !14
  %16 = xor i32 %15, 1073741823
  %17 = add i32 %16, %13
  %18 = and i32 %17, 1073741823
  %.not = icmp eq i32 %18, 1073741823
  br i1 %.not, label %19, label %.loopexit

19:                                               ; 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 = %19
  %20 = lshr i32 %17, 30
  %21 = zext nneg i32 %11 to i64
  br label %._crit_edge

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

33:                                               ; preds = %._crit_edge, %1
  br label %.loopexit

.loopexit:                                        ; preds = %12, %._crit_edge, %33
  %.010 = phi i1 [ false, %33 ], [ true, %._crit_edge ], [ false, %12 ]
  ret i1 %.010
}

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_dt9sc_signedE", !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,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass