; LLVM IR from JessonYue/LeetCodeLearning :: Array/zhangjunwei/8月 (insertBits)

; 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: nounwind uwtable
define dso_local i32 @insertBits(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  store i32 %3, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %10 = load i32, ptr %7, align 4, !tbaa !5
  store i32 %10, ptr %9, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %22, %4
  %12 = load i32, ptr %9, align 4, !tbaa !5
  %13 = load i32, ptr %8, align 4, !tbaa !5
  %14 = icmp sle i32 %12, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %25

16:                                               ; preds = %11
  %17 = load i32, ptr %9, align 4, !tbaa !5
  %18 = shl i32 1, %17
  %19 = xor i32 %18, -1
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = and i32 %20, %19
  store i32 %21, ptr %5, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %16
  %23 = load i32, ptr %9, align 4, !tbaa !5
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr %9, align 4, !tbaa !5
  br label %11, !llvm.loop !9

25:                                               ; preds = %15
  %26 = load i32, ptr %6, align 4, !tbaa !5
  %27 = load i32, ptr %7, align 4, !tbaa !5
  %28 = shl i32 %26, %27
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = or i32 %29, %28
  store i32 %30, ptr %5, align 4, !tbaa !5
  %31 = load i32, ptr %5, align 4, !tbaa !5
  ret i32 %31
}

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

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"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: nofree norecurse nosync nounwind memory(none) uwtable
define dso_local i32 @insertBits(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %.not11 = icmp sgt i32 %2, %3
  br i1 %.not11, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %4
  %5 = add i32 %3, 1
  %6 = sub i32 %5, %2
  %7 = sub i32 %3, %2
  %xtraiter = and i32 %6, 3
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader, %.lr.ph.prol
  %.013.prol = phi i32 [ %11, %.lr.ph.prol ], [ %2, %.lr.ph.preheader ]
  %.01012.prol = phi i32 [ %10, %.lr.ph.prol ], [ %0, %.lr.ph.preheader ]
  %prol.iter = phi i32 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader ]
  %8 = shl nuw i32 1, %.013.prol
  %9 = xor i32 %8, -1
  %10 = and i32 %.01012.prol, %9
  %11 = add i32 %.013.prol, 1
  %prol.iter.next = add i32 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i32 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol, !llvm.loop !5

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.lcssa.unr = phi i32 [ poison, %.lr.ph.preheader ], [ %10, %.lr.ph.prol ]
  %.013.unr = phi i32 [ %2, %.lr.ph.preheader ], [ %11, %.lr.ph.prol ]
  %.01012.unr = phi i32 [ %0, %.lr.ph.preheader ], [ %10, %.lr.ph.prol ]
  %12 = icmp ult i32 %7, 3
  br i1 %12, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %4
  %.010.lcssa = phi i32 [ %0, %4 ], [ %.lcssa.unr, %.lr.ph.prol.loopexit ], [ %18, %.lr.ph ]
  %13 = shl i32 %1, %2
  %14 = or i32 %.010.lcssa, %13
  ret i32 %14

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.013 = phi i32 [ %19, %.lr.ph ], [ %.013.unr, %.lr.ph.prol.loopexit ]
  %.01012 = phi i32 [ %18, %.lr.ph ], [ %.01012.unr, %.lr.ph.prol.loopexit ]
  %15 = add i32 %.013, 3
  %16 = shl i32 15, %.013
  %17 = xor i32 %16, -1
  %18 = and i32 %.01012, %17
  %19 = add i32 %.013, 4
  %exitcond.not.3 = icmp eq i32 %15, %3
  br i1 %exitcond.not.3, label %._crit_edge, label %.lr.ph, !llvm.loop !7
}

attributes #0 = { nofree norecurse nosync nounwind memory(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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.unroll.disable"}
!7 = distinct !{!7, !8}
!8 = !{!"llvm.loop.mustprogress"}


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