; LLVM IR from Dev-XYS/Algorithms :: Merge-Sort.ll (Z5mergeiii)

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

@d = external dso_local global [100000 x i32], align 16
@_ZZ5mergeiiiE2al = external hidden global [100000 x i32], align 16
@_ZZ5mergeiiiE2ar = external hidden global [100000 x i32], align 16

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

11:                                               ; preds = %23, %3
  %12 = load i32, ptr %7, align 4, !tbaa !5
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = icmp sle i32 %12, %13
  br i1 %14, label %15, label %26

15:                                               ; preds = %11
  %16 = load i32, ptr %7, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = load i32, ptr %7, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiE2al, i64 0, i64 %21
  store i32 %19, ptr %22, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %15
  %24 = load i32, ptr %7, align 4, !tbaa !5
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %7, align 4, !tbaa !5
  br label %11, !llvm.loop !9

26:                                               ; preds = %11
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = add nsw i32 %27, 1
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiE2al, i64 0, i64 %29
  store i32 2000000000, ptr %30, align 4, !tbaa !5
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %8, align 4, !tbaa !5
  br label %33

33:                                               ; preds = %45, %26
  %34 = load i32, ptr %8, align 4, !tbaa !5
  %35 = load i32, ptr %6, align 4, !tbaa !5
  %36 = icmp sle i32 %34, %35
  br i1 %36, label %37, label %48

37:                                               ; preds = %33
  %38 = load i32, ptr %8, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = load i32, ptr %8, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiE2ar, i64 0, i64 %43
  store i32 %41, ptr %44, align 4, !tbaa !5
  br label %45

45:                                               ; preds = %37
  %46 = load i32, ptr %8, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %8, align 4, !tbaa !5
  br label %33, !llvm.loop !11

48:                                               ; preds = %33
  %49 = load i32, ptr %6, align 4, !tbaa !5
  %50 = add nsw i32 %49, 1
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiE2ar, i64 0, i64 %51
  store i32 2000000000, ptr %52, align 4, !tbaa !5
  %53 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %53, ptr %7, align 4, !tbaa !5
  %54 = load i32, ptr %5, align 4, !tbaa !5
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %56 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %56, ptr %9, align 4, !tbaa !5
  br label %57

57:                                               ; preds = %91, %48
  %58 = load i32, ptr %9, align 4, !tbaa !5
  %59 = load i32, ptr %6, align 4, !tbaa !5
  %60 = icmp sle i32 %58, %59
  br i1 %60, label %62, label %61

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

62:                                               ; preds = %57
  %63 = load i32, ptr %7, align 4, !tbaa !5
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiE2al, i64 0, i64 %64
  %66 = load i32, ptr %65, align 4, !tbaa !5
  %67 = load i32, ptr %8, align 4, !tbaa !5
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiE2ar, i64 0, i64 %68
  %70 = load i32, ptr %69, align 4, !tbaa !5
  %71 = icmp slt i32 %66, %70
  br i1 %71, label %72, label %81

72:                                               ; preds = %62
  %73 = load i32, ptr %7, align 4, !tbaa !5
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %7, align 4, !tbaa !5
  %75 = sext i32 %73 to i64
  %76 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiE2al, i64 0, i64 %75
  %77 = load i32, ptr %76, align 4, !tbaa !5
  %78 = load i32, ptr %9, align 4, !tbaa !5
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %79
  store i32 %77, ptr %80, align 4, !tbaa !5
  br label %90

81:                                               ; preds = %62
  %82 = load i32, ptr %8, align 4, !tbaa !5
  %83 = add nsw i32 %82, 1
  store i32 %83, ptr %8, align 4, !tbaa !5
  %84 = sext i32 %82 to i64
  %85 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiE2ar, i64 0, i64 %84
  %86 = load i32, ptr %85, align 4, !tbaa !5
  %87 = load i32, ptr %9, align 4, !tbaa !5
  %88 = sext i32 %87 to i64
  %89 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %88
  store i32 %86, ptr %89, align 4, !tbaa !5
  br label %90

90:                                               ; preds = %81, %72
  br label %91

91:                                               ; preds = %90
  %92 = load i32, ptr %9, align 4, !tbaa !5
  %93 = add nsw i32 %92, 1
  store i32 %93, ptr %9, align 4, !tbaa !5
  br label %57, !llvm.loop !12

94:                                               ; preds = %61
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { mustprogress 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


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

@d = external dso_local local_unnamed_addr global [100000 x i32], align 16
@_ZZ5mergeiiiE2al = external hidden local_unnamed_addr global [100000 x i32], align 16
@_ZZ5mergeiiiE2ar = external hidden local_unnamed_addr global [100000 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z5mergeiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %.not36 = icmp sgt i32 %0, %1
  br i1 %.not36, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %4 = sext i32 %0 to i64
  %5 = shl nsw i64 %4, 2
  %scevgep = getelementptr i8, ptr @_ZZ5mergeiiiE2al, i64 %5
  %scevgep50 = getelementptr i8, ptr @d, i64 %5
  %6 = sub i32 %1, %0
  %7 = zext i32 %6 to i64
  %8 = shl nuw nsw i64 %7, 2
  %9 = add nuw nsw i64 %8, 4
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(1) %scevgep, ptr noundef nonnull align 4 dereferenceable(1) %scevgep50, i64 %9, i1 false), !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.preheader, %3
  %10 = add nsw i32 %1, 1
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiE2al, i64 0, i64 %11
  store i32 2000000000, ptr %12, align 4, !tbaa !5
  %.not3438.not = icmp slt i32 %1, %2
  br i1 %.not3438.not, label %.lr.ph41.preheader, label %._crit_edge42

.lr.ph41.preheader:                               ; preds = %._crit_edge
  %13 = sext i32 %1 to i64
  %14 = shl nsw i64 %13, 2
  %15 = add nsw i64 %14, 4
  %scevgep52 = getelementptr i8, ptr @_ZZ5mergeiiiE2ar, i64 %15
  %scevgep53 = getelementptr i8, ptr @d, i64 %15
  %16 = xor i32 %1, -1
  %17 = add i32 %16, %2
  %18 = zext i32 %17 to i64
  %19 = shl nuw nsw i64 %18, 2
  %20 = add nuw nsw i64 %19, 4
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(1) %scevgep52, ptr noundef nonnull align 4 dereferenceable(1) %scevgep53, i64 %20, i1 false), !tbaa !5
  br label %._crit_edge42

._crit_edge42:                                    ; preds = %.lr.ph41.preheader, %._crit_edge
  %21 = add i32 %2, 1
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiE2ar, i64 0, i64 %22
  store i32 2000000000, ptr %23, align 4, !tbaa !5
  %.not3543 = icmp sgt i32 %0, %2
  br i1 %.not3543, label %._crit_edge49, label %.lr.ph48.preheader

.lr.ph48.preheader:                               ; preds = %._crit_edge42
  %24 = sext i32 %0 to i64
  br label %.lr.ph48

._crit_edge49:                                    ; preds = %.lr.ph48, %._crit_edge42
  ret void

.lr.ph48:                                         ; preds = %.lr.ph48.preheader, %.lr.ph48
  %indvars.iv = phi i64 [ %24, %.lr.ph48.preheader ], [ %indvars.iv.next, %.lr.ph48 ]
  %.145 = phi i32 [ %10, %.lr.ph48.preheader ], [ %.2, %.lr.ph48 ]
  %.12844 = phi i32 [ %0, %.lr.ph48.preheader ], [ %.229, %.lr.ph48 ]
  %25 = sext i32 %.12844 to i64
  %26 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiE2al, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = sext i32 %.145 to i64
  %29 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiE2ar, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = icmp slt i32 %27, %30
  %.sink = tail call i32 @llvm.smin.i32(i32 %27, i32 %30)
  %32 = zext i1 %31 to i32
  %.229 = add nsw i32 %.12844, %32
  %not. = xor i1 %31, true
  %33 = zext i1 %not. to i32
  %.2 = add nsw i32 %.145, %33
  %34 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %indvars.iv
  store i32 %.sink, ptr %34, align 4
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  %exitcond.not = icmp eq i32 %21, %lftr.wideiv
  br i1 %exitcond.not, label %._crit_edge49, label %.lr.ph48, !llvm.loop !9
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #2

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) 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 nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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