; LLVM IR from aap/librw :: src/pipeline.ll (ZN2rw25findMinVertAndNumVerticesEPtjPjPi)

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

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

12:                                               ; preds = %36, %4
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = add i32 %13, -1
  store i32 %14, ptr %6, align 4, !tbaa !9
  %15 = icmp ne i32 %13, 0
  br i1 %15, label %16, label %39

16:                                               ; preds = %12
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = load i16, ptr %17, align 2, !tbaa !11
  %19 = zext i16 %18 to i32
  %20 = load i32, ptr %9, align 4, !tbaa !9
  %21 = icmp ult i32 %19, %20
  br i1 %21, label %22, label %26

22:                                               ; preds = %16
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = load i16, ptr %23, align 2, !tbaa !11
  %25 = zext i16 %24 to i32
  store i32 %25, ptr %9, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %22, %16
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = load i16, ptr %27, align 2, !tbaa !11
  %29 = zext i16 %28 to i32
  %30 = load i32, ptr %10, align 4, !tbaa !9
  %31 = icmp ugt i32 %29, %30
  br i1 %31, label %32, label %36

32:                                               ; preds = %26
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = load i16, ptr %33, align 2, !tbaa !11
  %35 = zext i16 %34 to i32
  store i32 %35, ptr %10, align 4, !tbaa !9
  br label %36

36:                                               ; preds = %32, %26
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = getelementptr inbounds i16, ptr %37, i32 1
  store ptr %38, ptr %5, align 8, !tbaa !5
  br label %12, !llvm.loop !13

39:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %40 = load i32, ptr %10, align 4, !tbaa !9
  %41 = load i32, ptr %9, align 4, !tbaa !9
  %42 = sub i32 %40, %41
  %43 = add i32 %42, 1
  store i32 %43, ptr %11, align 4, !tbaa !9
  %44 = load i32, ptr %9, align 4, !tbaa !9
  %45 = load i32, ptr %10, align 4, !tbaa !9
  %46 = icmp ugt i32 %44, %45
  br i1 %46, label %47, label %48

47:                                               ; preds = %39
  store i32 0, ptr %9, align 4, !tbaa !9
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %48

48:                                               ; preds = %47, %39
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = icmp ne ptr %49, null
  br i1 %50, label %51, label %54

51:                                               ; preds = %48
  %52 = load i32, ptr %9, align 4, !tbaa !9
  %53 = load ptr, ptr %7, align 8, !tbaa !5
  store i32 %52, ptr %53, align 4, !tbaa !9
  br label %54

54:                                               ; preds = %51, %48
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  %56 = icmp ne ptr %55, null
  br i1 %56, label %57, label %60

57:                                               ; preds = %54
  %58 = load i32, ptr %11, align 4, !tbaa !9
  %59 = load ptr, ptr %8, align 8, !tbaa !5
  store i32 %58, ptr %59, align 4, !tbaa !9
  br label %60

60:                                               ; preds = %57, %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { 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 = !{!"short", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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: mustprogress nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local void @_ZN2rw25findMinVertAndNumVerticesEPtjPjPi(ptr nocapture noundef readonly %0, i32 noundef %1, ptr noundef writeonly %2, ptr noundef writeonly %3) local_unnamed_addr #0 {
  %.not30 = icmp eq i32 %1, 0
  br i1 %.not30, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %4
  %5 = zext i32 %1 to i64
  %min.iters.check = icmp ult i32 %1, 8
  br i1 %min.iters.check, label %.lr.ph.preheader45, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %5, 4294967288
  %6 = shl nuw nsw i64 %n.vec, 1
  %ind.end = getelementptr i8, ptr %0, i64 %6
  %.cast = trunc nuw i64 %n.vec to i32
  %ind.end36 = sub i32 %1, %.cast
  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 ], [ %12, %vector.body ]
  %vec.phi38 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %13, %vector.body ]
  %vec.phi39 = phi <4 x i32> [ <i32 -1, i32 -1, i32 -1, i32 -1>, %vector.ph ], [ %10, %vector.body ]
  %vec.phi40 = phi <4 x i32> [ <i32 -1, i32 -1, i32 -1, i32 -1>, %vector.ph ], [ %11, %vector.body ]
  %offset.idx = shl i64 %index, 1
  %next.gep = getelementptr i8, ptr %0, i64 %offset.idx
  %7 = getelementptr i8, ptr %next.gep, i64 8
  %wide.load = load <4 x i16>, ptr %next.gep, align 2, !tbaa !5
  %wide.load42 = load <4 x i16>, ptr %7, align 2, !tbaa !5
  %8 = zext <4 x i16> %wide.load to <4 x i32>
  %9 = zext <4 x i16> %wide.load42 to <4 x i32>
  %10 = tail call <4 x i32> @llvm.umin.v4i32(<4 x i32> %vec.phi39, <4 x i32> %8)
  %11 = tail call <4 x i32> @llvm.umin.v4i32(<4 x i32> %vec.phi40, <4 x i32> %9)
  %12 = tail call <4 x i32> @llvm.umax.v4i32(<4 x i32> %vec.phi, <4 x i32> %8)
  %13 = tail call <4 x i32> @llvm.umax.v4i32(<4 x i32> %vec.phi38, <4 x i32> %9)
  %index.next = add nuw i64 %index, 8
  %14 = icmp eq i64 %index.next, %n.vec
  br i1 %14, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %rdx.minmax = tail call <4 x i32> @llvm.umax.v4i32(<4 x i32> %12, <4 x i32> %13)
  %15 = tail call i32 @llvm.vector.reduce.umax.v4i32(<4 x i32> %rdx.minmax)
  %rdx.minmax43 = tail call <4 x i32> @llvm.umin.v4i32(<4 x i32> %10, <4 x i32> %11)
  %16 = tail call i32 @llvm.vector.reduce.umin.v4i32(<4 x i32> %rdx.minmax43)
  %cmp.n = icmp eq i64 %n.vec, %5
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader45

.lr.ph.preheader45:                               ; preds = %middle.block, %.lr.ph.preheader
  %.01734.ph = phi i32 [ 0, %.lr.ph.preheader ], [ %15, %middle.block ]
  %.01833.ph = phi i32 [ -1, %.lr.ph.preheader ], [ %16, %middle.block ]
  %.02032.ph = phi ptr [ %0, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.02131.ph = phi i32 [ %1, %.lr.ph.preheader ], [ %ind.end36, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader45, %.lr.ph
  %.01734 = phi i32 [ %.1, %.lr.ph ], [ %.01734.ph, %.lr.ph.preheader45 ]
  %.01833 = phi i32 [ %spec.select, %.lr.ph ], [ %.01833.ph, %.lr.ph.preheader45 ]
  %.02032 = phi ptr [ %20, %.lr.ph ], [ %.02032.ph, %.lr.ph.preheader45 ]
  %.02131 = phi i32 [ %17, %.lr.ph ], [ %.02131.ph, %.lr.ph.preheader45 ]
  %17 = add i32 %.02131, -1
  %18 = load i16, ptr %.02032, align 2, !tbaa !5
  %19 = zext i16 %18 to i32
  %spec.select = tail call i32 @llvm.umin.i32(i32 %.01833, i32 %19)
  %.1 = tail call i32 @llvm.umax.i32(i32 %.01734, i32 %19)
  %20 = getelementptr inbounds i8, ptr %.02032, i64 2
  %.not = icmp eq i32 %17, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %4
  %.018.lcssa = phi i32 [ -1, %4 ], [ %16, %middle.block ], [ %spec.select, %.lr.ph ]
  %.017.lcssa = phi i32 [ 0, %4 ], [ %15, %middle.block ], [ %.1, %.lr.ph ]
  %reass.sub = sub nsw i32 %.017.lcssa, %.018.lcssa
  %21 = add nsw i32 %reass.sub, 1
  %22 = icmp ugt i32 %.018.lcssa, %.017.lcssa
  %spec.select29 = select i1 %22, i32 0, i32 %21
  %.not26 = icmp eq ptr %2, null
  br i1 %.not26, label %24, label %23

23:                                               ; preds = %._crit_edge
  %spec.select28 = select i1 %22, i32 0, i32 %.018.lcssa
  store i32 %spec.select28, ptr %2, align 4, !tbaa !14
  br label %24

24:                                               ; preds = %23, %._crit_edge
  %.not27 = icmp eq ptr %3, null
  br i1 %.not27, label %26, label %25

25:                                               ; preds = %24
  store i32 %spec.select29, ptr %3, align 4, !tbaa !14
  br label %26

26:                                               ; preds = %25, %24
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umin.i32(i32, i32) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umax.i32(i32, i32) #1

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

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

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

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

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(read, argmem: readwrite, 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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple 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}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}


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