; LLVM IR from tessarin/dither :: src (DTPalette)

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

%struct.DTPixel = type { i8, i8, i8 }
%struct.DTPalette = type { i32, ptr }

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

15:                                               ; preds = %68, %2
  %16 = load i32, ptr %13, align 4, !tbaa !9
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.DTPalette, ptr %17, i32 0, i32 0
  %19 = load i32, ptr %18, align 8, !tbaa !11
  %20 = icmp slt i32 %16, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  br label %71

22:                                               ; preds = %15
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.DTPalette, ptr %23, i32 0, i32 1
  %25 = load ptr, ptr %24, align 8, !tbaa !13
  %26 = load i32, ptr %13, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds %struct.DTPixel, ptr %25, i64 %27
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %12, ptr align 1 %28, i64 3, i1 false), !tbaa.struct !14
  %29 = getelementptr inbounds %struct.DTPixel, ptr %4, i32 0, i32 0
  %30 = load i8, ptr %29, align 1, !tbaa !16
  %31 = zext i8 %30 to i32
  %32 = getelementptr inbounds %struct.DTPixel, ptr %12, i32 0, i32 0
  %33 = load i8, ptr %32, align 1, !tbaa !16
  %34 = zext i8 %33 to i32
  %35 = sub nsw i32 %31, %34
  store i32 %35, ptr %9, align 4, !tbaa !9
  %36 = getelementptr inbounds %struct.DTPixel, ptr %4, i32 0, i32 1
  %37 = load i8, ptr %36, align 1, !tbaa !18
  %38 = zext i8 %37 to i32
  %39 = getelementptr inbounds %struct.DTPixel, ptr %12, i32 0, i32 1
  %40 = load i8, ptr %39, align 1, !tbaa !18
  %41 = zext i8 %40 to i32
  %42 = sub nsw i32 %38, %41
  store i32 %42, ptr %10, align 4, !tbaa !9
  %43 = getelementptr inbounds %struct.DTPixel, ptr %4, i32 0, i32 2
  %44 = load i8, ptr %43, align 1, !tbaa !19
  %45 = zext i8 %44 to i32
  %46 = getelementptr inbounds %struct.DTPixel, ptr %12, i32 0, i32 2
  %47 = load i8, ptr %46, align 1, !tbaa !19
  %48 = zext i8 %47 to i32
  %49 = sub nsw i32 %45, %48
  store i32 %49, ptr %11, align 4, !tbaa !9
  %50 = load i32, ptr %9, align 4, !tbaa !9
  %51 = load i32, ptr %9, align 4, !tbaa !9
  %52 = mul nsw i32 %50, %51
  %53 = load i32, ptr %10, align 4, !tbaa !9
  %54 = load i32, ptr %10, align 4, !tbaa !9
  %55 = mul nsw i32 %53, %54
  %56 = add nsw i32 %52, %55
  %57 = load i32, ptr %11, align 4, !tbaa !9
  %58 = load i32, ptr %11, align 4, !tbaa !9
  %59 = mul nsw i32 %57, %58
  %60 = add nsw i32 %56, %59
  store i32 %60, ptr %7, align 4, !tbaa !9
  %61 = load i32, ptr %7, align 4, !tbaa !9
  %62 = load i32, ptr %8, align 4, !tbaa !9
  %63 = icmp slt i32 %61, %62
  br i1 %63, label %64, label %67

64:                                               ; preds = %22
  %65 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %65, ptr %8, align 4, !tbaa !9
  %66 = load i32, ptr %13, align 4, !tbaa !9
  store i32 %66, ptr %6, align 4, !tbaa !9
  br label %67

67:                                               ; preds = %64, %22
  br label %68

68:                                               ; preds = %67
  %69 = load i32, ptr %13, align 4, !tbaa !9
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %13, align 4, !tbaa !9
  br label %15, !llvm.loop !20

71:                                               ; preds = %21
  %72 = load ptr, ptr %5, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.DTPalette, ptr %72, i32 0, i32 1
  %74 = load ptr, ptr %73, align 8, !tbaa !13
  %75 = load i32, ptr %6, align 4, !tbaa !9
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds %struct.DTPixel, ptr %74, i64 %76
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %3, ptr align 1 %77, i64 3, i1 false), !tbaa.struct !14
  call void @llvm.lifetime.end.p0(i64 3, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %14, ptr align 1 %3, i64 3, i1 false)
  %78 = load i24, ptr %14, align 4
  ret i24 %78
}

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 = { 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 = !{!12, !10, i64 0}
!12 = !{!"", !10, i64 0, !6, i64 8}
!13 = !{!12, !6, i64 8}
!14 = !{i64 0, i64 1, !15, i64 1, i64 1, !15, i64 2, i64 1, !15}
!15 = !{!7, !7, i64 0}
!16 = !{!17, !7, i64 0}
!17 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2}
!18 = !{!17, !7, i64 1}
!19 = !{!17, !7, i64 2}
!20 = distinct !{!20, !21}
!21 = !{!"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"

%struct.DTPixel = type { i8, i8, i8 }

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local i24 @FindClosestColorFromPalette(i24 %0, ptr nocapture noundef readonly %1) local_unnamed_addr #0 {
  %.sroa.3.0.extract.shift = lshr i24 %0, 16
  %.sroa.3.0.extract.trunc = zext nneg i24 %.sroa.3.0.extract.shift to i32
  %3 = load i32, ptr %1, align 8, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph, label %.._crit_edge_crit_edge

.._crit_edge_crit_edge:                           ; preds = %2
  %.phi.trans.insert = getelementptr inbounds i8, ptr %1, i64 8
  %.pre = load ptr, ptr %.phi.trans.insert, align 8, !tbaa !11
  br label %._crit_edge

.lr.ph:                                           ; preds = %2
  %.sroa.2.0.extract.shift = lshr i24 %0, 8
  %5 = getelementptr inbounds i8, ptr %1, i64 8
  %6 = load ptr, ptr %5, align 8, !tbaa !11
  %7 = and i24 %0, 255
  %8 = zext nneg i24 %7 to i32
  %9 = and i24 %.sroa.2.0.extract.shift, 255
  %10 = zext nneg i24 %9 to i32
  %wide.trip.count = zext nneg i32 %3 to i64
  br label %14

._crit_edge.loopexit:                             ; preds = %14
  %11 = sext i32 %spec.select23 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %.._crit_edge_crit_edge, %._crit_edge.loopexit
  %12 = phi ptr [ %.pre, %.._crit_edge_crit_edge ], [ %6, %._crit_edge.loopexit ]
  %.0.lcssa = phi i64 [ 0, %.._crit_edge_crit_edge ], [ %11, %._crit_edge.loopexit ]
  %13 = getelementptr inbounds %struct.DTPixel, ptr %12, i64 %.0.lcssa
  %.sroa.018.0.copyload = load i24, ptr %13, align 1
  ret i24 %.sroa.018.0.copyload

14:                                               ; preds = %.lr.ph, %14
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %14 ]
  %.026 = phi i32 [ undef, %.lr.ph ], [ %spec.select23, %14 ]
  %.02024 = phi i32 [ 195076, %.lr.ph ], [ %spec.select, %14 ]
  %15 = getelementptr inbounds %struct.DTPixel, ptr %6, i64 %indvars.iv
  %.sroa.0.0.copyload = load i8, ptr %15, align 1, !tbaa !12
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %15, i64 1
  %.sroa.4.0.copyload = load i8, ptr %.sroa.4.0..sroa_idx, align 1, !tbaa !12
  %.sroa.5.0..sroa_idx = getelementptr inbounds i8, ptr %15, i64 2
  %.sroa.5.0.copyload = load i8, ptr %.sroa.5.0..sroa_idx, align 1, !tbaa !12
  %16 = zext i8 %.sroa.0.0.copyload to i32
  %17 = sub nsw i32 %8, %16
  %18 = zext i8 %.sroa.4.0.copyload to i32
  %19 = sub nsw i32 %10, %18
  %20 = zext i8 %.sroa.5.0.copyload to i32
  %21 = sub nsw i32 %.sroa.3.0.extract.trunc, %20
  %22 = mul nsw i32 %17, %17
  %23 = mul nsw i32 %19, %19
  %24 = add nuw nsw i32 %23, %22
  %25 = mul nsw i32 %21, %21
  %26 = add nuw nsw i32 %24, %25
  %27 = icmp slt i32 %26, %.02024
  %spec.select = tail call i32 @llvm.smin.i32(i32 %26, i32 %.02024)
  %28 = trunc nuw nsw i64 %indvars.iv to i32
  %spec.select23 = select i1 %27, i32 %28, i32 %.026
  %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.loopexit, label %14, !llvm.loop !13
}

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

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


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass