; LLVM IR from next-step-for-interview-prep/Coding-Ninjas-DSA-course_code_and_notes_cpp :: Milestone-2=Intro_to_C++/questions and solutions/Lec-8=Arrays (arrange_nums-array)

; 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 @_Z7arrangePii(ptr noundef %0, i32 noundef %1) #0 {
  %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
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 1, ptr %5, align 4, !tbaa !9
  br label %7

7:                                                ; preds = %23, %2
  %8 = load i32, ptr %5, align 4, !tbaa !9
  %9 = mul nsw i32 2, %8
  %10 = sub nsw i32 %9, 1
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = icmp sle i32 %10, %11
  br i1 %12, label %14, label %13

13:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %26

14:                                               ; preds = %7
  %15 = load i32, ptr %5, align 4, !tbaa !9
  %16 = mul nsw i32 2, %15
  %17 = sub nsw i32 %16, 1
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = load i32, ptr %5, align 4, !tbaa !9
  %20 = sub nsw i32 %19, 1
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds i32, ptr %18, i64 %21
  store i32 %17, ptr %22, align 4, !tbaa !9
  br label %23

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

26:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 1, ptr %6, align 4, !tbaa !9
  br label %27

27:                                               ; preds = %42, %26
  %28 = load i32, ptr %6, align 4, !tbaa !9
  %29 = mul nsw i32 2, %28
  %30 = load i32, ptr %4, align 4, !tbaa !9
  %31 = icmp sle i32 %29, %30
  br i1 %31, label %33, label %32

32:                                               ; preds = %27
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %45

33:                                               ; preds = %27
  %34 = load i32, ptr %6, align 4, !tbaa !9
  %35 = mul nsw i32 2, %34
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = load i32, ptr %4, align 4, !tbaa !9
  %38 = load i32, ptr %6, align 4, !tbaa !9
  %39 = sub nsw i32 %37, %38
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i32, ptr %36, i64 %40
  store i32 %35, ptr %41, align 4, !tbaa !9
  br label %42

42:                                               ; preds = %33
  %43 = load i32, ptr %6, align 4, !tbaa !9
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %6, align 4, !tbaa !9
  br label %27, !llvm.loop !13

45:                                               ; preds = %32
  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: write) uwtable
define dso_local void @_Z7arrangePii(ptr nocapture noundef writeonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %invariant.gep = getelementptr i8, ptr %0, i64 -4
  %.not17 = icmp slt i32 %1, 1
  br i1 %.not17, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %3 = add nsw i32 %1, -1
  %4 = lshr i32 %3, 1
  %5 = add nuw nsw i32 %4, 2
  %wide.trip.count = zext nneg i32 %5 to i64
  %6 = lshr i32 %3, 1
  %narrow = add nuw i32 %6, 1
  %7 = zext i32 %narrow to i64
  %min.iters.check = icmp ult i32 %1, 15
  br i1 %min.iters.check, label %.lr.ph.preheader55, label %vector.ph

.lr.ph.preheader55:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 1, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.ph56 = phi i32 [ 1, %.lr.ph.preheader ], [ %ind.end29, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %7, 4294967288
  %ind.end = or disjoint i64 %n.vec, 1
  %.cast = trunc nuw i64 %n.vec to i32
  %8 = shl i32 %.cast, 1
  %ind.end29 = or disjoint i32 %8, 1
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.ind = phi <4 x i32> [ <i32 1, i32 3, i32 5, i32 7>, %vector.ph ], [ %vec.ind.next, %vector.body ]
  %step.add = add <4 x i32> %vec.ind, <i32 8, i32 8, i32 8, i32 8>
  %9 = getelementptr i32, ptr %0, i64 %index
  %10 = getelementptr i8, ptr %9, i64 16
  store <4 x i32> %vec.ind, ptr %9, align 4, !tbaa !5
  store <4 x i32> %step.add, ptr %10, align 4, !tbaa !5
  %index.next = add nuw i64 %index, 8
  %vec.ind.next = add <4 x i32> %vec.ind, <i32 16, i32 16, i32 16, i32 16>
  %11 = icmp eq i64 %index.next, %n.vec
  br i1 %11, label %middle.block, label %vector.body, !llvm.loop !9

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

.preheader:                                       ; preds = %.lr.ph, %middle.block
  %.not1619 = icmp eq i32 %1, 1
  br i1 %.not1619, label %._crit_edge, label %.lr.ph21.preheader

.lr.ph21.preheader:                               ; preds = %.preheader
  %12 = zext nneg i32 %1 to i64
  %13 = add nsw i32 %1, -2
  %14 = lshr i32 %13, 1
  %15 = add nuw nsw i32 %14, 2
  %wide.trip.count26 = zext nneg i32 %15 to i64
  %16 = lshr i32 %13, 1
  %narrow53 = add nuw i32 %16, 1
  %17 = zext i32 %narrow53 to i64
  %min.iters.check34 = icmp ult i32 %13, 14
  br i1 %min.iters.check34, label %.lr.ph21.preheader54, label %vector.ph35

.lr.ph21.preheader54:                             ; preds = %middle.block32, %.lr.ph21.preheader
  %indvars.iv23.ph = phi i64 [ 1, %.lr.ph21.preheader ], [ %ind.end38, %middle.block32 ]
  %.ph = phi i32 [ 2, %.lr.ph21.preheader ], [ %ind.end41, %middle.block32 ]
  br label %.lr.ph21

vector.ph35:                                      ; preds = %.lr.ph21.preheader
  %n.vec37 = and i64 %17, 4294967288
  %ind.end38 = or disjoint i64 %n.vec37, 1
  %.cast40 = trunc nuw i64 %n.vec37 to i32
  %18 = shl i32 %.cast40, 1
  %ind.end41 = or disjoint i32 %18, 2
  %invariant.gep57 = getelementptr i32, ptr %0, i64 %12
  br label %vector.body43

vector.body43:                                    ; preds = %vector.body43, %vector.ph35
  %index44 = phi i64 [ 0, %vector.ph35 ], [ %index.next51, %vector.body43 ]
  %vec.ind45 = phi <4 x i32> [ <i32 2, i32 4, i32 6, i32 8>, %vector.ph35 ], [ %vec.ind.next48, %vector.body43 ]
  %step.add46 = add <4 x i32> %vec.ind45, <i32 8, i32 8, i32 8, i32 8>
  %offset.idx49.neg = xor i64 %index44, -1
  %gep58 = getelementptr i32, ptr %invariant.gep57, i64 %offset.idx49.neg
  %19 = getelementptr inbounds i8, ptr %gep58, i64 -12
  %20 = getelementptr inbounds i8, ptr %gep58, i64 -28
  %reverse = shufflevector <4 x i32> %vec.ind45, <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
  store <4 x i32> %reverse, ptr %19, align 4, !tbaa !5
  %reverse50 = shufflevector <4 x i32> %step.add46, <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
  store <4 x i32> %reverse50, ptr %20, align 4, !tbaa !5
  %index.next51 = add nuw i64 %index44, 8
  %vec.ind.next48 = add <4 x i32> %vec.ind45, <i32 16, i32 16, i32 16, i32 16>
  %21 = icmp eq i64 %index.next51, %n.vec37
  br i1 %21, label %middle.block32, label %vector.body43, !llvm.loop !13

middle.block32:                                   ; preds = %vector.body43
  %cmp.n52 = icmp eq i64 %n.vec37, %17
  br i1 %cmp.n52, label %._crit_edge, label %.lr.ph21.preheader54

.lr.ph:                                           ; preds = %.lr.ph.preheader55, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader55 ]
  %22 = phi i32 [ %24, %.lr.ph ], [ %.ph56, %.lr.ph.preheader55 ]
  %gep = getelementptr i32, ptr %invariant.gep, i64 %indvars.iv
  store i32 %22, ptr %gep, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %indvars.iv.next.tr = trunc i64 %indvars.iv.next to i32
  %23 = shl i32 %indvars.iv.next.tr, 1
  %24 = add i32 %23, -1
  %exitcond = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond, label %.preheader, label %.lr.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph21, %middle.block32, %2, %.preheader
  ret void

.lr.ph21:                                         ; preds = %.lr.ph21.preheader54, %.lr.ph21
  %indvars.iv23 = phi i64 [ %indvars.iv.next24, %.lr.ph21 ], [ %indvars.iv23.ph, %.lr.ph21.preheader54 ]
  %25 = phi i32 [ %28, %.lr.ph21 ], [ %.ph, %.lr.ph21.preheader54 ]
  %26 = sub nsw i64 %12, %indvars.iv23
  %27 = getelementptr inbounds i32, ptr %0, i64 %26
  store i32 %25, ptr %27, align 4, !tbaa !5
  %indvars.iv.next24 = add nuw nsw i64 %indvars.iv23, 1
  %indvars.iv.next24.tr = trunc i64 %indvars.iv.next24 to i32
  %28 = shl i32 %indvars.iv.next24.tr, 1
  %exitcond27 = icmp eq i64 %indvars.iv.next24, %wide.trip.count26
  br i1 %exitcond27, label %._crit_edge, label %.lr.ph21, !llvm.loop !15
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(argmem: write) 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 = 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, !12, !11}
!15 = distinct !{!15, !10, !12, !11}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass