; LLVM IR from sbshah97/DS-Algo-Handbook :: Algorithms/Sort_Algorithms/Shell_Sort (shellSort)

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

@sz = external dso_local global i32, align 4
@ar = external dso_local global [100 x i32], align 16

; Function Attrs: nounwind uwtable
define dso_local void @shellSort() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 1, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %6 = load i32, ptr @sz, align 4, !tbaa !5
  store i32 %6, ptr %5, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %12, %0
  %8 = load i32, ptr %4, align 4, !tbaa !5
  %9 = load i32, ptr %5, align 4, !tbaa !5
  %10 = sdiv i32 %9, 3
  %11 = icmp sle i32 %8, %10
  br i1 %11, label %12, label %16

12:                                               ; preds = %7
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = mul nsw i32 %13, 3
  %15 = add nsw i32 %14, 1
  store i32 %15, ptr %4, align 4, !tbaa !5
  br label %7, !llvm.loop !9

16:                                               ; preds = %7
  br label %17

17:                                               ; preds = %69, %16
  %18 = load i32, ptr %4, align 4, !tbaa !5
  %19 = icmp sgt i32 %18, 0
  br i1 %19, label %20, label %73

20:                                               ; preds = %17
  %21 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %21, ptr %2, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %66, %20
  %23 = load i32, ptr %2, align 4, !tbaa !5
  %24 = load i32, ptr %5, align 4, !tbaa !5
  %25 = icmp slt i32 %23, %24
  br i1 %25, label %26, label %69

26:                                               ; preds = %22
  %27 = load i32, ptr %2, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [100 x i32], ptr @ar, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !5
  store i32 %30, ptr %3, align 4, !tbaa !5
  %31 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %31, ptr %1, align 4, !tbaa !5
  br label %32

32:                                               ; preds = %48, %26
  %33 = load i32, ptr %1, align 4, !tbaa !5
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = sub nsw i32 %34, 1
  %36 = icmp sgt i32 %33, %35
  br i1 %36, label %37, label %46

37:                                               ; preds = %32
  %38 = load i32, ptr %1, align 4, !tbaa !5
  %39 = load i32, ptr %4, align 4, !tbaa !5
  %40 = sub nsw i32 %38, %39
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [100 x i32], ptr @ar, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = load i32, ptr %3, align 4, !tbaa !5
  %45 = icmp sge i32 %43, %44
  br label %46

46:                                               ; preds = %37, %32
  %47 = phi i1 [ false, %32 ], [ %45, %37 ]
  br i1 %47, label %48, label %61

48:                                               ; preds = %46
  %49 = load i32, ptr %1, align 4, !tbaa !5
  %50 = load i32, ptr %4, align 4, !tbaa !5
  %51 = sub nsw i32 %49, %50
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [100 x i32], ptr @ar, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !5
  %55 = load i32, ptr %1, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [100 x i32], ptr @ar, i64 0, i64 %56
  store i32 %54, ptr %57, align 4, !tbaa !5
  %58 = load i32, ptr %4, align 4, !tbaa !5
  %59 = load i32, ptr %1, align 4, !tbaa !5
  %60 = sub nsw i32 %59, %58
  store i32 %60, ptr %1, align 4, !tbaa !5
  br label %32, !llvm.loop !11

61:                                               ; preds = %46
  %62 = load i32, ptr %3, align 4, !tbaa !5
  %63 = load i32, ptr %1, align 4, !tbaa !5
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [100 x i32], ptr @ar, i64 0, i64 %64
  store i32 %62, ptr %65, align 4, !tbaa !5
  br label %66

66:                                               ; preds = %61
  %67 = load i32, ptr %2, align 4, !tbaa !5
  %68 = add nsw i32 %67, 1
  store i32 %68, ptr %2, align 4, !tbaa !5
  br label %22, !llvm.loop !12

69:                                               ; preds = %22
  %70 = load i32, ptr %4, align 4, !tbaa !5
  %71 = sub nsw i32 %70, 1
  %72 = sdiv i32 %71, 3
  store i32 %72, ptr %4, align 4, !tbaa !5
  br label %17, !llvm.loop !13

73:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !10}


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

@sz = external dso_local local_unnamed_addr global i32, align 4
@ar = external dso_local local_unnamed_addr global [100 x i32], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @shellSort() local_unnamed_addr #0 {
  %1 = load i32, ptr @sz, align 4, !tbaa !5
  %2 = sdiv i32 %1, 3
  %.not29 = icmp slt i32 %1, 3
  br i1 %.not29, label %.preheader.preheader, label %.lr.ph

.preheader28:                                     ; preds = %.lr.ph
  %3 = icmp sgt i32 %.02330, -1
  br i1 %3, label %.preheader.preheader, label %._crit_edge40

.preheader.preheader:                             ; preds = %0, %.preheader28
  %.139.ph = phi i32 [ 1, %0 ], [ %5, %.preheader28 ]
  br label %.preheader

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.02330 = phi i32 [ %5, %.lr.ph ], [ 1, %0 ]
  %4 = mul nsw i32 %.02330, 3
  %5 = add nsw i32 %4, 1
  %.not.not = icmp slt i32 %4, %2
  br i1 %.not.not, label %.lr.ph, label %.preheader28, !llvm.loop !9

.preheader:                                       ; preds = %.preheader.preheader, %._crit_edge
  %.139 = phi i32 [ %23, %._crit_edge ], [ %.139.ph, %.preheader.preheader ]
  %6 = icmp slt i32 %.139, %1
  br i1 %6, label %.lr.ph38.preheader, label %._crit_edge

.lr.ph38.preheader:                               ; preds = %.preheader
  %7 = zext nneg i32 %.139 to i64
  br label %.lr.ph38

.lr.ph38:                                         ; preds = %.lr.ph38.preheader, %.critedge
  %indvars.iv = phi i64 [ %7, %.lr.ph38.preheader ], [ %indvars.iv.next, %.critedge ]
  %8 = getelementptr inbounds [100 x i32], ptr @ar, i64 0, i64 %indvars.iv
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %.not2631 = icmp ult i64 %indvars.iv, %7
  %10 = trunc nuw i64 %indvars.iv to i32
  br i1 %.not2631, label %.critedge, label %.lr.ph33

.lr.ph33:                                         ; preds = %.lr.ph38, %15
  %.032 = phi i32 [ %11, %15 ], [ %10, %.lr.ph38 ]
  %11 = sub nsw i32 %.032, %.139
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [100 x i32], ptr @ar, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %.not27 = icmp slt i32 %14, %9
  br i1 %.not27, label %.critedge, label %15

15:                                               ; preds = %.lr.ph33
  %16 = zext nneg i32 %.032 to i64
  %17 = getelementptr inbounds [100 x i32], ptr @ar, i64 0, i64 %16
  store i32 %14, ptr %17, align 4, !tbaa !5
  %.not26 = icmp slt i32 %11, %.139
  br i1 %.not26, label %.critedge, label %.lr.ph33, !llvm.loop !11

.critedge:                                        ; preds = %.lr.ph33, %15, %.lr.ph38
  %.0.lcssa = phi i32 [ %10, %.lr.ph38 ], [ %11, %15 ], [ %.032, %.lr.ph33 ]
  %18 = sext i32 %.0.lcssa to i64
  %19 = getelementptr inbounds [100 x i32], ptr @ar, i64 0, i64 %18
  store i32 %9, ptr %19, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %20 = trunc nuw i64 %indvars.iv.next to i32
  %21 = icmp sgt i32 %1, %20
  br i1 %21, label %.lr.ph38, label %._crit_edge, !llvm.loop !12

._crit_edge:                                      ; preds = %.critedge, %.preheader
  %22 = add nsw i32 %.139, -1
  %23 = udiv i32 %22, 3
  %.not = icmp ult i32 %22, 3
  br i1 %.not, label %._crit_edge40, label %.preheader, !llvm.loop !13

._crit_edge40:                                    ; preds = %._crit_edge, %.preheader28
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, argmem: none, 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/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !10}


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