; LLVM IR from Innocentsax/sorting_algorithms :: 0-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: nounwind uwtable
declare dso_local void @swap_ints(ptr noundef, ptr noundef) #0

; 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

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

12:                                               ; preds = %2
  %13 = load i64, ptr %4, align 8, !tbaa !9
  %14 = icmp ult i64 %13, 2
  br i1 %14, label %15, label %16

15:                                               ; preds = %12, %2
  store i32 1, ptr %8, align 4
  br label %55

16:                                               ; preds = %12
  br label %17

17:                                               ; preds = %51, %16
  %18 = load i32, ptr %7, align 4, !tbaa !11
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %20, label %54

20:                                               ; preds = %17
  store i32 1, ptr %7, align 4, !tbaa !11
  store i64 0, ptr %5, align 8, !tbaa !9
  br label %21

21:                                               ; preds = %48, %20
  %22 = load i64, ptr %5, align 8, !tbaa !9
  %23 = load i64, ptr %6, align 8, !tbaa !9
  %24 = sub i64 %23, 1
  %25 = icmp ult i64 %22, %24
  br i1 %25, label %26, label %51

26:                                               ; preds = %21
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = load i64, ptr %5, align 8, !tbaa !9
  %29 = getelementptr inbounds i32, ptr %27, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !11
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = load i64, ptr %5, align 8, !tbaa !9
  %33 = add i64 %32, 1
  %34 = getelementptr inbounds i32, ptr %31, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !11
  %36 = icmp sgt i32 %30, %35
  br i1 %36, label %37, label %47

37:                                               ; preds = %26
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = load i64, ptr %5, align 8, !tbaa !9
  %40 = getelementptr inbounds i32, ptr %38, i64 %39
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = load i64, ptr %5, align 8, !tbaa !9
  %43 = getelementptr inbounds i32, ptr %41, i64 %42
  %44 = getelementptr inbounds i32, ptr %43, i64 1
  call void @swap_ints(ptr noundef %40, ptr noundef %44)
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = load i64, ptr %4, align 8, !tbaa !9
  call void @print_array(ptr noundef %45, i64 noundef %46)
  store i32 0, ptr %7, align 4, !tbaa !11
  br label %47

47:                                               ; preds = %37, %26
  br label %48

48:                                               ; preds = %47
  %49 = load i64, ptr %5, align 8, !tbaa !9
  %50 = add i64 %49, 1
  store i64 %50, ptr %5, align 8, !tbaa !9
  br label %21, !llvm.loop !13

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

54:                                               ; preds = %17
  store i32 0, ptr %8, align 4
  br label %55

55:                                               ; preds = %54, %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  %56 = load i32, ptr %8, align 4
  switch i32 %56, label %58 [
    i32 0, label %57
    i32 1, label %57
  ]

57:                                               ; preds = %55, %55
  ret void

58:                                               ; preds = %55
  unreachable
}

declare void @print_array(ptr noundef, i64 noundef) #2

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 = { "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 #3 = { 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
declare dso_local void @swap_ints(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @bubble_sort(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  %4 = icmp ult i64 %1, 2
  %or.cond = or i1 %3, %4
  br i1 %or.cond, label %.loopexit22, label %.preheader

.loopexit:                                        ; preds = %15
  %5 = icmp eq i32 %.2, 0
  br i1 %5, label %.preheader, label %.loopexit22, !llvm.loop !5

.preheader:                                       ; preds = %2, %.loopexit
  %.01826 = phi i64 [ %6, %.loopexit ], [ %1, %2 ]
  %6 = add i64 %.01826, -1
  %.not = icmp eq i64 %6, 0
  br i1 %.not, label %.loopexit22, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %15
  %.124 = phi i32 [ %.2, %15 ], [ 1, %.preheader ]
  %.01923 = phi i64 [ %9, %15 ], [ 0, %.preheader ]
  %7 = getelementptr inbounds i32, ptr %0, i64 %.01923
  %8 = load i32, ptr %7, align 4, !tbaa !7
  %9 = add nuw i64 %.01923, 1
  %10 = getelementptr inbounds i32, ptr %0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !7
  %12 = icmp sgt i32 %8, %11
  br i1 %12, label %13, label %15

13:                                               ; preds = %.lr.ph
  %14 = getelementptr inbounds i8, ptr %7, i64 4
  tail call void @swap_ints(ptr noundef nonnull %7, ptr noundef nonnull %14)
  tail call void @print_array(ptr noundef nonnull %0, i64 noundef %1) #2
  br label %15

15:                                               ; preds = %.lr.ph, %13
  %.2 = phi i32 [ 0, %13 ], [ %.124, %.lr.ph ]
  %16 = icmp ult i64 %9, %6
  br i1 %16, label %.lr.ph, label %.loopexit, !llvm.loop !11

.loopexit22:                                      ; preds = %.loopexit, %.preheader, %2
  ret void
}

declare void @print_array(ptr noundef, i64 noundef) local_unnamed_addr #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 = { "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 = 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,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass