; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/8VC Finals Div 1 2016/Problem C (C)

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

%"struct.std::pair" = type { i32, i32 }

@a = external dso_local global i32, align 4
@b = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@k = external dso_local global i32, align 4
@q = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z5buildiii(i32 noundef, i32 noundef, 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
declare dso_local void @_Z6updateiiiii(i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef) #0

; Function Attrs: mustprogress uwtable
declare dso_local i64 @_Z5queryiiiii(i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef) #0

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca %"struct.std::pair", align 4
  %7 = alloca %"struct.std::pair", align 4
  store i32 0, ptr %1, align 4
  %8 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n, ptr noundef @k)
  %9 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @a, ptr noundef @b)
  %10 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef @q)
  %11 = load i32, ptr @n, align 4, !tbaa !5
  call void @_Z5buildiii(i32 noundef 1, i32 noundef 0, i32 noundef %11)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  br label %12

12:                                               ; preds = %59, %0
  %13 = load i32, ptr @q, align 4, !tbaa !5
  %14 = add nsw i32 %13, -1
  store i32 %14, ptr @q, align 4, !tbaa !5
  %15 = icmp ne i32 %13, 0
  br i1 %15, label %16, label %60

16:                                               ; preds = %12
  %17 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %2)
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = icmp eq i32 %18, 1
  br i1 %19, label %20, label %27

20:                                               ; preds = %16
  %21 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %3, ptr noundef %4)
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = add nsw i32 %22, -1
  store i32 %23, ptr %3, align 4, !tbaa !5
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = load i32, ptr @n, align 4, !tbaa !5
  call void @_Z6updateiiiii(i32 noundef %24, i32 noundef %25, i32 noundef 1, i32 noundef 0, i32 noundef %26)
  br label %59

27:                                               ; preds = %16
  %28 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %3)
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = add nsw i32 %29, -1
  store i32 %30, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !5
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = icmp sgt i32 %31, 0
  br i1 %32, label %33, label %39

33:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %34 = load i32, ptr %3, align 4, !tbaa !5
  %35 = load i32, ptr @n, align 4, !tbaa !5
  %36 = call i64 @_Z5queryiiiii(i32 noundef 0, i32 noundef %34, i32 noundef 1, i32 noundef 0, i32 noundef %35)
  store i64 %36, ptr %6, align 4
  %37 = getelementptr inbounds %"struct.std::pair", ptr %6, i32 0, i32 0
  %38 = load i32, ptr %37, align 4, !tbaa !9
  store i32 %38, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %39

39:                                               ; preds = %33, %27
  %40 = load i32, ptr %3, align 4, !tbaa !5
  %41 = load i32, ptr @k, align 4, !tbaa !5
  %42 = add nsw i32 %40, %41
  %43 = load i32, ptr @n, align 4, !tbaa !5
  %44 = icmp slt i32 %42, %43
  br i1 %44, label %45, label %56

45:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %46 = load i32, ptr %3, align 4, !tbaa !5
  %47 = load i32, ptr @k, align 4, !tbaa !5
  %48 = add nsw i32 %46, %47
  %49 = load i32, ptr @n, align 4, !tbaa !5
  %50 = load i32, ptr @n, align 4, !tbaa !5
  %51 = call i64 @_Z5queryiiiii(i32 noundef %48, i32 noundef %49, i32 noundef 1, i32 noundef 0, i32 noundef %50)
  store i64 %51, ptr %7, align 4
  %52 = getelementptr inbounds %"struct.std::pair", ptr %7, i32 0, i32 1
  %53 = load i32, ptr %52, align 4, !tbaa !11
  %54 = load i32, ptr %5, align 4, !tbaa !5
  %55 = add nsw i32 %54, %53
  store i32 %55, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  br label %56

56:                                               ; preds = %45, %39
  %57 = load i32, ptr %5, align 4, !tbaa !5
  %58 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %57)
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %59

59:                                               ; preds = %56, %20
  br label %12, !llvm.loop !12

60:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  ret i32 0
}

declare i32 @__isoc99_scanf(ptr noundef, ...) #3

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

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 = { 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 #3 = { "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"_ZTSSt4pairIiiE", !6, i64 0, !6, i64 4}
!11 = !{!10, !6, i64 4}
!12 = distinct !{!12, !13}
!13 = !{!"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 global i32, align 4
@b = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@k = external dso_local global i32, align 4
@q = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

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

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

; 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 uwtable
declare dso_local void @_Z6updateiiiii(i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

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

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n, ptr noundef nonnull @k)
  %5 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @a, ptr noundef nonnull @b)
  %6 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull @q)
  %7 = load i32, ptr @n, align 4, !tbaa !5
  tail call void @_Z5buildiii(i32 noundef 1, i32 noundef 0, i32 noundef %7)
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #4
  %8 = load i32, ptr @q, align 4, !tbaa !5
  %9 = add nsw i32 %8, -1
  store i32 %9, ptr @q, align 4, !tbaa !5
  %.not6 = icmp eq i32 %8, 0
  br i1 %.not6, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %38
  %10 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %1)
  %11 = load i32, ptr %1, align 4, !tbaa !5
  %12 = icmp eq i32 %11, 1
  br i1 %12, label %13, label %19

13:                                               ; preds = %.lr.ph
  %14 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %2, ptr noundef nonnull %3)
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = add nsw i32 %15, -1
  store i32 %16, ptr %2, align 4, !tbaa !5
  %17 = load i32, ptr %3, align 4, !tbaa !5
  %18 = load i32, ptr @n, align 4, !tbaa !5
  call void @_Z6updateiiiii(i32 noundef %16, i32 noundef %17, i32 noundef 1, i32 noundef 0, i32 noundef %18)
  br label %38

19:                                               ; preds = %.lr.ph
  %20 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %2)
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = add nsw i32 %21, -1
  store i32 %22, ptr %2, align 4, !tbaa !5
  %23 = icmp sgt i32 %21, 1
  br i1 %23, label %24, label %27

24:                                               ; preds = %19
  %25 = load i32, ptr @n, align 4, !tbaa !5
  %26 = call i64 @_Z5queryiiiii(i32 noundef 0, i32 noundef %22, i32 noundef 1, i32 noundef 0, i32 noundef %25)
  %.sroa.01.0.extract.trunc = trunc i64 %26 to i32
  %.pre = load i32, ptr %2, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %24, %19
  %28 = phi i32 [ %.pre, %24 ], [ %22, %19 ]
  %.0 = phi i32 [ %.sroa.01.0.extract.trunc, %24 ], [ 0, %19 ]
  %29 = load i32, ptr @k, align 4, !tbaa !5
  %30 = add nsw i32 %29, %28
  %31 = load i32, ptr @n, align 4, !tbaa !5
  %32 = icmp slt i32 %30, %31
  br i1 %32, label %33, label %36

33:                                               ; preds = %27
  %34 = call i64 @_Z5queryiiiii(i32 noundef %30, i32 noundef %31, i32 noundef 1, i32 noundef 0, i32 noundef %31)
  %.sroa.3.0.extract.shift = lshr i64 %34, 32
  %.sroa.3.0.extract.trunc = trunc nuw i64 %.sroa.3.0.extract.shift to i32
  %35 = add nsw i32 %.0, %.sroa.3.0.extract.trunc
  br label %36

36:                                               ; preds = %33, %27
  %.1 = phi i32 [ %35, %33 ], [ %.0, %27 ]
  %37 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %.1)
  br label %38

38:                                               ; preds = %36, %13
  %39 = load i32, ptr @q, align 4, !tbaa !5
  %40 = add nsw i32 %39, -1
  store i32 %40, ptr @q, align 4, !tbaa !5
  %.not = icmp eq i32 %39, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9

._crit_edge:                                      ; preds = %38, %0
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #4
  ret i32 0
}

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

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

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { 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" }
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 = !{!"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: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass