; LLVM IR from codemistic/Data-Structures-and-Algorithms :: Arrays/18_Menu_Based_problem.ll (ZN5Array9RearrangeEv)

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

%class.Array = type { ptr, i32, i32 }

; 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
declare dso_local void @_ZN5Array4swapEPiS0_(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef, ptr noundef) #1 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN5Array9RearrangeEv(ptr noundef nonnull align 8 dereferenceable(16) %0) #1 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %5 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %3, align 4, !tbaa !9
  %6 = getelementptr inbounds %class.Array, ptr %5, i32 0, i32 2
  %7 = load i32, ptr %6, align 4, !tbaa !11
  %8 = sub nsw i32 %7, 1
  store i32 %8, ptr %4, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %52, %1
  %10 = load i32, ptr %3, align 4, !tbaa !9
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = icmp slt i32 %10, %11
  br i1 %12, label %13, label %53

13:                                               ; preds = %9
  br label %14

14:                                               ; preds = %22, %13
  %15 = getelementptr inbounds %class.Array, ptr %5, i32 0, i32 0
  %16 = load ptr, ptr %15, align 8, !tbaa !13
  %17 = load i32, ptr %3, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i32, ptr %16, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = icmp slt i32 %20, 0
  br i1 %21, label %22, label %25

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

25:                                               ; preds = %14
  br label %26

26:                                               ; preds = %34, %25
  %27 = getelementptr inbounds %class.Array, ptr %5, i32 0, i32 0
  %28 = load ptr, ptr %27, align 8, !tbaa !13
  %29 = load i32, ptr %4, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i32, ptr %28, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !9
  %33 = icmp sge i32 %32, 0
  br i1 %33, label %34, label %37

34:                                               ; preds = %26
  %35 = load i32, ptr %4, align 4, !tbaa !9
  %36 = add nsw i32 %35, -1
  store i32 %36, ptr %4, align 4, !tbaa !9
  br label %26, !llvm.loop !16

37:                                               ; preds = %26
  %38 = load i32, ptr %3, align 4, !tbaa !9
  %39 = load i32, ptr %4, align 4, !tbaa !9
  %40 = icmp slt i32 %38, %39
  br i1 %40, label %41, label %52

41:                                               ; preds = %37
  %42 = getelementptr inbounds %class.Array, ptr %5, i32 0, i32 0
  %43 = load ptr, ptr %42, align 8, !tbaa !13
  %44 = load i32, ptr %3, align 4, !tbaa !9
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds i32, ptr %43, i64 %45
  %47 = getelementptr inbounds %class.Array, ptr %5, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8, !tbaa !13
  %49 = load i32, ptr %4, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds i32, ptr %48, i64 %50
  call void @_ZN5Array4swapEPiS0_(ptr noundef nonnull align 8 dereferenceable(16) %5, ptr noundef %46, ptr noundef %51)
  br label %52

52:                                               ; preds = %41, %37
  br label %9, !llvm.loop !17

53:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret void
}

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 = !{!"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 = !{!12, !10, i64 12}
!12 = !{!"_ZTS5Array", !6, i64 0, !10, i64 8, !10, i64 12}
!13 = !{!12, !6, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = distinct !{!17, !15}


; 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 nounwind uwtable
declare dso_local void @_ZN5Array4swapEPiS0_(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef, ptr noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN5Array9RearrangeEv(ptr noundef nonnull align 8 dereferenceable(16) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 12
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 1
  br i1 %4, label %.preheader12.preheader, label %._crit_edge

.preheader12.preheader:                           ; preds = %1
  %5 = add nsw i32 %3, -1
  br label %.preheader12

.preheader12:                                     ; preds = %20, %.preheader12.preheader
  %.018 = phi i32 [ %21, %20 ], [ %5, %.preheader12.preheader ]
  %.01017 = phi i64 [ %indvars.iv, %20 ], [ 0, %.preheader12.preheader ]
  %6 = load ptr, ptr %0, align 8, !tbaa !11
  %sext = shl i64 %.01017, 32
  %7 = ashr exact i64 %sext, 32
  br label %8

8:                                                ; preds = %8, %.preheader12
  %indvars.iv = phi i64 [ %indvars.iv.next, %8 ], [ %7, %.preheader12 ]
  %9 = getelementptr inbounds i32, ptr %6, i64 %indvars.iv
  %10 = load i32, ptr %9, align 4, !tbaa !12
  %11 = icmp slt i32 %10, 0
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  br i1 %11, label %8, label %.preheader, !llvm.loop !13

.preheader:                                       ; preds = %8
  %12 = getelementptr inbounds i32, ptr %6, i64 %indvars.iv
  %13 = sext i32 %.018 to i64
  br label %14

14:                                               ; preds = %14, %.preheader
  %indvars.iv21 = phi i64 [ %indvars.iv.next22, %14 ], [ %13, %.preheader ]
  %15 = getelementptr inbounds i32, ptr %6, i64 %indvars.iv21
  %16 = load i32, ptr %15, align 4, !tbaa !12
  %17 = icmp sgt i32 %16, -1
  %indvars.iv.next22 = add nsw i64 %indvars.iv21, -1
  br i1 %17, label %14, label %18, !llvm.loop !15

18:                                               ; preds = %14
  %19 = icmp slt i64 %indvars.iv, %indvars.iv21
  br i1 %19, label %20, label %._crit_edge

20:                                               ; preds = %18
  %21 = trunc nsw i64 %indvars.iv21 to i32
  %22 = getelementptr inbounds i32, ptr %6, i64 %indvars.iv21
  tail call void @_ZN5Array4swapEPiS0_(ptr noundef nonnull align 8 dereferenceable(16) %0, ptr noundef nonnull %12, ptr noundef nonnull %22)
  br label %.preheader12

._crit_edge:                                      ; preds = %18, %1
  ret void
}

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

!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, !10, i64 12}
!6 = !{!"_ZTS5Array", !7, i64 0, !10, i64 8, !10, i64 12}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!10, !10, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass