; LLVM IR from chirag-singhal/Coding-Interview-101 :: Miscellaneous/Labs/Lab1/Assignment (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: nounwind uwtable
declare dso_local i64 @findnextfreeLoc(i64) #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: nounwind uwtable
declare dso_local void @rightShift(i32 noundef, i32 noundef) #0

; 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: nounwind uwtable
define dso_local i64 @insert(i64 %0, i64 %1, i64 %2) #0 {
  %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 %struct.SeqList, 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
  %11 = getelementptr inbounds %struct.SeqList, ptr %6, i32 0, i32 0
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = icmp sgt i32 %12, 0
  br i1 %13, label %14, label %82

14:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !10
  %15 = getelementptr inbounds %struct.SeqList, ptr %6, i32 0, i32 1
  %16 = load i32, ptr %15, align 4, !tbaa !11
  store i32 %16, ptr %7, align 4, !tbaa !10
  br label %17

17:                                               ; preds = %55, %14
  %18 = load i32, ptr %7, align 4, !tbaa !10
  %19 = getelementptr inbounds %struct.SeqList, ptr %6, i32 0, i32 1
  %20 = load i32, ptr %19, align 4, !tbaa !11
  %21 = getelementptr inbounds %struct.SeqList, ptr %6, i32 0, i32 0
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = add nsw i32 %20, %22
  %24 = icmp slt i32 %18, %23
  br i1 %24, label %25, label %58

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

40:                                               ; preds = %25
  %41 = getelementptr inbounds %struct.SeqList, ptr %6, i32 0, i32 0
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = load i32, ptr %7, align 4, !tbaa !10
  call void @rightShift(i32 noundef %42, i32 noundef %43)
  %44 = load i32, ptr %7, align 4, !tbaa !10
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [10 x %struct.Location], ptr @st, i64 0, i64 %45
  %47 = getelementptr inbounds %struct.Location, ptr %46, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %47, ptr align 4 %5, i64 16, i1 false), !tbaa.struct !12
  %48 = load i32, ptr %7, align 4, !tbaa !10
  %49 = add nsw i32 %48, 1
  %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
  store i32 %49, ptr %53, align 4, !tbaa !13
  br label %58

54:                                               ; preds = %25
  br label %55

55:                                               ; preds = %54
  %56 = load i32, ptr %7, align 4, !tbaa !10
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %7, align 4, !tbaa !10
  br label %17, !llvm.loop !16

58:                                               ; preds = %40, %17
  %59 = load i32, ptr %7, align 4, !tbaa !10
  %60 = getelementptr inbounds %struct.SeqList, ptr %6, i32 0, i32 1
  %61 = load i32, ptr %60, align 4, !tbaa !11
  %62 = getelementptr inbounds %struct.SeqList, ptr %6, i32 0, i32 0
  %63 = load i32, ptr %62, align 4, !tbaa !5
  %64 = add nsw i32 %61, %63
  %65 = icmp eq i32 %59, %64
  br i1 %65, label %66, label %81

66:                                               ; preds = %58
  %67 = load i32, ptr %7, align 4, !tbaa !10
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [10 x %struct.Location], ptr @st, i64 0, i64 %68
  %70 = getelementptr inbounds %struct.Location, ptr %69, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %70, ptr align 4 %5, i64 16, i1 false), !tbaa.struct !12
  %71 = load i32, ptr %7, align 4, !tbaa !10
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds [10 x %struct.Location], ptr @st, i64 0, i64 %72
  %74 = getelementptr inbounds %struct.Location, ptr %73, i32 0, i32 1
  store i32 -1, ptr %74, align 4, !tbaa !13
  %75 = load i32, ptr %7, align 4, !tbaa !10
  %76 = load i32, ptr %7, align 4, !tbaa !10
  %77 = sub nsw i32 %76, 1
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds [10 x %struct.Location], ptr @st, i64 0, i64 %78
  %80 = getelementptr inbounds %struct.Location, ptr %79, i32 0, i32 1
  store i32 %75, ptr %80, align 4, !tbaa !13
  br label %81

81:                                               ; preds = %66, %58
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %90

82:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %83 = load i64, ptr %6, align 4
  %84 = call i64 @findnextfreeLoc(i64 %83)
  store i64 %84, ptr %8, align 4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %6, ptr align 4 %8, i64 8, i1 false), !tbaa.struct !18
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %85 = getelementptr inbounds %struct.SeqList, ptr %6, i32 0, i32 1
  %86 = load i32, ptr %85, align 4, !tbaa !11
  %87 = sext i32 %86 to i64
  %88 = getelementptr inbounds [10 x %struct.Location], ptr @st, i64 0, i64 %87
  %89 = getelementptr inbounds %struct.Location, ptr %88, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %89, ptr align 4 %5, i64 16, i1 false), !tbaa.struct !12
  br label %90

90:                                               ; preds = %82, %81
  %91 = getelementptr inbounds %struct.SeqList, ptr %6, i32 0, i32 0
  %92 = load i32, ptr %91, align 4, !tbaa !5
  %93 = add nsw i32 %92, 1
  store i32 %93, ptr %91, align 4, !tbaa !5
  %94 = load i32, ptr @nextfreeloc, align 4, !tbaa !10
  %95 = add nsw i32 %94, 1
  store i32 %95, ptr @nextfreeloc, align 4, !tbaa !10
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %4, ptr align 4 %6, i64 8, i1 false), !tbaa.struct !18
  %96 = load i64, ptr %4, align 4
  ret i64 %96
}

declare i32 @compare(i64, i64, i64, i64) #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 nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync 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 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 = !{!6, !7, i64 4}
!12 = !{i64 0, i64 4, !10, i64 4, i64 4, !10, i64 8, i64 4, !10, i64 12, i64 4, !10}
!13 = !{!14, !7, i64 16}
!14 = !{!"", !15, i64 0, !7, i64 16}
!15 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!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
declare dso_local i64 @findnextfreeLoc(i64) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @rightShift(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i64 @insert(i64 %0, i64 %1, i64 %2) local_unnamed_addr #0 {
  %.sroa.012.0.extract.trunc = trunc i64 %2 to i32
  %.sroa.10.0.extract.shift = lshr i64 %2, 32
  %4 = icmp sgt i32 %.sroa.012.0.extract.trunc, 0
  br i1 %4, label %.preheader, label %30

.preheader:                                       ; preds = %3
  %.sroa.10.0.extract.trunc = trunc nuw i64 %.sroa.10.0.extract.shift to i32
  %5 = add nsw i32 %.sroa.10.0.extract.trunc, %.sroa.012.0.extract.trunc
  %6 = ashr i64 %2, 32
  %7 = sext i32 %5 to i64
  br label %8

8:                                                ; preds = %.preheader, %20
  %indvars.iv = phi i64 [ %6, %.preheader ], [ %indvars.iv.next, %20 ]
  %9 = getelementptr inbounds [10 x %struct.Location], ptr @st, i64 0, i64 %indvars.iv
  %10 = load i64, ptr %9, align 4
  %11 = getelementptr inbounds i8, ptr %9, i64 8
  %12 = load i64, ptr %11, align 4
  %13 = tail call i32 @compare(i64 %10, i64 %12, i64 %0, i64 %1) #2
  %14 = icmp eq i32 %13, 1
  br i1 %14, label %15, label %20

15:                                               ; preds = %8
  %16 = getelementptr inbounds i8, ptr %9, i64 8
  %17 = trunc nsw i64 %indvars.iv to i32
  tail call void @rightShift(i32 noundef %.sroa.012.0.extract.trunc, i32 noundef %17)
  store i64 %0, ptr %9, align 4
  store i64 %1, ptr %16, align 4
  %18 = add nsw i32 %17, 1
  %19 = getelementptr inbounds i8, ptr %9, i64 16
  store i32 %18, ptr %19, align 4, !tbaa !5
  br label %.loopexit

20:                                               ; preds = %8
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %21 = icmp slt i64 %indvars.iv.next, %7
  br i1 %21, label %8, label %.loopexit.loopexit, !llvm.loop !11

.loopexit.loopexit:                               ; preds = %20
  %22 = trunc nsw i64 %indvars.iv.next to i32
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit, %15
  %.034 = phi i32 [ %17, %15 ], [ %22, %.loopexit.loopexit ]
  %23 = icmp eq i32 %.034, %5
  br i1 %23, label %24, label %34

24:                                               ; preds = %.loopexit
  %25 = getelementptr inbounds [10 x %struct.Location], ptr @st, i64 0, i64 %7
  store i64 %0, ptr %25, align 4
  %.sroa.5.0..sroa_idx27 = getelementptr inbounds i8, ptr %25, i64 8
  store i64 %1, ptr %.sroa.5.0..sroa_idx27, align 4
  %26 = getelementptr inbounds i8, ptr %25, i64 16
  store i32 -1, ptr %26, align 4, !tbaa !5
  %27 = add nsw i32 %5, -1
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [10 x %struct.Location], ptr @st, i64 0, i64 %28, i32 1
  store i32 %5, ptr %29, align 4, !tbaa !5
  br label %34

30:                                               ; preds = %3
  %31 = tail call i64 @findnextfreeLoc(i64 %2)
  %.sroa.10.0.extract.shift19 = lshr i64 %31, 32
  %32 = ashr i64 %31, 32
  %33 = getelementptr inbounds [10 x %struct.Location], ptr @st, i64 0, i64 %32
  store i64 %0, ptr %33, align 4
  %.sroa.5.0..sroa_idx29 = getelementptr inbounds i8, ptr %33, i64 8
  store i64 %1, ptr %.sroa.5.0..sroa_idx29, align 4
  br label %34

34:                                               ; preds = %.loopexit, %24, %30
  %.sroa.012.0 = phi i64 [ %31, %30 ], [ %2, %24 ], [ %2, %.loopexit ]
  %.sroa.10.0 = phi i64 [ %.sroa.10.0.extract.shift19, %30 ], [ %.sroa.10.0.extract.shift, %24 ], [ %.sroa.10.0.extract.shift, %.loopexit ]
  %35 = add i64 %.sroa.012.0, 1
  %36 = load i32, ptr @nextfreeloc, align 4, !tbaa !13
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr @nextfreeloc, align 4, !tbaa !13
  %.sroa.2.0.insert.shift = shl nuw i64 %.sroa.10.0, 32
  %.sroa.031.0.insert.ext = and i64 %35, 4294967295
  %.sroa.031.0.insert.insert = or disjoint i64 %.sroa.2.0.insert.shift, %.sroa.031.0.insert.ext
  ret i64 %.sroa.031.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,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass