; LLVM IR from Tunghohin/Competitive_Coding :: Codeforces/cf1741_C.ll (Z4findix)

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

@n = external dso_local global i32, align 4
@a = external dso_local global [2010 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z4findix(i32 noundef %0, i64 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = sub nsw i32 %12, 1
  store i32 %13, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %14 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %14, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %15 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %15, ptr %8, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %59, %2
  %17 = load i32, ptr %7, align 4, !tbaa !5
  %18 = load i32, ptr @n, align 4, !tbaa !5
  %19 = icmp sle i32 %17, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %16
  store i32 2, ptr %9, align 4
  br label %62

21:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %22

22:                                               ; preds = %32, %21
  %23 = load i64, ptr %10, align 8, !tbaa !9
  %24 = load i64, ptr %5, align 8, !tbaa !9
  %25 = icmp slt i64 %23, %24
  br i1 %25, label %26, label %30

26:                                               ; preds = %22
  %27 = load i32, ptr %8, align 4, !tbaa !5
  %28 = load i32, ptr @n, align 4, !tbaa !5
  %29 = icmp sle i32 %27, %28
  br label %30

30:                                               ; preds = %26, %22
  %31 = phi i1 [ false, %22 ], [ %29, %26 ]
  br i1 %31, label %32, label %41

32:                                               ; preds = %30
  %33 = load i32, ptr %8, align 4, !tbaa !5
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr %8, align 4, !tbaa !5
  %35 = sext i32 %33 to i64
  %36 = getelementptr inbounds [2010 x i32], ptr @a, i64 0, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = load i64, ptr %10, align 8, !tbaa !9
  %40 = add nsw i64 %39, %38
  store i64 %40, ptr %10, align 8, !tbaa !9
  br label %22, !llvm.loop !11

41:                                               ; preds = %30
  %42 = load i64, ptr %10, align 8, !tbaa !9
  %43 = load i64, ptr %5, align 8, !tbaa !9
  %44 = icmp ne i64 %42, %43
  br i1 %44, label %45, label %47

45:                                               ; preds = %41
  %46 = load i32, ptr @n, align 4, !tbaa !5
  store i32 %46, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %56

47:                                               ; preds = %41
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %48 = load i32, ptr %8, align 4, !tbaa !5
  %49 = load i32, ptr %7, align 4, !tbaa !5
  %50 = sub nsw i32 %48, %49
  store i32 %50, ptr %11, align 4, !tbaa !5
  %51 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %6, ptr noundef nonnull align 4 dereferenceable(4) %11)
  %52 = load i32, ptr %51, align 4, !tbaa !5
  store i32 %52, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  %53 = load i32, ptr %8, align 4, !tbaa !5
  %54 = sub nsw i32 %53, 1
  store i32 %54, ptr %7, align 4, !tbaa !5
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %55

55:                                               ; preds = %47
  store i32 0, ptr %9, align 4
  br label %56

56:                                               ; preds = %55, %45
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %57 = load i32, ptr %9, align 4
  switch i32 %57, label %62 [
    i32 0, label %58
  ]

58:                                               ; preds = %56
  br label %59

59:                                               ; preds = %58
  %60 = load i32, ptr %7, align 4, !tbaa !5
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %7, align 4, !tbaa !5
  br label %16, !llvm.loop !13

62:                                               ; preds = %56, %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  %63 = load i32, ptr %9, align 4
  switch i32 %63, label %66 [
    i32 2, label %64
  ]

64:                                               ; preds = %62
  %65 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %65, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %66

66:                                               ; preds = %64, %62
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %67 = load i32, ptr %3, align 4
  ret i32 %67
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #2

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint mustprogress 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 #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 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

@n = external dso_local local_unnamed_addr global i32, align 4
@a = external dso_local local_unnamed_addr global [2010 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z4findix(i32 noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #3
  %5 = add nsw i32 %0, -1
  store i32 %5, ptr %3, align 4, !tbaa !5
  %6 = load i32, ptr @n, align 4
  %.not34 = icmp slt i32 %6, %0
  br i1 %.not34, label %._crit_edge37, label %.preheader.lr.ph

.preheader.lr.ph:                                 ; preds = %2
  %7 = icmp sgt i64 %1, 0
  br i1 %7, label %.preheader.us, label %.preheader.lr.ph.split

.preheader.us:                                    ; preds = %.preheader.lr.ph, %11
  %8 = phi i32 [ %17, %11 ], [ %6, %.preheader.lr.ph ]
  %.02035.us = phi i32 [ %12, %11 ], [ %0, %.preheader.lr.ph ]
  %9 = sext i32 %.02035.us to i64
  %10 = sext i32 %8 to i64
  br label %18

11:                                               ; preds = %._crit_edge.us
  %12 = trunc nsw i64 %indvars.iv.next to i32
  %13 = trunc nsw i64 %indvars.iv to i32
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #3
  %14 = sub nsw i32 %12, %.02035.us
  store i32 %14, ptr %4, align 4, !tbaa !5
  %15 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %3, ptr noundef nonnull align 4 dereferenceable(4) %4)
  %16 = load i32, ptr %15, align 4, !tbaa !5
  store i32 %16, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #3
  %17 = load i32, ptr @n, align 4
  %.not.us.not = icmp sgt i32 %17, %13
  br i1 %.not.us.not, label %.preheader.us, label %._crit_edge37, !llvm.loop !9

18:                                               ; preds = %.preheader.us, %18
  %indvars.iv = phi i64 [ %9, %.preheader.us ], [ %indvars.iv.next, %18 ]
  %.032.us = phi i64 [ 0, %.preheader.us ], [ %22, %18 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %19 = getelementptr inbounds [2010 x i32], ptr @a, i64 0, i64 %indvars.iv
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = add nsw i64 %.032.us, %21
  %23 = icmp slt i64 %22, %1
  %24 = icmp slt i64 %indvars.iv, %10
  %25 = and i1 %23, %24
  br i1 %25, label %18, label %._crit_edge.us, !llvm.loop !11

._crit_edge.us:                                   ; preds = %18
  %.not26.us = icmp eq i64 %22, %1
  br i1 %.not26.us, label %11, label %._crit_edge37

.preheader.lr.ph.split:                           ; preds = %.preheader.lr.ph
  %.not26 = icmp eq i64 %1, 0
  br i1 %.not26, label %.preheader.us48, label %._crit_edge37

.preheader.us48:                                  ; preds = %.preheader.lr.ph.split, %.preheader.us48
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #3
  store i32 0, ptr %4, align 4, !tbaa !5
  %26 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %3, ptr noundef nonnull align 4 dereferenceable(4) %4)
  %27 = load i32, ptr %26, align 4, !tbaa !5
  store i32 %27, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #3
  %28 = load i32, ptr @n, align 4
  %.not.us47 = icmp slt i32 %28, %0
  br i1 %.not.us47, label %._crit_edge37, label %.preheader.us48, !llvm.loop !9

._crit_edge37:                                    ; preds = %.preheader.us48, %11, %._crit_edge.us, %.preheader.lr.ph.split, %2
  %.not.lcssa = phi i32 [ %5, %2 ], [ %6, %.preheader.lr.ph.split ], [ %8, %._crit_edge.us ], [ %16, %11 ], [ %27, %.preheader.us48 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #3
  ret i32 %.not.lcssa
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #2

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

attributes #0 = { mustprogress 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint mustprogress 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 #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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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