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

; 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.DTDiff = type { i16, i16, i16 }
%struct.DTPixel = type { i8, i8, i8 }

; Function Attrs: nounwind uwtable
define dso_local void @ApplyDifference(ptr noundef %0, i48 %1, i32 noundef %2) #0 {
  %4 = alloca %struct.DTDiff, align 2
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store i48 %1, ptr %4, align 2
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  %7 = load ptr, ptr %5, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.DTPixel, ptr %7, i32 0, i32 0
  %9 = load i8, ptr %8, align 1, !tbaa !11
  %10 = zext i8 %9 to i32
  %11 = getelementptr inbounds %struct.DTDiff, ptr %4, i32 0, i32 0
  %12 = load i16, ptr %11, align 2, !tbaa !13
  %13 = sext i16 %12 to i32
  %14 = load i32, ptr %6, align 4, !tbaa !9
  %15 = mul nsw i32 %13, %14
  %16 = sdiv i32 %15, 16
  %17 = add nsw i32 %10, %16
  %18 = call zeroext i8 @ByteCap(i32 noundef %17)
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.DTPixel, ptr %19, i32 0, i32 0
  store i8 %18, ptr %20, align 1, !tbaa !11
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.DTPixel, ptr %21, i32 0, i32 1
  %23 = load i8, ptr %22, align 1, !tbaa !16
  %24 = zext i8 %23 to i32
  %25 = getelementptr inbounds %struct.DTDiff, ptr %4, i32 0, i32 1
  %26 = load i16, ptr %25, align 2, !tbaa !17
  %27 = sext i16 %26 to i32
  %28 = load i32, ptr %6, align 4, !tbaa !9
  %29 = mul nsw i32 %27, %28
  %30 = sdiv i32 %29, 16
  %31 = add nsw i32 %24, %30
  %32 = call zeroext i8 @ByteCap(i32 noundef %31)
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.DTPixel, ptr %33, i32 0, i32 1
  store i8 %32, ptr %34, align 1, !tbaa !16
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.DTPixel, ptr %35, i32 0, i32 2
  %37 = load i8, ptr %36, align 1, !tbaa !18
  %38 = zext i8 %37 to i32
  %39 = getelementptr inbounds %struct.DTDiff, ptr %4, i32 0, i32 2
  %40 = load i16, ptr %39, align 2, !tbaa !19
  %41 = sext i16 %40 to i32
  %42 = load i32, ptr %6, align 4, !tbaa !9
  %43 = mul nsw i32 %41, %42
  %44 = sdiv i32 %43, 16
  %45 = add nsw i32 %38, %44
  %46 = call zeroext i8 @ByteCap(i32 noundef %45)
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.DTPixel, ptr %47, i32 0, i32 2
  store i8 %46, ptr %48, align 1, !tbaa !18
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @ByteCap(i32 noundef) #0

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

!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, !7, i64 0}
!12 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2}
!13 = !{!14, !15, i64 0}
!14 = !{!"", !15, i64 0, !15, i64 2, !15, i64 4}
!15 = !{!"short", !7, i64 0}
!16 = !{!12, !7, i64 1}
!17 = !{!14, !15, i64 2}
!18 = !{!12, !7, i64 2}
!19 = !{!14, !15, i64 4}


; 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: nounwind uwtable
define dso_local void @ApplyDifference(ptr nocapture noundef %0, i48 %1, i32 noundef %2) local_unnamed_addr #0 {
  %.sroa.0.0.extract.trunc = trunc i48 %1 to i32
  %4 = load i8, ptr %0, align 1, !tbaa !5
  %5 = zext i8 %4 to i32
  %sext = shl i32 %.sroa.0.0.extract.trunc, 16
  %6 = ashr exact i32 %sext, 16
  %7 = mul nsw i32 %6, %2
  %8 = sdiv i32 %7, 16
  %9 = add nsw i32 %8, %5
  %10 = tail call zeroext i8 @ByteCap(i32 noundef %9)
  store i8 %10, ptr %0, align 1, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %0, i64 1
  %12 = load i8, ptr %11, align 1, !tbaa !9
  %13 = zext i8 %12 to i32
  %14 = ashr i32 %.sroa.0.0.extract.trunc, 16
  %15 = mul nsw i32 %14, %2
  %16 = sdiv i32 %15, 16
  %17 = add nsw i32 %16, %13
  %18 = tail call zeroext i8 @ByteCap(i32 noundef %17)
  store i8 %18, ptr %11, align 1, !tbaa !9
  %19 = getelementptr inbounds i8, ptr %0, i64 2
  %20 = load i8, ptr %19, align 1, !tbaa !10
  %21 = zext i8 %20 to i32
  %sh.diff = lshr i48 %1, 16
  %tr.sh.diff = trunc nuw i48 %sh.diff to i32
  %22 = ashr i32 %tr.sh.diff, 16
  %23 = mul nsw i32 %22, %2
  %24 = sdiv i32 %23, 16
  %25 = add nsw i32 %24, %21
  %26 = tail call zeroext i8 @ByteCap(i32 noundef %25)
  store i8 %26, ptr %19, align 1, !tbaa !10
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @ByteCap(i32 noundef) local_unnamed_addr #0

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

!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, !7, i64 1, !7, i64 2}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!6, !7, i64 1}
!10 = !{!6, !7, i64 2}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,TailCallElimPass