; LLVM IR from devparthgarg/Data-Structures-and-Algorithms :: 05. Arrays/11 Left Rotate Array by D Spaces (11b 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 ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = 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
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = srem i32 %12, %13
  store i32 %14, ptr %6, align 4, !tbaa !9
  %15 = load i32, ptr %6, align 4, !tbaa !9
  %16 = zext i32 %15 to i64
  %17 = call ptr @llvm.stacksave.p0()
  store ptr %17, ptr %7, align 8
  %18 = alloca i32, i64 %16, align 16
  store i64 %16, ptr %8, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %19

19:                                               ; preds = %33, %3
  %20 = load i32, ptr %9, align 4, !tbaa !9
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = icmp slt i32 %20, %21
  br i1 %22, label %24, label %23

23:                                               ; preds = %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %36

24:                                               ; preds = %19
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = load i32, ptr %9, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i32, ptr %25, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !9
  %30 = load i32, ptr %9, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i32, ptr %18, i64 %31
  store i32 %29, ptr %32, align 4, !tbaa !9
  br label %33

33:                                               ; preds = %24
  %34 = load i32, ptr %9, align 4, !tbaa !9
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %9, align 4, !tbaa !9
  br label %19, !llvm.loop !11

36:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %37 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %37, ptr %10, align 4, !tbaa !9
  br label %38

38:                                               ; preds = %55, %36
  %39 = load i32, ptr %10, align 4, !tbaa !9
  %40 = load i32, ptr %5, align 4, !tbaa !9
  %41 = icmp slt i32 %39, %40
  br i1 %41, label %43, label %42

42:                                               ; preds = %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %58

43:                                               ; preds = %38
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = load i32, ptr %10, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i32, ptr %44, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !9
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = load i32, ptr %10, align 4, !tbaa !9
  %51 = load i32, ptr %6, align 4, !tbaa !9
  %52 = sub nsw i32 %50, %51
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds i32, ptr %49, i64 %53
  store i32 %48, ptr %54, align 4, !tbaa !9
  br label %55

55:                                               ; preds = %43
  %56 = load i32, ptr %10, align 4, !tbaa !9
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %10, align 4, !tbaa !9
  br label %38, !llvm.loop !13

58:                                               ; preds = %42
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %59

59:                                               ; preds = %77, %58
  %60 = load i32, ptr %11, align 4, !tbaa !9
  %61 = load i32, ptr %6, align 4, !tbaa !9
  %62 = icmp slt i32 %60, %61
  br i1 %62, label %64, label %63

63:                                               ; preds = %59
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  br label %80

64:                                               ; preds = %59
  %65 = load i32, ptr %11, align 4, !tbaa !9
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds i32, ptr %18, i64 %66
  %68 = load i32, ptr %67, align 4, !tbaa !9
  %69 = load ptr, ptr %4, align 8, !tbaa !5
  %70 = load i32, ptr %5, align 4, !tbaa !9
  %71 = load i32, ptr %6, align 4, !tbaa !9
  %72 = sub nsw i32 %70, %71
  %73 = load i32, ptr %11, align 4, !tbaa !9
  %74 = add nsw i32 %72, %73
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds i32, ptr %69, i64 %75
  store i32 %68, ptr %76, align 4, !tbaa !9
  br label %77

77:                                               ; preds = %64
  %78 = load i32, ptr %11, align 4, !tbaa !9
  %79 = add nsw i32 %78, 1
  store i32 %79, ptr %11, align 4, !tbaa !9
  br label %59, !llvm.loop !14

80:                                               ; preds = %63
  %81 = load ptr, ptr %7, align 8
  call void @llvm.stackrestore.p0(ptr %81)
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #1

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #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 }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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}
!14 = distinct !{!14, !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 = zext i32 %4 to i64
  %6 = alloca i32, i64 %5, align 16
  %7 = icmp sgt i32 %4, 0
  br i1 %7, label %.lr.ph.preheader, label %.preheader30

.lr.ph.preheader:                                 ; preds = %3
  %8 = shl nuw nsw i64 %5, 2
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 16 %6, ptr align 4 %0, i64 %8, i1 false), !tbaa !5
  br label %.preheader30

.preheader30:                                     ; preds = %.lr.ph.preheader, %3
  %9 = icmp sgt i32 %1, -1
  br i1 %9, label %.lr.ph33.preheader, label %.preheader

.lr.ph33.preheader:                               ; preds = %.preheader30
  %10 = sext i32 %4 to i64
  %11 = zext nneg i32 %1 to i64
  %12 = add nsw i64 %10, 1
  %smax = tail call i64 @llvm.smax.i64(i64 %12, i64 %11)
  %13 = sub i64 %smax, %10
  %min.iters.check = icmp ult i64 %13, 8
  %14 = mul nsw i64 %10, -4
  %diff.check = icmp ult i64 %14, 32
  %or.cond = select i1 %min.iters.check, i1 true, i1 %diff.check
  br i1 %or.cond, label %.lr.ph33.preheader55, label %vector.ph

.lr.ph33.preheader55:                             ; preds = %middle.block, %.lr.ph33.preheader
  %indvars.iv.ph = phi i64 [ %10, %.lr.ph33.preheader ], [ %ind.end, %middle.block ]
  br label %.lr.ph33

vector.ph:                                        ; preds = %.lr.ph33.preheader
  %n.vec = and i64 %13, -8
  %ind.end = add i64 %n.vec, %10
  %invariant.gep56 = getelementptr i32, ptr %0, i64 %10
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %gep57 = getelementptr i32, ptr %invariant.gep56, i64 %index
  %15 = getelementptr inbounds i8, ptr %gep57, i64 16
  %wide.load = load <4 x i32>, ptr %gep57, align 4, !tbaa !5
  %wide.load41 = load <4 x i32>, ptr %15, align 4, !tbaa !5
  %16 = getelementptr inbounds i32, ptr %0, i64 %index
  %17 = getelementptr inbounds i8, ptr %16, i64 16
  store <4 x i32> %wide.load, ptr %16, align 4, !tbaa !5
  store <4 x i32> %wide.load41, ptr %17, align 4, !tbaa !5
  %index.next = add nuw i64 %index, 8
  %18 = icmp eq i64 %index.next, %n.vec
  br i1 %18, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %13, %n.vec
  br i1 %cmp.n, label %.preheader, label %.lr.ph33.preheader55

.preheader:                                       ; preds = %.lr.ph33, %middle.block, %.preheader30
  br i1 %7, label %.lr.ph35, label %._crit_edge

.lr.ph35:                                         ; preds = %.preheader
  %19 = sub i32 %1, %4
  %20 = sext i32 %19 to i64
  %invariant.gep = getelementptr i32, ptr %0, i64 %20
  %min.iters.check44 = icmp ult i32 %4, 8
  br i1 %min.iters.check44, label %scalar.ph43.preheader, label %vector.ph45

scalar.ph43.preheader:                            ; preds = %middle.block42, %.lr.ph35
  %indvars.iv38.ph = phi i64 [ 0, %.lr.ph35 ], [ %n.vec47, %middle.block42 ]
  br label %scalar.ph43

vector.ph45:                                      ; preds = %.lr.ph35
  %n.vec47 = and i64 %5, 2147483640
  br label %vector.body49

vector.body49:                                    ; preds = %vector.body49, %vector.ph45
  %index50 = phi i64 [ 0, %vector.ph45 ], [ %index.next53, %vector.body49 ]
  %21 = getelementptr inbounds i32, ptr %6, i64 %index50
  %22 = getelementptr inbounds i8, ptr %21, i64 16
  %wide.load51 = load <4 x i32>, ptr %21, align 16, !tbaa !5
  %wide.load52 = load <4 x i32>, ptr %22, align 16, !tbaa !5
  %23 = getelementptr i32, ptr %invariant.gep, i64 %index50
  %24 = getelementptr i8, ptr %23, i64 16
  store <4 x i32> %wide.load51, ptr %23, align 4, !tbaa !5
  store <4 x i32> %wide.load52, ptr %24, align 4, !tbaa !5
  %index.next53 = add nuw i64 %index50, 8
  %25 = icmp eq i64 %index.next53, %n.vec47
  br i1 %25, label %middle.block42, label %vector.body49, !llvm.loop !13

middle.block42:                                   ; preds = %vector.body49
  %cmp.n54 = icmp eq i64 %n.vec47, %5
  br i1 %cmp.n54, label %._crit_edge, label %scalar.ph43.preheader

.lr.ph33:                                         ; preds = %.lr.ph33.preheader55, %.lr.ph33
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph33 ], [ %indvars.iv.ph, %.lr.ph33.preheader55 ]
  %26 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = sub nsw i64 %indvars.iv, %10
  %29 = getelementptr inbounds i32, ptr %0, i64 %28
  store i32 %27, ptr %29, align 4, !tbaa !5
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %30 = icmp slt i64 %indvars.iv.next, %11
  br i1 %30, label %.lr.ph33, label %.preheader, !llvm.loop !14

._crit_edge:                                      ; preds = %scalar.ph43, %middle.block42, %.preheader
  ret void

scalar.ph43:                                      ; preds = %scalar.ph43.preheader, %scalar.ph43
  %indvars.iv38 = phi i64 [ %indvars.iv.next39, %scalar.ph43 ], [ %indvars.iv38.ph, %scalar.ph43.preheader ]
  %31 = getelementptr inbounds i32, ptr %6, i64 %indvars.iv38
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %gep = getelementptr i32, ptr %invariant.gep, i64 %indvars.iv38
  store i32 %32, ptr %gep, align 4, !tbaa !5
  %indvars.iv.next39 = add nuw nsw i64 %indvars.iv38, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next39, %5
  br i1 %exitcond.not, label %._crit_edge, label %scalar.ph43, !llvm.loop !15
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smax.i64(i64, i64) #2

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) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10, !11, !12}
!14 = distinct !{!14, !10, !11}
!15 = distinct !{!15, !10, !12, !11}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopIdiomRecognizePass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass