; LLVM IR from priyankchheda/algorithms :: _other_languages/c_language/sorts/generic (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
define dso_local void @bubble_sort(ptr noundef %0, i64 noundef %1, i64 noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca i64, align 8
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %15

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

20:                                               ; preds = %15
  store i32 2, ptr %11, align 4
  br label %61

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

22:                                               ; preds = %50, %21
  %23 = load i64, ptr %12, align 8, !tbaa !9
  %24 = load i64, ptr %6, align 8, !tbaa !9
  %25 = sub i64 %24, 1
  %26 = icmp ult i64 %23, %25
  br i1 %26, label %28, label %27

27:                                               ; preds = %22
  store i32 5, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  br label %53

28:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = load i64, ptr %12, align 8, !tbaa !9
  %31 = load i64, ptr %7, align 8, !tbaa !9
  %32 = mul i64 %30, %31
  %33 = getelementptr inbounds i8, ptr %29, i64 %32
  store ptr %33, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #2
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = load i64, ptr %12, align 8, !tbaa !9
  %36 = add i64 %35, 1
  %37 = load i64, ptr %7, align 8, !tbaa !9
  %38 = mul i64 %36, %37
  %39 = getelementptr inbounds i8, ptr %34, i64 %38
  store ptr %39, ptr %14, align 8, !tbaa !5
  %40 = load ptr, ptr %8, align 8, !tbaa !5
  %41 = load ptr, ptr %13, align 8, !tbaa !5
  %42 = load ptr, ptr %14, align 8, !tbaa !5
  %43 = call i32 %40(ptr noundef %41, ptr noundef %42)
  %44 = icmp sgt i32 %43, 0
  br i1 %44, label %45, label %49

45:                                               ; preds = %28
  %46 = load ptr, ptr %13, align 8, !tbaa !5
  %47 = load ptr, ptr %14, align 8, !tbaa !5
  %48 = load i64, ptr %7, align 8, !tbaa !9
  call void @swap(ptr noundef %46, ptr noundef %47, i64 noundef %48)
  store i32 1, ptr %9, align 4, !tbaa !11
  br label %49

49:                                               ; preds = %45, %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  br label %50

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

53:                                               ; preds = %27
  %54 = load i32, ptr %9, align 4, !tbaa !11
  %55 = icmp eq i32 %54, 0
  br i1 %55, label %56, label %57

56:                                               ; preds = %53
  store i32 2, ptr %11, align 4
  br label %61

57:                                               ; preds = %53
  br label %58

58:                                               ; preds = %57
  %59 = load i64, ptr %10, align 8, !tbaa !9
  %60 = add i64 %59, 1
  store i64 %60, ptr %10, align 8, !tbaa !9
  br label %15, !llvm.loop !15

61:                                               ; preds = %56, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  br label %62

62:                                               ; preds = %61
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #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: nounwind uwtable
declare hidden void @swap(ptr noundef, ptr noundef, i64 noundef) #0

; 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 = !{!"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, i64 noundef %2, ptr nocapture noundef readonly %3) local_unnamed_addr #0 {
  %5 = add i64 %1, -1
  %.not = icmp eq i64 %5, 0
  br i1 %.not, label %._crit_edge, label %.preheader

.thread29:                                        ; preds = %.thread
  %.old = add nuw i64 %.01823, 1
  %exitcond25.not.old = icmp eq i64 %.old, %5
  br i1 %exitcond25.not.old, label %._crit_edge, label %.preheader.backedge

.preheader.backedge:                              ; preds = %.thread29, %7
  %.01823.be = phi i64 [ %.old, %.thread29 ], [ %8, %7 ]
  br label %.preheader, !llvm.loop !5

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

.outer:                                           ; preds = %.thread, %.preheader
  %6 = phi i1 [ false, %.thread ], [ true, %.preheader ]
  %.01921.ph = phi i64 [ %12, %.thread ], [ 0, %.preheader ]
  br label %9

7:                                                ; preds = %17
  %8 = add nuw i64 %.01823, 1
  %exitcond25.not = icmp eq i64 %8, %5
  %or.cond = select i1 %6, i1 true, i1 %exitcond25.not
  br i1 %or.cond, label %._crit_edge, label %.preheader.backedge

9:                                                ; preds = %.outer, %17
  %.01921 = phi i64 [ %12, %17 ], [ %.01921.ph, %.outer ]
  %10 = mul i64 %.01921, %2
  %11 = getelementptr inbounds i8, ptr %0, i64 %10
  %12 = add nuw i64 %.01921, 1
  %13 = mul i64 %12, %2
  %14 = getelementptr inbounds i8, ptr %0, i64 %13
  %15 = tail call i32 %3(ptr noundef %11, ptr noundef %14) #1
  %16 = icmp sgt i32 %15, 0
  br i1 %16, label %.thread, label %17

17:                                               ; preds = %9
  %exitcond.not = icmp eq i64 %12, %5
  br i1 %exitcond.not, label %7, label %9, !llvm.loop !7

.thread:                                          ; preds = %9
  tail call void @swap(ptr noundef %11, ptr noundef %14, i64 noundef %2)
  %exitcond.not27 = icmp eq i64 %12, %5
  br i1 %exitcond.not27, label %.thread29, label %.outer, !llvm.loop !7

._crit_edge:                                      ; preds = %.thread29, %7, %4
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @swap(ptr noundef, ptr noundef, i64 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" }
attributes #1 = { 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 = distinct !{!7, !6}


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