; LLVM IR from ravireddy07/cpp-book :: src/Code-Chef/JAN2021-Long/FAIRELCT.ll (Z8checkSumPiS_)

; 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 i64, align 8
@m = external dso_local global i64, align 8

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

9:                                                ; preds = %23, %2
  %10 = load i32, ptr %7, align 4, !tbaa !9
  %11 = sext i32 %10 to i64
  %12 = load i64, ptr @n, align 8, !tbaa !11
  %13 = icmp slt i64 %11, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %26

15:                                               ; preds = %9
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i32, ptr %16, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = load i32, ptr %5, align 4, !tbaa !9
  %22 = add nsw i32 %21, %20
  store i32 %22, ptr %5, align 4, !tbaa !9
  br label %23

23:                                               ; preds = %15
  %24 = load i32, ptr %7, align 4, !tbaa !9
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %7, align 4, !tbaa !9
  br label %9, !llvm.loop !13

26:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %27

27:                                               ; preds = %41, %26
  %28 = load i32, ptr %8, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = load i64, ptr @m, align 8, !tbaa !11
  %31 = icmp slt i64 %29, %30
  br i1 %31, label %33, label %32

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

33:                                               ; preds = %27
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = load i32, ptr %8, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds i32, ptr %34, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !9
  %39 = load i32, ptr %6, align 4, !tbaa !9
  %40 = add nsw i32 %39, %38
  store i32 %40, ptr %6, align 4, !tbaa !9
  br label %41

41:                                               ; preds = %33
  %42 = load i32, ptr %8, align 4, !tbaa !9
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %8, align 4, !tbaa !9
  br label %27, !llvm.loop !15

44:                                               ; preds = %32
  %45 = load i32, ptr %5, align 4, !tbaa !9
  %46 = load i32, ptr %6, align 4, !tbaa !9
  %47 = icmp eq i32 %45, %46
  br i1 %47, label %48, label %49

48:                                               ; preds = %44
  br label %55

49:                                               ; preds = %44
  %50 = load i32, ptr %5, align 4, !tbaa !9
  %51 = load i32, ptr %6, align 4, !tbaa !9
  %52 = icmp sgt i32 %50, %51
  %53 = zext i1 %52 to i64
  %54 = select i1 %52, i32 1, i32 0
  br label %55

55:                                               ; preds = %49, %48
  %56 = phi i32 [ -1, %48 ], [ %54, %49 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i32 %56
}

; 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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 i64, align 8
@m = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef range(i32 -1, 2) i32 @_Z8checkSumPiS_(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1) local_unnamed_addr #0 {
  %3 = load i64, ptr @n, align 8, !tbaa !5
  %4 = icmp sgt i64 %3, 0
  br i1 %4, label %.lr.ph.preheader, label %.preheader

.lr.ph.preheader:                                 ; preds = %2
  %min.iters.check = icmp ult i64 %3, 8
  br i1 %min.iters.check, label %.lr.ph.preheader54, label %vector.ph

.lr.ph.preheader54:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  %.01518.ph = phi i32 [ 0, %.lr.ph.preheader ], [ %10, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %3, 9223372036854775800
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %7, %vector.body ]
  %vec.phi32 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %8, %vector.body ]
  %5 = getelementptr inbounds i32, ptr %0, i64 %index
  %6 = getelementptr inbounds i8, ptr %5, i64 16
  %wide.load = load <4 x i32>, ptr %5, align 4, !tbaa !9
  %wide.load33 = load <4 x i32>, ptr %6, align 4, !tbaa !9
  %7 = add <4 x i32> %wide.load, %vec.phi
  %8 = add <4 x i32> %wide.load33, %vec.phi32
  %index.next = add nuw i64 %index, 8
  %9 = icmp eq i64 %index.next, %n.vec
  br i1 %9, label %middle.block, label %vector.body, !llvm.loop !11

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %8, %7
  %10 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %3, %n.vec
  br i1 %cmp.n, label %.preheader, label %.lr.ph.preheader54

.preheader:                                       ; preds = %.lr.ph, %middle.block, %2
  %.015.lcssa = phi i32 [ 0, %2 ], [ %10, %middle.block ], [ %21, %.lr.ph ]
  %11 = load i64, ptr @m, align 8, !tbaa !5
  %12 = icmp sgt i64 %11, 0
  br i1 %12, label %.lr.ph22.preheader, label %._crit_edge

.lr.ph22.preheader:                               ; preds = %.preheader
  %min.iters.check36 = icmp ult i64 %11, 8
  br i1 %min.iters.check36, label %.lr.ph22.preheader51, label %vector.ph37

.lr.ph22.preheader51:                             ; preds = %middle.block34, %.lr.ph22.preheader
  %indvars.iv26.ph = phi i64 [ 0, %.lr.ph22.preheader ], [ %n.vec39, %middle.block34 ]
  %.01420.ph = phi i32 [ 0, %.lr.ph22.preheader ], [ %18, %middle.block34 ]
  br label %.lr.ph22

vector.ph37:                                      ; preds = %.lr.ph22.preheader
  %n.vec39 = and i64 %11, 9223372036854775800
  br label %vector.body41

vector.body41:                                    ; preds = %vector.body41, %vector.ph37
  %index42 = phi i64 [ 0, %vector.ph37 ], [ %index.next47, %vector.body41 ]
  %vec.phi43 = phi <4 x i32> [ zeroinitializer, %vector.ph37 ], [ %15, %vector.body41 ]
  %vec.phi44 = phi <4 x i32> [ zeroinitializer, %vector.ph37 ], [ %16, %vector.body41 ]
  %13 = getelementptr inbounds i32, ptr %1, i64 %index42
  %14 = getelementptr inbounds i8, ptr %13, i64 16
  %wide.load45 = load <4 x i32>, ptr %13, align 4, !tbaa !9
  %wide.load46 = load <4 x i32>, ptr %14, align 4, !tbaa !9
  %15 = add <4 x i32> %wide.load45, %vec.phi43
  %16 = add <4 x i32> %wide.load46, %vec.phi44
  %index.next47 = add nuw i64 %index42, 8
  %17 = icmp eq i64 %index.next47, %n.vec39
  br i1 %17, label %middle.block34, label %vector.body41, !llvm.loop !15

middle.block34:                                   ; preds = %vector.body41
  %bin.rdx48 = add <4 x i32> %16, %15
  %18 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx48)
  %cmp.n49 = icmp eq i64 %11, %n.vec39
  br i1 %cmp.n49, label %._crit_edge, label %.lr.ph22.preheader51

.lr.ph:                                           ; preds = %.lr.ph.preheader54, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader54 ]
  %.01518 = phi i32 [ %21, %.lr.ph ], [ %.01518.ph, %.lr.ph.preheader54 ]
  %19 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = add nsw i32 %20, %.01518
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %3
  br i1 %exitcond.not, label %.preheader, label %.lr.ph, !llvm.loop !16

._crit_edge:                                      ; preds = %.lr.ph22, %middle.block34, %.preheader
  %.014.lcssa = phi i32 [ 0, %.preheader ], [ %18, %middle.block34 ], [ %28, %.lr.ph22 ]
  %22 = icmp eq i32 %.015.lcssa, %.014.lcssa
  %23 = icmp sgt i32 %.015.lcssa, %.014.lcssa
  %24 = zext i1 %23 to i32
  %25 = select i1 %22, i32 -1, i32 %24
  ret i32 %25

.lr.ph22:                                         ; preds = %.lr.ph22.preheader51, %.lr.ph22
  %indvars.iv26 = phi i64 [ %indvars.iv.next27, %.lr.ph22 ], [ %indvars.iv26.ph, %.lr.ph22.preheader51 ]
  %.01420 = phi i32 [ %28, %.lr.ph22 ], [ %.01420.ph, %.lr.ph22.preheader51 ]
  %26 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv26
  %27 = load i32, ptr %26, align 4, !tbaa !9
  %28 = add nsw i32 %27, %.01420
  %indvars.iv.next27 = add nuw nsw i64 %indvars.iv26, 1
  %exitcond29.not = icmp eq i64 %indvars.iv.next27, %11
  br i1 %exitcond29.not, label %._crit_edge, label %.lr.ph22, !llvm.loop !17
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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 = { 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12, !13, !14}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!"llvm.loop.isvectorized", i32 1}
!14 = !{!"llvm.loop.unroll.runtime.disable"}
!15 = distinct !{!15, !12, !13, !14}
!16 = distinct !{!16, !12, !14, !13}
!17 = distinct !{!17, !12, !14, !13}


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