; LLVM IR from upupming/algorithm :: acwing/寒假每日一题（提高组）/week3/730. 机器人跳跃问题.ll (Z5validi)

; 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
@h = external dso_local global [100010 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z5validi(i32 noundef %0) #0 {
  %2 = alloca i1, align 1
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 1, ptr %4, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %46, %1
  %7 = load i32, ptr %4, align 4, !tbaa !5
  %8 = load i32, ptr @n, align 4, !tbaa !5
  %9 = icmp sle i32 %7, %8
  br i1 %9, label %11, label %10

10:                                               ; preds = %6
  store i32 2, ptr %5, align 4
  br label %49

11:                                               ; preds = %6
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [100010 x i32], ptr @h, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = icmp sgt i32 %15, %16
  br i1 %17, label %18, label %27

18:                                               ; preds = %11
  %19 = load i32, ptr %4, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [100010 x i32], ptr @h, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = load i32, ptr %3, align 4, !tbaa !5
  %24 = sub nsw i32 %22, %23
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = sub nsw i32 %25, %24
  store i32 %26, ptr %3, align 4, !tbaa !5
  br label %36

27:                                               ; preds = %11
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [100010 x i32], ptr @h, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = sub nsw i32 %28, %32
  %34 = load i32, ptr %3, align 4, !tbaa !5
  %35 = add nsw i32 %34, %33
  store i32 %35, ptr %3, align 4, !tbaa !5
  br label %36

36:                                               ; preds = %27, %18
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = sitofp i32 %37 to double
  %39 = fcmp ogt double %38, 1.000000e+05
  br i1 %39, label %40, label %41

40:                                               ; preds = %36
  store i1 true, ptr %2, align 1
  store i32 1, ptr %5, align 4
  br label %49

41:                                               ; preds = %36
  %42 = load i32, ptr %3, align 4, !tbaa !5
  %43 = icmp slt i32 %42, 0
  br i1 %43, label %44, label %45

44:                                               ; preds = %41
  store i1 false, ptr %2, align 1
  store i32 1, ptr %5, align 4
  br label %49

45:                                               ; preds = %41
  br label %46

46:                                               ; preds = %45
  %47 = load i32, ptr %4, align 4, !tbaa !5
  %48 = add nsw i32 %47, 1
  store i32 %48, ptr %4, align 4, !tbaa !5
  br label %6, !llvm.loop !9

49:                                               ; preds = %44, %40, %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %50 = load i32, ptr %5, align 4
  switch i32 %50, label %54 [
    i32 2, label %51
    i32 1, label %52
  ]

51:                                               ; preds = %49
  store i1 true, ptr %2, align 1
  br label %52

52:                                               ; preds = %51, %49
  %53 = load i1, ptr %2, align 1
  ret i1 %53

54:                                               ; preds = %49
  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 = { 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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"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"

@n = external dso_local local_unnamed_addr global i32, align 4
@h = external dso_local local_unnamed_addr global [100010 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i1 @_Z5validi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %.not19 = icmp slt i32 %2, 1
  br i1 %.not19, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %1
  %3 = zext nneg i32 %2 to i64
  %4 = add nuw i32 %2, 1
  %wide.trip.count = zext i32 %4 to i64
  %5 = load i32, ptr getelementptr inbounds (i8, ptr @h, i64 4), align 4, !tbaa !5
  %.neg30 = sub i32 %0, %5
  %6 = add i32 %.neg30, %0
  %7 = icmp sgt i32 %6, 100000
  br i1 %7, label %._crit_edge, label %.lr.ph33

8:                                                ; preds = %.lr.ph33
  %indvars.iv.next = add nuw nsw i64 %indvars.iv31, 1
  %.not = icmp uge i64 %indvars.iv31, %3
  %exitcond = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond, label %._crit_edge.loopexit.loopexit, label %.lr.ph, !llvm.loop !9

.lr.ph:                                           ; preds = %8
  %9 = getelementptr inbounds [100010 x i32], ptr @h, i64 0, i64 %indvars.iv.next
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %.neg = sub i32 %13, %10
  %11 = add i32 %.neg, %13
  %12 = icmp sgt i32 %11, 100000
  br i1 %12, label %._crit_edge.loopexit.loopexit, label %.lr.ph33, !llvm.loop !9

.lr.ph33:                                         ; preds = %.lr.ph.preheader, %.lr.ph
  %13 = phi i32 [ %11, %.lr.ph ], [ %6, %.lr.ph.preheader ]
  %.not2232 = phi i1 [ %.not, %.lr.ph ], [ false, %.lr.ph.preheader ]
  %indvars.iv31 = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 1, %.lr.ph.preheader ]
  %14 = icmp slt i32 %13, 0
  br i1 %14, label %._crit_edge.loopexit.loopexit, label %8

._crit_edge.loopexit.loopexit:                    ; preds = %.lr.ph33, %.lr.ph, %8
  %.not.lcssa.ph.ph = phi i1 [ %.not, %8 ], [ %.not, %.lr.ph ], [ %.not2232, %.lr.ph33 ]
  %.015.ph.ph = phi i1 [ undef, %8 ], [ true, %.lr.ph ], [ false, %.lr.ph33 ]
  %15 = or i1 %.not.lcssa.ph.ph, %.015.ph.ph
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.preheader, %._crit_edge.loopexit.loopexit, %1
  %spec.select = phi i1 [ true, %1 ], [ true, %.lr.ph.preheader ], [ %15, %._crit_edge.loopexit.loopexit ]
  ret i1 %spec.select
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) 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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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