; LLVM IR from ismdeep/sort-algos-c :: include/sort-algos (insertion-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 @insertion_sort(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  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 8, ptr %9) #5
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %14, ptr %9, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %64, %4
  %16 = load ptr, ptr %9, align 8, !tbaa !5
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = icmp ult ptr %16, %17
  br i1 %18, label %19, label %72

19:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %20 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %20, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %21 = load ptr, ptr %10, align 8, !tbaa !5
  %22 = load i64, ptr %7, align 8, !tbaa !9
  %23 = getelementptr inbounds i8, ptr %21, i64 %22
  store ptr %23, ptr %11, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %35, %19
  %25 = load ptr, ptr %11, align 8, !tbaa !5
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = icmp ult ptr %25, %26
  br i1 %27, label %28, label %39

28:                                               ; preds = %24
  %29 = load ptr, ptr %8, align 8, !tbaa !5
  %30 = load ptr, ptr %10, align 8, !tbaa !5
  %31 = load ptr, ptr %11, align 8, !tbaa !5
  %32 = call zeroext i1 %29(ptr noundef %30, ptr noundef %31)
  br i1 %32, label %35, label %33

33:                                               ; preds = %28
  %34 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr %34, ptr %10, align 8, !tbaa !5
  br label %35

35:                                               ; preds = %33, %28
  %36 = load i64, ptr %7, align 8, !tbaa !9
  %37 = load ptr, ptr %11, align 8, !tbaa !5
  %38 = getelementptr inbounds i8, ptr %37, i64 %36
  store ptr %38, ptr %11, align 8, !tbaa !5
  br label %24, !llvm.loop !11

39:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  %40 = load i64, ptr %7, align 8, !tbaa !9
  %41 = call noalias ptr @malloc(i64 noundef %40) #6
  store ptr %41, ptr %12, align 8, !tbaa !5
  %42 = load ptr, ptr %12, align 8, !tbaa !5
  %43 = load ptr, ptr %10, align 8, !tbaa !5
  %44 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %42, ptr align 1 %43, i64 %44, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  %45 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %45, ptr %13, align 8, !tbaa !5
  %46 = load i64, ptr %7, align 8, !tbaa !9
  %47 = load ptr, ptr %13, align 8, !tbaa !5
  %48 = sub i64 0, %46
  %49 = getelementptr inbounds i8, ptr %47, i64 %48
  store ptr %49, ptr %13, align 8, !tbaa !5
  br label %50

50:                                               ; preds = %54, %39
  %51 = load ptr, ptr %9, align 8, !tbaa !5
  %52 = load ptr, ptr %13, align 8, !tbaa !5
  %53 = icmp ule ptr %51, %52
  br i1 %53, label %54, label %64

54:                                               ; preds = %50
  %55 = load ptr, ptr %13, align 8, !tbaa !5
  %56 = load i64, ptr %7, align 8, !tbaa !9
  %57 = getelementptr inbounds i8, ptr %55, i64 %56
  %58 = load ptr, ptr %13, align 8, !tbaa !5
  %59 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %57, ptr align 1 %58, i64 %59, i1 false)
  %60 = load i64, ptr %7, align 8, !tbaa !9
  %61 = load ptr, ptr %13, align 8, !tbaa !5
  %62 = sub i64 0, %60
  %63 = getelementptr inbounds i8, ptr %61, i64 %62
  store ptr %63, ptr %13, align 8, !tbaa !5
  br label %50, !llvm.loop !13

64:                                               ; preds = %50
  %65 = load ptr, ptr %9, align 8, !tbaa !5
  %66 = load ptr, ptr %12, align 8, !tbaa !5
  %67 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %65, ptr align 1 %66, i64 %67, i1 false)
  %68 = load ptr, ptr %12, align 8, !tbaa !5
  call void @free(ptr noundef %68) #5
  %69 = load ptr, ptr %9, align 8, !tbaa !5
  %70 = load i64, ptr %7, align 8, !tbaa !9
  %71 = getelementptr inbounds i8, ptr %69, i64 %70
  store ptr %71, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  br label %15, !llvm.loop !14

72:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  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 allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: nounwind
declare void @free(ptr noundef) #4

; 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 allocsize(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 #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nounwind "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 #5 = { nounwind }
attributes #6 = { nounwind allocsize(0) }

!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 = 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
define dso_local void @insertion_sort(ptr noundef %0, ptr noundef readnone %1, i64 noundef %2, ptr nocapture noundef readonly %3) local_unnamed_addr #0 {
  %5 = icmp ult ptr %0, %1
  br i1 %5, label %.lr.ph49, label %._crit_edge50

.lr.ph49:                                         ; preds = %4
  %6 = sub i64 0, %2
  br label %7

7:                                                ; preds = %.lr.ph49, %._crit_edge46
  %.03447 = phi ptr [ %0, %.lr.ph49 ], [ %8, %._crit_edge46 ]
  %8 = getelementptr inbounds i8, ptr %.03447, i64 %2
  %9 = icmp ult ptr %8, %1
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %7, %.lr.ph
  %.03539 = phi ptr [ %11, %.lr.ph ], [ %8, %7 ]
  %.03638 = phi ptr [ %spec.select, %.lr.ph ], [ %.03447, %7 ]
  %10 = tail call zeroext i1 %3(ptr noundef %.03638, ptr noundef %.03539) #4
  %spec.select = select i1 %10, ptr %.03638, ptr %.03539
  %11 = getelementptr inbounds i8, ptr %.03539, i64 %2
  %12 = icmp ult ptr %11, %1
  br i1 %12, label %.lr.ph, label %._crit_edge, !llvm.loop !5

._crit_edge:                                      ; preds = %.lr.ph, %7
  %.036.lcssa = phi ptr [ %.03447, %7 ], [ %spec.select, %.lr.ph ]
  %13 = tail call noalias ptr @malloc(i64 noundef %2) #5
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %13, ptr align 1 %.036.lcssa, i64 %2, i1 false)
  %.040 = getelementptr inbounds i8, ptr %.036.lcssa, i64 %6
  %.not41 = icmp ugt ptr %.03447, %.040
  br i1 %.not41, label %._crit_edge46, label %.lr.ph45

.lr.ph45:                                         ; preds = %._crit_edge, %.lr.ph45
  %.043 = phi ptr [ %.0, %.lr.ph45 ], [ %.040, %._crit_edge ]
  %.036.pn42 = phi ptr [ %.043, %.lr.ph45 ], [ %.036.lcssa, %._crit_edge ]
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.036.pn42, ptr align 1 %.043, i64 %2, i1 false)
  %.0 = getelementptr inbounds i8, ptr %.043, i64 %6
  %.not = icmp ugt ptr %.03447, %.0
  br i1 %.not, label %._crit_edge46, label %.lr.ph45, !llvm.loop !7

._crit_edge46:                                    ; preds = %.lr.ph45, %._crit_edge
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %.03447, ptr align 1 %13, i64 %2, i1 false)
  tail call void @free(ptr noundef %13) #4
  br i1 %9, label %7, label %._crit_edge50, !llvm.loop !8

._crit_edge50:                                    ; preds = %._crit_edge46, %4
  ret void
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #3

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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0) }

!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,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass