; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 06. Sorting/Insertion_sort.ll (ZN9insertion4sortEv)

; 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.insertion = type { [20 x i32], i32, i32, i32 }

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

6:                                                ; preds = %62, %1
  %7 = getelementptr inbounds %class.insertion, ptr %4, i32 0, i32 2
  %8 = load i32, ptr %7, align 4, !tbaa !9
  %9 = getelementptr inbounds %class.insertion, ptr %4, i32 0, i32 1
  %10 = load i32, ptr %9, align 4, !tbaa !12
  %11 = icmp slt i32 %8, %10
  br i1 %11, label %12, label %66

12:                                               ; preds = %6
  %13 = getelementptr inbounds %class.insertion, ptr %4, i32 0, i32 0
  %14 = getelementptr inbounds %class.insertion, ptr %4, i32 0, i32 2
  %15 = load i32, ptr %14, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [20 x i32], ptr %13, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !13
  store i32 %18, ptr %3, align 4, !tbaa !13
  %19 = getelementptr inbounds %class.insertion, ptr %4, i32 0, i32 2
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = sub nsw i32 %20, 1
  %22 = getelementptr inbounds %class.insertion, ptr %4, i32 0, i32 3
  store i32 %21, ptr %22, align 4, !tbaa !14
  br label %23

23:                                               ; preds = %38, %12
  %24 = load i32, ptr %3, align 4, !tbaa !13
  %25 = getelementptr inbounds %class.insertion, ptr %4, i32 0, i32 0
  %26 = getelementptr inbounds %class.insertion, ptr %4, i32 0, i32 3
  %27 = load i32, ptr %26, align 4, !tbaa !14
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [20 x i32], ptr %25, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !13
  %31 = icmp slt i32 %24, %30
  br i1 %31, label %32, label %36

32:                                               ; preds = %23
  %33 = getelementptr inbounds %class.insertion, ptr %4, i32 0, i32 3
  %34 = load i32, ptr %33, align 4, !tbaa !14
  %35 = icmp sge i32 %34, 0
  br label %36

36:                                               ; preds = %32, %23
  %37 = phi i1 [ false, %23 ], [ %35, %32 ]
  br i1 %37, label %38, label %54

38:                                               ; preds = %36
  %39 = getelementptr inbounds %class.insertion, ptr %4, i32 0, i32 0
  %40 = getelementptr inbounds %class.insertion, ptr %4, i32 0, i32 3
  %41 = load i32, ptr %40, align 4, !tbaa !14
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [20 x i32], ptr %39, i64 0, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !13
  %45 = getelementptr inbounds %class.insertion, ptr %4, i32 0, i32 0
  %46 = getelementptr inbounds %class.insertion, ptr %4, i32 0, i32 3
  %47 = load i32, ptr %46, align 4, !tbaa !14
  %48 = add nsw i32 %47, 1
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [20 x i32], ptr %45, i64 0, i64 %49
  store i32 %44, ptr %50, align 4, !tbaa !13
  %51 = getelementptr inbounds %class.insertion, ptr %4, i32 0, i32 3
  %52 = load i32, ptr %51, align 4, !tbaa !14
  %53 = add nsw i32 %52, -1
  store i32 %53, ptr %51, align 4, !tbaa !14
  br label %23, !llvm.loop !15

54:                                               ; preds = %36
  %55 = load i32, ptr %3, align 4, !tbaa !13
  %56 = getelementptr inbounds %class.insertion, ptr %4, i32 0, i32 0
  %57 = getelementptr inbounds %class.insertion, ptr %4, i32 0, i32 3
  %58 = load i32, ptr %57, align 4, !tbaa !14
  %59 = add nsw i32 %58, 1
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [20 x i32], ptr %56, i64 0, i64 %60
  store i32 %55, ptr %61, align 4, !tbaa !13
  br label %62

62:                                               ; preds = %54
  %63 = getelementptr inbounds %class.insertion, ptr %4, i32 0, i32 2
  %64 = load i32, ptr %63, align 4, !tbaa !9
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %63, align 4, !tbaa !9
  br label %6, !llvm.loop !17

66:                                               ; preds = %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  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, !11, i64 84}
!10 = !{!"_ZTS9insertion", !7, i64 0, !11, i64 80, !11, i64 84, !11, i64 88}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 80}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !11, i64 88}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


; 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 @_ZN9insertion4sortEv(ptr nocapture noundef nonnull align 4 dereferenceable(92) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 84
  %3 = getelementptr inbounds i8, ptr %0, i64 80
  store i32 1, ptr %2, align 4, !tbaa !5
  %4 = load i32, ptr %3, align 4, !tbaa !10
  %5 = icmp sgt i32 %4, 1
  br i1 %5, label %.lr.ph9, label %._crit_edge

.lr.ph9:                                          ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 88
  br label %7

7:                                                ; preds = %.lr.ph9, %.critedge
  %storemerge8 = phi i32 [ 1, %.lr.ph9 ], [ %27, %.critedge ]
  %8 = sext i32 %storemerge8 to i64
  %9 = getelementptr inbounds [20 x i32], ptr %0, i64 0, i64 %8
  %10 = load i32, ptr %9, align 4, !tbaa !11
  %storemerge45 = add nsw i32 %storemerge8, -1
  store i32 %storemerge45, ptr %6, align 4, !tbaa !12
  %11 = sext i32 %storemerge45 to i64
  %12 = getelementptr inbounds [20 x i32], ptr %0, i64 0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !11
  %14 = icmp slt i32 %10, %13
  %15 = icmp sgt i32 %storemerge8, 0
  %or.cond6 = and i1 %15, %14
  br i1 %or.cond6, label %.lr.ph, label %.critedge

.lr.ph:                                           ; preds = %7, %.lr.ph
  %16 = phi i32 [ %22, %.lr.ph ], [ %13, %7 ]
  %storemerge4.in7 = phi i32 [ %19, %.lr.ph ], [ %storemerge8, %7 ]
  %17 = zext nneg i32 %storemerge4.in7 to i64
  %18 = getelementptr inbounds [20 x i32], ptr %0, i64 0, i64 %17
  store i32 %16, ptr %18, align 4, !tbaa !11
  %19 = load i32, ptr %6, align 4, !tbaa !12
  %storemerge4 = add nsw i32 %19, -1
  store i32 %storemerge4, ptr %6, align 4, !tbaa !12
  %20 = sext i32 %storemerge4 to i64
  %21 = getelementptr inbounds [20 x i32], ptr %0, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !11
  %23 = icmp slt i32 %10, %22
  %24 = icmp sgt i32 %19, 0
  %or.cond = and i1 %24, %23
  br i1 %or.cond, label %.lr.ph, label %.critedge.loopexit, !llvm.loop !13

.critedge.loopexit:                               ; preds = %.lr.ph
  %.pre = sext i32 %19 to i64
  br label %.critedge

.critedge:                                        ; preds = %.critedge.loopexit, %7
  %.pre-phi = phi i64 [ %.pre, %.critedge.loopexit ], [ %8, %7 ]
  %25 = getelementptr inbounds [20 x i32], ptr %0, i64 0, i64 %.pre-phi
  store i32 %10, ptr %25, align 4, !tbaa !11
  %26 = load i32, ptr %2, align 4, !tbaa !5
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %2, align 4, !tbaa !5
  %28 = load i32, ptr %3, align 4, !tbaa !10
  %29 = icmp slt i32 %27, %28
  br i1 %29, label %7, label %._crit_edge, !llvm.loop !15

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

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

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


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