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

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

14:                                               ; preds = %60, %5
  %15 = load i32, ptr %11, align 4, !tbaa !9
  %16 = load i32, ptr %7, align 4, !tbaa !9
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %18, label %22

18:                                               ; preds = %14
  %19 = load i32, ptr %12, align 4, !tbaa !9
  %20 = load i32, ptr %9, align 4, !tbaa !9
  %21 = icmp slt i32 %19, %20
  br label %22

22:                                               ; preds = %18, %14
  %23 = phi i1 [ false, %14 ], [ %21, %18 ]
  br i1 %23, label %24, label %61

24:                                               ; preds = %22
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = load i32, ptr %11, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds %struct.student, ptr %25, i64 %27
  %29 = getelementptr inbounds %struct.student, ptr %28, i32 0, i32 1
  %30 = load float, ptr %29, align 4, !tbaa !11
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = load i32, ptr %12, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds %struct.student, ptr %31, i64 %33
  %35 = getelementptr inbounds %struct.student, ptr %34, i32 0, i32 1
  %36 = load float, ptr %35, align 4, !tbaa !11
  %37 = fcmp olt float %30, %36
  br i1 %37, label %38, label %49

38:                                               ; preds = %24
  %39 = load ptr, ptr %10, align 8, !tbaa !5
  %40 = load i32, ptr %13, align 4, !tbaa !9
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %13, align 4, !tbaa !9
  %42 = sext i32 %40 to i64
  %43 = getelementptr inbounds %struct.student, ptr %39, i64 %42
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = load i32, ptr %11, align 4, !tbaa !9
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %11, align 4, !tbaa !9
  %47 = sext i32 %45 to i64
  %48 = getelementptr inbounds %struct.student, ptr %44, i64 %47
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %43, ptr align 4 %48, i64 24, i1 false), !tbaa.struct !14
  br label %60

49:                                               ; preds = %24
  %50 = load ptr, ptr %10, align 8, !tbaa !5
  %51 = load i32, ptr %13, align 4, !tbaa !9
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %13, align 4, !tbaa !9
  %53 = sext i32 %51 to i64
  %54 = getelementptr inbounds %struct.student, ptr %50, i64 %53
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  %56 = load i32, ptr %12, align 4, !tbaa !9
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %12, align 4, !tbaa !9
  %58 = sext i32 %56 to i64
  %59 = getelementptr inbounds %struct.student, ptr %55, i64 %58
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %54, ptr align 4 %59, i64 24, i1 false), !tbaa.struct !14
  br label %60

60:                                               ; preds = %49, %38
  br label %14, !llvm.loop !17

61:                                               ; preds = %22
  br label %62

62:                                               ; preds = %66, %61
  %63 = load i32, ptr %11, align 4, !tbaa !9
  %64 = load i32, ptr %7, align 4, !tbaa !9
  %65 = icmp slt i32 %63, %64
  br i1 %65, label %66, label %77

66:                                               ; preds = %62
  %67 = load ptr, ptr %10, align 8, !tbaa !5
  %68 = load i32, ptr %13, align 4, !tbaa !9
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %13, align 4, !tbaa !9
  %70 = sext i32 %68 to i64
  %71 = getelementptr inbounds %struct.student, ptr %67, i64 %70
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  %73 = load i32, ptr %11, align 4, !tbaa !9
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %11, align 4, !tbaa !9
  %75 = sext i32 %73 to i64
  %76 = getelementptr inbounds %struct.student, ptr %72, i64 %75
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %71, ptr align 4 %76, i64 24, i1 false), !tbaa.struct !14
  br label %62, !llvm.loop !19

77:                                               ; preds = %62
  br label %78

78:                                               ; preds = %82, %77
  %79 = load i32, ptr %12, align 4, !tbaa !9
  %80 = load i32, ptr %9, align 4, !tbaa !9
  %81 = icmp slt i32 %79, %80
  br i1 %81, label %82, label %93

82:                                               ; preds = %78
  %83 = load ptr, ptr %10, align 8, !tbaa !5
  %84 = load i32, ptr %13, align 4, !tbaa !9
  %85 = add nsw i32 %84, 1
  store i32 %85, ptr %13, align 4, !tbaa !9
  %86 = sext i32 %84 to i64
  %87 = getelementptr inbounds %struct.student, ptr %83, i64 %86
  %88 = load ptr, ptr %8, align 8, !tbaa !5
  %89 = load i32, ptr %12, align 4, !tbaa !9
  %90 = add nsw i32 %89, 1
  store i32 %90, ptr %12, align 4, !tbaa !9
  %91 = sext i32 %89 to i64
  %92 = getelementptr inbounds %struct.student, ptr %88, i64 %91
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %87, ptr align 4 %92, i64 24, i1 false), !tbaa.struct !14
  br label %78, !llvm.loop !20

93:                                               ; preds = %78
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !13, i64 20}
!12 = !{!"student", !7, i64 0, !13, i64 20}
!13 = !{!"float", !7, i64 0}
!14 = !{i64 0, i64 20, !15, i64 20, i64 4, !16}
!15 = !{!7, !7, i64 0}
!16 = !{!13, !13, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}
!20 = distinct !{!20, !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.student = type { [20 x i8], float }

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @merge(ptr nocapture noundef readonly %0, i32 noundef %1, ptr nocapture noundef readonly %2, i32 noundef %3, ptr nocapture noundef writeonly %4) local_unnamed_addr #0 {
  %6 = icmp sgt i32 %1, 0
  %7 = icmp sgt i32 %3, 0
  %8 = and i1 %6, %7
  br i1 %8, label %.lr.ph, label %.preheader33

.preheader33.loopexit:                            ; preds = %.lr.ph
  %9 = trunc nuw i64 %indvars.iv.next to i32
  br label %.preheader33

.preheader33:                                     ; preds = %.preheader33.loopexit, %5
  %.030.lcssa = phi i32 [ 0, %5 ], [ %.131, %.preheader33.loopexit ]
  %.027.lcssa = phi i32 [ 0, %5 ], [ %.128, %.preheader33.loopexit ]
  %.0.lcssa = phi i32 [ 0, %5 ], [ %9, %.preheader33.loopexit ]
  %10 = icmp slt i32 %.030.lcssa, %1
  br i1 %10, label %.lr.ph41.preheader, label %.preheader

.lr.ph41.preheader:                               ; preds = %.preheader33
  %11 = zext i32 %.0.lcssa to i64
  %12 = sext i32 %.030.lcssa to i64
  %wide.trip.count = sext i32 %1 to i64
  %13 = sub nsw i64 %wide.trip.count, %12
  %xtraiter = and i64 %13, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph41.prol.loopexit, label %.lr.ph41.prol

.lr.ph41.prol:                                    ; preds = %.lr.ph41.preheader
  %indvars.iv.next49.prol = add nuw nsw i64 %11, 1
  %14 = getelementptr inbounds %struct.student, ptr %4, i64 %11
  %indvars.iv.next51.prol = add nsw i64 %12, 1
  %15 = getelementptr inbounds %struct.student, ptr %0, i64 %12
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(24) %14, ptr noundef nonnull align 4 dereferenceable(24) %15, i64 24, i1 false), !tbaa.struct !5
  br label %.lr.ph41.prol.loopexit

.lr.ph41.prol.loopexit:                           ; preds = %.lr.ph41.prol, %.lr.ph41.preheader
  %indvars.iv.next49.lcssa.unr = phi i64 [ poison, %.lr.ph41.preheader ], [ %indvars.iv.next49.prol, %.lr.ph41.prol ]
  %indvars.iv50.unr = phi i64 [ %12, %.lr.ph41.preheader ], [ %indvars.iv.next51.prol, %.lr.ph41.prol ]
  %indvars.iv48.unr = phi i64 [ %11, %.lr.ph41.preheader ], [ %indvars.iv.next49.prol, %.lr.ph41.prol ]
  %16 = add nsw i64 %wide.trip.count, -1
  %17 = icmp eq i64 %16, %12
  br i1 %17, label %.preheader.loopexit, label %.lr.ph41.preheader.new

.lr.ph41.preheader.new:                           ; preds = %.lr.ph41.prol.loopexit
  %invariant.gep = getelementptr i8, ptr %4, i64 24
  %invariant.gep67 = getelementptr i8, ptr %0, i64 24
  br label %.lr.ph41

.lr.ph:                                           ; preds = %5, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %5 ]
  %.02735 = phi i32 [ %.128, %.lr.ph ], [ 0, %5 ]
  %.03034 = phi i32 [ %.131, %.lr.ph ], [ 0, %5 ]
  %18 = zext nneg i32 %.03034 to i64
  %19 = getelementptr inbounds %struct.student, ptr %0, i64 %18
  %20 = getelementptr inbounds i8, ptr %19, i64 20
  %21 = load float, ptr %20, align 4, !tbaa !11
  %22 = zext nneg i32 %.02735 to i64
  %23 = getelementptr inbounds %struct.student, ptr %2, i64 %22
  %24 = getelementptr inbounds i8, ptr %23, i64 20
  %25 = load float, ptr %24, align 4, !tbaa !11
  %26 = fcmp olt float %21, %25
  %27 = getelementptr inbounds %struct.student, ptr %4, i64 %indvars.iv
  %.sink = select i1 %26, ptr %19, ptr %23
  %28 = zext i1 %26 to i32
  %.131 = add nuw nsw i32 %.03034, %28
  %not. = xor i1 %26, true
  %29 = zext i1 %not. to i32
  %.128 = add nuw nsw i32 %.02735, %29
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(24) %27, ptr noundef nonnull align 4 dereferenceable(24) %.sink, i64 24, i1 false)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %30 = icmp slt i32 %.131, %1
  %31 = icmp slt i32 %.128, %3
  %32 = select i1 %30, i1 %31, i1 false
  br i1 %32, label %.lr.ph, label %.preheader33.loopexit, !llvm.loop !13

.preheader.loopexit:                              ; preds = %.lr.ph41, %.lr.ph41.prol.loopexit
  %indvars.iv.next49.lcssa = phi i64 [ %indvars.iv.next49.lcssa.unr, %.lr.ph41.prol.loopexit ], [ %indvars.iv.next49.1, %.lr.ph41 ]
  %33 = trunc nuw i64 %indvars.iv.next49.lcssa to i32
  br label %.preheader

.preheader:                                       ; preds = %.preheader.loopexit, %.preheader33
  %.2.lcssa = phi i32 [ %.0.lcssa, %.preheader33 ], [ %33, %.preheader.loopexit ]
  %34 = icmp slt i32 %.027.lcssa, %3
  br i1 %34, label %.lr.ph45.preheader, label %._crit_edge

.lr.ph45.preheader:                               ; preds = %.preheader
  %35 = zext i32 %.2.lcssa to i64
  %36 = sext i32 %.027.lcssa to i64
  %wide.trip.count63 = sext i32 %3 to i64
  %37 = sub nsw i64 %wide.trip.count63, %36
  %xtraiter65 = and i64 %37, 1
  %lcmp.mod66.not = icmp eq i64 %xtraiter65, 0
  br i1 %lcmp.mod66.not, label %.lr.ph45.prol.loopexit, label %.lr.ph45.prol

.lr.ph45.prol:                                    ; preds = %.lr.ph45.preheader
  %indvars.iv.next57.prol = add nuw nsw i64 %35, 1
  %38 = getelementptr inbounds %struct.student, ptr %4, i64 %35
  %indvars.iv.next59.prol = add nsw i64 %36, 1
  %39 = getelementptr inbounds %struct.student, ptr %2, i64 %36
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(24) %38, ptr noundef nonnull align 4 dereferenceable(24) %39, i64 24, i1 false), !tbaa.struct !5
  br label %.lr.ph45.prol.loopexit

.lr.ph45.prol.loopexit:                           ; preds = %.lr.ph45.prol, %.lr.ph45.preheader
  %indvars.iv58.unr = phi i64 [ %36, %.lr.ph45.preheader ], [ %indvars.iv.next59.prol, %.lr.ph45.prol ]
  %indvars.iv56.unr = phi i64 [ %35, %.lr.ph45.preheader ], [ %indvars.iv.next57.prol, %.lr.ph45.prol ]
  %40 = add nsw i64 %wide.trip.count63, -1
  %41 = icmp eq i64 %40, %36
  br i1 %41, label %._crit_edge, label %.lr.ph45.preheader.new

.lr.ph45.preheader.new:                           ; preds = %.lr.ph45.prol.loopexit
  %invariant.gep69 = getelementptr i8, ptr %4, i64 24
  %invariant.gep71 = getelementptr i8, ptr %2, i64 24
  br label %.lr.ph45

.lr.ph41:                                         ; preds = %.lr.ph41, %.lr.ph41.preheader.new
  %indvars.iv50 = phi i64 [ %indvars.iv50.unr, %.lr.ph41.preheader.new ], [ %indvars.iv.next51.1, %.lr.ph41 ]
  %indvars.iv48 = phi i64 [ %indvars.iv48.unr, %.lr.ph41.preheader.new ], [ %indvars.iv.next49.1, %.lr.ph41 ]
  %42 = getelementptr inbounds %struct.student, ptr %4, i64 %indvars.iv48
  %43 = getelementptr inbounds %struct.student, ptr %0, i64 %indvars.iv50
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(24) %42, ptr noundef nonnull align 4 dereferenceable(24) %43, i64 24, i1 false), !tbaa.struct !5
  %indvars.iv.next49.1 = add nuw nsw i64 %indvars.iv48, 2
  %gep = getelementptr %struct.student, ptr %invariant.gep, i64 %indvars.iv48
  %indvars.iv.next51.1 = add nsw i64 %indvars.iv50, 2
  %gep68 = getelementptr %struct.student, ptr %invariant.gep67, i64 %indvars.iv50
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(24) %gep, ptr noundef nonnull align 4 dereferenceable(24) %gep68, i64 24, i1 false), !tbaa.struct !5
  %exitcond.not.1 = icmp eq i64 %indvars.iv.next51.1, %wide.trip.count
  br i1 %exitcond.not.1, label %.preheader.loopexit, label %.lr.ph41, !llvm.loop !15

.lr.ph45:                                         ; preds = %.lr.ph45, %.lr.ph45.preheader.new
  %indvars.iv58 = phi i64 [ %indvars.iv58.unr, %.lr.ph45.preheader.new ], [ %indvars.iv.next59.1, %.lr.ph45 ]
  %indvars.iv56 = phi i64 [ %indvars.iv56.unr, %.lr.ph45.preheader.new ], [ %indvars.iv.next57.1, %.lr.ph45 ]
  %44 = getelementptr inbounds %struct.student, ptr %4, i64 %indvars.iv56
  %45 = getelementptr inbounds %struct.student, ptr %2, i64 %indvars.iv58
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(24) %44, ptr noundef nonnull align 4 dereferenceable(24) %45, i64 24, i1 false), !tbaa.struct !5
  %indvars.iv.next57.1 = add nuw nsw i64 %indvars.iv56, 2
  %gep70 = getelementptr %struct.student, ptr %invariant.gep69, i64 %indvars.iv56
  %indvars.iv.next59.1 = add nsw i64 %indvars.iv58, 2
  %gep72 = getelementptr %struct.student, ptr %invariant.gep71, i64 %indvars.iv58
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(24) %gep70, ptr noundef nonnull align 4 dereferenceable(24) %gep72, i64 24, i1 false), !tbaa.struct !5
  %exitcond64.not.1 = icmp eq i64 %indvars.iv.next59.1, %wide.trip.count63
  br i1 %exitcond64.not.1, label %._crit_edge, label %.lr.ph45, !llvm.loop !16

._crit_edge:                                      ; preds = %.lr.ph45.prol.loopexit, %.lr.ph45, %.preheader
  ret void
}

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

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, 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 = { mustprogress 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 = !{i64 0, i64 20, !6, i64 20, i64 4, !9}
!6 = !{!7, !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"float", !7, i64 0}
!11 = !{!12, !10, i64 20}
!12 = !{!"student", !7, i64 0, !10, i64 20}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !14}


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