; LLVM IR from chirag-singhal/Coding-Interview-101 :: Miscellaneous/Labs/Lab1/AssignmentFiles (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.7 = external hidden unnamed_addr constant [7 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @printJob(i64, i64) #0

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

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

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

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

8:                                                ; preds = %56, %2
  %9 = load i32, ptr %6, align 4, !tbaa !9
  %10 = icmp sgt i32 %9, -1
  br i1 %10, label %12, label %11

11:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %59

12:                                               ; preds = %8
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = load i32, ptr %6, 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 0
  %18 = load i32, ptr %17, align 4, !tbaa !11
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %20, label %21

20:                                               ; preds = %12
  br label %56

21:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = load i32, ptr %6, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds %struct.SeqList, ptr %22, i64 %24
  %26 = getelementptr inbounds %struct.SeqList, ptr %25, i32 0, i32 1
  %27 = load i32, ptr %26, align 4, !tbaa !13
  store i32 %27, ptr %7, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %31, %21
  %29 = load i32, ptr %7, align 4, !tbaa !9
  %30 = icmp ne i32 %29, -1
  br i1 %30, label %31, label %55

31:                                               ; preds = %28
  %32 = call i32 (ptr, ...) @printf(ptr noundef @.str.7)
  %33 = load i32, ptr %7, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [10 x %struct.Location], ptr @st, i64 0, i64 %34
  %36 = getelementptr inbounds %struct.Location, ptr %35, i32 0, i32 0
  %37 = getelementptr inbounds { i64, i64 }, ptr %36, i32 0, i32 0
  %38 = load i64, ptr %37, align 4
  %39 = getelementptr inbounds { i64, i64 }, ptr %36, i32 0, i32 1
  %40 = load i64, ptr %39, align 4
  call void @printJob(i64 %38, i64 %40)
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = load i32, ptr %5, align 4, !tbaa !9
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %5, align 4, !tbaa !9
  %44 = sext i32 %42 to i64
  %45 = getelementptr inbounds %struct.Job, ptr %41, i64 %44
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [10 x %struct.Location], ptr @st, i64 0, i64 %47
  %49 = getelementptr inbounds %struct.Location, ptr %48, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %45, ptr align 4 %49, i64 16, i1 false), !tbaa.struct !14
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [10 x %struct.Location], ptr @st, i64 0, i64 %51
  %53 = getelementptr inbounds %struct.Location, ptr %52, i32 0, i32 1
  %54 = load i32, ptr %53, align 4, !tbaa !15
  store i32 %54, ptr %7, align 4, !tbaa !9
  br label %28, !llvm.loop !18

55:                                               ; preds = %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %56

56:                                               ; preds = %55, %20
  %57 = load i32, ptr %6, align 4, !tbaa !9
  %58 = add nsw i32 %57, -1
  store i32 %58, ptr %6, align 4, !tbaa !9
  br label %8, !llvm.loop !20

59:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret void
}

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


; 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.7 = external hidden unnamed_addr constant [7 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @printJob(i64, i64) local_unnamed_addr #0

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #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) #2

; Function Attrs: nounwind uwtable
define dso_local void @copy_sorted_ele(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 16
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %.loopexit, label %6

6:                                                ; preds = %2
  %7 = getelementptr inbounds i8, ptr %0, i64 20
  %.013 = load i32, ptr %7, align 4, !tbaa !10
  %.not14 = icmp eq i32 %.013, -1
  br i1 %.not14, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %6, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %6 ]
  %.016 = phi i32 [ %.0, %.lr.ph ], [ %.013, %6 ]
  %8 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.7)
  %9 = sext i32 %.016 to i64
  %10 = getelementptr inbounds [10 x %struct.Location], ptr @st, i64 0, i64 %9
  %11 = load i64, ptr %10, align 4
  %12 = getelementptr inbounds i8, ptr %10, i64 8
  %13 = load i64, ptr %12, align 4
  tail call void @printJob(i64 %11, i64 %13)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %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) %10, i64 16, i1 false), !tbaa.struct !11
  %15 = getelementptr inbounds i8, ptr %10, i64 16
  %.0 = load i32, ptr %15, align 4, !tbaa !10
  %.not = icmp eq i32 %.0, -1
  br i1 %.not, label %.loopexit.loopexit, label %.lr.ph, !llvm.loop !12

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

.loopexit:                                        ; preds = %.loopexit.loopexit, %6, %2
  %.2 = phi i32 [ 0, %2 ], [ 0, %6 ], [ %16, %.loopexit.loopexit ]
  %17 = getelementptr inbounds i8, ptr %0, i64 8
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %.loopexit.1, label %20

20:                                               ; preds = %.loopexit
  %21 = getelementptr inbounds i8, ptr %0, i64 12
  %.013.1 = load i32, ptr %21, align 4, !tbaa !10
  %.not14.1 = icmp eq i32 %.013.1, -1
  br i1 %.not14.1, label %.loopexit.1, label %.lr.ph.preheader.1

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

.lr.ph.1:                                         ; preds = %.lr.ph.1, %.lr.ph.preheader.1
  %indvars.iv.1 = phi i64 [ %22, %.lr.ph.preheader.1 ], [ %indvars.iv.next.1, %.lr.ph.1 ]
  %.016.1 = phi i32 [ %.013.1, %.lr.ph.preheader.1 ], [ %.0.1, %.lr.ph.1 ]
  %23 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.7)
  %24 = sext i32 %.016.1 to i64
  %25 = getelementptr inbounds [10 x %struct.Location], ptr @st, i64 0, i64 %24
  %26 = load i64, ptr %25, align 4
  %27 = getelementptr inbounds i8, ptr %25, i64 8
  %28 = load i64, ptr %27, align 4
  tail call void @printJob(i64 %26, i64 %28)
  %indvars.iv.next.1 = add nsw i64 %indvars.iv.1, 1
  %29 = 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) %29, ptr noundef nonnull align 4 dereferenceable(16) %25, i64 16, i1 false), !tbaa.struct !11
  %30 = getelementptr inbounds i8, ptr %25, i64 16
  %.0.1 = load i32, ptr %30, align 4, !tbaa !10
  %.not.1 = icmp eq i32 %.0.1, -1
  br i1 %.not.1, label %.loopexit.loopexit.1, label %.lr.ph.1, !llvm.loop !12

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

.loopexit.1:                                      ; preds = %.loopexit.loopexit.1, %20, %.loopexit
  %.2.1 = phi i32 [ %.2, %.loopexit ], [ %.2, %20 ], [ %31, %.loopexit.loopexit.1 ]
  %32 = load i32, ptr %0, align 4, !tbaa !5
  %33 = icmp eq i32 %32, 0
  br i1 %33, label %.loopexit.2, label %34

34:                                               ; preds = %.loopexit.1
  %35 = getelementptr inbounds i8, ptr %0, i64 4
  %.013.2 = load i32, ptr %35, align 4, !tbaa !10
  %.not14.2 = icmp eq i32 %.013.2, -1
  br i1 %.not14.2, label %.loopexit.2, label %.lr.ph.preheader.2

.lr.ph.preheader.2:                               ; preds = %34
  %36 = sext i32 %.2.1 to i64
  br label %.lr.ph.2

.lr.ph.2:                                         ; preds = %.lr.ph.2, %.lr.ph.preheader.2
  %indvars.iv.2 = phi i64 [ %36, %.lr.ph.preheader.2 ], [ %indvars.iv.next.2, %.lr.ph.2 ]
  %.016.2 = phi i32 [ %.013.2, %.lr.ph.preheader.2 ], [ %.0.2, %.lr.ph.2 ]
  %37 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.7)
  %38 = sext i32 %.016.2 to i64
  %39 = getelementptr inbounds [10 x %struct.Location], ptr @st, i64 0, i64 %38
  %40 = load i64, ptr %39, align 4
  %41 = getelementptr inbounds i8, ptr %39, i64 8
  %42 = load i64, ptr %41, align 4
  tail call void @printJob(i64 %40, i64 %42)
  %indvars.iv.next.2 = add nsw i64 %indvars.iv.2, 1
  %43 = 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) %43, ptr noundef nonnull align 4 dereferenceable(16) %39, i64 16, i1 false), !tbaa.struct !11
  %44 = getelementptr inbounds i8, ptr %39, i64 16
  %.0.2 = load i32, ptr %44, align 4, !tbaa !10
  %.not.2 = icmp eq i32 %.0.2, -1
  br i1 %.not.2, label %.loopexit.2, label %.lr.ph.2, !llvm.loop !12

.loopexit.2:                                      ; preds = %.lr.ph.2, %34, %.loopexit.1
  ret void
}

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


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