; LLVM IR from chirag-singhal/Coding-Interview-101 :: Miscellaneous/Labs/Lab6 (recursive_merge_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"

%struct.student = type { [20 x i8], float }

@endmem = external dso_local global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @recursiveMergeSort(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %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 ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  %14 = alloca i64, align 8
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  %17 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  %18 = load i32, ptr %5, align 4, !tbaa !9
  %19 = load i32, ptr %6, align 4, !tbaa !9
  %20 = icmp sge i32 %18, %19
  br i1 %20, label %21, label %22

21:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  store ptr %7, ptr @endmem, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  br label %118

22:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  %23 = load i32, ptr %5, align 4, !tbaa !9
  %24 = load i32, ptr %6, align 4, !tbaa !9
  %25 = load i32, ptr %5, align 4, !tbaa !9
  %26 = sub nsw i32 %24, %25
  %27 = sdiv i32 %26, 2
  %28 = add nsw i32 %23, %27
  store i32 %28, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  %29 = load i32, ptr %8, align 4, !tbaa !9
  %30 = load i32, ptr %5, align 4, !tbaa !9
  %31 = sub nsw i32 %29, %30
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  %33 = load i32, ptr %6, align 4, !tbaa !9
  %34 = load i32, ptr %8, align 4, !tbaa !9
  %35 = sub nsw i32 %33, %34
  store i32 %35, ptr %10, align 4, !tbaa !9
  %36 = load i32, ptr %9, align 4, !tbaa !9
  %37 = zext i32 %36 to i64
  %38 = call ptr @llvm.stacksave.p0()
  store ptr %38, ptr %11, align 8
  %39 = alloca %struct.student, i64 %37, align 16
  store i64 %37, ptr %12, align 8
  %40 = load i32, ptr %10, align 4, !tbaa !9
  %41 = zext i32 %40 to i64
  %42 = alloca %struct.student, i64 %41, align 16
  store i64 %41, ptr %13, align 8
  %43 = load i32, ptr %9, align 4, !tbaa !9
  %44 = load i32, ptr %10, align 4, !tbaa !9
  %45 = add nsw i32 %43, %44
  %46 = zext i32 %45 to i64
  %47 = alloca %struct.student, i64 %46, align 16
  store i64 %46, ptr %14, align 8
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = load i32, ptr %5, align 4, !tbaa !9
  %50 = load i32, ptr %8, align 4, !tbaa !9
  call void @recursiveMergeSort(ptr noundef %48, i32 noundef %49, i32 noundef %50)
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = load i32, ptr %8, align 4, !tbaa !9
  %53 = add nsw i32 %52, 1
  %54 = load i32, ptr %6, align 4, !tbaa !9
  call void @recursiveMergeSort(ptr noundef %51, i32 noundef %53, i32 noundef %54)
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #5
  store i32 0, ptr %15, align 4, !tbaa !9
  br label %55

55:                                               ; preds = %70, %22
  %56 = load i32, ptr %15, align 4, !tbaa !9
  %57 = load i32, ptr %9, align 4, !tbaa !9
  %58 = icmp slt i32 %56, %57
  br i1 %58, label %60, label %59

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

60:                                               ; preds = %55
  %61 = load i32, ptr %15, align 4, !tbaa !9
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds %struct.student, ptr %39, i64 %62
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = load i32, ptr %5, align 4, !tbaa !9
  %66 = load i32, ptr %15, align 4, !tbaa !9
  %67 = add nsw i32 %65, %66
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds %struct.student, ptr %64, i64 %68
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %63, ptr align 4 %69, i64 24, i1 false), !tbaa.struct !11
  br label %70

70:                                               ; preds = %60
  %71 = load i32, ptr %15, align 4, !tbaa !9
  %72 = add nsw i32 %71, 1
  store i32 %72, ptr %15, align 4, !tbaa !9
  br label %55, !llvm.loop !15

73:                                               ; preds = %59
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #5
  store i32 0, ptr %16, align 4, !tbaa !9
  br label %74

74:                                               ; preds = %90, %73
  %75 = load i32, ptr %16, align 4, !tbaa !9
  %76 = load i32, ptr %10, align 4, !tbaa !9
  %77 = icmp slt i32 %75, %76
  br i1 %77, label %79, label %78

78:                                               ; preds = %74
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #5
  br label %93

79:                                               ; preds = %74
  %80 = load i32, ptr %16, align 4, !tbaa !9
  %81 = sext i32 %80 to i64
  %82 = getelementptr inbounds %struct.student, ptr %42, i64 %81
  %83 = load ptr, ptr %4, align 8, !tbaa !5
  %84 = load i32, ptr %8, align 4, !tbaa !9
  %85 = add nsw i32 %84, 1
  %86 = load i32, ptr %16, align 4, !tbaa !9
  %87 = add nsw i32 %85, %86
  %88 = sext i32 %87 to i64
  %89 = getelementptr inbounds %struct.student, ptr %83, i64 %88
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %82, ptr align 4 %89, i64 24, i1 false), !tbaa.struct !11
  br label %90

90:                                               ; preds = %79
  %91 = load i32, ptr %16, align 4, !tbaa !9
  %92 = add nsw i32 %91, 1
  store i32 %92, ptr %16, align 4, !tbaa !9
  br label %74, !llvm.loop !17

93:                                               ; preds = %78
  %94 = load i32, ptr %9, align 4, !tbaa !9
  %95 = load i32, ptr %10, align 4, !tbaa !9
  call void @merge(ptr noundef %39, i32 noundef %94, ptr noundef %42, i32 noundef %95, ptr noundef %47)
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #5
  store i32 0, ptr %17, align 4, !tbaa !9
  br label %96

96:                                               ; preds = %113, %93
  %97 = load i32, ptr %17, align 4, !tbaa !9
  %98 = load i32, ptr %9, align 4, !tbaa !9
  %99 = load i32, ptr %10, align 4, !tbaa !9
  %100 = add nsw i32 %98, %99
  %101 = icmp slt i32 %97, %100
  br i1 %101, label %103, label %102

102:                                              ; preds = %96
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #5
  br label %116

103:                                              ; preds = %96
  %104 = load ptr, ptr %4, align 8, !tbaa !5
  %105 = load i32, ptr %5, align 4, !tbaa !9
  %106 = load i32, ptr %17, align 4, !tbaa !9
  %107 = add nsw i32 %105, %106
  %108 = sext i32 %107 to i64
  %109 = getelementptr inbounds %struct.student, ptr %104, i64 %108
  %110 = load i32, ptr %17, align 4, !tbaa !9
  %111 = sext i32 %110 to i64
  %112 = getelementptr inbounds %struct.student, ptr %47, i64 %111
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %109, ptr align 8 %112, i64 24, i1 false), !tbaa.struct !11
  br label %113

113:                                              ; preds = %103
  %114 = load i32, ptr %17, align 4, !tbaa !9
  %115 = add nsw i32 %114, 1
  store i32 %115, ptr %17, align 4, !tbaa !9
  br label %96, !llvm.loop !18

116:                                              ; preds = %102
  %117 = load ptr, ptr %11, align 8
  call void @llvm.stackrestore.p0(ptr %117)
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  br label %118

118:                                              ; preds = %116, %21
  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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #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

declare void @merge(ptr noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef) #4

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #2

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

!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 = !{i64 0, i64 20, !12, i64 20, i64 4, !13}
!12 = !{!7, !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"float", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}
!18 = distinct !{!18, !16}


; 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.student = type { [20 x i8], float }

@endmem = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @recursiveMergeSort(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = alloca i32, align 4
  %.not = icmp slt i32 %1, %2
  br i1 %.not, label %6, label %5

5:                                                ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #5
  store ptr %4, ptr @endmem, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #5
  br label %36

6:                                                ; preds = %3
  %7 = sub nsw i32 %2, %1
  %8 = sdiv i32 %7, 2
  %9 = add nsw i32 %8, %1
  %10 = add nsw i32 %8, 1
  %11 = sub i32 %2, %9
  %12 = zext i32 %10 to i64
  %13 = tail call ptr @llvm.stacksave.p0()
  %14 = alloca %struct.student, i64 %12, align 16
  %15 = zext i32 %11 to i64
  %16 = alloca %struct.student, i64 %15, align 16
  %17 = add nsw i32 %11, %10
  %18 = zext i32 %17 to i64
  %19 = alloca %struct.student, i64 %18, align 16
  tail call void @recursiveMergeSort(ptr noundef %0, i32 noundef %1, i32 noundef %9)
  %20 = add nsw i32 %9, 1
  tail call void @recursiveMergeSort(ptr noundef %0, i32 noundef %20, i32 noundef %2)
  %.not4950 = icmp slt i32 %7, -1
  br i1 %.not4950, label %.preheader, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %6
  %21 = sext i32 %1 to i64
  %22 = mul nsw i64 %21, 24
  %scevgep = getelementptr i8, ptr %0, i64 %22
  %23 = mul nuw nsw i64 %12, 24
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 16 %14, ptr align 4 %scevgep, i64 %23, i1 false)
  br label %.preheader

.preheader:                                       ; preds = %.lr.ph.preheader, %6
  %24 = icmp sgt i32 %11, 0
  br i1 %24, label %.lr.ph53.preheader, label %._crit_edge

.lr.ph53.preheader:                               ; preds = %.preheader
  %25 = sext i32 %20 to i64
  %invariant.gep = getelementptr %struct.student, ptr %0, i64 %25
  %xtraiter = and i64 %15, 1
  %26 = icmp eq i32 %11, 1
  br i1 %26, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph53.preheader.new

.lr.ph53.preheader.new:                           ; preds = %.lr.ph53.preheader
  %unroll_iter = and i64 %15, 2147483646
  br label %.lr.ph53

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph53, %.lr.ph53.preheader
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph53.preheader ], [ %indvars.iv.next.1, %.lr.ph53 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph53.epil

.lr.ph53.epil:                                    ; preds = %._crit_edge.loopexit.unr-lcssa
  %27 = getelementptr inbounds %struct.student, ptr %16, i64 %indvars.iv.unr
  %gep.epil = getelementptr %struct.student, ptr %invariant.gep, i64 %indvars.iv.unr
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %27, ptr noundef nonnull align 4 dereferenceable(24) %gep.epil, i64 24, i1 false), !tbaa.struct !9
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph53.epil, %._crit_edge.loopexit.unr-lcssa, %.preheader
  call void @merge(ptr noundef nonnull %14, i32 noundef %10, ptr noundef nonnull %16, i32 noundef %11, ptr noundef nonnull %19) #5
  %28 = icmp sgt i32 %17, 0
  br i1 %28, label %.lr.ph56.preheader, label %._crit_edge57

.lr.ph56.preheader:                               ; preds = %._crit_edge
  %29 = sext i32 %1 to i64
  %30 = mul nsw i64 %29, 24
  %scevgep60 = getelementptr i8, ptr %0, i64 %30
  %31 = zext i32 %7 to i64
  %32 = mul nuw nsw i64 %31, 24
  %33 = add nuw nsw i64 %32, 24
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(1) %scevgep60, ptr noundef nonnull align 16 dereferenceable(1) %19, i64 %33, i1 false)
  br label %._crit_edge57

.lr.ph53:                                         ; preds = %.lr.ph53, %.lr.ph53.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph53.preheader.new ], [ %indvars.iv.next.1, %.lr.ph53 ]
  %niter = phi i64 [ 0, %.lr.ph53.preheader.new ], [ %niter.next.1, %.lr.ph53 ]
  %34 = getelementptr inbounds %struct.student, ptr %16, i64 %indvars.iv
  %gep = getelementptr %struct.student, ptr %invariant.gep, i64 %indvars.iv
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(24) %34, ptr noundef nonnull align 4 dereferenceable(24) %gep, i64 24, i1 false), !tbaa.struct !9
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %35 = getelementptr inbounds %struct.student, ptr %16, i64 %indvars.iv.next
  %gep.1 = getelementptr %struct.student, ptr %invariant.gep, i64 %indvars.iv.next
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %35, ptr noundef nonnull align 4 dereferenceable(24) %gep.1, i64 24, i1 false), !tbaa.struct !9
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph53, !llvm.loop !13

._crit_edge57:                                    ; preds = %.lr.ph56.preheader, %._crit_edge
  call void @llvm.stackrestore.p0(ptr %13)
  br label %36

36:                                               ; preds = %._crit_edge57, %5
  ret void
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

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

declare void @merge(ptr noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #4

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #2

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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nosync nounwind willreturn }
attributes #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { "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 }

!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 = !{i64 0, i64 20, !10, i64 20, i64 4, !11}
!10 = !{!7, !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"float", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass