; LLVM IR from VastoLorde95/Competitive-Programming :: CodeChef/Random/FRMQ/A.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
@sp = external dso_local global [17 x [131072 x i32]], align 16
@a = external dso_local global [131072 x i32], align 16
@k = external dso_local global [131072 x i32], align 16
@o = external dso_local global [131072 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 0, ptr %1, align 4, !tbaa !5
  br label %6

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

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

11:                                               ; preds = %6
  %12 = load i32, ptr %1, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [131072 x i32], ptr @a, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = load i32, ptr %1, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [131072 x i32], ptr @sp, i64 0, i64 %17
  store i32 %15, ptr %18, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %11
  %20 = load i32, ptr %1, align 4, !tbaa !5
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr %1, align 4, !tbaa !5
  br label %6, !llvm.loop !9

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

23:                                               ; preds = %68, %22
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = icmp slt i32 %24, 17
  br i1 %25, label %27, label %26

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

27:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %64, %27
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = load i32, ptr %2, align 4, !tbaa !5
  %31 = shl i32 1, %30
  %32 = add nsw i32 %29, %31
  %33 = sub nsw i32 %32, 1
  %34 = load i32, ptr @n, align 4, !tbaa !5
  %35 = icmp slt i32 %33, %34
  br i1 %35, label %37, label %36

36:                                               ; preds = %28
  store i32 8, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %67

37:                                               ; preds = %28
  %38 = load i32, ptr %2, align 4, !tbaa !5
  %39 = sub nsw i32 %38, 1
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [17 x [131072 x i32]], ptr @sp, i64 0, i64 %40
  %42 = load i32, ptr %4, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [131072 x i32], ptr %41, i64 0, i64 %43
  %45 = load i32, ptr %2, align 4, !tbaa !5
  %46 = sub nsw i32 %45, 1
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [17 x [131072 x i32]], ptr @sp, i64 0, i64 %47
  %49 = load i32, ptr %4, align 4, !tbaa !5
  %50 = load i32, ptr %2, align 4, !tbaa !5
  %51 = sub nsw i32 %50, 1
  %52 = shl i32 1, %51
  %53 = add nsw i32 %49, %52
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [131072 x i32], ptr %48, i64 0, i64 %54
  %56 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %44, ptr noundef nonnull align 4 dereferenceable(4) %55)
  %57 = load i32, ptr %56, align 4, !tbaa !5
  %58 = load i32, ptr %2, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [17 x [131072 x i32]], ptr @sp, i64 0, i64 %59
  %61 = load i32, ptr %4, align 4, !tbaa !5
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [131072 x i32], ptr %60, i64 0, i64 %62
  store i32 %57, ptr %63, align 4, !tbaa !5
  br label %64

64:                                               ; preds = %37
  %65 = load i32, ptr %4, align 4, !tbaa !5
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %4, align 4, !tbaa !5
  br label %28, !llvm.loop !11

67:                                               ; preds = %36
  br label %68

68:                                               ; preds = %67
  %69 = load i32, ptr %2, align 4, !tbaa !5
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %2, align 4, !tbaa !5
  br label %23, !llvm.loop !12

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

72:                                               ; preds = %125, %71
  %73 = load i32, ptr %5, align 4, !tbaa !5
  %74 = load i32, ptr @n, align 4, !tbaa !5
  %75 = icmp sle i32 %73, %74
  br i1 %75, label %77, label %76

76:                                               ; preds = %72
  store i32 11, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %128

77:                                               ; preds = %72
  %78 = load i32, ptr %5, align 4, !tbaa !5
  %79 = sub nsw i32 %78, 1
  %80 = sext i32 %79 to i64
  %81 = getelementptr inbounds [131072 x i32], ptr @k, i64 0, i64 %80
  %82 = load i32, ptr %81, align 4, !tbaa !5
  %83 = load i32, ptr %5, align 4, !tbaa !5
  %84 = load i32, ptr %5, align 4, !tbaa !5
  %85 = sub nsw i32 %84, 1
  %86 = and i32 %83, %85
  %87 = icmp eq i32 %86, 0
  %88 = zext i1 %87 to i32
  %89 = add nsw i32 %82, %88
  %90 = load i32, ptr %5, align 4, !tbaa !5
  %91 = sext i32 %90 to i64
  %92 = getelementptr inbounds [131072 x i32], ptr @k, i64 0, i64 %91
  store i32 %89, ptr %92, align 4, !tbaa !5
  %93 = load i32, ptr %5, align 4, !tbaa !5
  %94 = sext i32 %93 to i64
  %95 = getelementptr inbounds [131072 x i32], ptr @k, i64 0, i64 %94
  %96 = load i32, ptr %95, align 4, !tbaa !5
  %97 = load i32, ptr %5, align 4, !tbaa !5
  %98 = sub nsw i32 %97, 1
  %99 = sext i32 %98 to i64
  %100 = getelementptr inbounds [131072 x i32], ptr @k, i64 0, i64 %99
  %101 = load i32, ptr %100, align 4, !tbaa !5
  %102 = icmp eq i32 %96, %101
  br i1 %102, label %103, label %112

103:                                              ; preds = %77
  %104 = load i32, ptr %5, align 4, !tbaa !5
  %105 = sub nsw i32 %104, 1
  %106 = sext i32 %105 to i64
  %107 = getelementptr inbounds [131072 x i32], ptr @o, i64 0, i64 %106
  %108 = load i32, ptr %107, align 4, !tbaa !5
  %109 = load i32, ptr %5, align 4, !tbaa !5
  %110 = sext i32 %109 to i64
  %111 = getelementptr inbounds [131072 x i32], ptr @o, i64 0, i64 %110
  store i32 %108, ptr %111, align 4, !tbaa !5
  br label %124

112:                                              ; preds = %77
  %113 = load i32, ptr %5, align 4, !tbaa !5
  %114 = sub nsw i32 %113, 1
  %115 = sext i32 %114 to i64
  %116 = getelementptr inbounds [131072 x i32], ptr @o, i64 0, i64 %115
  %117 = load i32, ptr %116, align 4, !tbaa !5
  %118 = add nsw i32 %117, 1
  %119 = shl i32 %118, 1
  %120 = sub nsw i32 %119, 1
  %121 = load i32, ptr %5, align 4, !tbaa !5
  %122 = sext i32 %121 to i64
  %123 = getelementptr inbounds [131072 x i32], ptr @o, i64 0, i64 %122
  store i32 %120, ptr %123, align 4, !tbaa !5
  br label %124

124:                                              ; preds = %112, %103
  br label %125

125:                                              ; preds = %124
  %126 = load i32, ptr %5, align 4, !tbaa !5
  %127 = add nsw i32 %126, 1
  store i32 %127, ptr %5, align 4, !tbaa !5
  br label %72, !llvm.loop !13

128:                                              ; preds = %76
  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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.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

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}
!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
@sp = external dso_local global [17 x [131072 x i32]], align 16
@a = external dso_local local_unnamed_addr global [131072 x i32], align 16
@k = external dso_local local_unnamed_addr global [131072 x i32], align 16
@o = external dso_local local_unnamed_addr global [131072 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z11sparseTablev() local_unnamed_addr #0 {
  %1 = load i32, ptr @n, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph.preheader, label %.preheader32.preheader

.lr.ph.preheader:                                 ; preds = %0
  %3 = zext nneg i32 %1 to i64
  %4 = shl nuw nsw i64 %3, 2
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 16 @sp, ptr nonnull align 16 @a, i64 %4, i1 false), !tbaa !5
  br label %.preheader32.preheader

.preheader32.preheader:                           ; preds = %.lr.ph.preheader, %0
  br label %.preheader32

.preheader32:                                     ; preds = %.preheader32.preheader, %._crit_edge
  %5 = phi i32 [ %13, %._crit_edge ], [ %1, %.preheader32.preheader ]
  %indvars.iv45 = phi i64 [ %indvars.iv.next46, %._crit_edge ], [ 1, %.preheader32.preheader ]
  %6 = trunc nuw nsw i64 %indvars.iv45 to i32
  %7 = shl nuw nsw i32 1, %6
  %.not3135 = icmp sgt i32 %7, %5
  br i1 %.not3135, label %._crit_edge, label %.lr.ph37

.lr.ph37:                                         ; preds = %.preheader32
  %8 = add nsw i64 %indvars.iv45, -1
  %9 = trunc nuw nsw i64 %8 to i32
  %10 = shl nuw nsw i32 1, %9
  %11 = zext nneg i32 %10 to i64
  br label %14

.preheader:                                       ; preds = %._crit_edge
  %.not39 = icmp slt i32 %13, 2
  br i1 %.not39, label %._crit_edge42, label %.lr.ph41.preheader

.lr.ph41.preheader:                               ; preds = %.preheader
  %12 = add nuw nsw i32 %13, 1
  %wide.trip.count = zext nneg i32 %12 to i64
  %.pre = 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.ph41

._crit_edge:                                      ; preds = %14, %.preheader32
  %13 = phi i32 [ %5, %.preheader32 ], [ %21, %14 ]
  %indvars.iv.next46 = add nuw nsw i64 %indvars.iv45, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next46, 17
  br i1 %exitcond.not, label %.preheader, label %.preheader32, !llvm.loop !9

14:                                               ; preds = %.lr.ph37, %14
  %indvars.iv = phi i64 [ 0, %.lr.ph37 ], [ %indvars.iv.next, %14 ]
  %15 = getelementptr inbounds [17 x [131072 x i32]], ptr @sp, i64 0, i64 %8, i64 %indvars.iv
  %16 = add nuw nsw i64 %indvars.iv, %11
  %17 = getelementptr inbounds [17 x [131072 x i32]], ptr @sp, i64 0, i64 %8, i64 %16
  %18 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %15, ptr noundef nonnull align 4 dereferenceable(4) %17)
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = getelementptr inbounds [17 x [131072 x i32]], ptr @sp, i64 0, i64 %indvars.iv45, i64 %indvars.iv
  store i32 %19, ptr %20, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %21 = load i32, ptr @n, align 4, !tbaa !5
  %22 = trunc i64 %indvars.iv.next to i32
  %23 = add i32 %7, %22
  %.not31 = icmp sgt i32 %23, %21
  br i1 %.not31, label %._crit_edge, label %14, !llvm.loop !11

._crit_edge42:                                    ; preds = %.lr.ph41, %.preheader
  ret void

.lr.ph41:                                         ; preds = %.lr.ph41.preheader, %.lr.ph41
  %store_forwarded = phi i32 [ %load_initial, %.lr.ph41.preheader ], [ %.sink, %.lr.ph41 ]
  %24 = phi i32 [ %.pre, %.lr.ph41.preheader ], [ %29, %.lr.ph41 ]
  %indvars.iv48 = phi i64 [ 2, %.lr.ph41.preheader ], [ %indvars.iv.next49, %.lr.ph41 ]
  %25 = trunc nuw nsw i64 %indvars.iv48 to i32
  %26 = tail call range(i32 0, 32) i32 @llvm.ctpop.i32(i32 %25)
  %27 = icmp ult i32 %26, 2
  %28 = zext i1 %27 to i32
  %29 = add nsw i32 %24, %28
  %30 = getelementptr inbounds [131072 x i32], ptr @k, i64 0, i64 %indvars.iv48
  store i32 %29, ptr %30, align 4, !tbaa !5
  %31 = shl i32 %store_forwarded, 1
  %32 = or disjoint i32 %31, 1
  %.sink = select i1 %27, i32 %32, i32 %store_forwarded
  %33 = getelementptr inbounds [131072 x i32], ptr @o, i64 0, i64 %indvars.iv48
  store i32 %.sink, ptr %33, align 4
  %indvars.iv.next49 = add nuw nsw i64 %indvars.iv48, 1
  %exitcond51.not = icmp eq i64 %indvars.iv.next49, %wide.trip.count
  br i1 %exitcond51.not, label %._crit_edge42, label %.lr.ph41, !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: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.ctpop.i32(i32) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #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 = { 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) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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,LoopIdiomRecognizePass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass