; LLVM IR from devparthgarg/Data-Structures-and-Algorithms :: 05. Arrays/11 Left Rotate Array by D Spaces (11a Left Rotate Array by D Spaces)

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

14:                                               ; preds = %49, %3
  %15 = load i32, ptr %7, align 4, !tbaa !9
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %14
  store i32 2, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %52

19:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds i32, ptr %20, i64 0
  %22 = load i32, ptr %21, align 4, !tbaa !9
  store i32 %22, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 1, ptr %10, align 4, !tbaa !9
  br label %23

23:                                               ; preds = %39, %19
  %24 = load i32, ptr %10, align 4, !tbaa !9
  %25 = load i32, ptr %5, align 4, !tbaa !9
  %26 = icmp slt i32 %24, %25
  br i1 %26, label %28, label %27

27:                                               ; preds = %23
  store i32 5, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %42

28:                                               ; preds = %23
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = load i32, ptr %10, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i32, ptr %29, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !9
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = load i32, ptr %10, align 4, !tbaa !9
  %36 = sub nsw i32 %35, 1
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds i32, ptr %34, i64 %37
  store i32 %33, ptr %38, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %28
  %40 = load i32, ptr %10, align 4, !tbaa !9
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %10, align 4, !tbaa !9
  br label %23, !llvm.loop !11

42:                                               ; preds = %27
  %43 = load i32, ptr %9, align 4, !tbaa !9
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = load i32, ptr %5, align 4, !tbaa !9
  %46 = sub nsw i32 %45, 1
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds i32, ptr %44, i64 %47
  store i32 %43, ptr %48, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %49

49:                                               ; preds = %42
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %7, align 4, !tbaa !9
  br label %14, !llvm.loop !13

52:                                               ; preds = %18
  ret void
}

; 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 = { 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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !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 memory(argmem: readwrite) uwtable
define dso_local void @_Z13rotateDspacesPiii(ptr nocapture noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = srem i32 %2, %1
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.lr.ph20, label %._crit_edge21

.lr.ph20:                                         ; preds = %3
  %invariant.gep = getelementptr i8, ptr %0, i64 -4
  %6 = icmp sgt i32 %1, 1
  %7 = sext i32 %1 to i64
  %gep = getelementptr i32, ptr %invariant.gep, i64 %7
  br i1 %6, label %.lr.ph.us.preheader, label %.lr.ph20.split.preheader

.lr.ph20.split.preheader:                         ; preds = %.lr.ph20
  %.pre = load i32, ptr %0, align 4, !tbaa !5
  store i32 %.pre, ptr %gep, align 4, !tbaa !5
  br label %._crit_edge21

.lr.ph.us.preheader:                              ; preds = %.lr.ph20
  %scevgep = getelementptr i8, ptr %0, i64 4
  %8 = add nsw i32 %1, -1
  %9 = zext nneg i32 %8 to i64
  %10 = shl nuw nsw i64 %9, 2
  %xtraiter = and i32 %4, 3
  %11 = icmp ult i32 %4, 4
  br i1 %11, label %._crit_edge21.loopexit.unr-lcssa, label %.lr.ph.us.preheader.new

.lr.ph.us.preheader.new:                          ; preds = %.lr.ph.us.preheader
  %unroll_iter = and i32 %4, 2147483644
  br label %.lr.ph.us

.lr.ph.us:                                        ; preds = %.lr.ph.us, %.lr.ph.us.preheader.new
  %niter = phi i32 [ 0, %.lr.ph.us.preheader.new ], [ %niter.next.3, %.lr.ph.us ]
  %12 = load i32, ptr %0, align 4, !tbaa !5
  tail call void @llvm.memmove.p0.p0.i64(ptr nonnull align 4 %0, ptr align 4 %scevgep, i64 %10, i1 false), !tbaa !5
  store i32 %12, ptr %gep, align 4, !tbaa !5
  %13 = load i32, ptr %0, align 4, !tbaa !5
  tail call void @llvm.memmove.p0.p0.i64(ptr nonnull align 4 %0, ptr align 4 %scevgep, i64 %10, i1 false), !tbaa !5
  store i32 %13, ptr %gep, align 4, !tbaa !5
  %14 = load i32, ptr %0, align 4, !tbaa !5
  tail call void @llvm.memmove.p0.p0.i64(ptr nonnull align 4 %0, ptr align 4 %scevgep, i64 %10, i1 false), !tbaa !5
  store i32 %14, ptr %gep, align 4, !tbaa !5
  %15 = load i32, ptr %0, align 4, !tbaa !5
  tail call void @llvm.memmove.p0.p0.i64(ptr nonnull align 4 %0, ptr align 4 %scevgep, i64 %10, i1 false), !tbaa !5
  store i32 %15, ptr %gep, align 4, !tbaa !5
  %niter.next.3 = add i32 %niter, 4
  %niter.ncmp.3 = icmp eq i32 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge21.loopexit.unr-lcssa, label %.lr.ph.us, !llvm.loop !9

._crit_edge21.loopexit.unr-lcssa:                 ; preds = %.lr.ph.us, %.lr.ph.us.preheader
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge21, label %.lr.ph.us.epil

.lr.ph.us.epil:                                   ; preds = %._crit_edge21.loopexit.unr-lcssa, %.lr.ph.us.epil
  %epil.iter = phi i32 [ %epil.iter.next, %.lr.ph.us.epil ], [ 0, %._crit_edge21.loopexit.unr-lcssa ]
  %16 = load i32, ptr %0, align 4, !tbaa !5
  tail call void @llvm.memmove.p0.p0.i64(ptr nonnull align 4 %0, ptr align 4 %scevgep, i64 %10, i1 false), !tbaa !5
  store i32 %16, ptr %gep, align 4, !tbaa !5
  %epil.iter.next = add i32 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i32 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge21, label %.lr.ph.us.epil, !llvm.loop !11

._crit_edge21:                                    ; preds = %._crit_edge21.loopexit.unr-lcssa, %.lr.ph.us.epil, %.lr.ph20.split.preheader, %3
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #1

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" }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.unroll.disable"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass