; LLVM IR from zchrissirhcz/pixel :: legacy/matcalc (matrix_column_max)

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

15:                                               ; preds = %61, %5
  %16 = load i64, ptr %11, align 8, !tbaa !9
  %17 = load i64, ptr %8, align 8, !tbaa !9
  %18 = icmp ult i64 %16, %17
  br i1 %18, label %20, label %19

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

20:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  store i64 0, ptr %13, align 8, !tbaa !9
  br label %21

21:                                               ; preds = %57, %20
  %22 = load i64, ptr %13, align 8, !tbaa !9
  %23 = load i64, ptr %7, align 8, !tbaa !9
  %24 = icmp ult i64 %22, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %21
  store i32 5, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  br label %60

26:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #2
  %27 = load i64, ptr %13, align 8, !tbaa !9
  %28 = load i64, ptr %8, align 8, !tbaa !9
  %29 = mul i64 %27, %28
  %30 = load i64, ptr %11, align 8, !tbaa !9
  %31 = add i64 %29, %30
  store i64 %31, ptr %14, align 8, !tbaa !9
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = load i64, ptr %14, align 8, !tbaa !9
  %34 = getelementptr inbounds i8, ptr %32, i64 %33
  %35 = load i8, ptr %34, align 1, !tbaa !11
  %36 = zext i8 %35 to i32
  %37 = load ptr, ptr %9, align 8, !tbaa !5
  %38 = load i64, ptr %11, align 8, !tbaa !9
  %39 = getelementptr inbounds i8, ptr %37, i64 %38
  %40 = load i8, ptr %39, align 1, !tbaa !11
  %41 = zext i8 %40 to i32
  %42 = icmp sgt i32 %36, %41
  br i1 %42, label %43, label %56

43:                                               ; preds = %26
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = load i64, ptr %14, align 8, !tbaa !9
  %46 = getelementptr inbounds i8, ptr %44, i64 %45
  %47 = load i8, ptr %46, align 1, !tbaa !11
  %48 = load ptr, ptr %9, align 8, !tbaa !5
  %49 = load i64, ptr %11, align 8, !tbaa !9
  %50 = getelementptr inbounds i8, ptr %48, i64 %49
  store i8 %47, ptr %50, align 1, !tbaa !11
  %51 = load i64, ptr %13, align 8, !tbaa !9
  %52 = trunc i64 %51 to i32
  %53 = load ptr, ptr %10, align 8, !tbaa !5
  %54 = load i64, ptr %11, align 8, !tbaa !9
  %55 = getelementptr inbounds i32, ptr %53, i64 %54
  store i32 %52, ptr %55, align 4, !tbaa !12
  br label %56

56:                                               ; preds = %43, %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #2
  br label %57

57:                                               ; preds = %56
  %58 = load i64, ptr %13, align 8, !tbaa !9
  %59 = add i64 %58, 1
  store i64 %59, ptr %13, align 8, !tbaa !9
  br label %21, !llvm.loop !14

60:                                               ; preds = %25
  br label %61

61:                                               ; preds = %60
  %62 = load i64, ptr %11, align 8, !tbaa !9
  %63 = add i64 %62, 1
  store i64 %63, ptr %11, align 8, !tbaa !9
  br label %15, !llvm.loop !16

64:                                               ; preds = %19
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


; 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: readwrite) uwtable
define dso_local void @matrix_column_max_and_idx_u8_naive(ptr nocapture noundef readonly %0, i64 noundef %1, i64 noundef %2, ptr nocapture noundef %3, ptr nocapture noundef writeonly %4) local_unnamed_addr #0 {
  %.not = icmp eq i64 %2, 0
  %.not24 = icmp eq i64 %1, 0
  %or.cond = or i1 %.not, %.not24
  br i1 %or.cond, label %._crit_edge23, label %.preheader.us.preheader

.preheader.us.preheader:                          ; preds = %5
  %xtraiter = and i64 %1, 1
  %6 = icmp eq i64 %1, 1
  %unroll_iter = and i64 %1, -2
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us
  %.022.us = phi i64 [ %31, %._crit_edge.us ], [ 0, %.preheader.us.preheader ]
  %invariant.gep.us = getelementptr i8, ptr %0, i64 %.022.us
  %7 = getelementptr inbounds i8, ptr %3, i64 %.022.us
  %8 = getelementptr inbounds i32, ptr %4, i64 %.022.us
  br i1 %6, label %._crit_edge.us.unr-lcssa, label %.preheader.us.new

.preheader.us.new:                                ; preds = %.preheader.us, %23
  %.01921.us = phi i64 [ %24, %23 ], [ 0, %.preheader.us ]
  %niter = phi i64 [ %niter.next.1, %23 ], [ 0, %.preheader.us ]
  %9 = mul i64 %.01921.us, %2
  %gep.us = getelementptr i8, ptr %invariant.gep.us, i64 %9
  %10 = load i8, ptr %gep.us, align 1, !tbaa !5
  %11 = load i8, ptr %7, align 1, !tbaa !5
  %12 = icmp ugt i8 %10, %11
  br i1 %12, label %13, label %15

13:                                               ; preds = %.preheader.us.new
  store i8 %10, ptr %7, align 1, !tbaa !5
  %14 = trunc i64 %.01921.us to i32
  store i32 %14, ptr %8, align 4, !tbaa !8
  br label %15

15:                                               ; preds = %13, %.preheader.us.new
  %16 = or disjoint i64 %.01921.us, 1
  %17 = mul i64 %16, %2
  %gep.us.1 = getelementptr i8, ptr %invariant.gep.us, i64 %17
  %18 = load i8, ptr %gep.us.1, align 1, !tbaa !5
  %19 = load i8, ptr %7, align 1, !tbaa !5
  %20 = icmp ugt i8 %18, %19
  br i1 %20, label %21, label %23

21:                                               ; preds = %15
  store i8 %18, ptr %7, align 1, !tbaa !5
  %22 = trunc i64 %16 to i32
  store i32 %22, ptr %8, align 4, !tbaa !8
  br label %23

23:                                               ; preds = %21, %15
  %24 = add nuw i64 %.01921.us, 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.us.unr-lcssa, label %.preheader.us.new, !llvm.loop !10

._crit_edge.us.unr-lcssa:                         ; preds = %23, %.preheader.us
  %.01921.us.unr = phi i64 [ 0, %.preheader.us ], [ %24, %23 ]
  br i1 %lcmp.mod.not, label %._crit_edge.us, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.us.unr-lcssa
  %25 = mul i64 %.01921.us.unr, %2
  %gep.us.epil = getelementptr i8, ptr %invariant.gep.us, i64 %25
  %26 = load i8, ptr %gep.us.epil, align 1, !tbaa !5
  %27 = load i8, ptr %7, align 1, !tbaa !5
  %28 = icmp ugt i8 %26, %27
  br i1 %28, label %29, label %._crit_edge.us

29:                                               ; preds = %.epil.preheader
  store i8 %26, ptr %7, align 1, !tbaa !5
  %30 = trunc i64 %.01921.us.unr to i32
  store i32 %30, ptr %8, align 4, !tbaa !8
  br label %._crit_edge.us

._crit_edge.us:                                   ; preds = %.epil.preheader, %29, %._crit_edge.us.unr-lcssa
  %31 = add nuw i64 %.022.us, 1
  %exitcond26.not = icmp eq i64 %31, %2
  br i1 %exitcond26.not, label %._crit_edge23, label %.preheader.us, !llvm.loop !12

._crit_edge23:                                    ; preds = %._crit_edge.us, %5
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: readwrite) 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !6, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass