; 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: 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: nounwind uwtable
define dso_local i48 @CalculateDifference(i24 %0, i24 %1) #1 {
  %3 = alloca %struct.DTDiff, align 2
  %4 = alloca %struct.DTPixel, align 1
  %5 = alloca %struct.DTPixel, align 1
  %6 = alloca i48, align 8
  store i24 %0, ptr %4, align 1
  store i24 %1, ptr %5, align 1
  %7 = getelementptr inbounds %struct.DTPixel, ptr %4, i32 0, i32 0
  %8 = load i8, ptr %7, align 1, !tbaa !5
  %9 = zext i8 %8 to i32
  %10 = getelementptr inbounds %struct.DTPixel, ptr %5, i32 0, i32 0
  %11 = load i8, ptr %10, align 1, !tbaa !5
  %12 = zext i8 %11 to i32
  %13 = sub nsw i32 %9, %12
  %14 = trunc i32 %13 to i16
  %15 = getelementptr inbounds %struct.DTDiff, ptr %3, i32 0, i32 0
  store i16 %14, ptr %15, align 2, !tbaa !9
  %16 = getelementptr inbounds %struct.DTPixel, ptr %4, i32 0, i32 1
  %17 = load i8, ptr %16, align 1, !tbaa !12
  %18 = zext i8 %17 to i32
  %19 = getelementptr inbounds %struct.DTPixel, ptr %5, i32 0, i32 1
  %20 = load i8, ptr %19, align 1, !tbaa !12
  %21 = zext i8 %20 to i32
  %22 = sub nsw i32 %18, %21
  %23 = trunc i32 %22 to i16
  %24 = getelementptr inbounds %struct.DTDiff, ptr %3, i32 0, i32 1
  store i16 %23, ptr %24, align 2, !tbaa !13
  %25 = getelementptr inbounds %struct.DTPixel, ptr %4, i32 0, i32 2
  %26 = load i8, ptr %25, align 1, !tbaa !14
  %27 = zext i8 %26 to i32
  %28 = getelementptr inbounds %struct.DTPixel, ptr %5, i32 0, i32 2
  %29 = load i8, ptr %28, align 1, !tbaa !14
  %30 = zext i8 %29 to i32
  %31 = sub nsw i32 %27, %30
  %32 = trunc i32 %31 to i16
  %33 = getelementptr inbounds %struct.DTDiff, ptr %3, i32 0, i32 2
  store i16 %32, ptr %33, align 2, !tbaa !15
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %6, ptr align 2 %3, i64 6, i1 false)
  %34 = load i48, ptr %6, align 8
  ret i48 %34
}

attributes #0 = { nocallback nofree 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" }

!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 = !{!10, !11, i64 0}
!10 = !{!"", !11, i64 0, !11, i64 2, !11, i64 4}
!11 = !{!"short", !7, i64 0}
!12 = !{!6, !7, i64 1}
!13 = !{!10, !11, i64 2}
!14 = !{!6, !7, i64 2}
!15 = !{!10, !11, 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: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local i48 @CalculateDifference(i24 %0, i24 %1) local_unnamed_addr #0 {
  %.sroa.33.0.extract.shift = lshr i24 %0, 16
  %.sroa.33.0.extract.trunc = trunc nuw nsw i24 %.sroa.33.0.extract.shift to i16
  %.sroa.3.0.extract.shift = lshr i24 %1, 16
  %.sroa.3.0.extract.trunc = trunc nuw nsw i24 %.sroa.3.0.extract.shift to i16
  %3 = trunc i24 %0 to i16
  %4 = and i16 %3, 255
  %5 = trunc i24 %1 to i16
  %6 = and i16 %5, 255
  %7 = sub nsw i16 %4, %6
  %8 = lshr i16 %3, 8
  %9 = lshr i16 %5, 8
  %10 = sub nsw i16 %8, %9
  %11 = sub nsw i16 %.sroa.33.0.extract.trunc, %.sroa.3.0.extract.trunc
  %.sroa.36.0.insert.ext = zext i16 %11 to i48
  %.sroa.36.0.insert.shift = shl nuw i48 %.sroa.36.0.insert.ext, 32
  %.sroa.25.0.insert.ext = zext i16 %10 to i48
  %.sroa.25.0.insert.shift = shl nuw nsw i48 %.sroa.25.0.insert.ext, 16
  %.sroa.25.0.insert.insert = or disjoint i48 %.sroa.36.0.insert.shift, %.sroa.25.0.insert.shift
  %.sroa.04.0.insert.ext = zext i16 %7 to i48
  %.sroa.04.0.insert.insert = or disjoint i48 %.sroa.25.0.insert.insert, %.sroa.04.0.insert.ext
  ret i48 %.sroa.04.0.insert.insert
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(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" }

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


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