; LLVM IR from pixpark/gpupixel :: third_party/libyuv/util (psnr)

; 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: mustprogress uwtable
define dso_local double @ComputeSumSquareError(ptr noundef %0, ptr noundef %1, i32 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store ptr @_ZL16SumSquareError_CPKhS0_i, ptr %7, align 8, !tbaa !5
  %12 = call noundef i32 @_ZL10CpuHasSSE2v()
  %13 = icmp ne i32 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %3
  store ptr @_ZL19SumSquareError_SSE2PKhS0_i, ptr %7, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %14, %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 32768, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  store i64 0, ptr %9, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %36, %15
  %17 = load i32, ptr %10, align 4, !tbaa !9
  %18 = load i32, ptr %6, align 4, !tbaa !9
  %19 = sub nsw i32 %18, 32767
  %20 = icmp slt i32 %17, %19
  br i1 %20, label %22, label %21

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

22:                                               ; preds = %16
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = load i32, ptr %10, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i8, ptr %24, i64 %26
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = load i32, ptr %10, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i8, ptr %28, i64 %30
  %32 = call noundef i32 %23(ptr noundef %27, ptr noundef %31, i32 noundef 32768)
  %33 = zext i32 %32 to i64
  %34 = load i64, ptr %9, align 8, !tbaa !11
  %35 = add i64 %34, %33
  store i64 %35, ptr %9, align 8, !tbaa !11
  br label %36

36:                                               ; preds = %22
  %37 = load i32, ptr %10, align 4, !tbaa !9
  %38 = add nsw i32 %37, 32768
  store i32 %38, ptr %10, align 4, !tbaa !9
  br label %16, !llvm.loop !13

39:                                               ; preds = %21
  %40 = load i32, ptr %6, align 4, !tbaa !9
  %41 = and i32 %40, -32768
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = sext i32 %41 to i64
  %44 = getelementptr inbounds i8, ptr %42, i64 %43
  store ptr %44, ptr %4, align 8, !tbaa !5
  %45 = load i32, ptr %6, align 4, !tbaa !9
  %46 = and i32 %45, -32768
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = sext i32 %46 to i64
  %49 = getelementptr inbounds i8, ptr %47, i64 %48
  store ptr %49, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %50 = load i32, ptr %6, align 4, !tbaa !9
  %51 = and i32 %50, 32767
  %52 = and i32 %51, -16
  store i32 %52, ptr %11, align 4, !tbaa !9
  %53 = load i32, ptr %11, align 4, !tbaa !9
  %54 = icmp ne i32 %53, 0
  br i1 %54, label %55, label %72

55:                                               ; preds = %39
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = load i32, ptr %11, align 4, !tbaa !9
  %60 = call noundef i32 %56(ptr noundef %57, ptr noundef %58, i32 noundef %59)
  %61 = zext i32 %60 to i64
  %62 = load i64, ptr %9, align 8, !tbaa !11
  %63 = add i64 %62, %61
  store i64 %63, ptr %9, align 8, !tbaa !11
  %64 = load i32, ptr %11, align 4, !tbaa !9
  %65 = load ptr, ptr %4, align 8, !tbaa !5
  %66 = sext i32 %64 to i64
  %67 = getelementptr inbounds i8, ptr %65, i64 %66
  store ptr %67, ptr %4, align 8, !tbaa !5
  %68 = load i32, ptr %11, align 4, !tbaa !9
  %69 = load ptr, ptr %5, align 8, !tbaa !5
  %70 = sext i32 %68 to i64
  %71 = getelementptr inbounds i8, ptr %69, i64 %70
  store ptr %71, ptr %5, align 8, !tbaa !5
  br label %72

72:                                               ; preds = %55, %39
  %73 = load i32, ptr %6, align 4, !tbaa !9
  %74 = and i32 %73, 15
  store i32 %74, ptr %11, align 4, !tbaa !9
  %75 = load i32, ptr %11, align 4, !tbaa !9
  %76 = icmp ne i32 %75, 0
  br i1 %76, label %77, label %85

77:                                               ; preds = %72
  %78 = load ptr, ptr %4, align 8, !tbaa !5
  %79 = load ptr, ptr %5, align 8, !tbaa !5
  %80 = load i32, ptr %11, align 4, !tbaa !9
  %81 = call noundef i32 @_ZL16SumSquareError_CPKhS0_i(ptr noundef %78, ptr noundef %79, i32 noundef %80)
  %82 = zext i32 %81 to i64
  %83 = load i64, ptr %9, align 8, !tbaa !11
  %84 = add i64 %83, %82
  store i64 %84, ptr %9, align 8, !tbaa !11
  br label %85

85:                                               ; preds = %77, %72
  %86 = load i64, ptr %9, align 8, !tbaa !11
  %87 = uitofp i64 %86 to double
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret double %87
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: mustprogress nounwind uwtable
declare hidden noundef i32 @_ZL16SumSquareError_CPKhS0_i(ptr noundef, ptr noundef, i32 noundef) #2

; Function Attrs: mustprogress uwtable
declare hidden noundef i32 @_ZL10CpuHasSSE2v() #0

; Function Attrs: mustprogress nounwind uwtable
declare hidden noundef i32 @_ZL19SumSquareError_SSE2PKhS0_i(ptr noundef, ptr noundef, i32 noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { mustprogress 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 willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

; Function Attrs: mustprogress uwtable
define dso_local double @ComputeSumSquareError(ptr noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = tail call noundef i32 @_ZL10CpuHasSSE2v()
  %.not = icmp eq i32 %4, 0
  %spec.select = select i1 %.not, ptr @_ZL16SumSquareError_CPKhS0_i, ptr @_ZL19SumSquareError_SSE2PKhS0_i
  %5 = add nsw i32 %2, -32767
  %6 = icmp sgt i32 %2, 32767
  br i1 %6, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %3
  %.030.lcssa = phi i64 [ 0, %3 ], [ %16, %.lr.ph ]
  %7 = and i32 %2, -32768
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds i8, ptr %0, i64 %8
  %10 = getelementptr inbounds i8, ptr %1, i64 %8
  %11 = and i32 %2, 32752
  %.not36 = icmp eq i32 %11, 0
  br i1 %.not36, label %26, label %19

.lr.ph:                                           ; preds = %3, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %3 ]
  %.03038 = phi i64 [ %16, %.lr.ph ], [ 0, %3 ]
  %12 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv
  %13 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv
  %14 = tail call noundef i32 %spec.select(ptr noundef %12, ptr noundef %13, i32 noundef 32768), !callees !5
  %15 = zext i32 %14 to i64
  %16 = add i64 %.03038, %15
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 32768
  %17 = trunc nuw i64 %indvars.iv.next to i32
  %18 = icmp sgt i32 %5, %17
  br i1 %18, label %.lr.ph, label %._crit_edge, !llvm.loop !6

19:                                               ; preds = %._crit_edge
  %20 = tail call noundef i32 %spec.select(ptr noundef %9, ptr noundef %10, i32 noundef %11), !callees !5
  %21 = zext i32 %20 to i64
  %22 = add i64 %.030.lcssa, %21
  %23 = zext nneg i32 %11 to i64
  %24 = getelementptr inbounds i8, ptr %9, i64 %23
  %25 = getelementptr inbounds i8, ptr %10, i64 %23
  br label %26

26:                                               ; preds = %19, %._crit_edge
  %.033 = phi ptr [ %25, %19 ], [ %10, %._crit_edge ]
  %.031 = phi ptr [ %24, %19 ], [ %9, %._crit_edge ]
  %.1 = phi i64 [ %22, %19 ], [ %.030.lcssa, %._crit_edge ]
  %27 = and i32 %2, 15
  %.not37 = icmp eq i32 %27, 0
  br i1 %.not37, label %32, label %28

28:                                               ; preds = %26
  %29 = tail call noundef i32 @_ZL16SumSquareError_CPKhS0_i(ptr noundef %.031, ptr noundef %.033, i32 noundef %27)
  %30 = zext i32 %29 to i64
  %31 = add i64 %.1, %30
  br label %32

32:                                               ; preds = %28, %26
  %.2 = phi i64 [ %31, %28 ], [ %.1, %26 ]
  %33 = uitofp i64 %.2 to double
  ret double %33
}

; Function Attrs: mustprogress nounwind uwtable
declare hidden noundef i32 @_ZL16SumSquareError_CPKhS0_i(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
declare hidden noundef i32 @_ZL10CpuHasSSE2v() local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
declare hidden noundef i32 @_ZL19SumSquareError_SSE2PKhS0_i(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress 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 = { mustprogress 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 = !{ptr @_ZL16SumSquareError_CPKhS0_i, ptr @_ZL19SumSquareError_SSE2PKhS0_i}
!6 = distinct !{!6, !7}
!7 = !{!"llvm.loop.mustprogress"}


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