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

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

; 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 i32 @_ZN5sc_dt11vec_to_charEiPKjiPh(i32 noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %16 = load i32, ptr %5, align 4, !tbaa !5
  %17 = mul nsw i32 %16, 30
  store i32 %17, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 0, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %18 = load i32, ptr %10, align 4, !tbaa !5
  %19 = add nsw i32 %18, 8
  %20 = sub nsw i32 %19, 1
  store i32 %20, ptr %11, align 4, !tbaa !5
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %58, %4
  %22 = load i32, ptr %9, align 4, !tbaa !5
  %23 = icmp sgt i32 %22, 0
  br i1 %23, label %24, label %73

24:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %25 = load i32, ptr %11, align 4, !tbaa !5
  %26 = sdiv i32 %25, 30
  store i32 %26, ptr %12, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  %27 = load i32, ptr %10, align 4, !tbaa !5
  %28 = sdiv i32 %27, 30
  store i32 %28, ptr %13, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #2
  %29 = load i32, ptr %7, align 4, !tbaa !5
  %30 = shl i32 %29, 3
  %31 = srem i32 %30, 30
  store i32 %31, ptr %14, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #2
  %32 = load ptr, ptr %6, align 8, !tbaa !9
  %33 = load i32, ptr %13, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i32, ptr %32, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = load i32, ptr %14, align 4, !tbaa !5
  %38 = lshr i32 %36, %37
  store i32 %38, ptr %15, align 4, !tbaa !5
  %39 = load i32, ptr %12, align 4, !tbaa !5
  %40 = load i32, ptr %13, align 4, !tbaa !5
  %41 = icmp ne i32 %39, %40
  br i1 %41, label %42, label %58

42:                                               ; preds = %24
  %43 = load i32, ptr %12, align 4, !tbaa !5
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = icmp slt i32 %43, %44
  br i1 %45, label %46, label %57

46:                                               ; preds = %42
  %47 = load ptr, ptr %6, align 8, !tbaa !9
  %48 = load i32, ptr %12, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds i32, ptr %47, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = load i32, ptr %14, align 4, !tbaa !5
  %53 = sub nsw i32 30, %52
  %54 = shl i32 %51, %53
  %55 = load i32, ptr %15, align 4, !tbaa !5
  %56 = or i32 %55, %54
  store i32 %56, ptr %15, align 4, !tbaa !5
  br label %57

57:                                               ; preds = %46, %42
  br label %58

58:                                               ; preds = %57, %24
  %59 = load i32, ptr %15, align 4, !tbaa !5
  %60 = and i32 %59, 255
  %61 = trunc i32 %60 to i8
  %62 = load ptr, ptr %8, align 8, !tbaa !9
  %63 = load i32, ptr %7, align 4, !tbaa !5
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %7, align 4, !tbaa !5
  %65 = sext i32 %63 to i64
  %66 = getelementptr inbounds i8, ptr %62, i64 %65
  store i8 %61, ptr %66, align 1, !tbaa !11
  %67 = load i32, ptr %11, align 4, !tbaa !5
  %68 = add nsw i32 %67, 8
  store i32 %68, ptr %11, align 4, !tbaa !5
  %69 = load i32, ptr %10, align 4, !tbaa !5
  %70 = add nsw i32 %69, 8
  store i32 %70, ptr %10, align 4, !tbaa !5
  %71 = load i32, ptr %9, align 4, !tbaa !5
  %72 = sub nsw i32 %71, 8
  store i32 %72, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %21, !llvm.loop !12

73:                                               ; preds = %21
  %74 = load i32, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  ret i32 %74
}

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 = !{!"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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local noundef i32 @_ZN5sc_dt11vec_to_charEiPKjiPh(i32 noundef %0, ptr nocapture noundef readonly %1, i32 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = icmp sgt i32 %0, 0
  br i1 %5, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %4
  %6 = mul nuw nsw i32 %0, 30
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %22
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %22 ]
  %.02430 = phi i32 [ %6, %.lr.ph.preheader ], [ %27, %22 ]
  %.02529 = phi i32 [ 0, %.lr.ph.preheader ], [ %26, %22 ]
  %.02628 = phi i32 [ 7, %.lr.ph.preheader ], [ %25, %22 ]
  %7 = udiv i32 %.02628, 30
  %8 = udiv i32 %.02529, 30
  %indvars.iv.tr = trunc i64 %indvars.iv to i32
  %9 = shl i32 %indvars.iv.tr, 3
  %.urem = urem i32 %9, 30
  %10 = zext nneg i32 %8 to i64
  %11 = getelementptr inbounds i32, ptr %1, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = lshr i32 %12, %.urem
  %.not = icmp ne i32 %7, %8
  %14 = icmp slt i32 %7, %0
  %or.cond = and i1 %14, %.not
  br i1 %or.cond, label %15, label %22

15:                                               ; preds = %.lr.ph
  %16 = zext nneg i32 %7 to i64
  %17 = getelementptr inbounds i32, ptr %1, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = sub nuw nsw i32 30, %.urem
  %20 = shl i32 %18, %19
  %21 = or i32 %20, %13
  br label %22

22:                                               ; preds = %15, %.lr.ph
  %.0 = phi i32 [ %21, %15 ], [ %13, %.lr.ph ]
  %23 = trunc i32 %.0 to i8
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %24 = getelementptr inbounds i8, ptr %3, i64 %indvars.iv
  store i8 %23, ptr %24, align 1, !tbaa !9
  %25 = add nuw nsw i32 %.02628, 8
  %26 = add nuw nsw i32 %.02529, 8
  %27 = add nsw i32 %.02430, -8
  %28 = icmp sgt i32 %.02430, 8
  br i1 %28, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !10

._crit_edge.loopexit:                             ; preds = %22
  %29 = trunc nuw nsw i64 %indvars.iv.next to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %4
  %.023.lcssa = phi i32 [ 0, %4 ], [ %29, %._crit_edge.loopexit ]
  ret i32 %.023.lcssa
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite) 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, !6, i64 0}
!6 = !{!"int", !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,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass