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

; 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.8 = 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 @_Z10LocateElem6SqListiRi(ptr noundef byval(%struct.SqList) align 8 %0, i32 noundef %1, ptr noundef nonnull align 4 dereferenceable(4) %2) #2 {
  %4 = alloca i1, align 1
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 %1, ptr %5, align 4, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %28, %3
  %10 = load i32, ptr %7, align 4, !tbaa !5
  %11 = getelementptr inbounds %struct.SqList, ptr %0, i32 0, i32 1
  %12 = load i32, ptr %11, align 4, !tbaa !11
  %13 = icmp slt i32 %10, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %9
  store i32 2, ptr %8, align 4
  br label %31

15:                                               ; preds = %9
  %16 = getelementptr inbounds %struct.SqList, ptr %0, i32 0, i32 0
  %17 = load i32, ptr %7, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [5 x i32], ptr %16, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = load i32, ptr %5, align 4, !tbaa !5
  %22 = icmp eq i32 %20, %21
  br i1 %22, label %23, label %27

23:                                               ; preds = %15
  %24 = load i32, ptr %7, align 4, !tbaa !5
  %25 = add nsw i32 %24, 1
  %26 = load ptr, ptr %6, align 8, !tbaa !9
  store i32 %25, ptr %26, align 4, !tbaa !5
  store i1 true, ptr %4, align 1
  store i32 1, ptr %8, align 4
  br label %31

27:                                               ; preds = %15
  br label %28

28:                                               ; preds = %27
  %29 = load i32, ptr %7, align 4, !tbaa !5
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %7, align 4, !tbaa !5
  br label %9, !llvm.loop !13

31:                                               ; preds = %23, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  %32 = load i32, ptr %8, align 4
  switch i32 %32, label %37 [
    i32 2, label %33
    i32 1, label %35
  ]

33:                                               ; preds = %31
  %34 = call i32 (ptr, ...) @printf(ptr noundef @.str.8)
  store i1 false, ptr %4, align 1
  br label %35

35:                                               ; preds = %33, %31
  %36 = load i1, ptr %4, align 1
  ret i1 %36

37:                                               ; preds = %31
  unreachable
}

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 20}
!12 = !{!"_ZTS6SqList", !7, i64 0, !6, 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"

%struct.SqList = type { [5 x i32], i32 }

@.str.8 = 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 @_Z10LocateElem6SqListiRi(ptr nocapture noundef readonly byval(%struct.SqList) align 8 %0, i32 noundef %1, ptr nocapture noundef nonnull writeonly align 4 dereferenceable(4) %2) local_unnamed_addr #1 {
  %4 = getelementptr inbounds i8, ptr %0, i64 20
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %3
  %7 = zext nneg i32 %5 to i64
  %wide.trip.count = zext nneg i32 %5 to i64
  %8 = load i32, ptr %0, align 8, !tbaa !10
  %9 = icmp eq i32 %8, %1
  br i1 %9, label %16, label %.lr.ph28

.lr.ph:                                           ; preds = %.lr.ph28
  %10 = getelementptr inbounds [5 x i32], ptr %0, i64 0, i64 %indvars.iv.next
  %11 = load i32, ptr %10, align 4, !tbaa !10
  %12 = icmp eq i32 %11, %1
  br i1 %12, label %.lr.ph._crit_edge, label %.lr.ph28, !llvm.loop !11

.lr.ph28:                                         ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv27 = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv27, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !11

.lr.ph._crit_edge:                                ; preds = %.lr.ph
  %13 = icmp ult i64 %indvars.iv.next, %7
  %14 = trunc nuw nsw i64 %indvars.iv.next to i32
  %15 = add nuw nsw i32 %14, 1
  br label %16

16:                                               ; preds = %.lr.ph._crit_edge, %.lr.ph.preheader
  %indvars.iv.lcssa = phi i32 [ %15, %.lr.ph._crit_edge ], [ 1, %.lr.ph.preheader ]
  %.lcssa23 = phi i1 [ %13, %.lr.ph._crit_edge ], [ true, %.lr.ph.preheader ]
  store i32 %indvars.iv.lcssa, ptr %2, align 4, !tbaa !10
  br label %19

._crit_edge.loopexit:                             ; preds = %.lr.ph28
  %17 = icmp ult i64 %indvars.iv.next, %7
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %3
  %.lcssa = phi i1 [ false, %3 ], [ %17, %._crit_edge.loopexit ]
  %18 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.8)
  br label %19

19:                                               ; preds = %16, %._crit_edge
  %20 = phi i1 [ %.lcssa23, %16 ], [ %.lcssa, %._crit_edge ]
  ret i1 %20
}

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

!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}
!12 = !{!"llvm.loop.mustprogress"}


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