; LLVM IR from CactiLab/Sherloc-Cortex-M-CFVD :: Sherloc_runtime/evaluation/src/mergesort (libmergesort)

; 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"

%struct.Range = type { i64, i64 }
%struct.Test = type { i32, i32 }

; 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 @InsertionSort(ptr noundef %0, i64 %1, i64 %2, ptr noundef %3) #1 {
  %5 = alloca %struct.Range, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca %struct.Test, align 4
  %10 = alloca i64, align 8
  %11 = getelementptr inbounds { i64, i64 }, ptr %5, i32 0, i32 0
  store i64 %1, ptr %11, align 8
  %12 = getelementptr inbounds { i64, i64 }, ptr %5, i32 0, i32 1
  store i64 %2, ptr %12, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %3, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %13 = getelementptr inbounds %struct.Range, ptr %5, i32 0, i32 0
  %14 = load i64, ptr %13, align 8, !tbaa !9
  %15 = add nsw i64 %14, 1
  store i64 %15, ptr %8, align 8, !tbaa !12
  br label %16

16:                                               ; preds = %59, %4
  %17 = load i64, ptr %8, align 8, !tbaa !12
  %18 = getelementptr inbounds %struct.Range, ptr %5, i32 0, i32 1
  %19 = load i64, ptr %18, align 8, !tbaa !13
  %20 = icmp slt i64 %17, %19
  br i1 %20, label %21, label %62

21:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = load i64, ptr %8, align 8, !tbaa !12
  %24 = getelementptr inbounds %struct.Test, ptr %22, i64 %23
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %9, ptr align 4 %24, i64 8, i1 false), !tbaa.struct !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %25 = load i64, ptr %8, align 8, !tbaa !12
  store i64 %25, ptr %10, align 8, !tbaa !12
  br label %26

26:                                               ; preds = %52, %21
  %27 = load i64, ptr %10, align 8, !tbaa !12
  %28 = getelementptr inbounds %struct.Range, ptr %5, i32 0, i32 0
  %29 = load i64, ptr %28, align 8, !tbaa !9
  %30 = icmp sgt i64 %27, %29
  br i1 %30, label %31, label %42

31:                                               ; preds = %26
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = load i64, ptr %10, align 8, !tbaa !12
  %35 = sub nsw i64 %34, 1
  %36 = getelementptr inbounds %struct.Test, ptr %33, i64 %35
  %37 = load i64, ptr %9, align 4
  %38 = load i64, ptr %36, align 4
  %39 = call zeroext i8 %32(i64 %37, i64 %38)
  %40 = zext i8 %39 to i32
  %41 = icmp ne i32 %40, 0
  br label %42

42:                                               ; preds = %31, %26
  %43 = phi i1 [ false, %26 ], [ %41, %31 ]
  br i1 %43, label %44, label %55

44:                                               ; preds = %42
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = load i64, ptr %10, align 8, !tbaa !12
  %47 = getelementptr inbounds %struct.Test, ptr %45, i64 %46
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = load i64, ptr %10, align 8, !tbaa !12
  %50 = sub nsw i64 %49, 1
  %51 = getelementptr inbounds %struct.Test, ptr %48, i64 %50
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %47, ptr align 4 %51, i64 8, i1 false), !tbaa.struct !14
  br label %52

52:                                               ; preds = %44
  %53 = load i64, ptr %10, align 8, !tbaa !12
  %54 = add nsw i64 %53, -1
  store i64 %54, ptr %10, align 8, !tbaa !12
  br label %26, !llvm.loop !17

55:                                               ; preds = %42
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = load i64, ptr %10, align 8, !tbaa !12
  %58 = getelementptr inbounds %struct.Test, ptr %56, i64 %57
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %58, ptr align 4 %9, i64 8, i1 false), !tbaa.struct !14
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %59

59:                                               ; preds = %55
  %60 = load i64, ptr %8, align 8, !tbaa !12
  %61 = add nsw i64 %60, 1
  store i64 %61, ptr %8, align 8, !tbaa !12
  br label %16, !llvm.loop !19

62:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  ret void
}

; 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) #2

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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, !11, i64 0}
!10 = !{!"", !11, i64 0, !11, i64 8}
!11 = !{!"long", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!10, !11, i64 8}
!14 = !{i64 0, i64 4, !15, i64 4, i64 4, !15}
!15 = !{!16, !16, i64 0}
!16 = !{!"int", !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


; 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"

%struct.Test = type { i32, i32 }

; Function Attrs: nounwind uwtable
define dso_local void @InsertionSort(ptr nocapture noundef %0, i64 %1, i64 %2, ptr nocapture noundef readonly %3) local_unnamed_addr #0 {
  %.01822 = add nsw i64 %1, 1
  %5 = icmp slt i64 %.01822, %2
  br i1 %5, label %.lr.ph24, label %._crit_edge

.lr.ph24:                                         ; preds = %4, %.critedge
  %.01823 = phi i64 [ %.018, %.critedge ], [ %.01822, %4 ]
  %6 = getelementptr inbounds %struct.Test, ptr %0, i64 %.01823
  %.sroa.0.0.copyload = load i64, ptr %6, align 4
  %7 = icmp sgt i64 %.01823, %1
  br i1 %7, label %.lr.ph, label %.critedge

.lr.ph:                                           ; preds = %.lr.ph24, %12
  %.019 = phi i64 [ %14, %12 ], [ %.01823, %.lr.ph24 ]
  %8 = getelementptr %struct.Test, ptr %0, i64 %.019
  %9 = getelementptr i8, ptr %8, i64 -8
  %10 = load i64, ptr %9, align 4
  %11 = tail call zeroext i8 %3(i64 %.sroa.0.0.copyload, i64 %10) #1
  %.not = icmp eq i8 %11, 0
  br i1 %.not, label %.critedge, label %12

12:                                               ; preds = %.lr.ph
  %13 = load i64, ptr %9, align 4
  store i64 %13, ptr %8, align 4
  %14 = add nsw i64 %.019, -1
  %15 = icmp sgt i64 %14, %1
  br i1 %15, label %.lr.ph, label %.critedge, !llvm.loop !5

.critedge:                                        ; preds = %.lr.ph, %12, %.lr.ph24
  %.0.lcssa = phi i64 [ %.01823, %.lr.ph24 ], [ %1, %12 ], [ %.019, %.lr.ph ]
  %16 = getelementptr inbounds %struct.Test, ptr %0, i64 %.0.lcssa
  store i64 %.sroa.0.0.copyload, ptr %16, align 4
  %.018 = add i64 %.01823, 1
  %exitcond.not = icmp eq i64 %.018, %2
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph24, !llvm.loop !7

._crit_edge:                                      ; preds = %.critedge, %4
  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 = { 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}


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