; LLVM IR from chirag-singhal/Coding-Interview-101 :: Miscellaneous/Labs/Lab1/AssignmentSolution (seqListOps)

; 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.LOCATION = type { %struct.Job, i32 }
%struct.Job = type { i32, i32, i32, i32 }
%struct.seqlist = type { i32, i32 }

@st = external dso_local global [10 x %struct.LOCATION], align 16
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

; 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 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 willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

declare i32 @printf(ptr noundef, ...) #2

; Function Attrs: nounwind uwtable
define dso_local void @copy_sorted_ele(ptr noundef %0, ptr noundef %1) #3 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !9
  %8 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  store i32 2, ptr %5, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %58, %2
  %10 = load i32, ptr %5, align 4, !tbaa !9
  %11 = icmp sge i32 %10, 0
  br i1 %11, label %12, label %61

12:                                               ; preds = %9
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = load i32, ptr %5, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds %struct.seqlist, ptr %13, i64 %15
  %17 = getelementptr inbounds %struct.seqlist, ptr %16, i32 0, i32 1
  %18 = load i32, ptr %17, align 4, !tbaa !11
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %19
  %21 = getelementptr inbounds %struct.LOCATION, ptr %20, i32 0, i32 1
  %22 = load i32, ptr %21, align 4, !tbaa !13
  store i32 %22, ptr %6, align 4, !tbaa !9
  br label %23

23:                                               ; preds = %41, %12
  %24 = load i32, ptr %6, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %25
  %27 = getelementptr inbounds %struct.LOCATION, ptr %26, i32 0, i32 1
  %28 = load i32, ptr %27, align 4, !tbaa !13
  %29 = icmp ne i32 %28, -1
  br i1 %29, label %30, label %47

30:                                               ; preds = %23
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = load i32, ptr %7, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds %struct.Job, ptr %31, i64 %33
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %36
  %38 = getelementptr inbounds %struct.LOCATION, ptr %37, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %34, ptr align 4 %38, i64 16, i1 false), !tbaa.struct !16
  %39 = load i32, ptr %7, align 4, !tbaa !9
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %7, align 4, !tbaa !9
  br label %41

41:                                               ; preds = %30
  %42 = load i32, ptr %6, align 4, !tbaa !9
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %43
  %45 = getelementptr inbounds %struct.LOCATION, ptr %44, i32 0, i32 1
  %46 = load i32, ptr %45, align 4, !tbaa !13
  store i32 %46, ptr %6, align 4, !tbaa !9
  br label %23, !llvm.loop !17

47:                                               ; preds = %23
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = load i32, ptr %7, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds %struct.Job, ptr %48, i64 %50
  %52 = load i32, ptr %6, align 4, !tbaa !9
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %53
  %55 = getelementptr inbounds %struct.LOCATION, ptr %54, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %51, ptr align 4 %55, i64 16, i1 false), !tbaa.struct !16
  %56 = load i32, ptr %7, align 4, !tbaa !9
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %7, align 4, !tbaa !9
  br label %58

58:                                               ; preds = %47
  %59 = load i32, ptr %5, align 4, !tbaa !9
  %60 = add nsw i32 %59, -1
  store i32 %60, ptr %5, align 4, !tbaa !9
  br label %9, !llvm.loop !19

61:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree 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 = { 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 #4 = { 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, !10, i64 4}
!12 = !{!"", !10, i64 0, !10, i64 4}
!13 = !{!14, !10, i64 16}
!14 = !{!"", !15, i64 0, !10, i64 16}
!15 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12}
!16 = !{i64 0, i64 4, !9, i64 4, i64 4, !9, i64 8, i64 4, !9, i64 12, i64 4, !9}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !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.LOCATION = type { %struct.Job, i32 }
%struct.Job = type { i32, i32, i32, i32 }

@st = external dso_local local_unnamed_addr global [10 x %struct.LOCATION], align 16
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

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

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nofree nounwind uwtable
define dso_local void @copy_sorted_ele(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #2 {
  %3 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  %4 = getelementptr inbounds i8, ptr %0, i64 20
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %6, i32 1
  %.01315 = load i32, ptr %7, align 4, !tbaa !10
  %8 = sext i32 %.01315 to i64
  %9 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %8
  %10 = getelementptr inbounds i8, ptr %9, i64 16
  %11 = load i32, ptr %10, align 4, !tbaa !10
  %.not16 = icmp eq i32 %11, -1
  br i1 %.not16, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %2 ]
  %12 = phi ptr [ %17, %.lr.ph ], [ %10, %2 ]
  %13 = phi ptr [ %16, %.lr.ph ], [ %9, %2 ]
  %14 = getelementptr inbounds %struct.Job, ptr %1, i64 %indvars.iv
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(16) %14, ptr noundef nonnull align 4 dereferenceable(16) %13, i64 16, i1 false), !tbaa.struct !13
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %.013 = load i32, ptr %12, align 4, !tbaa !10
  %15 = sext i32 %.013 to i64
  %16 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %15
  %17 = getelementptr inbounds i8, ptr %16, i64 16
  %18 = load i32, ptr %17, align 4, !tbaa !10
  %.not = icmp eq i32 %18, -1
  br i1 %.not, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !15

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %19 = trunc nsw i64 %indvars.iv.next to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %2
  %.1.lcssa = phi i32 [ 0, %2 ], [ %19, %._crit_edge.loopexit ]
  %.lcssa = phi ptr [ %9, %2 ], [ %16, %._crit_edge.loopexit ]
  %20 = sext i32 %.1.lcssa to i64
  %21 = getelementptr inbounds %struct.Job, ptr %1, i64 %20
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(16) %21, ptr noundef nonnull align 4 dereferenceable(16) %.lcssa, i64 16, i1 false), !tbaa.struct !13
  %22 = add nsw i32 %.1.lcssa, 1
  %23 = getelementptr inbounds i8, ptr %0, i64 12
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %25, i32 1
  %.01315.1 = load i32, ptr %26, align 4, !tbaa !10
  %27 = sext i32 %.01315.1 to i64
  %28 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %27
  %29 = getelementptr inbounds i8, ptr %28, i64 16
  %30 = load i32, ptr %29, align 4, !tbaa !10
  %.not16.1 = icmp eq i32 %30, -1
  br i1 %.not16.1, label %._crit_edge.1, label %.lr.ph.preheader.1

.lr.ph.preheader.1:                               ; preds = %._crit_edge
  %31 = sext i32 %22 to i64
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %.lr.ph.1, %.lr.ph.preheader.1
  %indvars.iv.1 = phi i64 [ %31, %.lr.ph.preheader.1 ], [ %indvars.iv.next.1, %.lr.ph.1 ]
  %32 = phi ptr [ %29, %.lr.ph.preheader.1 ], [ %37, %.lr.ph.1 ]
  %33 = phi ptr [ %28, %.lr.ph.preheader.1 ], [ %36, %.lr.ph.1 ]
  %34 = getelementptr inbounds %struct.Job, ptr %1, i64 %indvars.iv.1
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(16) %34, ptr noundef nonnull align 4 dereferenceable(16) %33, i64 16, i1 false), !tbaa.struct !13
  %indvars.iv.next.1 = add nsw i64 %indvars.iv.1, 1
  %.013.1 = load i32, ptr %32, align 4, !tbaa !10
  %35 = sext i32 %.013.1 to i64
  %36 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %35
  %37 = getelementptr inbounds i8, ptr %36, i64 16
  %38 = load i32, ptr %37, align 4, !tbaa !10
  %.not.1 = icmp eq i32 %38, -1
  br i1 %.not.1, label %._crit_edge.loopexit.1, label %.lr.ph.1, !llvm.loop !15

._crit_edge.loopexit.1:                           ; preds = %.lr.ph.1
  %39 = trunc nsw i64 %indvars.iv.next.1 to i32
  br label %._crit_edge.1

._crit_edge.1:                                    ; preds = %._crit_edge.loopexit.1, %._crit_edge
  %.1.lcssa.1 = phi i32 [ %22, %._crit_edge ], [ %39, %._crit_edge.loopexit.1 ]
  %.lcssa.1 = phi ptr [ %28, %._crit_edge ], [ %36, %._crit_edge.loopexit.1 ]
  %40 = sext i32 %.1.lcssa.1 to i64
  %41 = getelementptr inbounds %struct.Job, ptr %1, i64 %40
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(16) %41, ptr noundef nonnull align 4 dereferenceable(16) %.lcssa.1, i64 16, i1 false), !tbaa.struct !13
  %42 = add nsw i32 %.1.lcssa.1, 1
  %43 = getelementptr inbounds i8, ptr %0, i64 4
  %44 = load i32, ptr %43, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %45, i32 1
  %.01315.2 = load i32, ptr %46, align 4, !tbaa !10
  %47 = sext i32 %.01315.2 to i64
  %48 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %47
  %49 = getelementptr inbounds i8, ptr %48, i64 16
  %50 = load i32, ptr %49, align 4, !tbaa !10
  %.not16.2 = icmp eq i32 %50, -1
  br i1 %.not16.2, label %._crit_edge.2, label %.lr.ph.preheader.2

.lr.ph.preheader.2:                               ; preds = %._crit_edge.1
  %51 = sext i32 %42 to i64
  br label %.lr.ph.2

.lr.ph.2:                                         ; preds = %.lr.ph.2, %.lr.ph.preheader.2
  %indvars.iv.2 = phi i64 [ %51, %.lr.ph.preheader.2 ], [ %indvars.iv.next.2, %.lr.ph.2 ]
  %52 = phi ptr [ %49, %.lr.ph.preheader.2 ], [ %57, %.lr.ph.2 ]
  %53 = phi ptr [ %48, %.lr.ph.preheader.2 ], [ %56, %.lr.ph.2 ]
  %54 = getelementptr inbounds %struct.Job, ptr %1, i64 %indvars.iv.2
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(16) %54, ptr noundef nonnull align 4 dereferenceable(16) %53, i64 16, i1 false), !tbaa.struct !13
  %indvars.iv.next.2 = add nsw i64 %indvars.iv.2, 1
  %.013.2 = load i32, ptr %52, align 4, !tbaa !10
  %55 = sext i32 %.013.2 to i64
  %56 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %55
  %57 = getelementptr inbounds i8, ptr %56, i64 16
  %58 = load i32, ptr %57, align 4, !tbaa !10
  %.not.2 = icmp eq i32 %58, -1
  br i1 %.not.2, label %._crit_edge.loopexit.2, label %.lr.ph.2, !llvm.loop !15

._crit_edge.loopexit.2:                           ; preds = %.lr.ph.2
  %59 = trunc nsw i64 %indvars.iv.next.2 to i32
  br label %._crit_edge.2

._crit_edge.2:                                    ; preds = %._crit_edge.loopexit.2, %._crit_edge.1
  %.1.lcssa.2 = phi i32 [ %42, %._crit_edge.1 ], [ %59, %._crit_edge.loopexit.2 ]
  %.lcssa.2 = phi ptr [ %48, %._crit_edge.1 ], [ %56, %._crit_edge.loopexit.2 ]
  %60 = sext i32 %.1.lcssa.2 to i64
  %61 = getelementptr inbounds %struct.Job, ptr %1, i64 %60
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(16) %61, ptr noundef nonnull align 4 dereferenceable(16) %.lcssa.2, i64 16, i1 false), !tbaa.struct !13
  ret void
}

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

!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, !7, i64 4}
!6 = !{!"", !7, i64 0, !7, i64 4}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !7, i64 16}
!11 = !{!"", !12, i64 0, !7, i64 16}
!12 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12}
!13 = !{i64 0, i64 4, !14, i64 4, i64 4, !14, i64 8, i64 4, !14, i64 12, i64 4, !14}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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