; LLVM IR from abeaumont/competitive-programming :: cses/1093.ll (Z1fii)

; 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
@t = external dso_local global i32, align 4
@c = external dso_local global [501 x [250001 x i32]], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z1fii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  %7 = load i32, ptr %4, align 4, !tbaa !5
  %8 = load i32, ptr @n, align 4, !tbaa !5
  %9 = icmp eq i32 %7, %8
  br i1 %9, label %10, label %15

10:                                               ; preds = %2
  %11 = load i32, ptr %5, align 4, !tbaa !5
  %12 = load i32, ptr @t, align 4, !tbaa !5
  %13 = icmp eq i32 %11, %12
  %14 = zext i1 %13 to i64
  store i64 %14, ptr %3, align 8
  br label %64

15:                                               ; preds = %2
  %16 = load i32, ptr %4, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [501 x [250001 x i32]], ptr @c, i64 0, i64 %17
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [250001 x i32], ptr %18, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = icmp ne i32 %22, -1
  br i1 %23, label %24, label %33

24:                                               ; preds = %15
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [501 x [250001 x i32]], ptr @c, i64 0, i64 %26
  %28 = load i32, ptr %5, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [250001 x i32], ptr %27, i64 0, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  store i64 %32, ptr %3, align 8
  br label %64

33:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = add nsw i32 %34, 1
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = call noundef i64 @_Z1fii(i32 noundef %35, i32 noundef %36)
  store i64 %37, ptr %6, align 8, !tbaa !9
  %38 = load i32, ptr %5, align 4, !tbaa !5
  %39 = load i32, ptr %4, align 4, !tbaa !5
  %40 = add nsw i32 %38, %39
  %41 = load i32, ptr @t, align 4, !tbaa !5
  %42 = icmp sle i32 %40, %41
  br i1 %42, label %43, label %52

43:                                               ; preds = %33
  %44 = load i32, ptr %4, align 4, !tbaa !5
  %45 = add nsw i32 %44, 1
  %46 = load i32, ptr %5, align 4, !tbaa !5
  %47 = load i32, ptr %4, align 4, !tbaa !5
  %48 = add nsw i32 %46, %47
  %49 = call noundef i64 @_Z1fii(i32 noundef %45, i32 noundef %48)
  %50 = load i64, ptr %6, align 8, !tbaa !9
  %51 = add nsw i64 %50, %49
  store i64 %51, ptr %6, align 8, !tbaa !9
  br label %52

52:                                               ; preds = %43, %33
  %53 = load i64, ptr %6, align 8, !tbaa !9
  %54 = srem i64 %53, 1000000007
  store i64 %54, ptr %6, align 8, !tbaa !9
  %55 = load i64, ptr %6, align 8, !tbaa !9
  %56 = trunc i64 %55 to i32
  %57 = load i32, ptr %4, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [501 x [250001 x i32]], ptr @c, i64 0, i64 %58
  %60 = load i32, ptr %5, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [250001 x i32], ptr %59, i64 0, i64 %61
  store i32 %56, ptr %62, align 4, !tbaa !5
  %63 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %63, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %64

64:                                               ; preds = %52, %24, %10
  %65 = load i64, ptr %3, align 8
  ret i64 %65
}

; 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 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 = !{!10, !10, i64 0}
!10 = !{!"long long", !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"

@n = external dso_local local_unnamed_addr global i32, align 4
@t = external dso_local local_unnamed_addr global i32, align 4
@c = external dso_local local_unnamed_addr global [501 x [250001 x i32]], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef range(i64 -2147483648, 2147483648) i64 @_Z1fii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load i32, ptr @n, align 4, !tbaa !5
  %4 = icmp eq i32 %3, %0
  br i1 %4, label %5, label %9

5:                                                ; preds = %2
  %6 = load i32, ptr @t, align 4, !tbaa !5
  %7 = icmp eq i32 %6, %1
  %8 = zext i1 %7 to i64
  br label %27

9:                                                ; preds = %2
  %10 = sext i32 %0 to i64
  %11 = sext i32 %1 to i64
  %12 = getelementptr inbounds [501 x [250001 x i32]], ptr @c, i64 0, i64 %10, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %.not = icmp eq i32 %13, -1
  br i1 %.not, label %16, label %14

14:                                               ; preds = %9
  %15 = sext i32 %13 to i64
  br label %27

16:                                               ; preds = %9
  %17 = add nsw i32 %0, 1
  %18 = tail call noundef i64 @_Z1fii(i32 noundef %17, i32 noundef %1)
  %19 = add nsw i32 %1, %0
  %20 = load i32, ptr @t, align 4, !tbaa !5
  %.not24 = icmp sgt i32 %19, %20
  br i1 %.not24, label %24, label %21

21:                                               ; preds = %16
  %22 = tail call noundef i64 @_Z1fii(i32 noundef %17, i32 noundef %19)
  %23 = add nsw i64 %22, %18
  br label %24

24:                                               ; preds = %21, %16
  %.0 = phi i64 [ %23, %21 ], [ %18, %16 ]
  %25 = srem i64 %.0, 1000000007
  %26 = trunc nsw i64 %25 to i32
  store i32 %26, ptr %12, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %24, %14, %5
  %.020 = phi i64 [ %8, %5 ], [ %15, %14 ], [ %25, %24 ]
  ret i64 %.020
}

attributes #0 = { mustprogress nofree 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" }

!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"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,TailCallElimPass