; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/Manthan 2016/Problem E/E.ll (Z11sparseTablev)

; 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
@k = external dso_local global [1048576 x i32], align 16
@o = external dso_local global [1048576 x i32], align 16
@maxv = external dso_local global [20 x [1048576 x i32]], align 16
@minc = external dso_local global [20 x [1048576 x i32]], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z11sparseTablev() #0 {
  %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
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  store i32 1, ptr %1, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %78, %0
  %7 = load i32, ptr %1, align 4, !tbaa !5
  %8 = icmp slt i32 %7, 20
  br i1 %8, label %10, label %9

9:                                                ; preds = %6
  store i32 2, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  br label %81

10:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %11 = load i32, ptr %1, align 4, !tbaa !5
  %12 = shl i32 1, %11
  store i32 %12, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 1, ptr %4, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %74, %10
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = add nsw i32 %14, %15
  %17 = sub nsw i32 %16, 1
  %18 = load i32, ptr @n, align 4, !tbaa !5
  %19 = icmp sle i32 %17, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %13
  store i32 5, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %77

21:                                               ; preds = %13
  %22 = load i32, ptr %1, align 4, !tbaa !5
  %23 = sub nsw i32 %22, 1
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [20 x [1048576 x i32]], ptr @maxv, i64 0, i64 %24
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [1048576 x i32], ptr %25, i64 0, i64 %27
  %29 = load i32, ptr %1, align 4, !tbaa !5
  %30 = sub nsw i32 %29, 1
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [20 x [1048576 x i32]], ptr @maxv, i64 0, i64 %31
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = load i32, ptr %1, align 4, !tbaa !5
  %35 = sub nsw i32 %34, 1
  %36 = shl i32 1, %35
  %37 = add nsw i32 %33, %36
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [1048576 x i32], ptr %32, i64 0, i64 %38
  %40 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %28, ptr noundef nonnull align 4 dereferenceable(4) %39)
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = load i32, ptr %1, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [20 x [1048576 x i32]], ptr @maxv, i64 0, i64 %43
  %45 = load i32, ptr %4, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [1048576 x i32], ptr %44, i64 0, i64 %46
  store i32 %41, ptr %47, align 4, !tbaa !5
  %48 = load i32, ptr %1, align 4, !tbaa !5
  %49 = sub nsw i32 %48, 1
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [20 x [1048576 x i32]], ptr @minc, i64 0, i64 %50
  %52 = load i32, ptr %4, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [1048576 x i32], ptr %51, i64 0, i64 %53
  %55 = load i32, ptr %1, align 4, !tbaa !5
  %56 = sub nsw i32 %55, 1
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [20 x [1048576 x i32]], ptr @minc, i64 0, i64 %57
  %59 = load i32, ptr %4, align 4, !tbaa !5
  %60 = load i32, ptr %1, align 4, !tbaa !5
  %61 = sub nsw i32 %60, 1
  %62 = shl i32 1, %61
  %63 = add nsw i32 %59, %62
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [1048576 x i32], ptr %58, i64 0, i64 %64
  %66 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %54, ptr noundef nonnull align 4 dereferenceable(4) %65)
  %67 = load i32, ptr %66, align 4, !tbaa !5
  %68 = load i32, ptr %1, align 4, !tbaa !5
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds [20 x [1048576 x i32]], ptr @minc, i64 0, i64 %69
  %71 = load i32, ptr %4, align 4, !tbaa !5
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds [1048576 x i32], ptr %70, i64 0, i64 %72
  store i32 %67, ptr %73, align 4, !tbaa !5
  br label %74

74:                                               ; preds = %21
  %75 = load i32, ptr %4, align 4, !tbaa !5
  %76 = add nsw i32 %75, 1
  store i32 %76, ptr %4, align 4, !tbaa !5
  br label %13, !llvm.loop !9

77:                                               ; preds = %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %78

78:                                               ; preds = %77
  %79 = load i32, ptr %1, align 4, !tbaa !5
  %80 = add nsw i32 %79, 1
  store i32 %80, ptr %1, align 4, !tbaa !5
  br label %6, !llvm.loop !11

81:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 2, ptr %5, align 4, !tbaa !5
  br label %82

82:                                               ; preds = %135, %81
  %83 = load i32, ptr %5, align 4, !tbaa !5
  %84 = load i32, ptr @n, align 4, !tbaa !5
  %85 = icmp sle i32 %83, %84
  br i1 %85, label %87, label %86

86:                                               ; preds = %82
  store i32 8, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %138

87:                                               ; preds = %82
  %88 = load i32, ptr %5, align 4, !tbaa !5
  %89 = sub nsw i32 %88, 1
  %90 = sext i32 %89 to i64
  %91 = getelementptr inbounds [1048576 x i32], ptr @k, i64 0, i64 %90
  %92 = load i32, ptr %91, align 4, !tbaa !5
  %93 = load i32, ptr %5, align 4, !tbaa !5
  %94 = load i32, ptr %5, align 4, !tbaa !5
  %95 = sub nsw i32 %94, 1
  %96 = and i32 %93, %95
  %97 = icmp eq i32 %96, 0
  %98 = zext i1 %97 to i32
  %99 = add nsw i32 %92, %98
  %100 = load i32, ptr %5, align 4, !tbaa !5
  %101 = sext i32 %100 to i64
  %102 = getelementptr inbounds [1048576 x i32], ptr @k, i64 0, i64 %101
  store i32 %99, ptr %102, align 4, !tbaa !5
  %103 = load i32, ptr %5, align 4, !tbaa !5
  %104 = sext i32 %103 to i64
  %105 = getelementptr inbounds [1048576 x i32], ptr @k, i64 0, i64 %104
  %106 = load i32, ptr %105, align 4, !tbaa !5
  %107 = load i32, ptr %5, align 4, !tbaa !5
  %108 = sub nsw i32 %107, 1
  %109 = sext i32 %108 to i64
  %110 = getelementptr inbounds [1048576 x i32], ptr @k, i64 0, i64 %109
  %111 = load i32, ptr %110, align 4, !tbaa !5
  %112 = icmp eq i32 %106, %111
  br i1 %112, label %113, label %122

113:                                              ; preds = %87
  %114 = load i32, ptr %5, align 4, !tbaa !5
  %115 = sub nsw i32 %114, 1
  %116 = sext i32 %115 to i64
  %117 = getelementptr inbounds [1048576 x i32], ptr @o, i64 0, i64 %116
  %118 = load i32, ptr %117, align 4, !tbaa !5
  %119 = load i32, ptr %5, align 4, !tbaa !5
  %120 = sext i32 %119 to i64
  %121 = getelementptr inbounds [1048576 x i32], ptr @o, i64 0, i64 %120
  store i32 %118, ptr %121, align 4, !tbaa !5
  br label %134

122:                                              ; preds = %87
  %123 = load i32, ptr %5, align 4, !tbaa !5
  %124 = sub nsw i32 %123, 1
  %125 = sext i32 %124 to i64
  %126 = getelementptr inbounds [1048576 x i32], ptr @o, i64 0, i64 %125
  %127 = load i32, ptr %126, align 4, !tbaa !5
  %128 = add nsw i32 %127, 1
  %129 = shl i32 %128, 1
  %130 = sub nsw i32 %129, 1
  %131 = load i32, ptr %5, align 4, !tbaa !5
  %132 = sext i32 %131 to i64
  %133 = getelementptr inbounds [1048576 x i32], ptr @o, i64 0, i64 %132
  store i32 %130, ptr %133, align 4, !tbaa !5
  br label %134

134:                                              ; preds = %122, %113
  br label %135

135:                                              ; preds = %134
  %136 = load i32, ptr %5, align 4, !tbaa !5
  %137 = add nsw i32 %136, 1
  store i32 %137, ptr %5, align 4, !tbaa !5
  br label %82, !llvm.loop !12

138:                                              ; preds = %86
  ret void
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #2

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

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 = { inlinehint 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 #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}
!12 = distinct !{!12, !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
@k = external dso_local local_unnamed_addr global [1048576 x i32], align 16
@o = external dso_local local_unnamed_addr global [1048576 x i32], align 16
@maxv = external dso_local global [20 x [1048576 x i32]], align 16
@minc = external dso_local global [20 x [1048576 x i32]], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z11sparseTablev() local_unnamed_addr #0 {
  %.pre = load i32, ptr @n, align 4, !tbaa !5
  br label %2

.preheader:                                       ; preds = %._crit_edge
  %.not38 = icmp slt i32 %10, 2
  br i1 %.not38, label %._crit_edge41, label %.lr.ph40.preheader

.lr.ph40.preheader:                               ; preds = %.preheader
  %1 = add nuw nsw i32 %10, 1
  %wide.trip.count = zext nneg i32 %1 to i64
  %.pre50 = load i32, ptr getelementptr inbounds (i8, ptr @k, i64 4), align 4, !tbaa !5
  %load_initial = load i32, ptr getelementptr inbounds (i8, ptr @o, i64 4), align 4
  br label %.lr.ph40

2:                                                ; preds = %0, %._crit_edge
  %3 = phi i32 [ %.pre, %0 ], [ %10, %._crit_edge ]
  %indvars.iv43 = phi i64 [ 1, %0 ], [ %indvars.iv.next44, %._crit_edge ]
  %4 = trunc nuw nsw i64 %indvars.iv43 to i32
  %5 = shl nuw nsw i32 1, %4
  %.not3435 = icmp sgt i32 %5, %3
  br i1 %.not3435, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %6 = add nsw i64 %indvars.iv43, -1
  %7 = trunc nuw nsw i64 %6 to i32
  %8 = shl nuw nsw i32 1, %7
  %9 = zext nneg i32 %8 to i64
  br label %11

._crit_edge:                                      ; preds = %11, %2
  %10 = phi i32 [ %3, %2 ], [ %23, %11 ]
  %indvars.iv.next44 = add nuw nsw i64 %indvars.iv43, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next44, 20
  br i1 %exitcond.not, label %.preheader, label %2, !llvm.loop !9

11:                                               ; preds = %.lr.ph, %11
  %indvars.iv = phi i64 [ 1, %.lr.ph ], [ %indvars.iv.next, %11 ]
  %12 = getelementptr inbounds [20 x [1048576 x i32]], ptr @maxv, i64 0, i64 %6, i64 %indvars.iv
  %13 = add nuw nsw i64 %indvars.iv, %9
  %14 = getelementptr inbounds [20 x [1048576 x i32]], ptr @maxv, i64 0, i64 %6, i64 %13
  %15 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %12, ptr noundef nonnull align 4 dereferenceable(4) %14)
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = getelementptr inbounds [20 x [1048576 x i32]], ptr @maxv, i64 0, i64 %indvars.iv43, i64 %indvars.iv
  store i32 %16, ptr %17, align 4, !tbaa !5
  %18 = getelementptr inbounds [20 x [1048576 x i32]], ptr @minc, i64 0, i64 %6, i64 %indvars.iv
  %19 = getelementptr inbounds [20 x [1048576 x i32]], ptr @minc, i64 0, i64 %6, i64 %13
  %20 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %18, ptr noundef nonnull align 4 dereferenceable(4) %19)
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = getelementptr inbounds [20 x [1048576 x i32]], ptr @minc, i64 0, i64 %indvars.iv43, i64 %indvars.iv
  store i32 %21, ptr %22, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %23 = load i32, ptr @n, align 4, !tbaa !5
  %24 = trunc i64 %indvars.iv to i32
  %25 = add i32 %5, %24
  %.not34 = icmp sgt i32 %25, %23
  br i1 %.not34, label %._crit_edge, label %11, !llvm.loop !11

._crit_edge41:                                    ; preds = %.lr.ph40, %.preheader
  ret void

.lr.ph40:                                         ; preds = %.lr.ph40.preheader, %.lr.ph40
  %store_forwarded = phi i32 [ %load_initial, %.lr.ph40.preheader ], [ %.sink, %.lr.ph40 ]
  %26 = phi i32 [ %.pre50, %.lr.ph40.preheader ], [ %31, %.lr.ph40 ]
  %indvars.iv46 = phi i64 [ 2, %.lr.ph40.preheader ], [ %indvars.iv.next47, %.lr.ph40 ]
  %27 = trunc nuw nsw i64 %indvars.iv46 to i32
  %28 = tail call range(i32 0, 32) i32 @llvm.ctpop.i32(i32 %27)
  %29 = icmp ult i32 %28, 2
  %30 = zext i1 %29 to i32
  %31 = add nsw i32 %26, %30
  %32 = getelementptr inbounds [1048576 x i32], ptr @k, i64 0, i64 %indvars.iv46
  store i32 %31, ptr %32, align 4, !tbaa !5
  %33 = shl i32 %store_forwarded, 1
  %34 = or disjoint i32 %33, 1
  %.sink = select i1 %29, i32 %34, i32 %store_forwarded
  %35 = getelementptr inbounds [1048576 x i32], ptr @o, i64 0, i64 %indvars.iv46
  store i32 %.sink, ptr %35, align 4
  %indvars.iv.next47 = add nuw nsw i64 %indvars.iv46, 1
  %exitcond49.not = icmp eq i64 %indvars.iv.next47, %wide.trip.count
  br i1 %exitcond49.not, label %._crit_edge41, label %.lr.ph40, !llvm.loop !12
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.ctpop.i32(i32) #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 = { inlinehint 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 #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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}


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