; LLVM IR from chirag-singhal/Coding-Interview-101 :: Miscellaneous/Labs/Lab6 (iterative_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 @mergeSort(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %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
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  %17 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  store i32 1, ptr %5, align 4, !tbaa !9
  br label %18

18:                                               ; preds = %126, %2
  %19 = load i32, ptr %5, align 4, !tbaa !9
  %20 = load i32, ptr %4, align 4, !tbaa !9
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %18
  store i32 2, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  br label %129

23:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %24

24:                                               ; preds = %120, %23
  %25 = load i32, ptr %7, align 4, !tbaa !9
  %26 = load i32, ptr %4, align 4, !tbaa !9
  %27 = icmp slt i32 %25, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %24
  store i32 5, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  br label %125

29:                                               ; preds = %24
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = load i32, ptr %5, align 4, !tbaa !9
  %32 = mul nsw i32 2, %31
  %33 = add nsw i32 %30, %32
  %34 = load i32, ptr %4, align 4, !tbaa !9
  %35 = icmp sgt i32 %33, %34
  br i1 %35, label %36, label %37

36:                                               ; preds = %29
  br label %120

37:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  %38 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %38, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  %39 = load i32, ptr %5, align 4, !tbaa !9
  %40 = load i32, ptr %4, align 4, !tbaa !9
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = sub nsw i32 %40, %41
  %43 = call i32 @min(i32 noundef %39, i32 noundef %42)
  store i32 %43, ptr %9, align 4, !tbaa !9
  %44 = load i32, ptr %8, align 4, !tbaa !9
  %45 = zext i32 %44 to i64
  %46 = call ptr @llvm.stacksave.p0()
  store ptr %46, ptr %10, align 8
  %47 = alloca %struct.student, i64 %45, align 16
  store i64 %45, ptr %11, align 8
  %48 = load i32, ptr %9, align 4, !tbaa !9
  %49 = zext i32 %48 to i64
  %50 = alloca %struct.student, i64 %49, align 16
  store i64 %49, ptr %12, align 8
  %51 = load i32, ptr %8, align 4, !tbaa !9
  %52 = load i32, ptr %9, align 4, !tbaa !9
  %53 = add nsw i32 %51, %52
  %54 = zext i32 %53 to i64
  %55 = alloca %struct.student, i64 %54, align 16
  store i64 %54, ptr %13, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #5
  store i32 0, ptr %14, align 4, !tbaa !9
  br label %56

56:                                               ; preds = %71, %37
  %57 = load i32, ptr %14, align 4, !tbaa !9
  %58 = load i32, ptr %5, align 4, !tbaa !9
  %59 = icmp slt i32 %57, %58
  br i1 %59, label %61, label %60

60:                                               ; preds = %56
  store i32 8, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #5
  br label %74

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

71:                                               ; preds = %61
  %72 = load i32, ptr %14, align 4, !tbaa !9
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %14, align 4, !tbaa !9
  br label %56, !llvm.loop !15

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

75:                                               ; preds = %92, %74
  %76 = load i32, ptr %15, align 4, !tbaa !9
  %77 = load i32, ptr %9, align 4, !tbaa !9
  %78 = icmp slt i32 %76, %77
  br i1 %78, label %80, label %79

79:                                               ; preds = %75
  store i32 11, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #5
  br label %95

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

92:                                               ; preds = %80
  %93 = load i32, ptr %15, align 4, !tbaa !9
  %94 = add nsw i32 %93, 1
  store i32 %94, ptr %15, align 4, !tbaa !9
  br label %75, !llvm.loop !17

95:                                               ; preds = %79
  %96 = load i32, ptr %8, align 4, !tbaa !9
  %97 = load i32, ptr %9, align 4, !tbaa !9
  call void @merge(ptr noundef %47, i32 noundef %96, ptr noundef %50, i32 noundef %97, ptr noundef %55)
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #5
  store i32 0, ptr %16, align 4, !tbaa !9
  br label %98

98:                                               ; preds = %115, %95
  %99 = load i32, ptr %16, align 4, !tbaa !9
  %100 = load i32, ptr %8, align 4, !tbaa !9
  %101 = load i32, ptr %9, align 4, !tbaa !9
  %102 = add nsw i32 %100, %101
  %103 = icmp slt i32 %99, %102
  br i1 %103, label %105, label %104

104:                                              ; preds = %98
  store i32 14, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #5
  br label %118

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

115:                                              ; preds = %105
  %116 = load i32, ptr %16, align 4, !tbaa !9
  %117 = add nsw i32 %116, 1
  store i32 %117, ptr %16, align 4, !tbaa !9
  br label %98, !llvm.loop !18

118:                                              ; preds = %104
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #5
  store ptr %17, ptr @endmem, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #5
  %119 = load ptr, ptr %10, align 8
  call void @llvm.stackrestore.p0(ptr %119)
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  br label %120

120:                                              ; preds = %118, %36
  %121 = load i32, ptr %5, align 4, !tbaa !9
  %122 = mul nsw i32 2, %121
  %123 = load i32, ptr %7, align 4, !tbaa !9
  %124 = add nsw i32 %123, %122
  store i32 %124, ptr %7, align 4, !tbaa !9
  br label %24, !llvm.loop !19

125:                                              ; preds = %28
  br label %126

126:                                              ; preds = %125
  %127 = load i32, ptr %5, align 4, !tbaa !9
  %128 = mul nsw i32 %127, 2
  store i32 %128, ptr %5, align 4, !tbaa !9
  br label %18, !llvm.loop !20

129:                                              ; preds = %22
  ret void
}

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

declare i32 @min(i32 noundef, i32 noundef) #2

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

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

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 nosync nounwind willreturn }
attributes #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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}
!19 = distinct !{!19, !16}
!20 = distinct !{!20, !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 @mergeSort(ptr nocapture noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = alloca i32, align 4
  %4 = icmp sgt i32 %1, 1
  br i1 %4, label %.preheader49.us, label %._crit_edge61

.preheader49.us:                                  ; preds = %2, %._crit_edge59.us
  %.04560.us = phi i32 [ %5, %._crit_edge59.us ], [ 1, %2 ]
  %5 = shl nsw i32 %.04560.us, 1
  %6 = zext i32 %.04560.us to i64
  %7 = icmp sgt i32 %.04560.us, 0
  %8 = sext i32 %5 to i64
  %9 = mul nsw i64 %8, 24
  %10 = mul nuw nsw i64 %6, 24
  %11 = add i32 %.04560.us, -1
  br label %12

12:                                               ; preds = %.preheader49.us, %36
  %indvar = phi i64 [ 0, %.preheader49.us ], [ %indvar.next, %36 ]
  %.04657.us = phi i32 [ 0, %.preheader49.us ], [ %19, %36 ]
  %13 = mul i64 %9, %indvar
  %scevgep65 = getelementptr i8, ptr %0, i64 %13
  %14 = trunc i64 %indvar to i32
  %15 = mul i32 %5, %14
  %16 = add i32 %15, %.04560.us
  %17 = sext i32 %16 to i64
  %18 = mul nsw i64 %17, 24
  %scevgep63 = getelementptr i8, ptr %0, i64 %18
  %19 = add nsw i32 %.04657.us, %5
  %20 = icmp sgt i32 %19, %1
  br i1 %20, label %36, label %21

21:                                               ; preds = %12
  %22 = sub nsw i32 %1, %.04657.us
  %23 = call i32 @min(i32 noundef %.04560.us, i32 noundef %22) #5
  %24 = call ptr @llvm.stacksave.p0()
  %25 = alloca %struct.student, i64 %6, align 16
  %26 = zext i32 %23 to i64
  %27 = alloca %struct.student, i64 %26, align 16
  %28 = add nsw i32 %23, %.04560.us
  %29 = zext i32 %28 to i64
  %30 = alloca %struct.student, i64 %29, align 16
  br i1 %7, label %.lr.ph.us.preheader, label %.preheader.us

.lr.ph.us.preheader:                              ; preds = %21
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(1) %25, ptr noundef nonnull align 4 dereferenceable(1) %scevgep65, i64 %10, i1 false)
  br label %.preheader.us

._crit_edge.us:                                   ; preds = %.lr.ph52.us, %.preheader.us
  call void @merge(ptr noundef nonnull %25, i32 noundef %.04560.us, ptr noundef nonnull %27, i32 noundef %23, ptr noundef nonnull %30) #5
  %31 = icmp sgt i32 %28, 0
  br i1 %31, label %.lr.ph55.us.preheader, label %._crit_edge56.us

.lr.ph55.us.preheader:                            ; preds = %._crit_edge.us
  %32 = add i32 %11, %23
  %33 = zext i32 %32 to i64
  %34 = mul nuw nsw i64 %33, 24
  %35 = add nuw nsw i64 %34, 24
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(1) %scevgep65, ptr noundef nonnull align 16 dereferenceable(1) %30, i64 %35, i1 false)
  br label %._crit_edge56.us

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

36:                                               ; preds = %._crit_edge56.us, %12
  %37 = icmp slt i32 %19, %1
  %indvar.next = add i64 %indvar, 1
  br i1 %37, label %12, label %._crit_edge59.us, !llvm.loop !9

.preheader.us:                                    ; preds = %.lr.ph.us.preheader, %21
  %38 = icmp sgt i32 %23, 0
  br i1 %38, label %.lr.ph52.us, label %._crit_edge.us

.lr.ph52.us:                                      ; preds = %.preheader.us
  %39 = mul nuw nsw i64 %26, 24
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 16 %27, ptr align 4 %scevgep63, i64 %39, i1 false)
  br label %._crit_edge.us

._crit_edge59.us:                                 ; preds = %36
  %40 = icmp slt i32 %5, %1
  br i1 %40, label %.preheader49.us, label %._crit_edge61, !llvm.loop !11

._crit_edge61:                                    ; preds = %._crit_edge59.us, %2
  ret void
}

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

declare i32 @min(i32 noundef, i32 noundef) local_unnamed_addr #2

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

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

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

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

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = { mustprogress nocallback nofree nosync nounwind willreturn }
attributes #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass