; LLVM IR from bqi343/cp-notebook :: Contests/USACO Solutions/2012-13/Feb 2013/13 Feb G1.ll (Z4posiiii)

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

@N = external dso_local global i32, align 4
@grid = external dso_local global [15 x [15 x i32]], align 16

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

16:                                               ; preds = %24, %3
  %17 = load i32, ptr %10, align 4, !tbaa !5
  %18 = icmp slt i32 %17, 15
  br i1 %18, label %20, label %19

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

20:                                               ; preds = %16
  %21 = load i32, ptr %10, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [15 x i32], ptr %9, i64 0, i64 %22
  store i32 0, ptr %23, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %20
  %25 = load i32, ptr %10, align 4, !tbaa !5
  %26 = add nsw i32 %25, 1
  store i32 %26, ptr %10, align 4, !tbaa !5
  br label %16, !llvm.loop !9

27:                                               ; preds = %19
  br label %28

28:                                               ; preds = %100, %27
  %29 = load i32, ptr %6, align 4, !tbaa !5
  %30 = icmp sge i32 %29, 0
  br i1 %30, label %31, label %36

31:                                               ; preds = %28
  %32 = load i32, ptr %8, align 4, !tbaa !5
  %33 = load i32, ptr @N, align 4, !tbaa !5
  %34 = sub nsw i32 %33, 1
  %35 = icmp slt i32 %32, %34
  br label %36

36:                                               ; preds = %31, %28
  %37 = phi i1 [ false, %28 ], [ %35, %31 ]
  br i1 %37, label %38, label %101

38:                                               ; preds = %36
  %39 = load i32, ptr %8, align 4, !tbaa !5
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  store i32 0, ptr %11, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  store i32 0, ptr %12, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  store i32 0, ptr %13, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %76, %38
  %42 = load i32, ptr %13, align 4, !tbaa !5
  %43 = load i32, ptr @N, align 4, !tbaa !5
  %44 = icmp slt i32 %42, %43
  br i1 %44, label %46, label %45

45:                                               ; preds = %41
  store i32 7, ptr %14, align 4
  br label %79

46:                                               ; preds = %41
  %47 = load i32, ptr %8, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [15 x [15 x i32]], ptr @grid, i64 0, i64 %48
  %50 = load i32, ptr %13, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [15 x i32], ptr %49, i64 0, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !5
  %54 = load i32, ptr %11, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [15 x i32], ptr %9, i64 0, i64 %55
  %57 = load i32, ptr %56, align 4, !tbaa !5
  %58 = add nsw i32 %57, %53
  store i32 %58, ptr %56, align 4, !tbaa !5
  %59 = load i32, ptr %11, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [15 x i32], ptr %9, i64 0, i64 %60
  %62 = load i32, ptr %61, align 4, !tbaa !5
  %63 = load i32, ptr %5, align 4, !tbaa !5
  %64 = icmp sgt i32 %62, %63
  br i1 %64, label %65, label %66

65:                                               ; preds = %46
  store i32 1, ptr %12, align 4, !tbaa !5
  store i32 7, ptr %14, align 4
  br label %79

66:                                               ; preds = %46
  %67 = load i32, ptr %7, align 4, !tbaa !5
  %68 = load i32, ptr %13, align 4, !tbaa !5
  %69 = shl i32 1, %68
  %70 = and i32 %67, %69
  %71 = icmp ne i32 %70, 0
  br i1 %71, label %72, label %75

72:                                               ; preds = %66
  %73 = load i32, ptr %11, align 4, !tbaa !5
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %11, align 4, !tbaa !5
  br label %75

75:                                               ; preds = %72, %66
  br label %76

76:                                               ; preds = %75
  %77 = load i32, ptr %13, align 4, !tbaa !5
  %78 = add nsw i32 %77, 1
  store i32 %78, ptr %13, align 4, !tbaa !5
  br label %41, !llvm.loop !11

79:                                               ; preds = %65, %45
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  br label %80

80:                                               ; preds = %79
  %81 = load i32, ptr %12, align 4, !tbaa !5
  %82 = icmp ne i32 %81, 0
  br i1 %82, label %83, label %100

83:                                               ; preds = %80
  %84 = load i32, ptr %8, align 4, !tbaa !5
  %85 = add nsw i32 %84, -1
  store i32 %85, ptr %8, align 4, !tbaa !5
  %86 = load i32, ptr %6, align 4, !tbaa !5
  %87 = add nsw i32 %86, -1
  store i32 %87, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #2
  store i32 0, ptr %15, align 4, !tbaa !5
  br label %88

88:                                               ; preds = %96, %83
  %89 = load i32, ptr %15, align 4, !tbaa !5
  %90 = icmp slt i32 %89, 15
  br i1 %90, label %92, label %91

91:                                               ; preds = %88
  store i32 10, ptr %14, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #2
  br label %99

92:                                               ; preds = %88
  %93 = load i32, ptr %15, align 4, !tbaa !5
  %94 = sext i32 %93 to i64
  %95 = getelementptr inbounds [15 x i32], ptr %9, i64 0, i64 %94
  store i32 0, ptr %95, align 4, !tbaa !5
  br label %96

96:                                               ; preds = %92
  %97 = load i32, ptr %15, align 4, !tbaa !5
  %98 = add nsw i32 %97, 1
  store i32 %98, ptr %15, align 4, !tbaa !5
  br label %88, !llvm.loop !12

99:                                               ; preds = %91
  br label %100

100:                                              ; preds = %99, %80
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %28, !llvm.loop !13

101:                                              ; preds = %36
  %102 = load i32, ptr %6, align 4, !tbaa !5
  %103 = icmp slt i32 %102, 0
  br i1 %103, label %104, label %105

104:                                              ; preds = %101
  store i1 false, ptr %4, align 1
  store i32 1, ptr %14, align 4
  br label %106

105:                                              ; preds = %101
  store i1 true, ptr %4, align 1
  store i32 1, ptr %14, align 4
  br label %106

106:                                              ; preds = %105, %104
  call void @llvm.lifetime.end.p0(i64 60, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %107 = load i1, ptr %4, align 1
  ret i1 %107
}

; 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

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 = { 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"

@N = external dso_local local_unnamed_addr global i32, align 4
@grid = external dso_local local_unnamed_addr global [15 x [15 x i32]], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i1 @_Z4posiiii(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
.preheader:
  %3 = alloca [15 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 60, ptr nonnull %3) #3
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(60) %3, i8 0, i64 60, i1 false), !tbaa !5
  %4 = load i32, ptr @N, align 4
  %5 = add nsw i32 %4, -1
  %6 = icmp sgt i32 %1, -1
  %7 = icmp sgt i32 %4, 0
  %8 = select i1 %6, i1 %7, i1 false
  br i1 %8, label %.lr.ph.us.preheader, label %._crit_edge

.lr.ph.us.preheader:                              ; preds = %.preheader
  %wide.trip.count = zext nneg i32 %4 to i64
  br label %.lr.ph.us

.lr.ph.us:                                        ; preds = %.lr.ph.us.preheader, %.critedge.us
  %.02741.us = phi i32 [ %.128.us, %.critedge.us ], [ -1, %.lr.ph.us.preheader ]
  %.03040.us = phi i32 [ %.131.us, %.critedge.us ], [ %1, %.lr.ph.us.preheader ]
  %9 = add nsw i32 %.02741.us, 1
  %10 = sext i32 %9 to i64
  br label %11

11:                                               ; preds = %.lr.ph.us, %19
  %indvars.iv = phi i64 [ 0, %.lr.ph.us ], [ %indvars.iv.next, %19 ]
  %.02537.us = phi i32 [ 0, %.lr.ph.us ], [ %spec.select.us, %19 ]
  %12 = getelementptr inbounds [15 x [15 x i32]], ptr @grid, i64 0, i64 %10, i64 %indvars.iv
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = zext nneg i32 %.02537.us to i64
  %15 = getelementptr inbounds [15 x i32], ptr %3, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = add nsw i32 %16, %13
  store i32 %17, ptr %15, align 4, !tbaa !5
  %18 = icmp sgt i32 %17, %0
  br i1 %18, label %.critedge.us.loopexit, label %19

19:                                               ; preds = %11
  %20 = trunc nuw nsw i64 %indvars.iv to i32
  %21 = lshr i32 %2, %20
  %22 = and i32 %21, 1
  %spec.select.us = add nuw nsw i32 %22, %.02537.us
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.critedge.us, label %11, !llvm.loop !9

.critedge.us.loopexit:                            ; preds = %11
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(60) %3, i8 0, i64 60, i1 false), !tbaa !5
  %23 = add nsw i32 %.03040.us, -1
  br label %.critedge.us

.critedge.us:                                     ; preds = %19, %.critedge.us.loopexit
  %.131.us = phi i32 [ %23, %.critedge.us.loopexit ], [ %.03040.us, %19 ]
  %.128.us = phi i32 [ %.02741.us, %.critedge.us.loopexit ], [ %9, %19 ]
  %24 = icmp sgt i32 %.131.us, -1
  %25 = icmp slt i32 %.128.us, %5
  %26 = select i1 %24, i1 %25, i1 false
  br i1 %26, label %.lr.ph.us, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %.critedge.us, %.preheader
  %.lcssa = phi i1 [ %6, %.preheader ], [ %24, %.critedge.us ]
  call void @llvm.lifetime.end.p0(i64 60, ptr nonnull %3) #3
  ret i1 %.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: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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"}
!11 = distinct !{!11, !10}


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