; LLVM IR from h-ssiqueira/Sort_Algorithms :: src/esotericFunMiscellaneous (badSort)

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

12:                                               ; preds = %67, %2
  %13 = load ptr, ptr %7, align 8, !tbaa !5
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = load i32, ptr %4, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds i64, ptr %14, i64 %16
  %18 = icmp ult ptr %13, %17
  br i1 %18, label %19, label %70

19:                                               ; preds = %12
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %20, ptr %6, align 8, !tbaa !5
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %21, ptr %8, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %56, %19
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = load i32, ptr %4, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i64, ptr %24, i64 %26
  %28 = icmp ult ptr %23, %27
  br i1 %28, label %29, label %59

29:                                               ; preds = %22
  store i8 1, ptr %10, align 1, !tbaa !11
  %30 = load ptr, ptr %8, align 8, !tbaa !5
  %31 = getelementptr inbounds i64, ptr %30, i64 1
  store ptr %31, ptr %9, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %47, %29
  %33 = load ptr, ptr %9, align 8, !tbaa !5
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = load i32, ptr %4, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds i64, ptr %34, i64 %36
  %38 = icmp ult ptr %33, %37
  br i1 %38, label %39, label %50

39:                                               ; preds = %32
  %40 = load ptr, ptr %8, align 8, !tbaa !5
  %41 = load i64, ptr %40, align 8, !tbaa !13
  %42 = load ptr, ptr %9, align 8, !tbaa !5
  %43 = load i64, ptr %42, align 8, !tbaa !13
  %44 = icmp sgt i64 %41, %43
  br i1 %44, label %45, label %46

45:                                               ; preds = %39
  store i8 0, ptr %10, align 1, !tbaa !11
  br label %50

46:                                               ; preds = %39
  br label %47

47:                                               ; preds = %46
  %48 = load ptr, ptr %9, align 8, !tbaa !5
  %49 = getelementptr inbounds i64, ptr %48, i32 1
  store ptr %49, ptr %9, align 8, !tbaa !5
  br label %32, !llvm.loop !15

50:                                               ; preds = %45, %32
  %51 = load i8, ptr %10, align 1, !tbaa !11, !range !17, !noundef !18
  %52 = trunc i8 %51 to i1
  br i1 %52, label %53, label %55

53:                                               ; preds = %50
  %54 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %54, ptr %6, align 8, !tbaa !5
  br label %59

55:                                               ; preds = %50
  br label %56

56:                                               ; preds = %55
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  %58 = getelementptr inbounds i64, ptr %57, i32 1
  store ptr %58, ptr %8, align 8, !tbaa !5
  br label %22, !llvm.loop !19

59:                                               ; preds = %53, %22
  %60 = load ptr, ptr %7, align 8, !tbaa !5
  %61 = load i64, ptr %60, align 8, !tbaa !13
  store i64 %61, ptr %5, align 8, !tbaa !13
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  %63 = load i64, ptr %62, align 8, !tbaa !13
  %64 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %63, ptr %64, align 8, !tbaa !13
  %65 = load i64, ptr %5, align 8, !tbaa !13
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  store i64 %65, ptr %66, align 8, !tbaa !13
  br label %67

67:                                               ; preds = %59
  %68 = load ptr, ptr %7, align 8, !tbaa !5
  %69 = getelementptr inbounds i64, ptr %68, i32 1
  store ptr %69, ptr %7, align 8, !tbaa !5
  br label %12, !llvm.loop !20

70:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  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 8, 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}
!20 = distinct !{!20, !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 @badSort(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, label %._crit_edge

.preheader:                                       ; preds = %2, %.critedge
  %.02636 = phi ptr [ %17, %.critedge ], [ %0, %2 ]
  %6 = getelementptr inbounds i8, ptr %.02636, i64 8
  %.not3145 = icmp ult ptr %6, %4
  br i1 %.not3145, label %.lr.ph, label %.critedge.loopexit38

.loopexit.loopexit:                               ; preds = %12
  %7 = getelementptr inbounds i8, ptr %8, i64 8
  %.not31 = icmp ult ptr %7, %4
  br i1 %.not31, label %.lr.ph, label %.critedge.loopexit38

.lr.ph:                                           ; preds = %.preheader, %.loopexit.loopexit
  %8 = phi ptr [ %7, %.loopexit.loopexit ], [ %6, %.preheader ]
  %.0253546 = phi ptr [ %8, %.loopexit.loopexit ], [ %.02636, %.preheader ]
  %9 = load i64, ptr %.0253546, align 8, !tbaa !5
  br label %12

10:                                               ; preds = %12
  %11 = getelementptr inbounds i8, ptr %.02432, i64 8
  %.not = icmp ult ptr %11, %4
  br i1 %.not, label %12, label %.critedge, !llvm.loop !9

12:                                               ; preds = %.lr.ph, %10
  %.02432 = phi ptr [ %8, %.lr.ph ], [ %11, %10 ]
  %13 = load i64, ptr %.02432, align 8, !tbaa !5
  %14 = icmp sgt i64 %9, %13
  br i1 %14, label %.loopexit.loopexit, label %10

.critedge.loopexit38:                             ; preds = %.loopexit.loopexit, %.preheader
  %.02535.lcssa = phi ptr [ %.02636, %.preheader ], [ %8, %.loopexit.loopexit ]
  %.pre.pre = load i64, ptr %.02535.lcssa, align 8, !tbaa !5
  br label %.critedge

.critedge:                                        ; preds = %10, %.critedge.loopexit38
  %.0253544 = phi ptr [ %.02535.lcssa, %.critedge.loopexit38 ], [ %.0253546, %10 ]
  %15 = phi i64 [ %.pre.pre, %.critedge.loopexit38 ], [ %9, %10 ]
  %16 = load i64, ptr %.02636, align 8, !tbaa !5
  store i64 %15, ptr %.02636, align 8, !tbaa !5
  store i64 %16, ptr %.0253544, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %.02636, i64 8
  %18 = icmp ult ptr %17, %4
  br i1 %18, label %.preheader, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %.critedge, %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: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass