; LLVM IR from andreimaximov/algorithms :: codeforces/karen-and-supermarket/main.ll (Z5itemsv)

; 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
@b = external dso_local global i32, align 4
@F = external dso_local global [5000 x [5001 x i64]], align 16
@G = external dso_local global [5000 x [5001 x i64]], align 16
@_ZL3INF = external hidden constant i32, align 4

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z3dfsi(i32 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 i32 @_Z5itemsv() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  call void @_ZSt4fillIPliEvT_S1_RKT0_(ptr noundef @F, ptr noundef getelementptr inbounds (i64, ptr @F, i64 25005000), ptr noundef nonnull align 4 dereferenceable(4) @_ZL3INF)
  call void @_ZSt4fillIPliEvT_S1_RKT0_(ptr noundef @G, ptr noundef getelementptr inbounds (i64, ptr @G, i64 25005000), ptr noundef nonnull align 4 dereferenceable(4) @_ZL3INF)
  call void @_Z3dfsi(i32 noundef 0)
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  store i32 0, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %5 = load i32, ptr @n, align 4, !tbaa !5
  store i32 %5, ptr %3, align 4, !tbaa !5
  br label %6

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

10:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = load i32, ptr %2, align 4, !tbaa !5
  %14 = sub nsw i32 %12, %13
  %15 = sdiv i32 %14, 2
  %16 = add nsw i32 %11, %15
  store i32 %16, ptr %4, align 4, !tbaa !5
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [5001 x i64], ptr @F, i64 0, i64 %18
  %20 = load i64, ptr %19, align 8, !tbaa !9
  %21 = load i32, ptr @b, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = icmp sle i64 %20, %22
  br i1 %23, label %32, label %24

24:                                               ; preds = %10
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [5001 x i64], ptr @G, i64 0, i64 %26
  %28 = load i64, ptr %27, align 8, !tbaa !9
  %29 = load i32, ptr @b, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = icmp sle i64 %28, %30
  br i1 %31, label %32, label %36

32:                                               ; preds = %24, %10
  %33 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %33, ptr %1, align 4, !tbaa !5
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %2, align 4, !tbaa !5
  br label %39

36:                                               ; preds = %24
  %37 = load i32, ptr %4, align 4, !tbaa !5
  %38 = sub nsw i32 %37, 1
  store i32 %38, ptr %3, align 4, !tbaa !5
  br label %39

39:                                               ; preds = %36, %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %6, !llvm.loop !11

40:                                               ; preds = %6
  %41 = load i32, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret i32 %41
}

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

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 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", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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
@b = external dso_local local_unnamed_addr global i32, align 4
@F = external dso_local global [5000 x [5001 x i64]], align 16
@G = external dso_local global [5000 x [5001 x i64]], align 16
@_ZL3INF = external hidden constant i32, align 4

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z3dfsi(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z5itemsv() local_unnamed_addr #0 {
  tail call void @_ZSt4fillIPliEvT_S1_RKT0_(ptr noundef nonnull @F, ptr noundef nonnull getelementptr inbounds (i8, ptr @F, i64 200040000), ptr noundef nonnull align 4 dereferenceable(4) @_ZL3INF)
  tail call void @_ZSt4fillIPliEvT_S1_RKT0_(ptr noundef nonnull @G, ptr noundef nonnull getelementptr inbounds (i8, ptr @G, i64 200040000), ptr noundef nonnull align 4 dereferenceable(4) @_ZL3INF)
  tail call void @_Z3dfsi(i32 noundef 0)
  %1 = load i32, ptr @n, align 4, !tbaa !5
  %.not18 = icmp slt i32 %1, 0
  br i1 %.not18, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0
  %2 = load i32, ptr @b, align 4, !tbaa !5
  %3 = sext i32 %2 to i64
  br label %4

4:                                                ; preds = %.lr.ph, %18
  %.021 = phi i32 [ 0, %.lr.ph ], [ %.1, %18 ]
  %.01120 = phi i32 [ %1, %.lr.ph ], [ %.112, %18 ]
  %.01319 = phi i32 [ 0, %.lr.ph ], [ %.114, %18 ]
  %5 = sub nsw i32 %.01120, %.01319
  %6 = sdiv i32 %5, 2
  %7 = add nsw i32 %6, %.01319
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds [5001 x i64], ptr @F, i64 0, i64 %8
  %10 = load i64, ptr %9, align 8, !tbaa !9
  %.not16 = icmp sgt i64 %10, %3
  br i1 %.not16, label %11, label %14

11:                                               ; preds = %4
  %12 = getelementptr inbounds [5001 x i64], ptr @G, i64 0, i64 %8
  %13 = load i64, ptr %12, align 8, !tbaa !9
  %.not17 = icmp sgt i64 %13, %3
  br i1 %.not17, label %16, label %14

14:                                               ; preds = %11, %4
  %15 = add nsw i32 %7, 1
  br label %18

16:                                               ; preds = %11
  %17 = add nsw i32 %7, -1
  br label %18

18:                                               ; preds = %16, %14
  %.114 = phi i32 [ %15, %14 ], [ %.01319, %16 ]
  %.112 = phi i32 [ %.01120, %14 ], [ %17, %16 ]
  %.1 = phi i32 [ %7, %14 ], [ %.021, %16 ]
  %.not = icmp sgt i32 %.114, %.112
  br i1 %.not, label %._crit_edge, label %4, !llvm.loop !11

._crit_edge:                                      ; preds = %18, %0
  %.0.lcssa = phi i32 [ 0, %0 ], [ %.1, %18 ]
  ret i32 %.0.lcssa
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4fillIPliEvT_S1_RKT0_(ptr noundef, ptr noundef, ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #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 = { inlinehint 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" }

!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", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass