; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/Good Bye 2014/Problem E/E.ll (Z5queryv)

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

@x = external dso_local global i32, align 4
@y = external dso_local global i32, align 4
@pa = external dso_local global [18 x [262144 x i32]], align 16
@cost = external dso_local global [18 x [262144 x i32]], align 16
@.str = external hidden unnamed_addr constant [4 x i8], align 1

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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5queryv() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  store i32 0, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 17, ptr %2, align 4, !tbaa !5
  br label %3

3:                                                ; preds = %41, %0
  %4 = load i32, ptr %2, align 4, !tbaa !5
  %5 = icmp sge i32 %4, 0
  br i1 %5, label %6, label %10

6:                                                ; preds = %3
  %7 = load i32, ptr @x, align 4, !tbaa !5
  %8 = load i32, ptr @y, align 4, !tbaa !5
  %9 = icmp slt i32 %7, %8
  br label %10

10:                                               ; preds = %6, %3
  %11 = phi i1 [ false, %3 ], [ %9, %6 ]
  br i1 %11, label %13, label %12

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

13:                                               ; preds = %10
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [18 x [262144 x i32]], ptr @pa, i64 0, i64 %15
  %17 = load i32, ptr @x, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [262144 x i32], ptr %16, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = load i32, ptr @y, align 4, !tbaa !5
  %22 = icmp sgt i32 %20, %21
  br i1 %22, label %23, label %24

23:                                               ; preds = %13
  br label %41

24:                                               ; preds = %13
  %25 = load i32, ptr %2, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [18 x [262144 x i32]], ptr @cost, i64 0, i64 %26
  %28 = load i32, ptr @x, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [262144 x i32], ptr %27, i64 0, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !5
  %32 = load i32, ptr %1, align 4, !tbaa !5
  %33 = add nsw i32 %32, %31
  store i32 %33, ptr %1, align 4, !tbaa !5
  %34 = load i32, ptr %2, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [18 x [262144 x i32]], ptr @pa, i64 0, i64 %35
  %37 = load i32, ptr @x, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [262144 x i32], ptr %36, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  store i32 %40, ptr @x, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %24, %23
  %42 = load i32, ptr %2, align 4, !tbaa !5
  %43 = add nsw i32 %42, -1
  store i32 %43, ptr %2, align 4, !tbaa !5
  br label %3, !llvm.loop !9

44:                                               ; preds = %12
  %45 = load i32, ptr %1, align 4, !tbaa !5
  %46 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %45)
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret void
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"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"

@x = external dso_local local_unnamed_addr global i32, align 4
@y = external dso_local local_unnamed_addr global i32, align 4
@pa = external dso_local local_unnamed_addr global [18 x [262144 x i32]], align 16
@cost = external dso_local local_unnamed_addr global [18 x [262144 x i32]], align 16
@.str = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local void @_Z5queryv() local_unnamed_addr #0 {
  %1 = load i32, ptr @y, align 4
  %x.promoted = load i32, ptr @x, align 4
  %2 = icmp slt i32 %x.promoted, %1
  br i1 %2, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %13, %0
  %.07.lcssa = phi i32 [ 0, %0 ], [ %.1, %13 ]
  %3 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %.07.lcssa)
  ret void

.lr.ph:                                           ; preds = %0, %13
  %indvars.iv = phi i64 [ %indvars.iv.next, %13 ], [ 17, %0 ]
  %.0711 = phi i32 [ %.1, %13 ], [ 0, %0 ]
  %4 = phi i32 [ %14, %13 ], [ %x.promoted, %0 ]
  %5 = sext i32 %4 to i64
  %6 = getelementptr inbounds [18 x [262144 x i32]], ptr @pa, i64 0, i64 %indvars.iv, i64 %5
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = icmp sgt i32 %7, %1
  br i1 %8, label %13, label %9

9:                                                ; preds = %.lr.ph
  %10 = getelementptr inbounds [18 x [262144 x i32]], ptr @cost, i64 0, i64 %indvars.iv, i64 %5
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = add nsw i32 %11, %.0711
  store i32 %7, ptr @x, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %.lr.ph, %9
  %14 = phi i32 [ %4, %.lr.ph ], [ %7, %9 ]
  %.1 = phi i32 [ %.0711, %.lr.ph ], [ %12, %9 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %15 = icmp ne i64 %indvars.iv, 0
  %16 = icmp slt i32 %14, %1
  %17 = and i1 %15, %16
  br i1 %17, label %.lr.ph, label %._crit_edge, !llvm.loop !9
}

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

attributes #0 = { mustprogress nofree 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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