; 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 }

@nextfreeloc = external dso_local global i32, align 4
@st = external dso_local global [10 x %struct.LOCATION], align 16

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

; Function Attrs: nounwind uwtable
define dso_local i64 @insert(i64 %0, i64 %1, i64 %2) #1 {
  %4 = alloca %struct.seqlist, align 4
  %5 = alloca %struct.Job, align 4
  %6 = alloca %struct.seqlist, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = getelementptr inbounds { i64, i64 }, ptr %5, i32 0, i32 0
  store i64 %0, ptr %9, align 4
  %10 = getelementptr inbounds { i64, i64 }, ptr %5, i32 0, i32 1
  store i64 %1, ptr %10, align 4
  store i64 %2, ptr %6, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  %11 = getelementptr inbounds %struct.seqlist, ptr %6, i32 0, i32 1
  %12 = load i32, ptr %11, align 4, !tbaa !5
  store i32 %12, ptr %7, align 4, !tbaa !10
  br label %13

13:                                               ; preds = %41, %3
  %14 = load i32, ptr %7, align 4, !tbaa !10
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %15
  %17 = getelementptr inbounds %struct.LOCATION, ptr %16, i32 0, i32 1
  %18 = load i32, ptr %17, align 4, !tbaa !11
  %19 = icmp ne i32 %18, -1
  br i1 %19, label %20, label %47

20:                                               ; preds = %13
  %21 = load i32, ptr %7, align 4, !tbaa !10
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %22
  %24 = getelementptr inbounds %struct.LOCATION, ptr %23, i32 0, i32 1
  %25 = load i32, ptr %24, align 4, !tbaa !11
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %26
  %28 = getelementptr inbounds %struct.LOCATION, ptr %27, i32 0, i32 0
  %29 = getelementptr inbounds { i64, i64 }, ptr %28, i32 0, i32 0
  %30 = load i64, ptr %29, align 4
  %31 = getelementptr inbounds { i64, i64 }, ptr %28, i32 0, i32 1
  %32 = load i64, ptr %31, align 4
  %33 = getelementptr inbounds { i64, i64 }, ptr %5, i32 0, i32 0
  %34 = load i64, ptr %33, align 4
  %35 = getelementptr inbounds { i64, i64 }, ptr %5, i32 0, i32 1
  %36 = load i64, ptr %35, align 4
  %37 = call i32 @compare(i64 %30, i64 %32, i64 %34, i64 %36)
  %38 = icmp eq i32 %37, 1
  br i1 %38, label %39, label %40

39:                                               ; preds = %20
  br label %47

40:                                               ; preds = %20
  br label %41

41:                                               ; preds = %40
  %42 = load i32, ptr %7, align 4, !tbaa !10
  %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 !11
  store i32 %46, ptr %7, align 4, !tbaa !10
  br label %13, !llvm.loop !14

47:                                               ; preds = %39, %13
  %48 = load i32, ptr @nextfreeloc, align 4, !tbaa !10
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr @nextfreeloc, align 4, !tbaa !10
  store i32 %48, ptr %8, align 4, !tbaa !10
  %50 = load i32, ptr %7, align 4, !tbaa !10
  %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 !11
  %55 = load i32, ptr %8, align 4, !tbaa !10
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %56
  %58 = getelementptr inbounds %struct.LOCATION, ptr %57, i32 0, i32 1
  store i32 %54, ptr %58, align 4, !tbaa !11
  %59 = load i32, ptr %8, align 4, !tbaa !10
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %60
  %62 = getelementptr inbounds %struct.LOCATION, ptr %61, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %62, ptr align 4 %5, i64 16, i1 false), !tbaa.struct !16
  %63 = load i32, ptr %8, align 4, !tbaa !10
  %64 = load i32, ptr %7, align 4, !tbaa !10
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %65
  %67 = getelementptr inbounds %struct.LOCATION, ptr %66, i32 0, i32 1
  store i32 %63, ptr %67, align 4, !tbaa !11
  %68 = getelementptr inbounds %struct.seqlist, ptr %6, i32 0, i32 0
  %69 = load i32, ptr %68, align 4, !tbaa !17
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %68, align 4, !tbaa !17
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %4, ptr align 4 %6, i64 8, i1 false), !tbaa.struct !18
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  %71 = load i64, ptr %4, align 4
  ret i64 %71
}

; 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

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

declare i32 @compare(i64, i64, i64, i64) #3

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


; 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 }

@nextfreeloc = external dso_local local_unnamed_addr global i32, align 4
@st = external dso_local local_unnamed_addr global [10 x %struct.LOCATION], align 16

; Function Attrs: nounwind uwtable
define dso_local i64 @insert(i64 %0, i64 %1, i64 %2) local_unnamed_addr #0 {
  %4 = ashr i64 %2, 32
  %5 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %4, i32 1
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %.not11 = icmp eq i32 %6, -1
  br i1 %.not11, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3, %16
  %7 = phi i32 [ %20, %16 ], [ %6, %3 ]
  %8 = phi ptr [ %19, %16 ], [ %5, %3 ]
  %9 = sext i32 %7 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
  %14 = tail call i32 @compare(i64 %11, i64 %13, i64 %0, i64 %1) #2
  %15 = icmp eq i32 %14, 1
  br i1 %15, label %._crit_edge, label %16

16:                                               ; preds = %.lr.ph
  %17 = load i32, ptr %8, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %18, i32 1
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %.not = icmp eq i32 %20, -1
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !11

._crit_edge:                                      ; preds = %16, %.lr.ph, %3
  %.lcssa = phi ptr [ %5, %3 ], [ %8, %.lr.ph ], [ %19, %16 ]
  %21 = load i32, ptr @nextfreeloc, align 4, !tbaa !13
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr @nextfreeloc, align 4, !tbaa !13
  %23 = load i32, ptr %.lcssa, align 4, !tbaa !5
  %24 = sext i32 %21 to i64
  %25 = getelementptr inbounds [10 x %struct.LOCATION], ptr @st, i64 0, i64 %24
  %26 = getelementptr inbounds i8, ptr %25, i64 16
  store i32 %23, ptr %26, align 4, !tbaa !5
  store i64 %0, ptr %25, align 4
  %.sroa.3.0..sroa_idx = getelementptr inbounds i8, ptr %25, i64 8
  store i64 %1, ptr %.sroa.3.0..sroa_idx, align 4
  store i32 %21, ptr %.lcssa, align 4, !tbaa !5
  %27 = add i64 %2, 1
  %.sroa.2.0.insert.shift = and i64 %2, -4294967296
  %.sroa.09.0.insert.ext = and i64 %27, 4294967295
  %.sroa.09.0.insert.insert = or disjoint i64 %.sroa.09.0.insert.ext, %.sroa.2.0.insert.shift
  ret i64 %.sroa.09.0.insert.insert
}

declare i32 @compare(i64, i64, i64, i64) local_unnamed_addr #1

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass