; LLVM IR from DimitrisJim/leetcode_solutions :: C/Algorithms (495)

; 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: nounwind uwtable
define dso_local i32 @findPoisonedDuration(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  %12 = load i32, ptr %7, align 4, !tbaa !9
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %3
  store i32 0, ptr %4, align 4
  br label %60

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

16:                                               ; preds = %53, %15
  %17 = load i32, ptr %9, align 4, !tbaa !9
  %18 = load i32, ptr %6, align 4, !tbaa !9
  %19 = icmp slt i32 %17, %18
  br i1 %19, label %21, label %20

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

21:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = load i32, ptr %9, align 4, !tbaa !9
  %24 = sub nsw i32 %23, 1
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i32, ptr %22, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !9
  store i32 %27, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = load i32, ptr %9, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i32, ptr %28, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !9
  store i32 %32, ptr %11, align 4, !tbaa !9
  %33 = load i32, ptr %10, align 4, !tbaa !9
  %34 = load i32, ptr %11, align 4, !tbaa !9
  %35 = icmp ne i32 %33, %34
  br i1 %35, label %36, label %52

36:                                               ; preds = %21
  %37 = load i32, ptr %11, align 4, !tbaa !9
  %38 = load i32, ptr %10, align 4, !tbaa !9
  %39 = load i32, ptr %7, align 4, !tbaa !9
  %40 = add nsw i32 %38, %39
  %41 = icmp slt i32 %37, %40
  br i1 %41, label %42, label %46

42:                                               ; preds = %36
  %43 = load i32, ptr %11, align 4, !tbaa !9
  %44 = load i32, ptr %10, align 4, !tbaa !9
  %45 = sub nsw i32 %43, %44
  br label %48

46:                                               ; preds = %36
  %47 = load i32, ptr %7, align 4, !tbaa !9
  br label %48

48:                                               ; preds = %46, %42
  %49 = phi i32 [ %45, %42 ], [ %47, %46 ]
  %50 = load i32, ptr %8, align 4, !tbaa !9
  %51 = add nsw i32 %50, %49
  store i32 %51, ptr %8, align 4, !tbaa !9
  br label %52

52:                                               ; preds = %48, %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %53

53:                                               ; preds = %52
  %54 = load i32, ptr %9, align 4, !tbaa !9
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %9, align 4, !tbaa !9
  br label %16, !llvm.loop !11

56:                                               ; preds = %20
  %57 = load i32, ptr %8, align 4, !tbaa !9
  %58 = load i32, ptr %7, align 4, !tbaa !9
  %59 = add nsw i32 %57, %58
  store i32 %59, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %60

60:                                               ; preds = %56, %14
  %61 = load i32, ptr %4, align 4
  ret i32 %61
}

; 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 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; 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: nofree norecurse nosync nounwind memory(argmem: read) uwtable
define dso_local i32 @findPoisonedDuration(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq i32 %2, 0
  br i1 %4, label %37, label %.preheader

.preheader:                                       ; preds = %3
  %5 = icmp sgt i32 %1, 1
  br i1 %5, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.preheader
  %wide.trip.count = zext nneg i32 %1 to i64
  %6 = add nsw i64 %wide.trip.count, -1
  %min.iters.check = icmp ult i32 %1, 9
  br i1 %min.iters.check, label %.lr.ph.preheader32, label %vector.ph

.lr.ph.preheader32:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 1, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.01925.ph = phi i32 [ 0, %.lr.ph.preheader ], [ %24, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %6, -8
  %ind.end = or disjoint i64 %n.vec, 1
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %2, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  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 ], [ %predphi, %vector.body ]
  %vec.phi27 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %predphi31, %vector.body ]
  %offset.idx = or disjoint i64 %index, 1
  %7 = getelementptr i32, ptr %0, i64 %offset.idx
  %8 = getelementptr i8, ptr %7, i64 -4
  %9 = getelementptr i8, ptr %7, i64 12
  %wide.load = load <4 x i32>, ptr %8, align 4, !tbaa !5
  %wide.load28 = load <4 x i32>, ptr %9, align 4, !tbaa !5
  %10 = getelementptr i8, ptr %7, i64 16
  %wide.load29 = load <4 x i32>, ptr %7, align 4, !tbaa !5
  %wide.load30 = load <4 x i32>, ptr %10, align 4, !tbaa !5
  %11 = icmp eq <4 x i32> %wide.load, %wide.load29
  %12 = icmp eq <4 x i32> %wide.load28, %wide.load30
  %13 = add nsw <4 x i32> %wide.load, %broadcast.splat
  %14 = add nsw <4 x i32> %wide.load28, %broadcast.splat
  %15 = icmp slt <4 x i32> %wide.load29, %13
  %16 = icmp slt <4 x i32> %wide.load30, %14
  %17 = sub nsw <4 x i32> %wide.load29, %wide.load
  %18 = sub nsw <4 x i32> %wide.load30, %wide.load28
  %19 = select <4 x i1> %15, <4 x i32> %17, <4 x i32> %broadcast.splat
  %20 = select <4 x i1> %16, <4 x i32> %18, <4 x i32> %broadcast.splat
  %21 = select <4 x i1> %11, <4 x i32> zeroinitializer, <4 x i32> %19
  %predphi = add <4 x i32> %vec.phi, %21
  %22 = select <4 x i1> %12, <4 x i32> zeroinitializer, <4 x i32> %20
  %predphi31 = add <4 x i32> %vec.phi27, %22
  %index.next = add nuw i64 %index, 8
  %23 = icmp eq i64 %index.next, %n.vec
  br i1 %23, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %predphi31, %predphi
  %24 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %6, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader32

._crit_edge:                                      ; preds = %36, %middle.block, %.preheader
  %.019.lcssa = phi i32 [ 0, %.preheader ], [ %24, %middle.block ], [ %.1, %36 ]
  %25 = add nsw i32 %.019.lcssa, %2
  br label %37

.lr.ph:                                           ; preds = %.lr.ph.preheader32, %36
  %indvars.iv = phi i64 [ %indvars.iv.next, %36 ], [ %indvars.iv.ph, %.lr.ph.preheader32 ]
  %.01925 = phi i32 [ %.1, %36 ], [ %.01925.ph, %.lr.ph.preheader32 ]
  %26 = getelementptr i32, ptr %0, i64 %indvars.iv
  %27 = getelementptr i8, ptr %26, i64 -4
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = load i32, ptr %26, align 4, !tbaa !5
  %.not = icmp eq i32 %28, %29
  br i1 %.not, label %36, label %30

30:                                               ; preds = %.lr.ph
  %31 = add nsw i32 %28, %2
  %32 = icmp slt i32 %29, %31
  %33 = sub nsw i32 %29, %28
  %34 = select i1 %32, i32 %33, i32 %2
  %35 = add nsw i32 %34, %.01925
  br label %36

36:                                               ; preds = %30, %.lr.ph
  %.1 = phi i32 [ %35, %30 ], [ %.01925, %.lr.ph ]
  %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 %._crit_edge, label %.lr.ph, !llvm.loop !13

37:                                               ; preds = %3, %._crit_edge
  %.0 = phi i32 [ %25, %._crit_edge ], [ 0, %3 ]
  ret i32 %.0
}

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

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


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