; LLVM IR from eyangch/competitive-programming :: Codeforces/1547/f.ll (Z5worksx)

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

@st = external dso_local global ptr, align 8
@N = external dso_local global i64, align 8

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

14:                                               ; preds = %65, %1
  %15 = load i64, ptr %5, align 8, !tbaa !5
  %16 = load i64, ptr @N, align 8, !tbaa !5
  %17 = icmp slt i64 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %14
  store i32 2, ptr %6, align 4
  br label %68

19:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %20 = load i64, ptr %5, align 8, !tbaa !5
  store i64 %20, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %21 = load i64, ptr @N, align 8, !tbaa !5
  %22 = sub nsw i64 %21, 1
  store i64 %22, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %23 = load i64, ptr %5, align 8, !tbaa !5
  %24 = load i64, ptr %3, align 8, !tbaa !5
  %25 = add nsw i64 %23, %24
  store i64 %25, ptr %10, align 8, !tbaa !5
  %26 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %9, ptr noundef nonnull align 8 dereferenceable(8) %10)
  %27 = load i64, ptr %26, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  store i64 %27, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  store i64 0, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %28 = load i64, ptr %5, align 8, !tbaa !5
  %29 = load i64, ptr %3, align 8, !tbaa !5
  %30 = add nsw i64 %28, %29
  %31 = load i64, ptr @N, align 8, !tbaa !5
  %32 = icmp sge i64 %30, %31
  %33 = zext i1 %32 to i64
  %34 = load i64, ptr %5, align 8, !tbaa !5
  %35 = load i64, ptr %3, align 8, !tbaa !5
  %36 = add nsw i64 %34, %35
  %37 = load i64, ptr @N, align 8, !tbaa !5
  %38 = srem i64 %36, %37
  %39 = add nsw i64 1, %38
  %40 = mul nsw i64 %33, %39
  %41 = add nsw i64 -1, %40
  store i64 %41, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %42 = load ptr, ptr @st, align 8, !tbaa !9
  %43 = load i64, ptr %7, align 8, !tbaa !5
  %44 = load i64, ptr %8, align 8, !tbaa !5
  %45 = call noundef i64 @_ZN6sparse3qryExx(ptr noundef nonnull align 8 dereferenceable(33600016) %42, i64 noundef %43, i64 noundef %44)
  %46 = load ptr, ptr @st, align 8, !tbaa !9
  %47 = load i64, ptr %11, align 8, !tbaa !5
  %48 = load i64, ptr %12, align 8, !tbaa !5
  %49 = call noundef i64 @_ZN6sparse3qryExx(ptr noundef nonnull align 8 dereferenceable(33600016) %46, i64 noundef %47, i64 noundef %48)
  %50 = call noundef i64 @_ZSt3gcdIxxENSt11common_typeIJT_T0_EE4typeES1_S2_(i64 noundef %45, i64 noundef %49) #4
  store i64 %50, ptr %13, align 8, !tbaa !5
  %51 = load i64, ptr %4, align 8, !tbaa !5
  %52 = icmp eq i64 %51, -1
  br i1 %52, label %53, label %55

53:                                               ; preds = %19
  %54 = load i64, ptr %13, align 8, !tbaa !5
  store i64 %54, ptr %4, align 8, !tbaa !5
  br label %61

55:                                               ; preds = %19
  %56 = load i64, ptr %13, align 8, !tbaa !5
  %57 = load i64, ptr %4, align 8, !tbaa !5
  %58 = icmp ne i64 %56, %57
  br i1 %58, label %59, label %60

59:                                               ; preds = %55
  store i1 false, ptr %2, align 1
  store i32 1, ptr %6, align 4
  br label %62

60:                                               ; preds = %55
  br label %61

61:                                               ; preds = %60, %53
  store i32 0, ptr %6, align 4
  br label %62

62:                                               ; preds = %61, %59
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  %63 = load i32, ptr %6, align 4
  switch i32 %63, label %68 [
    i32 0, label %64
  ]

64:                                               ; preds = %62
  br label %65

65:                                               ; preds = %64
  %66 = load i64, ptr %5, align 8, !tbaa !5
  %67 = add nsw i64 %66, 1
  store i64 %67, ptr %5, align 8, !tbaa !5
  br label %14, !llvm.loop !11

68:                                               ; preds = %62, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  %69 = load i32, ptr %6, align 4
  switch i32 %69, label %71 [
    i32 2, label %70
  ]

70:                                               ; preds = %68
  store i1 true, ptr %2, align 1
  store i32 1, ptr %6, align 4
  br label %71

71:                                               ; preds = %70, %68
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %72 = load i1, ptr %2, align 1
  ret i1 %72
}

; 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 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #2

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZSt3gcdIxxENSt11common_typeIJT_T0_EE4typeES1_S2_(i64 noundef, i64 noundef) #3

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZN6sparse3qryExx(ptr noundef nonnull align 8 dereferenceable(33600016), i64 noundef, i64 noundef) #3 align 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 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 = { 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 #4 = { 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !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"

@st = external dso_local local_unnamed_addr global ptr, align 8
@N = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z5worksx(i64 noundef %0) local_unnamed_addr #0 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = load i64, ptr @N, align 8, !tbaa !5
  %.not2731 = icmp slt i64 %4, 1
  br i1 %.not2731, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %1, %.thread
  %5 = phi i64 [ %21, %.thread ], [ %4, %1 ]
  %.02033 = phi i64 [ %.22230, %.thread ], [ -1, %1 ]
  %.02332 = phi i64 [ %20, %.thread ], [ 0, %1 ]
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #3
  %6 = add nsw i64 %5, -1
  store i64 %6, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  %7 = add nsw i64 %.02332, %0
  store i64 %7, ptr %3, align 8, !tbaa !5
  %8 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %2, ptr noundef nonnull align 8 dereferenceable(8) %3)
  %9 = load i64, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #3
  %10 = load i64, ptr @N, align 8, !tbaa !5
  %.not = icmp slt i64 %7, %10
  %11 = srem i64 %7, %10
  %12 = select i1 %.not, i64 -1, i64 %11
  %13 = load ptr, ptr @st, align 8, !tbaa !9
  %14 = call noundef i64 @_ZN6sparse3qryExx(ptr noundef nonnull align 8 dereferenceable(33600016) %13, i64 noundef %.02332, i64 noundef %9)
  %15 = load ptr, ptr @st, align 8, !tbaa !9
  %16 = call noundef i64 @_ZN6sparse3qryExx(ptr noundef nonnull align 8 dereferenceable(33600016) %15, i64 noundef 0, i64 noundef %12)
  %17 = call noundef i64 @_ZSt3gcdIxxENSt11common_typeIJT_T0_EE4typeES1_S2_(i64 noundef %14, i64 noundef %16) #3
  %18 = icmp eq i64 %.02033, -1
  br i1 %18, label %.thread, label %19

19:                                               ; preds = %.lr.ph
  %.not26 = icmp eq i64 %17, %.02033
  br i1 %.not26, label %.thread, label %.critedge

.thread:                                          ; preds = %.lr.ph, %19
  %.22230 = phi i64 [ %.02033, %19 ], [ %17, %.lr.ph ]
  %20 = add nuw nsw i64 %.02332, 1
  %21 = load i64, ptr @N, align 8, !tbaa !5
  %.not27.not = icmp slt i64 %20, %21
  br i1 %.not27.not, label %.lr.ph, label %.critedge, !llvm.loop !11

.critedge:                                        ; preds = %19, %.thread, %1
  %.not27.lcssa = phi i1 [ true, %1 ], [ true, %.thread ], [ false, %19 ]
  ret i1 %.not27.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 8 dereferenceable(8) ptr @_ZSt3minIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) 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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZSt3gcdIxxENSt11common_typeIJT_T0_EE4typeES1_S2_(i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZN6sparse3qryExx(ptr noundef nonnull align 8 dereferenceable(33600016), i64 noundef, i64 noundef) local_unnamed_addr #0 align 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 = { 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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