; LLVM IR from Leet-Us-Code/DSA-A-Z-Practice :: Sid's Levels/Level - 2/Hashing/NumberOfSubarraysXOR.ll (Z11subarrayXorPiii)

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

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

12:                                               ; preds = %43, %3
  %13 = load i32, ptr %8, align 4, !tbaa !9
  %14 = load i32, ptr %5, align 4, !tbaa !9
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %12
  store i32 2, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %46

17:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 0, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %18 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %18, ptr %11, align 4, !tbaa !9
  br label %19

19:                                               ; preds = %39, %17
  %20 = load i32, ptr %11, align 4, !tbaa !9
  %21 = load i32, ptr %5, align 4, !tbaa !9
  %22 = icmp slt i32 %20, %21
  br i1 %22, label %24, label %23

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

24:                                               ; preds = %19
  %25 = load i32, ptr %10, align 4, !tbaa !9
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = load i32, ptr %11, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds i32, ptr %26, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !9
  %31 = xor i32 %25, %30
  store i32 %31, ptr %10, align 4, !tbaa !9
  %32 = load i32, ptr %10, align 4, !tbaa !9
  %33 = load i32, ptr %6, align 4, !tbaa !9
  %34 = icmp eq i32 %32, %33
  br i1 %34, label %35, label %38

35:                                               ; preds = %24
  %36 = load i64, ptr %7, align 8, !tbaa !11
  %37 = add nsw i64 %36, 1
  store i64 %37, ptr %7, align 8, !tbaa !11
  br label %38

38:                                               ; preds = %35, %24
  br label %39

39:                                               ; preds = %38
  %40 = load i32, ptr %11, align 4, !tbaa !9
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %11, align 4, !tbaa !9
  br label %19, !llvm.loop !13

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

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

46:                                               ; preds = %16
  %47 = load i64, ptr %7, align 8, !tbaa !11
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  ret i64 %47
}

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

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local noundef i64 @_Z11subarrayXorPiii(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp sgt i32 %1, 0
  br i1 %4, label %.preheader.preheader, label %._crit_edge

.preheader.preheader:                             ; preds = %3
  %wide.trip.count26 = zext nneg i32 %1 to i64
  %xtraiter = and i64 %wide.trip.count26, 1
  %5 = icmp eq i32 %1, 1
  br i1 %5, label %._crit_edge.loopexit.unr-lcssa, label %.preheader.preheader.new

.preheader.preheader.new:                         ; preds = %.preheader.preheader
  %unroll_iter = and i64 %wide.trip.count26, 2147483646
  br label %.preheader

.preheader:                                       ; preds = %17, %.preheader.preheader.new
  %indvars.iv = phi i64 [ 0, %.preheader.preheader.new ], [ %indvars.iv.next.1, %17 ]
  %.01520 = phi i64 [ 0, %.preheader.preheader.new ], [ %spec.select.1, %17 ]
  %niter = phi i64 [ 0, %.preheader.preheader.new ], [ %niter.next.1, %17 ]
  br label %18

._crit_edge.loopexit.unr-lcssa:                   ; preds = %17, %.preheader.preheader
  %spec.select.lcssa.lcssa.ph = phi i64 [ poison, %.preheader.preheader ], [ %spec.select.1, %17 ]
  %indvars.iv.unr = phi i64 [ 0, %.preheader.preheader ], [ %indvars.iv.next.1, %17 ]
  %.01520.unr = phi i64 [ 0, %.preheader.preheader ], [ %spec.select.1, %17 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.preheader.epil

.preheader.epil:                                  ; preds = %._crit_edge.loopexit.unr-lcssa, %.preheader.epil
  %indvars.iv22.epil = phi i64 [ %indvars.iv.next23.epil, %.preheader.epil ], [ %indvars.iv.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.01318.epil = phi i32 [ %8, %.preheader.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %.117.epil = phi i64 [ %spec.select.epil, %.preheader.epil ], [ %.01520.unr, %._crit_edge.loopexit.unr-lcssa ]
  %6 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv22.epil
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = xor i32 %7, %.01318.epil
  %9 = icmp eq i32 %8, %2
  %10 = zext i1 %9 to i64
  %spec.select.epil = add nsw i64 %.117.epil, %10
  %indvars.iv.next23.epil = add nuw nsw i64 %indvars.iv22.epil, 1
  %exitcond.not.epil = icmp eq i64 %indvars.iv.next23.epil, %wide.trip.count26
  br i1 %exitcond.not.epil, label %._crit_edge, label %.preheader.epil, !llvm.loop !9

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.preheader.epil, %3
  %.015.lcssa = phi i64 [ 0, %3 ], [ %spec.select.lcssa.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %spec.select.epil, %.preheader.epil ]
  ret i64 %.015.lcssa

.preheader.1:                                     ; preds = %18
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  br label %11

11:                                               ; preds = %11, %.preheader.1
  %indvars.iv22.1 = phi i64 [ %indvars.iv.next, %.preheader.1 ], [ %indvars.iv.next23.1, %11 ]
  %.01318.1 = phi i32 [ 0, %.preheader.1 ], [ %14, %11 ]
  %.117.1 = phi i64 [ %spec.select, %.preheader.1 ], [ %spec.select.1, %11 ]
  %12 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv22.1
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = xor i32 %13, %.01318.1
  %15 = icmp eq i32 %14, %2
  %16 = zext i1 %15 to i64
  %spec.select.1 = add nsw i64 %.117.1, %16
  %indvars.iv.next23.1 = add nuw nsw i64 %indvars.iv22.1, 1
  %exitcond.not.1 = icmp eq i64 %indvars.iv.next23.1, %wide.trip.count26
  br i1 %exitcond.not.1, label %17, label %11, !llvm.loop !9

17:                                               ; preds = %11
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.preheader, !llvm.loop !11

18:                                               ; preds = %.preheader, %18
  %indvars.iv22 = phi i64 [ %indvars.iv, %.preheader ], [ %indvars.iv.next23, %18 ]
  %.01318 = phi i32 [ 0, %.preheader ], [ %21, %18 ]
  %.117 = phi i64 [ %.01520, %.preheader ], [ %spec.select, %18 ]
  %19 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv22
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = xor i32 %20, %.01318
  %22 = icmp eq i32 %21, %2
  %23 = zext i1 %22 to i64
  %spec.select = add nsw i64 %.117, %23
  %indvars.iv.next23 = add nuw nsw i64 %indvars.iv22, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next23, %wide.trip.count26
  br i1 %exitcond.not, label %.preheader.1, label %18, !llvm.loop !9
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) 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" }

!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: GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass