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

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

@c = external dso_local global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z9shellSortPii(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
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = 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
  %10 = load i32, ptr %4, align 4, !tbaa !9
  %11 = sdiv i32 %10, 2
  store i32 %11, ptr %5, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %76, %2
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = icmp sgt i32 %13, 0
  br i1 %14, label %16, label %15

15:                                               ; preds = %12
  store i32 2, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %81

16:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %17 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %17, ptr %7, align 4, !tbaa !9
  br label %18

18:                                               ; preds = %70, %16
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = load i32, ptr %4, align 4, !tbaa !9
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %23, label %22

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

23:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = load i32, ptr %7, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i32, ptr %24, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !9
  store i32 %28, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %29 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %29, ptr %9, align 4, !tbaa !9
  br label %30

30:                                               ; preds = %58, %23
  %31 = load i32, ptr %9, align 4, !tbaa !9
  %32 = load i32, ptr %5, align 4, !tbaa !9
  %33 = icmp sge i32 %31, %32
  br i1 %33, label %34, label %44

34:                                               ; preds = %30
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = load i32, ptr %9, align 4, !tbaa !9
  %37 = load i32, ptr %5, align 4, !tbaa !9
  %38 = sub nsw i32 %36, %37
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds i32, ptr %35, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !9
  %42 = load i32, ptr %8, align 4, !tbaa !9
  %43 = icmp sgt i32 %41, %42
  br label %44

44:                                               ; preds = %34, %30
  %45 = phi i1 [ false, %30 ], [ %43, %34 ]
  br i1 %45, label %46, label %64

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

58:                                               ; preds = %46
  %59 = load i32, ptr %5, align 4, !tbaa !9
  %60 = load i32, ptr %9, align 4, !tbaa !9
  %61 = sub nsw i32 %60, %59
  store i32 %61, ptr %9, align 4, !tbaa !9
  %62 = load i32, ptr @c, align 4, !tbaa !9
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr @c, align 4, !tbaa !9
  br label %30, !llvm.loop !11

64:                                               ; preds = %44
  %65 = load i32, ptr %8, align 4, !tbaa !9
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = load i32, ptr %9, align 4, !tbaa !9
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds i32, ptr %66, i64 %68
  store i32 %65, ptr %69, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %70

70:                                               ; preds = %64
  %71 = load i32, ptr %7, align 4, !tbaa !9
  %72 = add nsw i32 %71, 1
  store i32 %72, ptr %7, align 4, !tbaa !9
  %73 = load i32, ptr @c, align 4, !tbaa !9
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr @c, align 4, !tbaa !9
  br label %18, !llvm.loop !13

75:                                               ; preds = %22
  br label %76

76:                                               ; preds = %75
  %77 = load i32, ptr %5, align 4, !tbaa !9
  %78 = sdiv i32 %77, 2
  store i32 %78, ptr %5, align 4, !tbaa !9
  %79 = load i32, ptr @c, align 4, !tbaa !9
  %80 = add nsw i32 %79, 1
  store i32 %80, ptr @c, align 4, !tbaa !9
  br label %12, !llvm.loop !14

81:                                               ; preds = %15
  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}
!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"

@c = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @_Z9shellSortPii(ptr nocapture noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp sgt i32 %1, 1
  br i1 %3, label %.preheader, label %._crit_edge35

.preheader:                                       ; preds = %2, %._crit_edge
  %.02534.in = phi i32 [ %.02534, %._crit_edge ], [ %1, %2 ]
  %.02534 = lshr i32 %.02534.in, 1
  %4 = icmp slt i32 %.02534, %1
  br i1 %4, label %.lr.ph33.preheader, label %.preheader.._crit_edge_crit_edge

.preheader.._crit_edge_crit_edge:                 ; preds = %.preheader
  %.pre = load i32, ptr @c, align 4, !tbaa !5
  br label %._crit_edge

.lr.ph33.preheader:                               ; preds = %.preheader
  %5 = zext nneg i32 %.02534 to i64
  br label %.lr.ph33

._crit_edge35:                                    ; preds = %._crit_edge, %2
  ret void

._crit_edge:                                      ; preds = %.critedge, %.preheader.._crit_edge_crit_edge
  %6 = phi i32 [ %.pre, %.preheader.._crit_edge_crit_edge ], [ %24, %.critedge ]
  %7 = add nsw i32 %6, 1
  store i32 %7, ptr @c, align 4, !tbaa !5
  %.not36 = icmp ult i32 %.02534.in, 4
  br i1 %.not36, label %._crit_edge35, label %.preheader, !llvm.loop !9

.lr.ph33:                                         ; preds = %.lr.ph33.preheader, %.critedge
  %indvars.iv = phi i64 [ %5, %.lr.ph33.preheader ], [ %indvars.iv.next, %.critedge ]
  %8 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = trunc nuw i64 %indvars.iv to i32
  %.not28 = icmp sgt i32 %.02534, %10
  br i1 %.not28, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph33, %16
  %.029 = phi i32 [ %11, %16 ], [ %10, %.lr.ph33 ]
  %11 = sub nsw i32 %.029, %.02534
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds i32, ptr %0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = icmp sgt i32 %14, %9
  br i1 %15, label %16, label %.critedge

16:                                               ; preds = %.lr.ph
  %17 = zext nneg i32 %.029 to i64
  %18 = getelementptr inbounds i32, ptr %0, i64 %17
  store i32 %14, ptr %18, align 4, !tbaa !5
  %19 = load i32, ptr @c, align 4, !tbaa !5
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr @c, align 4, !tbaa !5
  %.not = icmp slt i32 %11, %.02534
  br i1 %.not, label %.critedge, label %.lr.ph, !llvm.loop !11

.critedge:                                        ; preds = %.lr.ph, %16, %.lr.ph33
  %.0.lcssa = phi i32 [ %10, %.lr.ph33 ], [ %11, %16 ], [ %.029, %.lr.ph ]
  %21 = sext i32 %.0.lcssa to i64
  %22 = getelementptr inbounds i32, ptr %0, i64 %21
  store i32 %9, ptr %22, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %23 = load i32, ptr @c, align 4, !tbaa !5
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr @c, align 4, !tbaa !5
  %25 = trunc nuw i64 %indvars.iv.next to i32
  %26 = icmp slt i32 %25, %1
  br i1 %26, label %.lr.ph33, label %._crit_edge, !llvm.loop !12
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


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