; LLVM IR from AlgoGenesis/C :: Sorting Algorithms/Tim 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 i32 @min(i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @insertionSort(ptr 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 @merge(ptr noundef, i32 noundef, i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @timSort(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  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 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %25, %2
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = load i32, ptr %4, align 4, !tbaa !9
  %14 = icmp slt i32 %12, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %28

16:                                               ; preds = %11
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = load i32, ptr %5, align 4, !tbaa !9
  %19 = load i32, ptr %5, align 4, !tbaa !9
  %20 = add nsw i32 %19, 32
  %21 = sub nsw i32 %20, 1
  %22 = load i32, ptr %4, align 4, !tbaa !9
  %23 = sub nsw i32 %22, 1
  %24 = call i32 @min(i32 noundef %21, i32 noundef %23)
  call void @insertionSort(ptr noundef %17, i32 noundef %18, i32 noundef %24)
  br label %25

25:                                               ; preds = %16
  %26 = load i32, ptr %5, align 4, !tbaa !9
  %27 = add nsw i32 %26, 32
  store i32 %27, ptr %5, align 4, !tbaa !9
  br label %11, !llvm.loop !11

28:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 32, ptr %6, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %68, %28
  %30 = load i32, ptr %6, align 4, !tbaa !9
  %31 = load i32, ptr %4, align 4, !tbaa !9
  %32 = icmp slt i32 %30, %31
  br i1 %32, label %34, label %33

33:                                               ; preds = %29
  store i32 5, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %71

34:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %35

35:                                               ; preds = %62, %34
  %36 = load i32, ptr %8, align 4, !tbaa !9
  %37 = load i32, ptr %4, align 4, !tbaa !9
  %38 = icmp slt i32 %36, %37
  br i1 %38, label %40, label %39

39:                                               ; preds = %35
  store i32 8, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %67

40:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %41 = load i32, ptr %8, align 4, !tbaa !9
  %42 = load i32, ptr %6, align 4, !tbaa !9
  %43 = add nsw i32 %41, %42
  %44 = sub nsw i32 %43, 1
  store i32 %44, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %45 = load i32, ptr %8, align 4, !tbaa !9
  %46 = load i32, ptr %6, align 4, !tbaa !9
  %47 = mul nsw i32 2, %46
  %48 = add nsw i32 %45, %47
  %49 = sub nsw i32 %48, 1
  %50 = load i32, ptr %4, align 4, !tbaa !9
  %51 = sub nsw i32 %50, 1
  %52 = call i32 @min(i32 noundef %49, i32 noundef %51)
  store i32 %52, ptr %10, align 4, !tbaa !9
  %53 = load i32, ptr %9, align 4, !tbaa !9
  %54 = load i32, ptr %10, align 4, !tbaa !9
  %55 = icmp slt i32 %53, %54
  br i1 %55, label %56, label %61

56:                                               ; preds = %40
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  %58 = load i32, ptr %8, align 4, !tbaa !9
  %59 = load i32, ptr %9, align 4, !tbaa !9
  %60 = load i32, ptr %10, align 4, !tbaa !9
  call void @merge(ptr noundef %57, i32 noundef %58, i32 noundef %59, i32 noundef %60)
  br label %61

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

62:                                               ; preds = %61
  %63 = load i32, ptr %6, align 4, !tbaa !9
  %64 = mul nsw i32 2, %63
  %65 = load i32, ptr %8, align 4, !tbaa !9
  %66 = add nsw i32 %65, %64
  store i32 %66, ptr %8, align 4, !tbaa !9
  br label %35, !llvm.loop !13

67:                                               ; preds = %39
  br label %68

68:                                               ; preds = %67
  %69 = load i32, ptr %6, align 4, !tbaa !9
  %70 = mul nsw i32 2, %69
  store i32 %70, ptr %6, align 4, !tbaa !9
  br label %29, !llvm.loop !14

71:                                               ; preds = %33
  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}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @timSort(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp sgt i32 %1, 0
  br i1 %3, label %.lr.ph, label %._crit_edge34

.lr.ph:                                           ; preds = %2
  %4 = add nsw i32 %1, -1
  br label %18

.preheader29:                                     ; preds = %18
  %5 = icmp sgt i32 %1, 32
  br i1 %5, label %.preheader.lr.ph, label %._crit_edge34

.preheader.lr.ph:                                 ; preds = %.preheader29
  %6 = add nsw i32 %1, -1
  br label %.preheader.us

.preheader.us:                                    ; preds = %._crit_edge.us, %.preheader.lr.ph
  %.02433.us = phi i32 [ 32, %.preheader.lr.ph ], [ %7, %._crit_edge.us ]
  %7 = shl nsw i32 %.02433.us, 1
  br label %8

8:                                                ; preds = %.preheader.us, %15
  %.02531.us = phi i32 [ 0, %.preheader.us ], [ %10, %15 ]
  %9 = add nsw i32 %.02531.us, %.02433.us
  %10 = add nsw i32 %.02531.us, %7
  %11 = add nsw i32 %10, -1
  %12 = tail call i32 @min(i32 noundef %11, i32 noundef %6)
  %.not.us = icmp sgt i32 %9, %12
  br i1 %.not.us, label %15, label %13

13:                                               ; preds = %8
  %14 = add nsw i32 %9, -1
  tail call void @merge(ptr noundef %0, i32 noundef %.02531.us, i32 noundef %14, i32 noundef %12)
  br label %15

15:                                               ; preds = %13, %8
  %16 = icmp slt i32 %10, %1
  br i1 %16, label %8, label %._crit_edge.us, !llvm.loop !5

._crit_edge.us:                                   ; preds = %15
  %17 = icmp slt i32 %7, %1
  br i1 %17, label %.preheader.us, label %._crit_edge34, !llvm.loop !7

18:                                               ; preds = %.lr.ph, %18
  %.030 = phi i32 [ 0, %.lr.ph ], [ %19, %18 ]
  %19 = add nuw nsw i32 %.030, 32
  %20 = or disjoint i32 %.030, 31
  %21 = tail call i32 @min(i32 noundef %20, i32 noundef %4)
  tail call void @insertionSort(ptr noundef %0, i32 noundef %.030, i32 noundef %21)
  %22 = icmp slt i32 %19, %1
  br i1 %22, label %18, label %.preheader29, !llvm.loop !8

._crit_edge34:                                    ; preds = %._crit_edge.us, %2, %.preheader29
  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)"}
!5 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}
!8 = distinct !{!8, !6}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass