; LLVM IR from AdrianWR/push_swap :: src (utils_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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local void @insertion_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 i32, align 4
  %7 = 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 4, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 1, ptr %6, align 4, !tbaa !11
  br label %8

8:                                                ; preds = %57, %2
  %9 = load i32, ptr %6, align 4, !tbaa !11
  %10 = zext i32 %9 to i64
  %11 = load i64, ptr %4, align 8, !tbaa !9
  %12 = icmp ult i64 %10, %11
  br i1 %12, label %13, label %58

13:                                               ; preds = %8
  %14 = load i32, ptr %6, align 4, !tbaa !11
  %15 = sub i32 %14, 1
  store i32 %15, ptr %5, align 4, !tbaa !11
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = load i32, ptr %6, align 4, !tbaa !11
  %18 = add i32 %17, 1
  store i32 %18, ptr %6, align 4, !tbaa !11
  %19 = zext i32 %17 to i64
  %20 = getelementptr inbounds i32, ptr %16, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !11
  store i32 %21, ptr %7, align 4, !tbaa !11
  br label %22

22:                                               ; preds = %40, %13
  %23 = load i32, ptr %5, align 4, !tbaa !11
  %24 = icmp sge i32 %23, 0
  br i1 %24, label %25, label %38

25:                                               ; preds = %22
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = load i32, ptr %5, align 4, !tbaa !11
  %28 = add nsw i32 %27, 1
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i32, ptr %26, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !11
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = load i32, ptr %5, align 4, !tbaa !11
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i32, ptr %32, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !11
  %37 = icmp slt i32 %31, %36
  br label %38

38:                                               ; preds = %25, %22
  %39 = phi i1 [ false, %22 ], [ %37, %25 ]
  br i1 %39, label %40, label %57

40:                                               ; preds = %38
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = load i32, ptr %5, align 4, !tbaa !11
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds i32, ptr %41, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !11
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = load i32, ptr %5, align 4, !tbaa !11
  %48 = add nsw i32 %47, 1
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds i32, ptr %46, i64 %49
  store i32 %45, ptr %50, align 4, !tbaa !11
  %51 = load i32, ptr %7, align 4, !tbaa !11
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = load i32, ptr %5, align 4, !tbaa !11
  %54 = add nsw i32 %53, -1
  store i32 %54, ptr %5, align 4, !tbaa !11
  %55 = sext i32 %53 to i64
  %56 = getelementptr inbounds i32, ptr %52, i64 %55
  store i32 %51, ptr %56, align 4, !tbaa !11
  br label %22, !llvm.loop !13

57:                                               ; preds = %38
  br label %8, !llvm.loop !15

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

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: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @insertion_sort(ptr nocapture noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = icmp ugt i64 %1, 1
  br i1 %3, label %.lr.ph24, label %._crit_edge

.lr.ph24:                                         ; preds = %2, %.critedge
  %4 = phi i64 [ %24, %.critedge ], [ 1, %2 ]
  %.01623 = phi i32 [ %5, %.critedge ], [ 1, %2 ]
  %5 = add i32 %.01623, 1
  %6 = getelementptr inbounds i32, ptr %0, i64 %4
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %.019 = add i32 %.01623, -1
  %8 = icmp sgt i32 %.019, -1
  br i1 %8, label %.lr.ph.preheader, label %.critedge

.lr.ph.preheader:                                 ; preds = %.lr.ph24
  %.phi.trans.insert = zext i32 %.01623 to i64
  %.phi.trans.insert25 = getelementptr inbounds i32, ptr %0, i64 %.phi.trans.insert
  %.pre = load i32, ptr %.phi.trans.insert25, align 4, !tbaa !5
  %9 = zext nneg i32 %.019 to i64
  %10 = getelementptr inbounds i32, ptr %0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = icmp slt i32 %.pre, %11
  br i1 %12, label %13, label %.critedge

13:                                               ; preds = %.lr.ph.preheader
  %14 = zext nneg i32 %.01623 to i64
  %15 = getelementptr inbounds i32, ptr %0, i64 %14
  store i32 %11, ptr %15, align 4, !tbaa !5
  store i32 %7, ptr %10, align 4, !tbaa !5
  %.0.peel = add i32 %.01623, -2
  %.not = icmp eq i32 %.019, 0
  br i1 %.not, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %13, %20
  %.021 = phi i32 [ %.0, %20 ], [ %.0.peel, %13 ]
  %.0.in20 = phi i32 [ %.021, %20 ], [ %.019, %13 ]
  %16 = zext nneg i32 %.021 to i64
  %17 = getelementptr inbounds i32, ptr %0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = icmp slt i32 %7, %18
  br i1 %19, label %20, label %.critedge

20:                                               ; preds = %.lr.ph
  %21 = zext nneg i32 %.0.in20 to i64
  %22 = getelementptr inbounds i32, ptr %0, i64 %21
  store i32 %18, ptr %22, align 4, !tbaa !5
  store i32 %7, ptr %17, align 4, !tbaa !5
  %.0 = add nsw i32 %.021, -1
  %23 = icmp sgt i32 %.021, 0
  br i1 %23, label %.lr.ph, label %.critedge, !llvm.loop !9

.critedge:                                        ; preds = %.lr.ph.preheader, %13, %.lr.ph, %20, %.lr.ph24
  %24 = zext i32 %5 to i64
  %25 = icmp ult i64 %24, %1
  br i1 %25, label %.lr.ph24, label %._crit_edge, !llvm.loop !12

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

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: readwrite) 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10, !11}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.peeled.count", i32 1}
!12 = distinct !{!12, !10}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass