; LLVM IR from PacktPublishing/CPP-Data-Structures-and-Algorithms :: Chapter02/List/src/List.ll (ZN4List6InsertEii)

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

%class.List = type { i32, ptr }

; Function Attrs: nobuiltin nounwind
declare void @_ZdaPv(ptr noundef) #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN4List6InsertEii(ptr noundef nonnull align 8 dereferenceable(16) %0, i32 noundef %1, i32 noundef %2) #1 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  %10 = load ptr, ptr %4, align 8
  %11 = load i32, ptr %5, align 4, !tbaa !9
  %12 = icmp slt i32 %11, 0
  br i1 %12, label %18, label %13

13:                                               ; preds = %3
  %14 = load i32, ptr %5, align 4, !tbaa !9
  %15 = getelementptr inbounds %class.List, ptr %10, i32 0, i32 0
  %16 = load i32, ptr %15, align 8, !tbaa !11
  %17 = icmp sgt i32 %14, %16
  br i1 %17, label %18, label %19

18:                                               ; preds = %13, %3
  br label %73

19:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %20 = getelementptr inbounds %class.List, ptr %10, i32 0, i32 1
  %21 = load ptr, ptr %20, align 8, !tbaa !13
  store ptr %21, ptr %7, align 8, !tbaa !5
  %22 = getelementptr inbounds %class.List, ptr %10, i32 0, i32 0
  %23 = load i32, ptr %22, align 8, !tbaa !11
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr %22, align 8, !tbaa !11
  %25 = getelementptr inbounds %class.List, ptr %10, i32 0, i32 0
  %26 = load i32, ptr %25, align 8, !tbaa !11
  %27 = sext i32 %26 to i64
  %28 = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 %27, i64 4)
  %29 = extractvalue { i64, i1 } %28, 1
  %30 = extractvalue { i64, i1 } %28, 0
  %31 = select i1 %29, i64 -1, i64 %30
  %32 = call noalias noundef nonnull ptr @_Znam(i64 noundef %31) #6
  %33 = getelementptr inbounds %class.List, ptr %10, i32 0, i32 1
  store ptr %32, ptr %33, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  store i32 0, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %34

34:                                               ; preds = %65, %19
  %35 = load i32, ptr %8, align 4, !tbaa !9
  %36 = getelementptr inbounds %class.List, ptr %10, i32 0, i32 0
  %37 = load i32, ptr %36, align 8, !tbaa !11
  %38 = icmp slt i32 %35, %37
  br i1 %38, label %40, label %39

39:                                               ; preds = %34
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  br label %68

40:                                               ; preds = %34
  %41 = load i32, ptr %5, align 4, !tbaa !9
  %42 = load i32, ptr %8, align 4, !tbaa !9
  %43 = icmp eq i32 %41, %42
  br i1 %43, label %44, label %51

44:                                               ; preds = %40
  %45 = load i32, ptr %6, align 4, !tbaa !9
  %46 = getelementptr inbounds %class.List, ptr %10, i32 0, i32 1
  %47 = load ptr, ptr %46, align 8, !tbaa !13
  %48 = load i32, ptr %8, align 4, !tbaa !9
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds i32, ptr %47, i64 %49
  store i32 %45, ptr %50, align 4, !tbaa !9
  br label %64

51:                                               ; preds = %40
  %52 = load ptr, ptr %7, align 8, !tbaa !5
  %53 = load i32, ptr %9, align 4, !tbaa !9
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds i32, ptr %52, i64 %54
  %56 = load i32, ptr %55, align 4, !tbaa !9
  %57 = getelementptr inbounds %class.List, ptr %10, i32 0, i32 1
  %58 = load ptr, ptr %57, align 8, !tbaa !13
  %59 = load i32, ptr %8, align 4, !tbaa !9
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds i32, ptr %58, i64 %60
  store i32 %56, ptr %61, align 4, !tbaa !9
  %62 = load i32, ptr %9, align 4, !tbaa !9
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %9, align 4, !tbaa !9
  br label %64

64:                                               ; preds = %51, %44
  br label %65

65:                                               ; preds = %64
  %66 = load i32, ptr %8, align 4, !tbaa !9
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %8, align 4, !tbaa !9
  br label %34, !llvm.loop !14

68:                                               ; preds = %39
  %69 = load ptr, ptr %7, align 8, !tbaa !5
  %70 = icmp eq ptr %69, null
  br i1 %70, label %72, label %71

71:                                               ; preds = %68
  call void @_ZdaPv(ptr noundef %69) #7
  br label %72

72:                                               ; preds = %71, %68
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  br label %73

73:                                               ; preds = %72, %18
  ret void
}

; 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 speculatable willreturn memory(none)
declare { i64, i1 } @llvm.umul.with.overflow.i64(i64, i64) #3

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) #4

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

attributes #0 = { nobuiltin 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 #1 = { mustprogress 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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nobuiltin allocsize(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 #5 = { nounwind }
attributes #6 = { builtin allocsize(0) }
attributes #7 = { builtin 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_ZTS4List", !10, i64 0, !6, i64 8}
!13 = !{!12, !6, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"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"

; Function Attrs: nobuiltin nounwind
declare void @_ZdaPv(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN4List6InsertEii(ptr nocapture noundef nonnull align 8 dereferenceable(16) %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #1 align 2 {
  %4 = icmp slt i32 %1, 0
  br i1 %4, label %39, label %5

5:                                                ; preds = %3
  %6 = load i32, ptr %0, align 8, !tbaa !5
  %7 = icmp slt i32 %6, %1
  br i1 %7, label %39, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %5
  %8 = getelementptr inbounds i8, ptr %0, i64 8
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %10 = add nuw i32 %6, 1
  store i32 %10, ptr %0, align 8, !tbaa !5
  %11 = zext i32 %10 to i64
  %12 = shl nuw nsw i64 %11, 2
  %13 = tail call noalias noundef nonnull ptr @_Znam(i64 noundef %12) #3
  store ptr %13, ptr %8, align 8, !tbaa !11
  %14 = zext nneg i32 %1 to i64
  %xtraiter = and i64 %11, 1
  %15 = icmp eq i32 %6, 0
  br i1 %15, label %._crit_edge.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %11, 4294967294
  br label %.lr.ph

._crit_edge.unr-lcssa.loopexit:                   ; preds = %36
  %16 = sext i32 %.1.1 to i64
  br label %._crit_edge.unr-lcssa

._crit_edge.unr-lcssa:                            ; preds = %._crit_edge.unr-lcssa.loopexit, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next.1, %._crit_edge.unr-lcssa.loopexit ]
  %.018.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %16, %._crit_edge.unr-lcssa.loopexit ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.unr-lcssa
  %17 = icmp eq i64 %indvars.iv.unr, %14
  br i1 %17, label %._crit_edge.epilog-lcssa, label %18

18:                                               ; preds = %.lr.ph.epil
  %19 = getelementptr inbounds i32, ptr %9, i64 %.018.unr
  %20 = load i32, ptr %19, align 4, !tbaa !12
  br label %._crit_edge.epilog-lcssa

._crit_edge.epilog-lcssa:                         ; preds = %18, %.lr.ph.epil
  %.sink.epil = phi i32 [ %20, %18 ], [ %2, %.lr.ph.epil ]
  %21 = getelementptr inbounds i32, ptr %13, i64 %indvars.iv.unr
  store i32 %.sink.epil, ptr %21, align 4
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.unr-lcssa, %._crit_edge.epilog-lcssa
  %22 = icmp eq ptr %9, null
  br i1 %22, label %39, label %38

.lr.ph:                                           ; preds = %36, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %36 ]
  %.018 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %.1.1, %36 ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %36 ]
  %23 = icmp eq i64 %indvars.iv, %14
  br i1 %23, label %.lr.ph.1, label %24

24:                                               ; preds = %.lr.ph
  %25 = sext i32 %.018 to i64
  %26 = getelementptr inbounds i32, ptr %9, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !12
  %28 = add nsw i32 %.018, 1
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %.lr.ph, %24
  %.sink = phi i32 [ %27, %24 ], [ %2, %.lr.ph ]
  %.1 = phi i32 [ %28, %24 ], [ %.018, %.lr.ph ]
  %29 = getelementptr inbounds i32, ptr %13, i64 %indvars.iv
  store i32 %.sink, ptr %29, align 4
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %30 = icmp eq i64 %indvars.iv.next, %14
  br i1 %30, label %36, label %31

31:                                               ; preds = %.lr.ph.1
  %32 = sext i32 %.1 to i64
  %33 = getelementptr inbounds i32, ptr %9, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !12
  %35 = add nsw i32 %.1, 1
  br label %36

36:                                               ; preds = %31, %.lr.ph.1
  %.sink.1 = phi i32 [ %34, %31 ], [ %2, %.lr.ph.1 ]
  %.1.1 = phi i32 [ %35, %31 ], [ %.1, %.lr.ph.1 ]
  %37 = getelementptr inbounds i32, ptr %13, i64 %indvars.iv.next
  store i32 %.sink.1, ptr %37, align 4
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.unr-lcssa.loopexit, label %.lr.ph, !llvm.loop !13

38:                                               ; preds = %._crit_edge
  tail call void @_ZdaPv(ptr noundef nonnull %9) #4
  br label %39

39:                                               ; preds = %._crit_edge, %38, %3, %5
  ret void
}

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) local_unnamed_addr #2

attributes #0 = { nobuiltin 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 #1 = { mustprogress 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 = { nobuiltin allocsize(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 #3 = { builtin allocsize(0) }
attributes #4 = { builtin 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 = !{!"_ZTS4List", !7, i64 0, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!7, !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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