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

; 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
@ans = external dso_local global i32, align 4
@_ZZ5mergeiiiiE2a1 = external hidden global [100000 x i32], align 16
@_ZZ5mergeiiiiE2a2 = external hidden global [100000 x i32], align 16

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

14:                                               ; preds = %29, %4
  %15 = load i32, ptr %11, align 4, !tbaa !5
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %19, label %18

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

19:                                               ; preds = %14
  %20 = load i32, ptr %11, align 4, !tbaa !5
  %21 = load i32, ptr %5, align 4, !tbaa !5
  %22 = add nsw i32 %20, %21
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = load i32, ptr %11, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiiE2a1, i64 0, i64 %27
  store i32 %25, ptr %28, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %19
  %30 = load i32, ptr %11, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr %11, align 4, !tbaa !5
  br label %14, !llvm.loop !9

32:                                               ; preds = %18
  %33 = load i32, ptr %6, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiiE2a1, i64 0, i64 %34
  store i32 1000000000, ptr %35, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  store i32 0, ptr %12, align 4, !tbaa !5
  br label %36

36:                                               ; preds = %51, %32
  %37 = load i32, ptr %12, align 4, !tbaa !5
  %38 = load i32, ptr %8, align 4, !tbaa !5
  %39 = icmp slt i32 %37, %38
  br i1 %39, label %41, label %40

40:                                               ; preds = %36
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %54

41:                                               ; preds = %36
  %42 = load i32, ptr %12, align 4, !tbaa !5
  %43 = load i32, ptr %7, align 4, !tbaa !5
  %44 = add nsw i32 %42, %43
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = load i32, ptr %12, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiiE2a2, i64 0, i64 %49
  store i32 %47, ptr %50, align 4, !tbaa !5
  br label %51

51:                                               ; preds = %41
  %52 = load i32, ptr %12, align 4, !tbaa !5
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr %12, align 4, !tbaa !5
  br label %36, !llvm.loop !11

54:                                               ; preds = %40
  %55 = load i32, ptr %8, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiiE2a2, i64 0, i64 %56
  store i32 1000000000, ptr %57, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  store i32 0, ptr %13, align 4, !tbaa !5
  br label %58

58:                                               ; preds = %103, %54
  %59 = load i32, ptr %13, align 4, !tbaa !5
  %60 = load i32, ptr %6, align 4, !tbaa !5
  %61 = load i32, ptr %8, align 4, !tbaa !5
  %62 = add nsw i32 %60, %61
  %63 = icmp slt i32 %59, %62
  br i1 %63, label %65, label %64

64:                                               ; preds = %58
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  br label %106

65:                                               ; preds = %58
  %66 = load i32, ptr %9, align 4, !tbaa !5
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiiE2a1, i64 0, i64 %67
  %69 = load i32, ptr %68, align 4, !tbaa !5
  %70 = load i32, ptr %10, align 4, !tbaa !5
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiiE2a2, i64 0, i64 %71
  %73 = load i32, ptr %72, align 4, !tbaa !5
  %74 = icmp sle i32 %69, %73
  br i1 %74, label %75, label %86

75:                                               ; preds = %65
  %76 = load i32, ptr %9, align 4, !tbaa !5
  %77 = add nsw i32 %76, 1
  store i32 %77, ptr %9, align 4, !tbaa !5
  %78 = sext i32 %76 to i64
  %79 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiiE2a1, i64 0, i64 %78
  %80 = load i32, ptr %79, align 4, !tbaa !5
  %81 = load i32, ptr %13, align 4, !tbaa !5
  %82 = load i32, ptr %5, align 4, !tbaa !5
  %83 = add nsw i32 %81, %82
  %84 = sext i32 %83 to i64
  %85 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %84
  store i32 %80, ptr %85, align 4, !tbaa !5
  br label %102

86:                                               ; preds = %65
  %87 = load i32, ptr %10, align 4, !tbaa !5
  %88 = add nsw i32 %87, 1
  store i32 %88, ptr %10, align 4, !tbaa !5
  %89 = sext i32 %87 to i64
  %90 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiiE2a2, i64 0, i64 %89
  %91 = load i32, ptr %90, align 4, !tbaa !5
  %92 = load i32, ptr %13, align 4, !tbaa !5
  %93 = load i32, ptr %5, align 4, !tbaa !5
  %94 = add nsw i32 %92, %93
  %95 = sext i32 %94 to i64
  %96 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %95
  store i32 %91, ptr %96, align 4, !tbaa !5
  %97 = load i32, ptr %6, align 4, !tbaa !5
  %98 = load i32, ptr %9, align 4, !tbaa !5
  %99 = sub nsw i32 %97, %98
  %100 = load i32, ptr @ans, align 4, !tbaa !5
  %101 = add nsw i32 %100, %99
  store i32 %101, ptr @ans, align 4, !tbaa !5
  br label %102

102:                                              ; preds = %86, %75
  br label %103

103:                                              ; preds = %102
  %104 = load i32, ptr %13, align 4, !tbaa !5
  %105 = add nsw i32 %104, 1
  store i32 %105, ptr %13, align 4, !tbaa !5
  br label %58, !llvm.loop !12

106:                                              ; preds = %64
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #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
@ans = external dso_local local_unnamed_addr global i32, align 4
@_ZZ5mergeiiiiE2a1 = external hidden local_unnamed_addr global [100000 x i32], align 16
@_ZZ5mergeiiiiE2a2 = 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 @_Z5mergeiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = icmp sgt i32 %1, 0
  br i1 %5, label %.lr.ph.preheader, label %._crit_edge

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

._crit_edge:                                      ; preds = %.lr.ph.preheader, %4
  %10 = sext i32 %1 to i64
  %11 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiiE2a1, i64 0, i64 %10
  store i32 1000000000, ptr %11, align 4, !tbaa !5
  %12 = icmp sgt i32 %3, 0
  br i1 %12, label %.lr.ph40.preheader, label %._crit_edge41

.lr.ph40.preheader:                               ; preds = %._crit_edge
  %13 = sext i32 %2 to i64
  %14 = shl nsw i64 %13, 2
  %scevgep49 = getelementptr i8, ptr @d, i64 %14
  %15 = zext nneg i32 %3 to i64
  %16 = shl nuw nsw i64 %15, 2
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 16 @_ZZ5mergeiiiiE2a2, ptr align 4 %scevgep49, i64 %16, i1 false), !tbaa !5
  br label %._crit_edge41

._crit_edge41:                                    ; preds = %.lr.ph40.preheader, %._crit_edge
  %17 = sext i32 %3 to i64
  %18 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiiE2a2, i64 0, i64 %17
  store i32 1000000000, ptr %18, align 4, !tbaa !5
  %19 = add nsw i32 %3, %1
  %20 = icmp sgt i32 %19, 0
  br i1 %20, label %.lr.ph46.preheader, label %._crit_edge47

.lr.ph46.preheader:                               ; preds = %._crit_edge41
  %ans.promoted = load i32, ptr @ans, align 4, !tbaa !5
  %21 = sext i32 %0 to i64
  %wide.trip.count = zext nneg i32 %19 to i64
  br label %.lr.ph46

._crit_edge47:                                    ; preds = %37, %._crit_edge41
  ret void

.lr.ph46:                                         ; preds = %.lr.ph46.preheader, %37
  %indvars.iv = phi i64 [ 0, %.lr.ph46.preheader ], [ %indvars.iv.next, %37 ]
  %.03043 = phi i32 [ 0, %.lr.ph46.preheader ], [ %.1, %37 ]
  %.03142 = phi i32 [ 0, %.lr.ph46.preheader ], [ %.132, %37 ]
  %22 = phi i32 [ %ans.promoted, %.lr.ph46.preheader ], [ %38, %37 ]
  %23 = sext i32 %.03142 to i64
  %24 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiiE2a1, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = sext i32 %.03043 to i64
  %27 = getelementptr inbounds [100000 x i32], ptr @_ZZ5mergeiiiiE2a2, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %.not = icmp sgt i32 %25, %28
  %29 = add nsw i64 %indvars.iv, %21
  %30 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %29
  br i1 %.not, label %33, label %31

31:                                               ; preds = %.lr.ph46
  %32 = add nsw i32 %.03142, 1
  store i32 %25, ptr %30, align 4, !tbaa !5
  br label %37

33:                                               ; preds = %.lr.ph46
  %34 = add nsw i32 %.03043, 1
  store i32 %28, ptr %30, align 4, !tbaa !5
  %35 = sub i32 %1, %.03142
  %36 = add nsw i32 %35, %22
  store i32 %36, ptr @ans, align 4, !tbaa !5
  br label %37

37:                                               ; preds = %31, %33
  %38 = phi i32 [ %22, %31 ], [ %36, %33 ]
  %.132 = phi i32 [ %32, %31 ], [ %.03142, %33 ]
  %.1 = phi i32 [ %.03043, %31 ], [ %34, %33 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge47, label %.lr.ph46, !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

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

!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,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass