; LLVM IR from eyangch/competitive-programming :: Codeforces/1311/d.ll (Z5testAi)

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

@A = external dso_local global i32, align 4
@cost = external dso_local global [20001 x i32], align 16
@ans = external dso_local global i32, align 4
@aid = external dso_local global i32, align 4
@bid = external dso_local global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z5testAi(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 20000
  br i1 %6, label %17, label %7

7:                                                ; preds = %1
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = icmp sle i32 %8, 0
  br i1 %9, label %17, label %10

10:                                               ; preds = %7
  %11 = load i32, ptr @A, align 4, !tbaa !5
  %12 = load i32, ptr %2, align 4, !tbaa !5
  %13 = sub nsw i32 %11, %12
  %14 = call i32 @llvm.abs.i32(i32 %13, i1 true)
  %15 = load i32, ptr @ans, align 4, !tbaa !5
  %16 = icmp sgt i32 %14, %15
  br i1 %16, label %17, label %18

17:                                               ; preds = %10, %7, %1
  br label %46

18:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %19 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %19, ptr %3, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %42, %18
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = icmp sle i32 %21, 20000
  br i1 %22, label %24, label %23

23:                                               ; preds = %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %46

24:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %25 = load i32, ptr @A, align 4, !tbaa !5
  %26 = load i32, ptr %2, align 4, !tbaa !5
  %27 = sub nsw i32 %25, %26
  %28 = call i32 @llvm.abs.i32(i32 %27, i1 true)
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [20001 x i32], ptr @cost, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = add nsw i32 %28, %32
  store i32 %33, ptr %4, align 4, !tbaa !5
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = load i32, ptr @ans, align 4, !tbaa !5
  %36 = icmp slt i32 %34, %35
  br i1 %36, label %37, label %41

37:                                               ; preds = %24
  %38 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %38, ptr @aid, align 4, !tbaa !5
  %39 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %39, ptr @bid, align 4, !tbaa !5
  %40 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %40, ptr @ans, align 4, !tbaa !5
  br label %41

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

42:                                               ; preds = %41
  %43 = load i32, ptr %2, align 4, !tbaa !5
  %44 = load i32, ptr %3, align 4, !tbaa !5
  %45 = add nsw i32 %44, %43
  store i32 %45, ptr %3, align 4, !tbaa !5
  br label %20, !llvm.loop !9

46:                                               ; preds = %23, %17
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #1

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

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

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 speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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"}


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

@A = external dso_local local_unnamed_addr global i32, align 4
@cost = external dso_local local_unnamed_addr global [20001 x i32], align 16
@ans = external dso_local local_unnamed_addr global i32, align 4
@aid = external dso_local local_unnamed_addr global i32, align 4
@bid = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z5testAi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = add i32 %0, -20001
  %or.cond = icmp ult i32 %2, -20000
  br i1 %or.cond, label %.loopexit, label %3

3:                                                ; preds = %1
  %4 = load i32, ptr @A, align 4, !tbaa !5
  %5 = sub nsw i32 %4, %0
  %6 = tail call i32 @llvm.abs.i32(i32 %5, i1 true)
  %7 = load i32, ptr @ans, align 4
  %.not = icmp sgt i32 %6, %7
  br i1 %.not, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %8 = zext nneg i32 %0 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %16
  %indvars.iv = phi i64 [ %8, %.lr.ph.preheader ], [ %indvars.iv.next, %16 ]
  %9 = phi i32 [ %7, %.lr.ph.preheader ], [ %17, %16 ]
  %10 = getelementptr inbounds [20001 x i32], ptr @cost, i64 0, i64 %indvars.iv
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = add nsw i32 %11, %6
  %13 = icmp slt i32 %12, %9
  br i1 %13, label %14, label %16

14:                                               ; preds = %.lr.ph
  store i32 %0, ptr @aid, align 4, !tbaa !5
  %15 = trunc nuw i64 %indvars.iv to i32
  store i32 %15, ptr @bid, align 4, !tbaa !5
  store i32 %12, ptr @ans, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %14, %.lr.ph
  %17 = phi i32 [ %12, %14 ], [ %9, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, %8
  %18 = trunc nuw i64 %indvars.iv.next to i32
  %19 = icmp slt i32 %18, 20001
  br i1 %19, label %.lr.ph, label %.loopexit, !llvm.loop !9

.loopexit:                                        ; preds = %16, %1, %3
  ret void
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, 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" }
attributes #1 = { mustprogress 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, !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,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass