; LLVM IR from chirag-singhal/Coding-Interview-101 :: Miscellaneous/Labs/Lab5 (exer_1)

; 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.credit_card = type { i64, [6 x i8], [8 x i8], [30 x i8], [30 x i8] }

; 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 @insertInOrder(ptr noundef %0, i64 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca %struct.credit_card, align 8
  %6 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 88, ptr %5) #3
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = load i64, ptr %4, align 8, !tbaa !9
  %9 = getelementptr inbounds %struct.credit_card, ptr %7, i64 %8
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %5, ptr align 8 %9, i64 88, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %10 = load i64, ptr %4, align 8, !tbaa !9
  %11 = sub nsw i64 %10, 1
  store i64 %11, ptr %6, align 8, !tbaa !9
  br label %12

12:                                               ; preds = %26, %2
  %13 = load i64, ptr %6, align 8, !tbaa !9
  %14 = icmp sge i64 %13, 0
  br i1 %14, label %15, label %24

15:                                               ; preds = %12
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = load i64, ptr %6, align 8, !tbaa !9
  %18 = getelementptr inbounds %struct.credit_card, ptr %16, i64 %17
  %19 = getelementptr inbounds %struct.credit_card, ptr %18, i32 0, i32 0
  %20 = load i64, ptr %19, align 8, !tbaa !15
  %21 = getelementptr inbounds %struct.credit_card, ptr %5, i32 0, i32 0
  %22 = load i64, ptr %21, align 8, !tbaa !15
  %23 = icmp sgt i64 %20, %22
  br label %24

24:                                               ; preds = %15, %12
  %25 = phi i1 [ false, %12 ], [ %23, %15 ]
  br i1 %25, label %26, label %36

26:                                               ; preds = %24
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = load i64, ptr %6, align 8, !tbaa !9
  %29 = add nsw i64 %28, 1
  %30 = getelementptr inbounds %struct.credit_card, ptr %27, i64 %29
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = load i64, ptr %6, align 8, !tbaa !9
  %33 = getelementptr inbounds %struct.credit_card, ptr %31, i64 %32
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %30, ptr align 8 %33, i64 88, i1 false), !tbaa.struct !11
  %34 = load i64, ptr %6, align 8, !tbaa !9
  %35 = add nsw i64 %34, -1
  store i64 %35, ptr %6, align 8, !tbaa !9
  br label %12, !llvm.loop !17

36:                                               ; preds = %24
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = load i64, ptr %6, align 8, !tbaa !9
  %39 = add nsw i64 %38, 1
  %40 = getelementptr inbounds %struct.credit_card, ptr %37, i64 %39
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %40, ptr align 8 %5, i64 88, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 88, ptr %5) #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 = !{!"long", !7, i64 0}
!11 = !{i64 0, i64 8, !12, i64 8, i64 6, !14, i64 14, i64 8, !14, i64 22, i64 30, !14, i64 52, i64 30, !14}
!12 = !{!13, !13, i64 0}
!13 = !{!"long long", !7, i64 0}
!14 = !{!7, !7, i64 0}
!15 = !{!16, !13, i64 0}
!16 = !{!"credit_card", !13, i64 0, !7, i64 8, !7, i64 14, !7, i64 22, !7, i64 52}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; 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.credit_card = type { i64, [6 x i8], [8 x i8], [30 x i8], [30 x i8] }

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

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

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @insertInOrder(ptr nocapture noundef %0, i64 noundef %1) local_unnamed_addr #1 {
  %.sroa.5 = alloca [80 x i8], align 8
  call void @llvm.lifetime.start.p0(i64 80, ptr nonnull %.sroa.5)
  %3 = getelementptr inbounds %struct.credit_card, ptr %0, i64 %1
  %.sroa.0.0.copyload = load i64, ptr %3, align 8, !tbaa !5
  %.sroa.5.0..sroa_idx = getelementptr inbounds i8, ptr %3, i64 8
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(80) %.sroa.5, ptr noundef nonnull align 8 dereferenceable(80) %.sroa.5.0..sroa_idx, i64 80, i1 false), !tbaa.struct !9
  %4 = icmp sgt i64 %1, 0
  br i1 %4, label %.lr.ph, label %.critedge

.lr.ph:                                           ; preds = %2, %8
  %.0.in15 = phi i64 [ %.016, %8 ], [ %1, %2 ]
  %.016 = add nsw i64 %.0.in15, -1
  %5 = getelementptr inbounds %struct.credit_card, ptr %0, i64 %.016
  %6 = load i64, ptr %5, align 8, !tbaa !11
  %7 = icmp sgt i64 %6, %.sroa.0.0.copyload
  br i1 %7, label %8, label %.critedge

8:                                                ; preds = %.lr.ph
  %9 = getelementptr inbounds %struct.credit_card, ptr %0, i64 %.0.in15
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(88) %9, ptr noundef nonnull align 8 dereferenceable(88) %5, i64 88, i1 false), !tbaa.struct !13
  %10 = icmp ugt i64 %.0.in15, 1
  br i1 %10, label %.lr.ph, label %.critedge, !llvm.loop !14

.critedge:                                        ; preds = %.lr.ph, %8, %2
  %.0.in.lcssa = phi i64 [ %1, %2 ], [ 0, %8 ], [ %.0.in15, %.lr.ph ]
  %11 = getelementptr inbounds %struct.credit_card, ptr %0, i64 %.0.in.lcssa
  store i64 %.sroa.0.0.copyload, ptr %11, align 8, !tbaa !5
  %.sroa.5.0..sroa_idx7 = getelementptr inbounds i8, ptr %11, i64 8
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(80) %.sroa.5.0..sroa_idx7, ptr noundef nonnull align 8 dereferenceable(80) %.sroa.5, i64 80, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.end.p0(i64 80, ptr nonnull %.sroa.5)
  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) #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nofree norecurse nosync nounwind memory(argmem: readwrite) 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 = { 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, !6, i64 0}
!6 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{i64 0, i64 6, !10, i64 6, i64 8, !10, i64 14, i64 30, !10, i64 44, i64 30, !10}
!10 = !{!7, !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"credit_card", !6, i64 0, !7, i64 8, !7, i64 14, !7, i64 22, !7, i64 52}
!13 = !{i64 0, i64 8, !5, i64 8, i64 6, !10, i64 14, i64 8, !10, i64 22, i64 30, !10, i64 52, i64 30, !10}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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