; LLVM IR from overnote/over-algorithm :: code/code-c-like/01-list/01-SqList.ll (Z10InsertElemR6SqListii)

; 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.SqList = type { [5 x i32], i32 }

@.str.3 = external hidden unnamed_addr constant [23 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [23 x i8], align 1

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

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

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

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

11:                                               ; preds = %3
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.SqList, ptr %13, i32 0, i32 1
  %15 = load i32, ptr %14, align 4, !tbaa !11
  %16 = add nsw i32 %15, 1
  %17 = icmp sgt i32 %12, %16
  br i1 %17, label %18, label %20

18:                                               ; preds = %11, %3
  %19 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  store i1 false, ptr %4, align 1
  br label %66

20:                                               ; preds = %11
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.SqList, ptr %21, i32 0, i32 1
  %23 = load i32, ptr %22, align 4, !tbaa !11
  %24 = icmp eq i32 %23, 5
  br i1 %24, label %25, label %27

25:                                               ; preds = %20
  %26 = call i32 (ptr, ...) @printf(ptr noundef @.str.4)
  store i1 false, ptr %4, align 1
  br label %66

27:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.SqList, ptr %28, i32 0, i32 1
  %30 = load i32, ptr %29, align 4, !tbaa !11
  %31 = sub nsw i32 %30, 1
  store i32 %31, ptr %8, align 4, !tbaa !9
  br label %32

32:                                               ; preds = %51, %27
  %33 = load i32, ptr %8, align 4, !tbaa !9
  %34 = load i32, ptr %6, align 4, !tbaa !9
  %35 = sub nsw i32 %34, 1
  %36 = icmp sge i32 %33, %35
  br i1 %36, label %38, label %37

37:                                               ; preds = %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %54

38:                                               ; preds = %32
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.SqList, ptr %39, i32 0, i32 0
  %41 = load i32, ptr %8, align 4, !tbaa !9
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [5 x i32], ptr %40, i64 0, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !9
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.SqList, ptr %45, i32 0, i32 0
  %47 = load i32, ptr %8, align 4, !tbaa !9
  %48 = add nsw i32 %47, 1
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [5 x i32], ptr %46, i64 0, i64 %49
  store i32 %44, ptr %50, align 4, !tbaa !9
  br label %51

51:                                               ; preds = %38
  %52 = load i32, ptr %8, align 4, !tbaa !9
  %53 = add nsw i32 %52, -1
  store i32 %53, ptr %8, align 4, !tbaa !9
  br label %32, !llvm.loop !13

54:                                               ; preds = %37
  %55 = load i32, ptr %7, align 4, !tbaa !9
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.SqList, ptr %56, i32 0, i32 0
  %58 = load i32, ptr %6, align 4, !tbaa !9
  %59 = sub nsw i32 %58, 1
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [5 x i32], ptr %57, i64 0, i64 %60
  store i32 %55, ptr %61, align 4, !tbaa !9
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.SqList, ptr %62, i32 0, i32 1
  %64 = load i32, ptr %63, align 4, !tbaa !11
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %63, align 4, !tbaa !11
  store i1 true, ptr %4, align 1
  br label %66

66:                                               ; preds = %54, %25, %18
  %67 = load i1, ptr %4, align 1
  ret i1 %67
}

attributes #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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 20}
!12 = !{!"_ZTS6SqList", !7, i64 0, !10, i64 20}
!13 = distinct !{!13, !14}
!14 = !{!"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"

@.str.3 = external hidden unnamed_addr constant [23 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [23 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local noundef zeroext i1 @_Z10InsertElemR6SqListii(ptr nocapture noundef nonnull align 4 dereferenceable(24) %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = icmp slt i32 %1, 1
  br i1 %4, label %10, label %5

5:                                                ; preds = %3
  %6 = getelementptr inbounds i8, ptr %0, i64 20
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = add nsw i32 %7, 1
  %9 = icmp slt i32 %8, %1
  br i1 %9, label %10, label %12

10:                                               ; preds = %5, %3
  %11 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3)
  br label %60

12:                                               ; preds = %5
  %13 = icmp eq i32 %7, 5
  br i1 %13, label %48, label %.preheader

.preheader:                                       ; preds = %12
  %.not20 = icmp ult i32 %7, %1
  br i1 %.not20, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %14 = add i32 %7, 1
  %smin32 = tail call i32 @llvm.smin.i32(i32 %7, i32 %1)
  %15 = sub i32 %14, %smin32
  %min.iters.check = icmp ult i32 %15, 84
  br i1 %min.iters.check, label %.lr.ph.preheader37, label %vector.scevcheck

.lr.ph.preheader37:                               ; preds = %middle.block, %vector.memcheck, %vector.scevcheck, %.lr.ph.preheader
  %.0.in21.ph = phi i32 [ %7, %vector.memcheck ], [ %7, %vector.scevcheck ], [ %7, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  br label %.lr.ph

vector.scevcheck:                                 ; preds = %.lr.ph.preheader
  %smin = tail call i32 @llvm.smin.i32(i32 %7, i32 %1)
  %16 = sub i32 %7, %smin
  %17 = icmp ugt i32 %smin, %7
  %18 = add i32 %7, -1
  %19 = icmp ugt i32 %16, %18
  %20 = zext i32 %7 to i64
  %21 = shl nuw nsw i64 %20, 2
  %scevgep = getelementptr i8, ptr %0, i64 %21
  %22 = zext i32 %16 to i64
  %mul25.neg = mul nsw i64 %22, -4
  %23 = getelementptr i8, ptr %scevgep, i64 %mul25.neg
  %24 = icmp ugt ptr %23, %scevgep
  %25 = zext i32 %18 to i64
  %26 = shl nuw nsw i64 %25, 2
  %scevgep28 = getelementptr i8, ptr %0, i64 %26
  %27 = zext i32 %16 to i64
  %mul29.neg = mul nsw i64 %27, -4
  %28 = getelementptr i8, ptr %scevgep28, i64 %mul29.neg
  %29 = icmp ugt ptr %28, %scevgep28
  %30 = or i1 %17, %19
  %31 = or i1 %30, %24
  %32 = or i1 %31, %29
  br i1 %32, label %.lr.ph.preheader37, label %vector.memcheck

vector.memcheck:                                  ; preds = %vector.scevcheck
  %33 = add i32 %7, -1
  %34 = zext i32 %33 to i64
  %35 = zext i32 %7 to i64
  %36 = sub nsw i64 %34, %35
  %37 = and i64 %36, 4611686018427387896
  %diff.check = icmp eq i64 %37, 0
  br i1 %diff.check, label %.lr.ph.preheader37, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %n.vec = and i32 %15, -8
  %ind.end = sub i32 %7, %n.vec
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i32 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = sub i32 %7, %index
  %38 = add nsw i32 %offset.idx, -1
  %39 = zext nneg i32 %38 to i64
  %40 = getelementptr inbounds [5 x i32], ptr %0, i64 0, i64 %39
  %41 = getelementptr inbounds i8, ptr %40, i64 -12
  %42 = getelementptr inbounds i8, ptr %40, i64 -28
  %wide.load = load <4 x i32>, ptr %41, align 4, !tbaa !10
  %wide.load33 = load <4 x i32>, ptr %42, align 4, !tbaa !10
  %43 = zext nneg i32 %offset.idx to i64
  %44 = getelementptr inbounds [5 x i32], ptr %0, i64 0, i64 %43
  %45 = getelementptr inbounds i8, ptr %44, i64 -12
  %46 = getelementptr inbounds i8, ptr %44, i64 -28
  store <4 x i32> %wide.load, ptr %45, align 4, !tbaa !10
  store <4 x i32> %wide.load33, ptr %46, align 4, !tbaa !10
  %index.next = add nuw i32 %index, 8
  %47 = icmp eq i32 %index.next, %n.vec
  br i1 %47, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i32 %15, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader37

48:                                               ; preds = %12
  %49 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4)
  br label %60

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %.preheader
  %50 = add nsw i32 %1, -1
  %51 = zext nneg i32 %50 to i64
  %52 = getelementptr inbounds [5 x i32], ptr %0, i64 0, i64 %51
  store i32 %2, ptr %52, align 4, !tbaa !10
  %53 = load i32, ptr %6, align 4, !tbaa !5
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %6, align 4, !tbaa !5
  br label %60

.lr.ph:                                           ; preds = %.lr.ph.preheader37, %.lr.ph
  %.0.in21 = phi i32 [ %.0, %.lr.ph ], [ %.0.in21.ph, %.lr.ph.preheader37 ]
  %.0 = add nsw i32 %.0.in21, -1
  %55 = zext nneg i32 %.0 to i64
  %56 = getelementptr inbounds [5 x i32], ptr %0, i64 0, i64 %55
  %57 = load i32, ptr %56, align 4, !tbaa !10
  %58 = zext nneg i32 %.0.in21 to i64
  %59 = getelementptr inbounds [5 x i32], ptr %0, i64 0, i64 %58
  store i32 %57, ptr %59, align 4, !tbaa !10
  %.not.not = icmp sgt i32 %.0.in21, %1
  br i1 %.not.not, label %.lr.ph, label %._crit_edge, !llvm.loop !15

60:                                               ; preds = %._crit_edge, %48, %10
  %.016 = phi i1 [ false, %10 ], [ false, %48 ], [ true, %._crit_edge ]
  ret i1 %.016
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #2

attributes #0 = { 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 #1 = { mustprogress nofree 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 nosync nounwind speculatable willreturn memory(none) }

!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, !9, i64 20}
!6 = !{!"_ZTS6SqList", !7, i64 0, !9, i64 20}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!9, !9, i64 0}
!11 = distinct !{!11, !12, !13, !14}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!"llvm.loop.isvectorized", i32 1}
!14 = !{!"llvm.loop.unroll.runtime.disable"}
!15 = distinct !{!15, !12, !13}


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