; LLVM IR from Dev-XYS/Algorithms :: Shell-Sort(Shell's-Gap-Sequence).ll (Z10Shell_sortv)

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

@n = external dso_local global i32, align 4
@d = external dso_local global [100000 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z10Shell_sortv() #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
  %6 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  %7 = load i32, ptr @n, align 4, !tbaa !5
  %8 = ashr i32 %7, 1
  store i32 %8, ptr %1, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %82, %0
  %10 = load i32, ptr %1, align 4, !tbaa !5
  %11 = icmp sge i32 %10, 1
  br i1 %11, label %13, label %12

12:                                               ; preds = %9
  store i32 2, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  br label %85

13:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %78, %13
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = load i32, ptr %1, align 4, !tbaa !5
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %19, label %18

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

19:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %20 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %20, ptr %4, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %73, %19
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = load i32, ptr @n, align 4, !tbaa !5
  %24 = icmp slt i32 %22, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %21
  store i32 8, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %77

26:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %27 = load i32, ptr %4, align 4, !tbaa !5
  %28 = load i32, ptr %1, align 4, !tbaa !5
  %29 = sub nsw i32 %27, %28
  store i32 %29, ptr %5, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %68, %26
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = icmp sge i32 %31, 0
  br i1 %32, label %33, label %45

33:                                               ; preds = %30
  %34 = load i32, ptr %5, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = load i32, ptr %5, align 4, !tbaa !5
  %39 = load i32, ptr %1, align 4, !tbaa !5
  %40 = add nsw i32 %38, %39
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = icmp sgt i32 %37, %43
  br label %45

45:                                               ; preds = %33, %30
  %46 = phi i1 [ false, %30 ], [ %44, %33 ]
  br i1 %46, label %48, label %47

47:                                               ; preds = %45
  store i32 11, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %72

48:                                               ; preds = %45
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %49 = load i32, ptr %5, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !5
  store i32 %52, ptr %6, align 4, !tbaa !5
  %53 = load i32, ptr %5, align 4, !tbaa !5
  %54 = load i32, ptr %1, align 4, !tbaa !5
  %55 = add nsw i32 %53, %54
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !5
  %59 = load i32, ptr %5, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %60
  store i32 %58, ptr %61, align 4, !tbaa !5
  %62 = load i32, ptr %6, align 4, !tbaa !5
  %63 = load i32, ptr %5, align 4, !tbaa !5
  %64 = load i32, ptr %1, align 4, !tbaa !5
  %65 = add nsw i32 %63, %64
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %66
  store i32 %62, ptr %67, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %68

68:                                               ; preds = %48
  %69 = load i32, ptr %1, align 4, !tbaa !5
  %70 = load i32, ptr %5, align 4, !tbaa !5
  %71 = sub nsw i32 %70, %69
  store i32 %71, ptr %5, align 4, !tbaa !5
  br label %30, !llvm.loop !9

72:                                               ; preds = %47
  br label %73

73:                                               ; preds = %72
  %74 = load i32, ptr %1, align 4, !tbaa !5
  %75 = load i32, ptr %4, align 4, !tbaa !5
  %76 = add nsw i32 %75, %74
  store i32 %76, ptr %4, align 4, !tbaa !5
  br label %21, !llvm.loop !11

77:                                               ; preds = %25
  br label %78

78:                                               ; preds = %77
  %79 = load i32, ptr %3, align 4, !tbaa !5
  %80 = add nsw i32 %79, 1
  store i32 %80, ptr %3, align 4, !tbaa !5
  br label %14, !llvm.loop !12

81:                                               ; preds = %18
  br label %82

82:                                               ; preds = %81
  %83 = load i32, ptr %1, align 4, !tbaa !5
  %84 = ashr i32 %83, 1
  store i32 %84, ptr %1, align 4, !tbaa !5
  br label %9, !llvm.loop !13

85:                                               ; preds = %12
  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 = !{!"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}
!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"

@n = external dso_local local_unnamed_addr global i32, align 4
@d = external dso_local local_unnamed_addr global [100000 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z10Shell_sortv() local_unnamed_addr #0 {
  %1 = load i32, ptr @n, align 4, !tbaa !5
  %2 = ashr i32 %1, 1
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.preheader30, label %._crit_edge38

.preheader30:                                     ; preds = %0, %5
  %.037 = phi i32 [ %6, %5 ], [ %2, %0 ]
  %umax = tail call i32 @llvm.umax.i32(i32 %.037, i32 1)
  br label %.preheader29

._crit_edge38:                                    ; preds = %5, %0
  ret void

.preheader29:                                     ; preds = %.preheader30, %._crit_edge
  %.02636 = phi i32 [ 0, %.preheader30 ], [ %8, %._crit_edge ]
  %4 = icmp slt i32 %.02636, %1
  br i1 %4, label %.preheader, label %._crit_edge

5:                                                ; preds = %._crit_edge
  %6 = lshr i32 %.037, 1
  %.not = icmp ult i32 %.037, 2
  br i1 %.not, label %._crit_edge38, label %.preheader30, !llvm.loop !9

.preheader:                                       ; preds = %.preheader29, %.critedge
  %.02535 = phi i32 [ %16, %.critedge ], [ %.02636, %.preheader29 ]
  %.02431 = sub nsw i32 %.02535, %.037
  %7 = icmp sgt i32 %.02431, -1
  br i1 %7, label %.lr.ph, label %.critedge

._crit_edge:                                      ; preds = %.critedge, %.preheader29
  %8 = add nuw nsw i32 %.02636, 1
  %exitcond.not = icmp eq i32 %8, %umax
  br i1 %exitcond.not, label %5, label %.preheader29, !llvm.loop !11

.lr.ph:                                           ; preds = %.preheader, %18
  %.02433 = phi i32 [ %.024, %18 ], [ %.02431, %.preheader ]
  %.025.pn32 = phi i32 [ %.02433, %18 ], [ %.02535, %.preheader ]
  %9 = zext nneg i32 %.02433 to i64
  %10 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = zext nneg i32 %.025.pn32 to i64
  %13 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = icmp sgt i32 %11, %14
  br i1 %15, label %18, label %.critedge

.critedge:                                        ; preds = %.lr.ph, %18, %.preheader
  %16 = add nuw nsw i32 %.02535, %.037
  %17 = icmp slt i32 %16, %1
  br i1 %17, label %.preheader, label %._crit_edge, !llvm.loop !12

18:                                               ; preds = %.lr.ph
  store i32 %14, ptr %10, align 4, !tbaa !5
  store i32 %11, ptr %13, align 4, !tbaa !5
  %.024 = sub nsw i32 %.02433, %.037
  %19 = icmp sgt i32 %.024, -1
  br i1 %19, label %.lr.ph, label %.critedge, !llvm.loop !13
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umax.i32(i32, i32) #1

attributes #0 = { mustprogress 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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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}
!13 = distinct !{!13, !10}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass