; LLVM IR from VastoLorde95/Competitive-Programming :: CodeChef/LONG/JULY15/EGBOBRD (A)

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

@t = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@a = external dso_local global i32, align 4
@k = external dso_local global i64, align 8
@cnt = external dso_local global i64, align 8
@cur = external dso_local global i64, align 8
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %3 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @t)
  br label %4

4:                                                ; preds = %71, %0
  %5 = load i32, ptr @t, align 4, !tbaa !5
  %6 = add nsw i32 %5, -1
  store i32 %6, ptr @t, align 4, !tbaa !5
  %7 = icmp ne i32 %5, 0
  br i1 %7, label %8, label %74

8:                                                ; preds = %4
  %9 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef @n, ptr noundef @k)
  store i64 1, ptr @cnt, align 8, !tbaa !9
  %10 = load i64, ptr @k, align 8, !tbaa !9
  store i64 %10, ptr @cur, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %61, %8
  %12 = load i32, ptr %2, align 4, !tbaa !5
  %13 = load i32, ptr @n, align 4, !tbaa !5
  %14 = icmp slt i32 %12, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %64

16:                                               ; preds = %11
  %17 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @a)
  %18 = load i64, ptr @cur, align 8, !tbaa !9
  %19 = icmp sle i64 %18, 0
  br i1 %19, label %20, label %24

20:                                               ; preds = %16
  %21 = load i64, ptr @cnt, align 8, !tbaa !9
  %22 = add nsw i64 %21, 1
  store i64 %22, ptr @cnt, align 8, !tbaa !9
  %23 = load i64, ptr @k, align 8, !tbaa !9
  store i64 %23, ptr @cur, align 8, !tbaa !9
  br label %24

24:                                               ; preds = %20, %16
  %25 = load i32, ptr @a, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = load i64, ptr @cur, align 8, !tbaa !9
  %28 = icmp sgt i64 %26, %27
  br i1 %28, label %29, label %49

29:                                               ; preds = %24
  %30 = load i64, ptr @cur, align 8, !tbaa !9
  %31 = load i32, ptr @a, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = sub nsw i64 %32, %30
  %34 = trunc i64 %33 to i32
  store i32 %34, ptr @a, align 4, !tbaa !5
  %35 = load i64, ptr @k, align 8, !tbaa !9
  store i64 %35, ptr @cur, align 8, !tbaa !9
  %36 = load i64, ptr @cnt, align 8, !tbaa !9
  %37 = add nsw i64 %36, 1
  store i64 %37, ptr @cnt, align 8, !tbaa !9
  %38 = load i32, ptr @a, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = load i64, ptr @k, align 8, !tbaa !9
  %41 = sdiv i64 %39, %40
  %42 = load i64, ptr @cnt, align 8, !tbaa !9
  %43 = add nsw i64 %42, %41
  store i64 %43, ptr @cnt, align 8, !tbaa !9
  %44 = load i64, ptr @k, align 8, !tbaa !9
  %45 = load i32, ptr @a, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = srem i64 %46, %44
  %48 = trunc i64 %47 to i32
  store i32 %48, ptr @a, align 4, !tbaa !5
  br label %49

49:                                               ; preds = %29, %24
  %50 = load i32, ptr @a, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = load i64, ptr @cur, align 8, !tbaa !9
  %53 = sub nsw i64 %52, %51
  store i64 %53, ptr @cur, align 8, !tbaa !9
  %54 = load i64, ptr @cur, align 8, !tbaa !9
  %55 = load i64, ptr @k, align 8, !tbaa !9
  %56 = icmp ne i64 %54, %55
  br i1 %56, label %57, label %60

57:                                               ; preds = %49
  %58 = load i64, ptr @cur, align 8, !tbaa !9
  %59 = add nsw i64 %58, -1
  store i64 %59, ptr @cur, align 8, !tbaa !9
  br label %60

60:                                               ; preds = %57, %49
  br label %61

61:                                               ; preds = %60
  %62 = load i32, ptr %2, align 4, !tbaa !5
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %2, align 4, !tbaa !5
  br label %11, !llvm.loop !11

64:                                               ; preds = %15
  %65 = load i64, ptr @cur, align 8, !tbaa !9
  %66 = load i64, ptr @k, align 8, !tbaa !9
  %67 = icmp eq i64 %65, %66
  br i1 %67, label %68, label %71

68:                                               ; preds = %64
  %69 = load i64, ptr @cnt, align 8, !tbaa !9
  %70 = add nsw i64 %69, -1
  store i64 %70, ptr @cnt, align 8, !tbaa !9
  br label %71

71:                                               ; preds = %68, %64
  %72 = load i64, ptr @cnt, align 8, !tbaa !9
  %73 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i64 noundef %72)
  br label %4, !llvm.loop !13

74:                                               ; preds = %4
  ret i32 0
}

declare i32 @__isoc99_scanf(ptr noundef, ...) #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

declare i32 @printf(ptr noundef, ...) #1

attributes #0 = { mustprogress norecurse 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 = { "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 #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 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

@t = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@a = external dso_local global i32, align 4
@k = external dso_local global i64, align 8
@cnt = external dso_local local_unnamed_addr global i64, align 8
@cur = external dso_local local_unnamed_addr global i64, align 8
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @t)
  %2 = load i32, ptr @t, align 4, !tbaa !5
  %3 = add nsw i32 %2, -1
  store i32 %3, ptr @t, align 4, !tbaa !5
  %.not7 = icmp eq i32 %2, 0
  br i1 %.not7, label %._crit_edge10, label %.lr.ph9

.lr.ph9:                                          ; preds = %0, %39
  %4 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull @n, ptr noundef nonnull @k)
  store i64 1, ptr @cnt, align 8, !tbaa !9
  %5 = load i64, ptr @k, align 8, !tbaa !9
  store i64 %5, ptr @cur, align 8, !tbaa !9
  %6 = load i32, ptr @n, align 4, !tbaa !5
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.lr.ph, label %._crit_edge.thread

._crit_edge:                                      ; preds = %31
  %.pre11.pre = load i64, ptr @cnt, align 8, !tbaa !9
  %8 = icmp eq i64 %34, %.pre.pre
  %9 = select i1 %.not5, i1 true, i1 %8
  br i1 %9, label %._crit_edge.thread, label %39

.lr.ph:                                           ; preds = %.lr.ph9, %31
  %.06 = phi i32 [ %35, %31 ], [ 0, %.lr.ph9 ]
  %10 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @a)
  %11 = load i64, ptr @cur, align 8, !tbaa !9
  %12 = icmp slt i64 %11, 1
  %.pre.pre = load i64, ptr @k, align 8, !tbaa !9
  br i1 %12, label %13, label %16

13:                                               ; preds = %.lr.ph
  %14 = load i64, ptr @cnt, align 8, !tbaa !9
  %15 = add nsw i64 %14, 1
  store i64 %15, ptr @cnt, align 8, !tbaa !9
  br label %16

16:                                               ; preds = %13, %.lr.ph
  %17 = phi i64 [ %.pre.pre, %13 ], [ %11, %.lr.ph ]
  %18 = load i32, ptr @a, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = icmp slt i64 %17, %19
  br i1 %20, label %21, label %31

21:                                               ; preds = %16
  %22 = trunc i64 %17 to i32
  %23 = sub i32 %18, %22
  %24 = load i64, ptr @cnt, align 8, !tbaa !9
  %25 = add nsw i64 %24, 1
  %26 = sext i32 %23 to i64
  %27 = sdiv i64 %26, %.pre.pre
  %28 = add nsw i64 %25, %27
  store i64 %28, ptr @cnt, align 8, !tbaa !9
  %29 = srem i64 %26, %.pre.pre
  %30 = trunc nsw i64 %29 to i32
  store i32 %30, ptr @a, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %21, %16
  %.pre-phi = phi i64 [ %29, %21 ], [ %19, %16 ]
  %32 = phi i64 [ %.pre.pre, %21 ], [ %17, %16 ]
  %33 = sub nsw i64 %32, %.pre-phi
  %.not5 = icmp eq i64 %33, %.pre.pre
  %34 = add nsw i64 %33, -1
  %spec.select = select i1 %.not5, i64 %33, i64 %34
  store i64 %spec.select, ptr @cur, align 8, !tbaa !9
  %35 = add nuw nsw i32 %.06, 1
  %36 = load i32, ptr @n, align 4, !tbaa !5
  %37 = icmp slt i32 %35, %36
  br i1 %37, label %.lr.ph, label %._crit_edge, !llvm.loop !11

._crit_edge.thread:                               ; preds = %.lr.ph9, %._crit_edge
  %.pre1114 = phi i64 [ %.pre11.pre, %._crit_edge ], [ 1, %.lr.ph9 ]
  %38 = add nsw i64 %.pre1114, -1
  store i64 %38, ptr @cnt, align 8, !tbaa !9
  br label %39

39:                                               ; preds = %._crit_edge.thread, %._crit_edge
  %40 = phi i64 [ %38, %._crit_edge.thread ], [ %.pre11.pre, %._crit_edge ]
  %41 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i64 noundef %40)
  %42 = load i32, ptr @t, align 4, !tbaa !5
  %43 = add nsw i32 %42, -1
  store i32 %43, ptr @t, align 4, !tbaa !5
  %.not = icmp eq i32 %42, 0
  br i1 %.not, label %._crit_edge10, label %.lr.ph9, !llvm.loop !13

._crit_edge10:                                    ; preds = %39, %0
  ret i32 0
}

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

attributes #0 = { mustprogress nofree norecurse 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 = { nofree nounwind "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 long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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