; LLVM IR from JessonYue/LeetCodeLearning :: Array/yangxin (array)

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

; Function Attrs: nounwind uwtable
define dso_local i32 @findFirst(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = icmp eq ptr %12, null
  br i1 %13, label %18, label %14

14:                                               ; preds = %4
  %15 = load i32, ptr %8, align 4, !tbaa !9
  %16 = load i32, ptr %9, align 4, !tbaa !9
  %17 = icmp sge i32 %15, %16
  br i1 %17, label %18, label %19

18:                                               ; preds = %14, %4
  store i32 -1, ptr %5, align 4
  br label %79

19:                                               ; preds = %14
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = load i32, ptr %8, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds i32, ptr %20, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !9
  %25 = load i32, ptr %7, align 4, !tbaa !9
  %26 = icmp eq i32 %24, %25
  br i1 %26, label %27, label %29

27:                                               ; preds = %19
  %28 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %28, ptr %5, align 4
  br label %79

29:                                               ; preds = %19
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = load i32, ptr %9, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds i32, ptr %30, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !9
  %35 = load i32, ptr %7, align 4, !tbaa !9
  %36 = icmp eq i32 %34, %35
  br i1 %36, label %37, label %39

37:                                               ; preds = %29
  %38 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %38, ptr %5, align 4
  br label %79

39:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %40 = load i32, ptr %9, align 4, !tbaa !9
  %41 = load i32, ptr %8, align 4, !tbaa !9
  %42 = add nsw i32 %40, %41
  %43 = sdiv i32 %42, 2
  store i32 %43, ptr %10, align 4, !tbaa !9
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = load i32, ptr %10, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i32, ptr %44, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !9
  %49 = load i32, ptr %7, align 4, !tbaa !9
  %50 = icmp eq i32 %48, %49
  br i1 %50, label %51, label %53

51:                                               ; preds = %39
  %52 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %52, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %77

53:                                               ; preds = %39
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = load i32, ptr %10, align 4, !tbaa !9
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds i32, ptr %54, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !9
  %59 = load i32, ptr %7, align 4, !tbaa !9
  %60 = icmp sgt i32 %58, %59
  br i1 %60, label %61, label %68

61:                                               ; preds = %53
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  %63 = load i32, ptr %7, align 4, !tbaa !9
  %64 = load i32, ptr %8, align 4, !tbaa !9
  %65 = load i32, ptr %10, align 4, !tbaa !9
  %66 = sub nsw i32 %65, 1
  %67 = call i32 @findFirst(ptr noundef %62, i32 noundef %63, i32 noundef %64, i32 noundef %66)
  br label %75

68:                                               ; preds = %53
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = load i32, ptr %7, align 4, !tbaa !9
  %71 = load i32, ptr %10, align 4, !tbaa !9
  %72 = add nsw i32 %71, 1
  %73 = load i32, ptr %9, align 4, !tbaa !9
  %74 = call i32 @findFirst(ptr noundef %69, i32 noundef %70, i32 noundef %72, i32 noundef %73)
  br label %75

75:                                               ; preds = %68, %61
  br label %76

76:                                               ; preds = %75
  store i32 0, ptr %11, align 4
  br label %77

77:                                               ; preds = %76, %51
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  %78 = load i32, ptr %11, align 4
  switch i32 %78, label %81 [
    i32 0, label %79
    i32 1, label %79
  ]

79:                                               ; preds = %77, %77, %37, %27, %18
  %80 = load i32, ptr %5, align 4
  ret i32 %80

81:                                               ; preds = %77
  unreachable
}

; 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

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 nosync nounwind willreturn memory(argmem: readwrite) }
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, !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 = !{!"int", !7, i64 0}


; 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: nofree norecurse nosync nounwind memory(argmem: read) uwtable
define dso_local i32 @findFirst(ptr noundef readonly %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = icmp ne ptr %0, null
  %.not40100 = icmp slt i32 %2, %3
  %or.cond41101 = and i1 %5, %.not40100
  br i1 %or.cond41101, label %.lr.ph.us, label %tailrecurse.outer._crit_edge.thread165

.lr.ph.us:                                        ; preds = %4, %.split.us112.thread
  %ret.known.tr.ph105.us = phi i1 [ true, %.split.us112.thread ], [ false, %4 ]
  %ret.tr.ph104.us = phi i32 [ %current.ret.tr.us114.peel, %.split.us112.thread ], [ poison, %4 ]
  %.tr34.ph103.us = phi i32 [ %.tr3442.us111.lcssa146159, %.split.us112.thread ], [ %3, %4 ]
  %.tr33.ph102.us = phi i32 [ %37, %.split.us112.thread ], [ %2, %4 ]
  %6 = sext i32 %.tr33.ph102.us to i64
  %7 = getelementptr inbounds i32, ptr %0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = icmp eq i32 %8, %1
  br i1 %9, label %tailrecurse.outer._crit_edge, label %.lr.ph.split.us115.preheader

.lr.ph.split.us115.preheader:                     ; preds = %.lr.ph.us
  %10 = sext i32 %.tr34.ph103.us to i64
  %11 = getelementptr inbounds i32, ptr %0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = icmp eq i32 %12, %1
  br i1 %13, label %tailrecurse.outer._crit_edge, label %14

14:                                               ; preds = %.lr.ph.split.us115.preheader
  %15 = add nsw i32 %.tr34.ph103.us, %.tr33.ph102.us
  %16 = sdiv i32 %15, 2
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds i32, ptr %0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = icmp eq i32 %19, %1
  br i1 %20, label %tailrecurse.outer._crit_edge, label %21

21:                                               ; preds = %14
  %22 = icmp sgt i32 %19, %1
  %current.ret.tr.us114.peel = select i1 %ret.known.tr.ph105.us, i32 %ret.tr.ph104.us, i32 %16
  br i1 %22, label %tailrecurse.us113.peel, label %.split.us112.thread

tailrecurse.us113.peel:                           ; preds = %21
  %23 = add nsw i32 %16, -1
  %.not.us.peel = icmp slt i32 %.tr33.ph102.us, %23
  br i1 %.not.us.peel, label %.lr.ph.split.us115, label %tailrecurse.outer._crit_edge.thread165

.lr.ph.split.us115:                               ; preds = %tailrecurse.us113.peel, %tailrecurse.us113
  %.tr3442.us111 = phi i32 [ %38, %tailrecurse.us113 ], [ %23, %tailrecurse.us113.peel ]
  %24 = sext i32 %.tr3442.us111 to i64
  %25 = getelementptr inbounds i32, ptr %0, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = icmp eq i32 %26, %1
  br i1 %27, label %tailrecurse.outer._crit_edge.thread165, label %28

28:                                               ; preds = %.lr.ph.split.us115
  %29 = add nsw i32 %.tr3442.us111, %.tr33.ph102.us
  %30 = sdiv i32 %29, 2
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i32, ptr %0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = icmp eq i32 %33, %1
  br i1 %34, label %tailrecurse.outer._crit_edge.thread165, label %35

35:                                               ; preds = %28
  %36 = icmp sgt i32 %33, %1
  br i1 %36, label %tailrecurse.us113, label %.split.us112.thread

.split.us112.thread:                              ; preds = %35, %21
  %.sink = phi i32 [ %16, %21 ], [ %30, %35 ]
  %.tr3442.us111.lcssa146159 = phi i32 [ %.tr34.ph103.us, %21 ], [ %.tr3442.us111, %35 ]
  %37 = add nsw i32 %.sink, 1
  %.not40.us = icmp slt i32 %37, %.tr3442.us111.lcssa146159
  br i1 %.not40.us, label %.lr.ph.us, label %tailrecurse.outer._crit_edge.thread165

tailrecurse.us113:                                ; preds = %35
  %38 = add nsw i32 %30, -1
  %.not.us = icmp slt i32 %.tr33.ph102.us, %38
  br i1 %.not.us, label %.lr.ph.split.us115, label %tailrecurse.outer._crit_edge.thread165, !llvm.loop !9

tailrecurse.outer._crit_edge:                     ; preds = %.lr.ph.us, %.lr.ph.split.us115.preheader, %14
  %.1 = phi i32 [ %16, %14 ], [ %.tr34.ph103.us, %.lr.ph.split.us115.preheader ], [ %.tr33.ph102.us, %.lr.ph.us ]
  %spec.select175 = select i1 %ret.known.tr.ph105.us, i32 %ret.tr.ph104.us, i32 %.1
  br label %tailrecurse.outer._crit_edge.thread165

tailrecurse.outer._crit_edge.thread165:           ; preds = %.split.us112.thread, %tailrecurse.us113.peel, %28, %.lr.ph.split.us115, %tailrecurse.us113, %tailrecurse.outer._crit_edge, %4
  %39 = phi i32 [ -1, %4 ], [ %spec.select175, %tailrecurse.outer._crit_edge ], [ %current.ret.tr.us114.peel, %tailrecurse.us113 ], [ %current.ret.tr.us114.peel, %.lr.ph.split.us115 ], [ %current.ret.tr.us114.peel, %28 ], [ %current.ret.tr.us114.peel, %tailrecurse.us113.peel ], [ %current.ret.tr.us114.peel, %.split.us112.thread ]
  ret i32 %39
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: read) 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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.peeled.count", i32 1}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopFullUnrollPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass