; LLVM IR from AlgoGenesis/C :: Sorting Algorithms/Intro Sort (Program)

; 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 @insertionSort(ptr noundef, i32 noundef, i32 noundef, i32 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
declare dso_local void @heapsort(ptr noundef, i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @partition(ptr noundef, i32 noundef, i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @introsortUtil(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #0 {
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  store i32 %4, ptr %10, align 4, !tbaa !9
  %12 = load i32, ptr %8, align 4, !tbaa !9
  %13 = load i32, ptr %7, align 4, !tbaa !9
  %14 = sub nsw i32 %12, %13
  %15 = icmp slt i32 %14, 16
  br i1 %15, label %16, label %21

16:                                               ; preds = %5
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = load i32, ptr %7, align 4, !tbaa !9
  %19 = load i32, ptr %8, align 4, !tbaa !9
  %20 = load i32, ptr %10, align 4, !tbaa !9
  call void @insertionSort(ptr noundef %17, i32 noundef %18, i32 noundef %19, i32 noundef %20)
  br label %54

21:                                               ; preds = %5
  %22 = load i32, ptr %9, align 4, !tbaa !9
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %34

24:                                               ; preds = %21
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = load i32, ptr %7, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i32, ptr %25, i64 %27
  %29 = load i32, ptr %8, align 4, !tbaa !9
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = sub nsw i32 %29, %30
  %32 = add nsw i32 %31, 1
  %33 = load i32, ptr %10, align 4, !tbaa !9
  call void @heapsort(ptr noundef %28, i32 noundef %32, i32 noundef %33)
  br label %54

34:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = load i32, ptr %7, align 4, !tbaa !9
  %37 = load i32, ptr %8, align 4, !tbaa !9
  %38 = load i32, ptr %10, align 4, !tbaa !9
  %39 = call i32 @partition(ptr noundef %35, i32 noundef %36, i32 noundef %37, i32 noundef %38)
  store i32 %39, ptr %11, align 4, !tbaa !9
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = load i32, ptr %11, align 4, !tbaa !9
  %43 = sub nsw i32 %42, 1
  %44 = load i32, ptr %9, align 4, !tbaa !9
  %45 = sub nsw i32 %44, 1
  %46 = load i32, ptr %10, align 4, !tbaa !9
  call void @introsortUtil(ptr noundef %40, i32 noundef %41, i32 noundef %43, i32 noundef %45, i32 noundef %46)
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = load i32, ptr %11, align 4, !tbaa !9
  %49 = add nsw i32 %48, 1
  %50 = load i32, ptr %8, align 4, !tbaa !9
  %51 = load i32, ptr %9, align 4, !tbaa !9
  %52 = sub nsw i32 %51, 1
  %53 = load i32, ptr %10, align 4, !tbaa !9
  call void @introsortUtil(ptr noundef %47, i32 noundef %49, i32 noundef %50, i32 noundef %52, i32 noundef %53)
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %54

54:                                               ; preds = %34, %24, %16
  ret void
}

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}


; 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 @insertionSort(ptr noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @heapsort(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @partition(ptr noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @introsortUtil(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %6 = sub nsw i32 %2, %1
  %7 = icmp slt i32 %6, 16
  br i1 %7, label %tailrecurse._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %5
  %8 = icmp eq i32 %3, 0
  br i1 %8, label %.lr.ph._crit_edge, label %tailrecurse

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %5
  %.tr26.lcssa = phi i32 [ %1, %5 ], [ %16, %tailrecurse ]
  tail call void @insertionSort(ptr noundef %0, i32 noundef %.tr26.lcssa, i32 noundef %2, i32 noundef %4)
  br label %19

.lr.ph:                                           ; preds = %tailrecurse
  %9 = icmp eq i32 %15, 0
  br i1 %9, label %.lr.ph._crit_edge, label %tailrecurse

.lr.ph._crit_edge:                                ; preds = %.lr.ph, %.lr.ph.preheader
  %.lcssa = phi i32 [ %6, %.lr.ph.preheader ], [ %17, %.lr.ph ]
  %.tr2632.lcssa = phi i32 [ %1, %.lr.ph.preheader ], [ %16, %.lr.ph ]
  %10 = sext i32 %.tr2632.lcssa to i64
  %11 = getelementptr inbounds i32, ptr %0, i64 %10
  %12 = add nuw nsw i32 %.lcssa, 1
  tail call void @heapsort(ptr noundef %11, i32 noundef %12, i32 noundef %4)
  br label %19

tailrecurse:                                      ; preds = %.lr.ph.preheader, %.lr.ph
  %.tr263244 = phi i32 [ %16, %.lr.ph ], [ %1, %.lr.ph.preheader ]
  %.tr283343 = phi i32 [ %15, %.lr.ph ], [ %3, %.lr.ph.preheader ]
  %13 = tail call i32 @partition(ptr noundef %0, i32 noundef %.tr263244, i32 noundef %2, i32 noundef %4)
  %14 = add nsw i32 %13, -1
  %15 = add nsw i32 %.tr283343, -1
  tail call void @introsortUtil(ptr noundef %0, i32 noundef %.tr263244, i32 noundef %14, i32 noundef %15, i32 noundef %4)
  %16 = add nsw i32 %13, 1
  %17 = sub nsw i32 %2, %16
  %18 = icmp slt i32 %17, 16
  br i1 %18, label %tailrecurse._crit_edge, label %.lr.ph

19:                                               ; preds = %.lr.ph._crit_edge, %tailrecurse._crit_edge
  ret void
}

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


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