; LLVM IR from VastoLorde95/Competitive-Programming :: Hackerrank/Random/Library Query (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"

@tree = external dso_local global [10001 x [1001 x i32]], align 16
@t = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@q = external dso_local global i32, align 4
@b = external dso_local global [10001 x i32], align 16
@type = external dso_local global i32, align 4
@x = external dso_local global i32, align 4
@y = external dso_local global i32, align 4
@k = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z6updateiii(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 nounwind uwtable
declare dso_local noundef i32 @_Z5queryii(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
  store i32 0, ptr %1, align 4
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @t)
  br label %7

7:                                                ; preds = %88, %0
  %8 = load i32, ptr @t, align 4, !tbaa !5
  %9 = add nsw i32 %8, -1
  store i32 %9, ptr @t, align 4, !tbaa !5
  %10 = icmp ne i32 %8, 0
  br i1 %10, label %11, label %89

11:                                               ; preds = %7
  call void @llvm.memset.p0.i64(ptr align 16 @tree, i8 0, i64 40044004, i1 false)
  %12 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  store i32 1, ptr %2, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %28, %11
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = load i32, ptr @n, align 4, !tbaa !5
  %16 = icmp sle i32 %14, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  br label %31

18:                                               ; preds = %13
  %19 = load i32, ptr %2, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [10001 x i32], ptr @b, i64 0, i64 %20
  %22 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %21)
  %23 = load i32, ptr %2, align 4, !tbaa !5
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [10001 x i32], ptr @b, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  call void @_Z6updateiii(i32 noundef %23, i32 noundef %27, i32 noundef 1)
  br label %28

28:                                               ; preds = %18
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %2, align 4, !tbaa !5
  br label %13, !llvm.loop !9

31:                                               ; preds = %17
  %32 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @q)
  br label %33

33:                                               ; preds = %87, %31
  %34 = load i32, ptr @q, align 4, !tbaa !5
  %35 = add nsw i32 %34, -1
  store i32 %35, ptr @q, align 4, !tbaa !5
  %36 = icmp ne i32 %34, 0
  br i1 %36, label %37, label %88

37:                                               ; preds = %33
  %38 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @type)
  %39 = load i32, ptr @type, align 4, !tbaa !5
  %40 = icmp eq i32 %39, 1
  br i1 %40, label %41, label %57

41:                                               ; preds = %37
  %42 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef @x, ptr noundef @k)
  %43 = load i32, ptr @x, align 4, !tbaa !5
  %44 = load i32, ptr @x, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [10001 x i32], ptr @b, i64 0, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  call void @_Z6updateiii(i32 noundef %43, i32 noundef %47, i32 noundef -1)
  %48 = load i32, ptr @k, align 4, !tbaa !5
  %49 = load i32, ptr @x, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [10001 x i32], ptr @b, i64 0, i64 %50
  store i32 %48, ptr %51, align 4, !tbaa !5
  %52 = load i32, ptr @x, align 4, !tbaa !5
  %53 = load i32, ptr @x, align 4, !tbaa !5
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [10001 x i32], ptr @b, i64 0, i64 %54
  %56 = load i32, ptr %55, align 4, !tbaa !5
  call void @_Z6updateiii(i32 noundef %52, i32 noundef %56, i32 noundef 1)
  br label %87

57:                                               ; preds = %37
  %58 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.2, ptr noundef @x, ptr noundef @y, ptr noundef @k)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  store i32 1, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  store i32 1001, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  br label %59

59:                                               ; preds = %83, %57
  %60 = load i32, ptr %3, align 4, !tbaa !5
  %61 = load i32, ptr %4, align 4, !tbaa !5
  %62 = icmp slt i32 %60, %61
  br i1 %62, label %63, label %84

63:                                               ; preds = %59
  %64 = load i32, ptr %3, align 4, !tbaa !5
  %65 = load i32, ptr %4, align 4, !tbaa !5
  %66 = add nsw i32 %64, %65
  %67 = ashr i32 %66, 1
  store i32 %67, ptr %5, align 4, !tbaa !5
  %68 = load i32, ptr @y, align 4, !tbaa !5
  %69 = load i32, ptr %5, align 4, !tbaa !5
  %70 = call noundef i32 @_Z5queryii(i32 noundef %68, i32 noundef %69)
  %71 = load i32, ptr @x, align 4, !tbaa !5
  %72 = sub nsw i32 %71, 1
  %73 = load i32, ptr %5, align 4, !tbaa !5
  %74 = call noundef i32 @_Z5queryii(i32 noundef %72, i32 noundef %73)
  %75 = sub nsw i32 %70, %74
  %76 = load i32, ptr @k, align 4, !tbaa !5
  %77 = icmp sge i32 %75, %76
  br i1 %77, label %78, label %80

78:                                               ; preds = %63
  %79 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %79, ptr %4, align 4, !tbaa !5
  br label %83

80:                                               ; preds = %63
  %81 = load i32, ptr %5, align 4, !tbaa !5
  %82 = add nsw i32 %81, 1
  store i32 %82, ptr %3, align 4, !tbaa !5
  br label %83

83:                                               ; preds = %80, %78
  br label %59, !llvm.loop !11

84:                                               ; preds = %59
  %85 = load i32, ptr %3, align 4, !tbaa !5
  %86 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i32 noundef %85)
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  br label %87

87:                                               ; preds = %84, %41
  br label %33, !llvm.loop !12

88:                                               ; preds = %33
  br label %7, !llvm.loop !13

89:                                               ; preds = %7
  ret i32 0
}

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

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

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 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { 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"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !10}


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

@tree = external dso_local local_unnamed_addr global [10001 x [1001 x i32]], align 16
@t = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@q = external dso_local global i32, align 4
@b = external dso_local global [10001 x i32], align 16
@type = external dso_local global i32, align 4
@x = external dso_local global i32, align 4
@y = external dso_local global i32, align 4
@k = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [4 x i8], align 1

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

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

; Function Attrs: mustprogress norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #1 {
  %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
  %.not27 = icmp eq i32 %2, 0
  br i1 %.not27, label %._crit_edge30, label %.lr.ph29

.loopexit:                                        ; preds = %46, %._crit_edge
  %4 = load i32, ptr @t, align 4, !tbaa !5
  %5 = add nsw i32 %4, -1
  store i32 %5, ptr @t, align 4, !tbaa !5
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %._crit_edge30, label %.lr.ph29, !llvm.loop !9

.lr.ph29:                                         ; preds = %0, %.loopexit
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(40044004) @tree, i8 0, i64 40044004, i1 false)
  %6 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %7 = load i32, ptr @n, align 4, !tbaa !5
  %.not1720 = icmp slt i32 %7, 1
  br i1 %.not1720, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph29
  %8 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @q)
  %9 = load i32, ptr @q, align 4, !tbaa !5
  %10 = add nsw i32 %9, -1
  store i32 %10, ptr @q, align 4, !tbaa !5
  %.not1824 = icmp eq i32 %9, 0
  br i1 %.not1824, label %.loopexit, label %.lr.ph26

.lr.ph:                                           ; preds = %.lr.ph29, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 1, %.lr.ph29 ]
  %11 = getelementptr inbounds [10001 x i32], ptr @b, i64 0, i64 %indvars.iv
  %12 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %11)
  %13 = load i32, ptr %11, align 4, !tbaa !5
  %14 = trunc nuw nsw i64 %indvars.iv to i32
  tail call void @_Z6updateiii(i32 noundef %14, i32 noundef %13, i32 noundef 1)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %15 = load i32, ptr @n, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %.not17.not = icmp slt i64 %indvars.iv, %16
  br i1 %.not17.not, label %.lr.ph, label %._crit_edge, !llvm.loop !11

.lr.ph26:                                         ; preds = %._crit_edge, %46
  %17 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @type)
  %18 = load i32, ptr @type, align 4, !tbaa !5
  %19 = icmp eq i32 %18, 1
  br i1 %19, label %20, label %30

20:                                               ; preds = %.lr.ph26
  %21 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull @x, ptr noundef nonnull @k)
  %22 = load i32, ptr @x, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [10001 x i32], ptr @b, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  tail call void @_Z6updateiii(i32 noundef %22, i32 noundef %25, i32 noundef -1)
  %26 = load i32, ptr @k, align 4, !tbaa !5
  %27 = load i32, ptr @x, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [10001 x i32], ptr @b, i64 0, i64 %28
  store i32 %26, ptr %29, align 4, !tbaa !5
  tail call void @_Z6updateiii(i32 noundef %27, i32 noundef %26, i32 noundef 1)
  br label %46

30:                                               ; preds = %.lr.ph26
  %31 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.2, ptr noundef nonnull @x, ptr noundef nonnull @y, ptr noundef nonnull @k)
  br label %32

32:                                               ; preds = %30, %32
  %.01423 = phi i32 [ 1001, %30 ], [ %.1, %32 ]
  %.01522 = phi i32 [ 1, %30 ], [ %.116, %32 ]
  %33 = add nsw i32 %.01423, %.01522
  %34 = ashr i32 %33, 1
  %35 = load i32, ptr @y, align 4, !tbaa !5
  %36 = tail call noundef i32 @_Z5queryii(i32 noundef %35, i32 noundef %34)
  %37 = load i32, ptr @x, align 4, !tbaa !5
  %38 = add nsw i32 %37, -1
  %39 = tail call noundef i32 @_Z5queryii(i32 noundef %38, i32 noundef %34)
  %40 = sub nsw i32 %36, %39
  %41 = load i32, ptr @k, align 4, !tbaa !5
  %.not19 = icmp slt i32 %40, %41
  %42 = add nsw i32 %34, 1
  %.116 = select i1 %.not19, i32 %42, i32 %.01522
  %.1 = select i1 %.not19, i32 %.01423, i32 %34
  %43 = icmp slt i32 %.116, %.1
  br i1 %43, label %32, label %44, !llvm.loop !12

44:                                               ; preds = %32
  %45 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef %.116)
  br label %46

46:                                               ; preds = %44, %20
  %47 = load i32, ptr @q, align 4, !tbaa !5
  %48 = add nsw i32 %47, -1
  store i32 %48, ptr @q, align 4, !tbaa !5
  %.not18 = icmp eq i32 %47, 0
  br i1 %.not18, label %.loopexit, label %.lr.ph26, !llvm.loop !13

._crit_edge30:                                    ; preds = %.loopexit, %0
  ret i32 0
}

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #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 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 #2 = { 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 #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }

!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"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !10}


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