; 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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local void @matrix_column_max_and_idx_u8_cacheline(ptr noundef %0, i64 noundef %1, i64 noundef %2, ptr noundef %3, ptr noundef %4) #2 {
  %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 ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca i32, align 4
  %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
  %15 = load ptr, ptr %9, align 8, !tbaa !5
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = load i64, ptr %8, align 8, !tbaa !9
  %18 = mul i64 %17, 1
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %15, ptr align 1 %16, i64 %18, i1 false)
  %19 = load ptr, ptr %10, align 8, !tbaa !5
  %20 = load i64, ptr %8, align 8, !tbaa !9
  %21 = mul i64 %20, 4
  call void @llvm.memset.p0.i64(ptr align 4 %19, i8 0, i64 %21, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = load i64, ptr %8, align 8, !tbaa !9
  %24 = getelementptr inbounds i8, ptr %22, i64 %23
  store ptr %24, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  store i64 1, ptr %12, align 8, !tbaa !9
  br label %25

25:                                               ; preds = %69, %5
  %26 = load i64, ptr %12, align 8, !tbaa !9
  %27 = load i64, ptr %7, align 8, !tbaa !9
  %28 = icmp ult i64 %26, %27
  br i1 %28, label %30, label %29

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

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

31:                                               ; preds = %62, %30
  %32 = load i64, ptr %14, align 8, !tbaa !9
  %33 = load i64, ptr %8, align 8, !tbaa !9
  %34 = icmp ult i64 %32, %33
  br i1 %34, label %36, label %35

35:                                               ; preds = %31
  store i32 5, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  br label %65

36:                                               ; preds = %31
  %37 = load ptr, ptr %11, align 8, !tbaa !5
  %38 = load i64, ptr %14, 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 = load ptr, ptr %9, align 8, !tbaa !5
  %43 = load i64, ptr %14, align 8, !tbaa !9
  %44 = getelementptr inbounds i8, ptr %42, i64 %43
  %45 = load i8, ptr %44, align 1, !tbaa !11
  %46 = zext i8 %45 to i32
  %47 = icmp sgt i32 %41, %46
  br i1 %47, label %48, label %61

48:                                               ; preds = %36
  %49 = load ptr, ptr %11, align 8, !tbaa !5
  %50 = load i64, ptr %14, align 8, !tbaa !9
  %51 = getelementptr inbounds i8, ptr %49, i64 %50
  %52 = load i8, ptr %51, align 1, !tbaa !11
  %53 = load ptr, ptr %9, align 8, !tbaa !5
  %54 = load i64, ptr %14, align 8, !tbaa !9
  %55 = getelementptr inbounds i8, ptr %53, i64 %54
  store i8 %52, ptr %55, align 1, !tbaa !11
  %56 = load i64, ptr %12, align 8, !tbaa !9
  %57 = trunc i64 %56 to i32
  %58 = load ptr, ptr %10, align 8, !tbaa !5
  %59 = load i64, ptr %14, align 8, !tbaa !9
  %60 = getelementptr inbounds i32, ptr %58, i64 %59
  store i32 %57, ptr %60, align 4, !tbaa !12
  br label %61

61:                                               ; preds = %48, %36
  br label %62

62:                                               ; preds = %61
  %63 = load i64, ptr %14, align 8, !tbaa !9
  %64 = add i64 %63, 1
  store i64 %64, ptr %14, align 8, !tbaa !9
  br label %31, !llvm.loop !14

65:                                               ; preds = %35
  %66 = load i64, ptr %8, align 8, !tbaa !9
  %67 = load ptr, ptr %11, align 8, !tbaa !5
  %68 = getelementptr inbounds i8, ptr %67, i64 %66
  store ptr %68, ptr %11, align 8, !tbaa !5
  br label %69

69:                                               ; preds = %65
  %70 = load i64, ptr %12, align 8, !tbaa !9
  %71 = add i64 %70, 1
  store i64 %71, ptr %12, align 8, !tbaa !9
  br label %25, !llvm.loop !16

72:                                               ; preds = %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { 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: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

; Function Attrs: nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local void @matrix_column_max_and_idx_u8_cacheline(ptr nocapture noundef readonly %0, i64 noundef %1, i64 noundef %2, ptr nocapture noundef %3, ptr nocapture noundef writeonly %4) local_unnamed_addr #1 {
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %3, ptr align 1 %0, i64 %2, i1 false)
  %6 = shl i64 %2, 2
  tail call void @llvm.memset.p0.i64(ptr align 4 %4, i8 0, i64 %6, i1 false)
  %7 = icmp ult i64 %1, 2
  %.not = icmp eq i64 %2, 0
  %or.cond = or i1 %7, %.not
  br i1 %or.cond, label %._crit_edge32, label %.preheader.us.preheader

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

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us
  %.02631.us.pn = phi ptr [ %.02631.us, %._crit_edge.us ], [ %0, %.preheader.us.preheader ]
  %.02530.us = phi i64 [ %35, %._crit_edge.us ], [ 1, %.preheader.us.preheader ]
  %.02631.us = getelementptr inbounds i8, ptr %.02631.us.pn, i64 %2
  %9 = trunc i64 %.02530.us to i32
  br i1 %8, label %._crit_edge.us.unr-lcssa, label %.preheader.us.new

.preheader.us.new:                                ; preds = %.preheader.us, %26
  %.028.us = phi i64 [ %27, %26 ], [ 0, %.preheader.us ]
  %niter = phi i64 [ %niter.next.1, %26 ], [ 0, %.preheader.us ]
  %10 = getelementptr inbounds i8, ptr %.02631.us, i64 %.028.us
  %11 = load i8, ptr %10, align 1, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %3, i64 %.028.us
  %13 = load i8, ptr %12, align 1, !tbaa !5
  %14 = icmp ugt i8 %11, %13
  br i1 %14, label %15, label %17

15:                                               ; preds = %.preheader.us.new
  store i8 %11, ptr %12, align 1, !tbaa !5
  %16 = getelementptr inbounds i32, ptr %4, i64 %.028.us
  store i32 %9, ptr %16, align 4, !tbaa !8
  br label %17

17:                                               ; preds = %15, %.preheader.us.new
  %18 = or disjoint i64 %.028.us, 1
  %19 = getelementptr inbounds i8, ptr %.02631.us, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %3, i64 %18
  %22 = load i8, ptr %21, align 1, !tbaa !5
  %23 = icmp ugt i8 %20, %22
  br i1 %23, label %24, label %26

24:                                               ; preds = %17
  store i8 %20, ptr %21, align 1, !tbaa !5
  %25 = getelementptr inbounds i32, ptr %4, i64 %18
  store i32 %9, ptr %25, align 4, !tbaa !8
  br label %26

26:                                               ; preds = %24, %17
  %27 = add nuw i64 %.028.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 = %26, %.preheader.us
  %.028.us.unr = phi i64 [ 0, %.preheader.us ], [ %27, %26 ]
  br i1 %lcmp.mod.not, label %._crit_edge.us, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.us.unr-lcssa
  %28 = getelementptr inbounds i8, ptr %.02631.us, i64 %.028.us.unr
  %29 = load i8, ptr %28, align 1, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %3, i64 %.028.us.unr
  %31 = load i8, ptr %30, align 1, !tbaa !5
  %32 = icmp ugt i8 %29, %31
  br i1 %32, label %33, label %._crit_edge.us

33:                                               ; preds = %.epil.preheader
  store i8 %29, ptr %30, align 1, !tbaa !5
  %34 = getelementptr inbounds i32, ptr %4, i64 %.028.us.unr
  store i32 %9, ptr %34, align 4, !tbaa !8
  br label %._crit_edge.us

._crit_edge.us:                                   ; preds = %.epil.preheader, %33, %._crit_edge.us.unr-lcssa
  %35 = add nuw i64 %.02530.us, 1
  %exitcond34.not = icmp eq i64 %35, %1
  br i1 %exitcond34.not, label %._crit_edge32, label %.preheader.us, !llvm.loop !12

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }

!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,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass