; LLVM IR from priyankchheda/algorithms :: _other_languages/c_language/sorts/bubble_sort (bubble_sort)

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

9:                                                ; preds = %50, %2
  %10 = load i64, ptr %6, align 8, !tbaa !9
  %11 = load i64, ptr %4, align 8, !tbaa !9
  %12 = sub i64 %11, 1
  %13 = icmp ult i64 %10, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %9
  store i32 2, ptr %7, align 4
  br label %53

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

16:                                               ; preds = %42, %15
  %17 = load i64, ptr %8, align 8, !tbaa !9
  %18 = load i64, ptr %4, align 8, !tbaa !9
  %19 = sub i64 %18, 1
  %20 = icmp ult i64 %17, %19
  br i1 %20, label %22, label %21

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

22:                                               ; preds = %16
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = load i64, ptr %8, align 8, !tbaa !9
  %25 = getelementptr inbounds i32, ptr %23, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !11
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = load i64, ptr %8, align 8, !tbaa !9
  %29 = add i64 %28, 1
  %30 = getelementptr inbounds i32, ptr %27, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !11
  %32 = icmp sgt i32 %26, %31
  br i1 %32, label %33, label %41

33:                                               ; preds = %22
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = load i64, ptr %8, align 8, !tbaa !9
  %36 = getelementptr inbounds i32, ptr %34, i64 %35
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = load i64, ptr %8, align 8, !tbaa !9
  %39 = add i64 %38, 1
  %40 = getelementptr inbounds i32, ptr %37, i64 %39
  call void @swap(ptr noundef %36, ptr noundef %40)
  store i32 1, ptr %5, align 4, !tbaa !11
  br label %41

41:                                               ; preds = %33, %22
  br label %42

42:                                               ; preds = %41
  %43 = load i64, ptr %8, align 8, !tbaa !9
  %44 = add i64 %43, 1
  store i64 %44, ptr %8, align 8, !tbaa !9
  br label %16, !llvm.loop !13

45:                                               ; preds = %21
  %46 = load i32, ptr %5, align 4, !tbaa !11
  %47 = icmp eq i32 %46, 0
  br i1 %47, label %48, label %49

48:                                               ; preds = %45
  store i32 2, ptr %7, align 4
  br label %53

49:                                               ; preds = %45
  br label %50

50:                                               ; preds = %49
  %51 = load i64, ptr %6, align 8, !tbaa !9
  %52 = add i64 %51, 1
  store i64 %52, ptr %6, align 8, !tbaa !9
  br label %9, !llvm.loop !15

53:                                               ; preds = %48, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %54

54:                                               ; preds = %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
declare hidden void @swap(ptr noundef, ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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: nounwind uwtable
define dso_local void @bubble_sort(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = add i64 %1, -1
  %.not = icmp eq i64 %3, 0
  br i1 %.not, label %._crit_edge, label %.preheader

.thread25:                                        ; preds = %.thread
  %.old = add nuw i64 %.01419, 1
  %exitcond21.not.old = icmp eq i64 %.old, %3
  br i1 %exitcond21.not.old, label %._crit_edge, label %.preheader.backedge

.preheader.backedge:                              ; preds = %.thread25, %5
  %.01419.be = phi i64 [ %.old, %.thread25 ], [ %6, %5 ]
  br label %.preheader, !llvm.loop !5

.preheader:                                       ; preds = %2, %.preheader.backedge
  %.01419 = phi i64 [ %.01419.be, %.preheader.backedge ], [ 0, %2 ]
  br label %.outer

.outer:                                           ; preds = %.thread, %.preheader
  %.018.ph = phi i64 [ %10, %.thread ], [ 0, %.preheader ]
  %4 = phi i1 [ false, %.thread ], [ true, %.preheader ]
  br label %7

5:                                                ; preds = %14
  %6 = add nuw i64 %.01419, 1
  %exitcond21.not = icmp eq i64 %6, %3
  %or.cond = select i1 %4, i1 true, i1 %exitcond21.not
  br i1 %or.cond, label %._crit_edge, label %.preheader.backedge

7:                                                ; preds = %.outer, %14
  %.018 = phi i64 [ %10, %14 ], [ %.018.ph, %.outer ]
  %8 = getelementptr inbounds i32, ptr %0, i64 %.018
  %9 = load i32, ptr %8, align 4, !tbaa !7
  %10 = add nuw i64 %.018, 1
  %11 = getelementptr inbounds i32, ptr %0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !7
  %13 = icmp sgt i32 %9, %12
  br i1 %13, label %.thread, label %14

14:                                               ; preds = %7
  %exitcond.not = icmp eq i64 %10, %3
  br i1 %exitcond.not, label %5, label %7, !llvm.loop !11

.thread:                                          ; preds = %7
  %15 = getelementptr inbounds i32, ptr %0, i64 %.018
  %16 = getelementptr inbounds i32, ptr %0, i64 %10
  tail call void @swap(ptr noundef nonnull %15, ptr noundef nonnull %16)
  %exitcond.not23 = icmp eq i64 %10, %3
  br i1 %exitcond.not23, label %.thread25, label %.outer, !llvm.loop !11

._crit_edge:                                      ; preds = %.thread25, %5, %2
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @swap(ptr noundef, ptr noundef) local_unnamed_addr #0

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

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !8, i64 0}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = distinct !{!11, !6}


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