; LLVM IR from h-ssiqueira/Sort_Algorithms :: src/exchange (optimizedBubbleSort)

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

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

10:                                               ; preds = %60, %2
  %11 = load ptr, ptr %8, align 8, !tbaa !5
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = load i32, ptr %4, align 4, !tbaa !9
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds i64, ptr %12, i64 %14
  %16 = icmp ult ptr %11, %15
  br i1 %16, label %17, label %63

17:                                               ; preds = %10
  store i8 0, ptr %5, align 1, !tbaa !11
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %18, ptr %7, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %52, %17
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = load i32, ptr %4, align 4, !tbaa !9
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds i64, ptr %21, i64 %23
  %25 = load ptr, ptr %8, align 8, !tbaa !5
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = ptrtoint ptr %25 to i64
  %28 = ptrtoint ptr %26 to i64
  %29 = sub i64 %27, %28
  %30 = sdiv exact i64 %29, 8
  %31 = sub i64 0, %30
  %32 = getelementptr inbounds i64, ptr %24, i64 %31
  %33 = icmp ult ptr %20, %32
  br i1 %33, label %34, label %55

34:                                               ; preds = %19
  %35 = load ptr, ptr %7, align 8, !tbaa !5
  %36 = getelementptr inbounds i64, ptr %35, i64 1
  %37 = load i64, ptr %36, align 8, !tbaa !13
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = load i64, ptr %38, align 8, !tbaa !13
  %40 = icmp slt i64 %37, %39
  br i1 %40, label %41, label %51

41:                                               ; preds = %34
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = load i64, ptr %42, align 8, !tbaa !13
  store i64 %43, ptr %6, align 8, !tbaa !13
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = getelementptr inbounds i64, ptr %44, i64 1
  %46 = load i64, ptr %45, align 8, !tbaa !13
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %46, ptr %47, align 8, !tbaa !13
  %48 = load i64, ptr %6, align 8, !tbaa !13
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = getelementptr inbounds i64, ptr %49, i64 1
  store i64 %48, ptr %50, align 8, !tbaa !13
  store i8 1, ptr %5, align 1, !tbaa !11
  br label %51

51:                                               ; preds = %41, %34
  br label %52

52:                                               ; preds = %51
  %53 = load ptr, ptr %7, align 8, !tbaa !5
  %54 = getelementptr inbounds i64, ptr %53, i32 1
  store ptr %54, ptr %7, align 8, !tbaa !5
  br label %19, !llvm.loop !15

55:                                               ; preds = %19
  %56 = load i8, ptr %5, align 1, !tbaa !11, !range !17, !noundef !18
  %57 = trunc i8 %56 to i1
  br i1 %57, label %59, label %58

58:                                               ; preds = %55
  br label %63

59:                                               ; preds = %55
  br label %60

60:                                               ; preds = %59
  %61 = load ptr, ptr %8, align 8, !tbaa !5
  %62 = getelementptr inbounds i64, ptr %61, i32 1
  store ptr %62, ptr %8, align 8, !tbaa !5
  br label %10, !llvm.loop !19

63:                                               ; preds = %58, %10
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{i8 0, i8 2}
!18 = !{}
!19 = distinct !{!19, !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: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @bubbleSortOptmized(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = sext i32 %1 to i64
  %4 = getelementptr inbounds i64, ptr %0, i64 %3
  %5 = icmp sgt i32 %1, 0
  br i1 %5, label %.preheader.lr.ph, label %._crit_edge27

.preheader.lr.ph:                                 ; preds = %2
  %6 = ptrtoint ptr %0 to i64
  br label %.preheader

.preheader:                                       ; preds = %._crit_edge, %.preheader.lr.ph
  %.026 = phi ptr [ %0, %.preheader.lr.ph ], [ %21, %._crit_edge ]
  %7 = ptrtoint ptr %.026 to i64
  %8 = sub i64 %7, %6
  %9 = ashr exact i64 %8, 3
  %10 = sub nsw i64 0, %9
  %11 = getelementptr inbounds i64, ptr %4, i64 %10
  %12 = icmp ugt ptr %11, %0
  br i1 %12, label %.lr.ph.preheader, label %._crit_edge27

.lr.ph.preheader:                                 ; preds = %.preheader
  %.pre = load i64, ptr %0, align 8, !tbaa !5
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %18
  %13 = phi i64 [ %19, %18 ], [ %.pre, %.lr.ph.preheader ]
  %.01925 = phi ptr [ %14, %18 ], [ %0, %.lr.ph.preheader ]
  %.02024 = phi i1 [ %.1, %18 ], [ false, %.lr.ph.preheader ]
  %14 = getelementptr inbounds i8, ptr %.01925, i64 8
  %15 = load i64, ptr %14, align 8, !tbaa !5
  %16 = icmp slt i64 %15, %13
  br i1 %16, label %17, label %18

17:                                               ; preds = %.lr.ph
  store i64 %15, ptr %.01925, align 8, !tbaa !5
  store i64 %13, ptr %14, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %.lr.ph, %17
  %19 = phi i64 [ %13, %17 ], [ %15, %.lr.ph ]
  %.1 = phi i1 [ true, %17 ], [ %.02024, %.lr.ph ]
  %20 = icmp ult ptr %14, %11
  br i1 %20, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %18
  %21 = getelementptr inbounds i8, ptr %.026, i64 8
  %22 = icmp ult ptr %21, %4
  %or.cond = select i1 %.1, i1 %22, i1 false
  br i1 %or.cond, label %.preheader, label %._crit_edge27, !llvm.loop !11

._crit_edge27:                                    ; preds = %.preheader, %._crit_edge, %2
  ret void
}

attributes #0 = { 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 = !{!"long", !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}


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