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

; 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.MCTriplet = type { [3 x i8] }
%struct.MCCube = type { %struct.MCTriplet, %struct.MCTriplet, i32, ptr }

; Function Attrs: nounwind uwtable
declare dso_local i24 @MCTripletMake(i8 noundef zeroext, i8 noundef zeroext, i8 noundef zeroext) #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 i24 @MCCubeAverage(ptr noundef %0) #0 {
  %2 = alloca %struct.MCTriplet, align 1
  %3 = alloca ptr, align 8
  %4 = alloca i24, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %5 = load ptr, ptr %3, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.MCCube, ptr %5, i32 0, i32 1
  %7 = getelementptr inbounds %struct.MCTriplet, ptr %6, i32 0, i32 0
  %8 = getelementptr inbounds [3 x i8], ptr %7, i64 0, i64 0
  %9 = load i8, ptr %8, align 1, !tbaa !9
  %10 = zext i8 %9 to i32
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.MCCube, ptr %11, i32 0, i32 0
  %13 = getelementptr inbounds %struct.MCTriplet, ptr %12, i32 0, i32 0
  %14 = getelementptr inbounds [3 x i8], ptr %13, i64 0, i64 0
  %15 = load i8, ptr %14, align 8, !tbaa !9
  %16 = zext i8 %15 to i32
  %17 = add nsw i32 %10, %16
  %18 = sdiv i32 %17, 2
  %19 = trunc i32 %18 to i8
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.MCCube, ptr %20, i32 0, i32 1
  %22 = getelementptr inbounds %struct.MCTriplet, ptr %21, i32 0, i32 0
  %23 = getelementptr inbounds [3 x i8], ptr %22, i64 0, i64 1
  %24 = load i8, ptr %23, align 1, !tbaa !9
  %25 = zext i8 %24 to i32
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.MCCube, ptr %26, i32 0, i32 0
  %28 = getelementptr inbounds %struct.MCTriplet, ptr %27, i32 0, i32 0
  %29 = getelementptr inbounds [3 x i8], ptr %28, i64 0, i64 1
  %30 = load i8, ptr %29, align 1, !tbaa !9
  %31 = zext i8 %30 to i32
  %32 = add nsw i32 %25, %31
  %33 = sdiv i32 %32, 2
  %34 = trunc i32 %33 to i8
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.MCCube, ptr %35, i32 0, i32 1
  %37 = getelementptr inbounds %struct.MCTriplet, ptr %36, i32 0, i32 0
  %38 = getelementptr inbounds [3 x i8], ptr %37, i64 0, i64 2
  %39 = load i8, ptr %38, align 1, !tbaa !9
  %40 = zext i8 %39 to i32
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.MCCube, ptr %41, i32 0, i32 0
  %43 = getelementptr inbounds %struct.MCTriplet, ptr %42, i32 0, i32 0
  %44 = getelementptr inbounds [3 x i8], ptr %43, i64 0, i64 2
  %45 = load i8, ptr %44, align 2, !tbaa !9
  %46 = zext i8 %45 to i32
  %47 = add nsw i32 %40, %46
  %48 = sdiv i32 %47, 2
  %49 = trunc i32 %48 to i8
  %50 = call i24 @MCTripletMake(i8 noundef zeroext %19, i8 noundef zeroext %34, i8 noundef zeroext %49)
  %51 = getelementptr inbounds %struct.MCTriplet, ptr %2, i32 0, i32 0
  store i24 %50, ptr %51, align 1
  %52 = getelementptr inbounds %struct.MCTriplet, ptr %2, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %4, ptr align 1 %52, i64 3, i1 false)
  %53 = load i24, ptr %4, align 4
  ret i24 %53
}

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

!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 = !{!7, !7, i64 0}


; 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
declare dso_local i24 @MCTripletMake(i8 noundef zeroext, i8 noundef zeroext, i8 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i24 @MCCubeAverage(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 3
  %3 = load i8, ptr %2, align 1, !tbaa !5
  %4 = zext i8 %3 to i16
  %5 = load i8, ptr %0, align 8, !tbaa !5
  %6 = zext i8 %5 to i16
  %7 = add nuw nsw i16 %6, %4
  %8 = lshr i16 %7, 1
  %9 = trunc nuw i16 %8 to i8
  %10 = getelementptr inbounds i8, ptr %0, i64 4
  %11 = load i8, ptr %10, align 1, !tbaa !5
  %12 = zext i8 %11 to i16
  %13 = getelementptr inbounds i8, ptr %0, i64 1
  %14 = load i8, ptr %13, align 1, !tbaa !5
  %15 = zext i8 %14 to i16
  %16 = add nuw nsw i16 %15, %12
  %17 = lshr i16 %16, 1
  %18 = trunc nuw i16 %17 to i8
  %19 = getelementptr inbounds i8, ptr %0, i64 5
  %20 = load i8, ptr %19, align 1, !tbaa !5
  %21 = zext i8 %20 to i16
  %22 = getelementptr inbounds i8, ptr %0, i64 2
  %23 = load i8, ptr %22, align 2, !tbaa !5
  %24 = zext i8 %23 to i16
  %25 = add nuw nsw i16 %24, %21
  %26 = lshr i16 %25, 1
  %27 = trunc nuw i16 %26 to i8
  %28 = tail call i24 @MCTripletMake(i8 noundef zeroext %9, i8 noundef zeroext %18, i8 noundef zeroext %27)
  ret i24 %28
}

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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}


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