; LLVM IR from zchrissirhcz/pixel :: src (px_rgb_to_gray)

; 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.px_image_t = type { ptr, i32, i32, i32, i32 }

@.str = external hidden unnamed_addr constant [27 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [73 x i8], align 1
@__PRETTY_FUNCTION__.px_rgb_to_gray_fixed = external hidden unnamed_addr constant [54 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [28 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [33 x i8], align 1

; 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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local void @px_rgb_to_gray_fixed(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %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 ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  %14 = alloca i8, align 1
  %15 = alloca i8, align 1
  %16 = alloca i8, align 1
  %17 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = call zeroext i1 @px_is_valid_rgb_image(ptr noundef %18)
  br i1 %19, label %20, label %21

20:                                               ; preds = %2
  br label %22

21:                                               ; preds = %2
  call void @__assert_fail(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 45, ptr noundef @__PRETTY_FUNCTION__.px_rgb_to_gray_fixed) #4
  unreachable

22:                                               ; preds = %20
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = call zeroext i1 @px_is_valid_gray_image(ptr noundef %23)
  br i1 %24, label %25, label %26

25:                                               ; preds = %22
  br label %27

26:                                               ; preds = %22
  call void @__assert_fail(ptr noundef @.str.2, ptr noundef @.str.1, i32 noundef 46, ptr noundef @__PRETTY_FUNCTION__.px_rgb_to_gray_fixed) #4
  unreachable

27:                                               ; preds = %25
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = call zeroext i1 @px_image_equal_in_size(ptr noundef %28, ptr noundef %29)
  br i1 %30, label %31, label %32

31:                                               ; preds = %27
  br label %33

32:                                               ; preds = %27
  call void @__assert_fail(ptr noundef @.str.3, ptr noundef @.str.1, i32 noundef 47, ptr noundef @__PRETTY_FUNCTION__.px_rgb_to_gray_fixed) #4
  unreachable

33:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  store i32 2, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.px_image_t, ptr %34, i32 0, i32 2
  %36 = load i32, ptr %35, align 4, !tbaa !11
  store i32 %36, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.px_image_t, ptr %37, i32 0, i32 1
  %39 = load i32, ptr %38, align 8, !tbaa !13
  store i32 %39, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.px_image_t, ptr %40, i32 0, i32 3
  %42 = load i32, ptr %41, align 8, !tbaa !14
  store i32 %42, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %110, %33
  %44 = load i32, ptr %9, align 4, !tbaa !9
  %45 = load i32, ptr %6, align 4, !tbaa !9
  %46 = icmp slt i32 %44, %45
  br i1 %46, label %48, label %47

47:                                               ; preds = %43
  store i32 2, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  br label %113

48:                                               ; preds = %43
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.px_image_t, ptr %49, i32 0, i32 0
  %51 = load ptr, ptr %50, align 8, !tbaa !15
  %52 = load i32, ptr %9, align 4, !tbaa !9
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.px_image_t, ptr %53, i32 0, i32 4
  %55 = load i32, ptr %54, align 4, !tbaa !16
  %56 = mul nsw i32 %52, %55
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds i8, ptr %51, i64 %57
  store ptr %58, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.px_image_t, ptr %59, i32 0, i32 0
  %61 = load ptr, ptr %60, align 8, !tbaa !15
  %62 = load i32, ptr %9, align 4, !tbaa !9
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.px_image_t, ptr %63, i32 0, i32 4
  %65 = load i32, ptr %64, align 4, !tbaa !16
  %66 = mul nsw i32 %62, %65
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds i8, ptr %61, i64 %67
  store ptr %68, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #5
  store i32 0, ptr %13, align 4, !tbaa !9
  br label %69

69:                                               ; preds = %106, %48
  %70 = load i32, ptr %13, align 4, !tbaa !9
  %71 = load i32, ptr %7, align 4, !tbaa !9
  %72 = icmp slt i32 %70, %71
  br i1 %72, label %74, label %73

73:                                               ; preds = %69
  store i32 5, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  br label %109

74:                                               ; preds = %69
  call void @llvm.lifetime.start.p0(i64 1, ptr %14) #5
  %75 = load ptr, ptr %11, align 8, !tbaa !5
  %76 = getelementptr inbounds i8, ptr %75, i64 0
  %77 = load i8, ptr %76, align 1, !tbaa !17
  store i8 %77, ptr %14, align 1, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 1, ptr %15) #5
  %78 = load ptr, ptr %11, align 8, !tbaa !5
  %79 = getelementptr inbounds i8, ptr %78, i64 1
  %80 = load i8, ptr %79, align 1, !tbaa !17
  store i8 %80, ptr %15, align 1, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 1, ptr %16) #5
  %81 = load ptr, ptr %11, align 8, !tbaa !5
  %82 = getelementptr inbounds i8, ptr %81, i64 2
  %83 = load i8, ptr %82, align 1, !tbaa !17
  store i8 %83, ptr %16, align 1, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #5
  %84 = load i8, ptr %14, align 1, !tbaa !17
  %85 = zext i8 %84 to i32
  %86 = mul nsw i32 4899, %85
  %87 = load i8, ptr %15, align 1, !tbaa !17
  %88 = zext i8 %87 to i32
  %89 = mul nsw i32 9617, %88
  %90 = add nsw i32 %86, %89
  %91 = load i8, ptr %16, align 1, !tbaa !17
  %92 = zext i8 %91 to i32
  %93 = mul nsw i32 1868, %92
  %94 = add nsw i32 %90, %93
  %95 = add nsw i32 %94, 8192
  %96 = ashr i32 %95, 14
  store i32 %96, ptr %17, align 4, !tbaa !9
  %97 = load i32, ptr %17, align 4, !tbaa !9
  %98 = call i32 @px_clamp(i32 noundef %97, i32 noundef 0, i32 noundef 255)
  %99 = trunc i32 %98 to i8
  %100 = load ptr, ptr %12, align 8, !tbaa !5
  %101 = getelementptr inbounds i8, ptr %100, i32 1
  store ptr %101, ptr %12, align 8, !tbaa !5
  store i8 %99, ptr %100, align 1, !tbaa !17
  %102 = load i32, ptr %8, align 4, !tbaa !9
  %103 = load ptr, ptr %11, align 8, !tbaa !5
  %104 = sext i32 %102 to i64
  %105 = getelementptr inbounds i8, ptr %103, i64 %104
  store ptr %105, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #5
  call void @llvm.lifetime.end.p0(i64 1, ptr %16) #5
  call void @llvm.lifetime.end.p0(i64 1, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 1, ptr %14) #5
  br label %106

106:                                              ; preds = %74
  %107 = load i32, ptr %13, align 4, !tbaa !9
  %108 = add nsw i32 %107, 1
  store i32 %108, ptr %13, align 4, !tbaa !9
  br label %69, !llvm.loop !18

109:                                              ; preds = %73
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  br label %110

110:                                              ; preds = %109
  %111 = load i32, ptr %9, align 4, !tbaa !9
  %112 = add nsw i32 %111, 1
  store i32 %112, ptr %9, align 4, !tbaa !9
  br label %43, !llvm.loop !20

113:                                              ; preds = %47
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  ret void
}

declare zeroext i1 @px_is_valid_rgb_image(ptr noundef) #2

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #3

declare zeroext i1 @px_is_valid_gray_image(ptr noundef) #2

declare zeroext i1 @px_image_equal_in_size(ptr noundef, ptr noundef) #2

declare i32 @px_clamp(i32 noundef, i32 noundef, i32 noundef) #2

attributes #0 = { nocallback nofree nosync 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" }
attributes #2 = { "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 = { noreturn nounwind "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 #4 = { noreturn nounwind }
attributes #5 = { 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 12}
!12 = !{!"px_image_t", !6, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !10, i64 16}
!15 = !{!12, !6, i64 0}
!16 = !{!12, !10, i64 20}
!17 = !{!7, !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = distinct !{!20, !19}


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

@.str = external hidden unnamed_addr constant [27 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [73 x i8], align 1
@__PRETTY_FUNCTION__.px_rgb_to_gray_fixed = external hidden unnamed_addr constant [54 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [28 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [33 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @px_rgb_to_gray_fixed(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = tail call zeroext i1 @px_is_valid_rgb_image(ptr noundef %0) #3
  br i1 %3, label %5, label %4

4:                                                ; preds = %2
  tail call void @__assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 45, ptr noundef nonnull @__PRETTY_FUNCTION__.px_rgb_to_gray_fixed) #4
  unreachable

5:                                                ; preds = %2
  %6 = tail call zeroext i1 @px_is_valid_gray_image(ptr noundef %1) #3
  br i1 %6, label %8, label %7

7:                                                ; preds = %5
  tail call void @__assert_fail(ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.1, i32 noundef 46, ptr noundef nonnull @__PRETTY_FUNCTION__.px_rgb_to_gray_fixed) #4
  unreachable

8:                                                ; preds = %5
  %9 = tail call zeroext i1 @px_image_equal_in_size(ptr noundef %0, ptr noundef %1) #3
  br i1 %9, label %11, label %10

10:                                               ; preds = %8
  tail call void @__assert_fail(ptr noundef nonnull @.str.3, ptr noundef nonnull @.str.1, i32 noundef 47, ptr noundef nonnull @__PRETTY_FUNCTION__.px_rgb_to_gray_fixed) #4
  unreachable

11:                                               ; preds = %8
  %12 = getelementptr inbounds i8, ptr %0, i64 12
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %0, i64 8
  %15 = load i32, ptr %14, align 8, !tbaa !11
  %16 = icmp sgt i32 %13, 0
  br i1 %16, label %.lr.ph36, label %._crit_edge37

.lr.ph36:                                         ; preds = %11
  %17 = getelementptr inbounds i8, ptr %0, i64 16
  %18 = load i32, ptr %17, align 8, !tbaa !12
  %19 = getelementptr inbounds i8, ptr %0, i64 20
  %20 = getelementptr inbounds i8, ptr %1, i64 20
  %21 = icmp sgt i32 %15, 0
  %22 = sext i32 %18 to i64
  br i1 %21, label %.lr.ph.us, label %._crit_edge37

.lr.ph.us:                                        ; preds = %.lr.ph36, %._crit_edge.us
  %.034.us = phi i32 [ %54, %._crit_edge.us ], [ 0, %.lr.ph36 ]
  %23 = load ptr, ptr %0, align 8, !tbaa !13
  %24 = load i32, ptr %19, align 4, !tbaa !14
  %25 = mul nsw i32 %24, %.034.us
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i8, ptr %23, i64 %26
  %28 = load ptr, ptr %1, align 8, !tbaa !13
  %29 = load i32, ptr %20, align 4, !tbaa !14
  %30 = mul nsw i32 %29, %.034.us
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i8, ptr %28, i64 %31
  br label %33

33:                                               ; preds = %.lr.ph.us, %33
  %.02833.us = phi ptr [ %27, %.lr.ph.us ], [ %52, %33 ]
  %.02932.us = phi ptr [ %32, %.lr.ph.us ], [ %51, %33 ]
  %.03031.us = phi i32 [ 0, %.lr.ph.us ], [ %53, %33 ]
  %34 = load i8, ptr %.02833.us, align 1, !tbaa !15
  %35 = getelementptr inbounds i8, ptr %.02833.us, i64 1
  %36 = load i8, ptr %35, align 1, !tbaa !15
  %37 = getelementptr inbounds i8, ptr %.02833.us, i64 2
  %38 = load i8, ptr %37, align 1, !tbaa !15
  %39 = zext i8 %34 to i32
  %40 = mul nuw nsw i32 %39, 4899
  %41 = zext i8 %36 to i32
  %42 = mul nuw nsw i32 %41, 9617
  %43 = zext i8 %38 to i32
  %44 = mul nuw nsw i32 %43, 1868
  %45 = add nuw nsw i32 %40, 8192
  %46 = add nuw nsw i32 %45, %42
  %47 = add nuw nsw i32 %46, %44
  %48 = lshr i32 %47, 14
  %49 = tail call i32 @px_clamp(i32 noundef %48, i32 noundef 0, i32 noundef 255) #3
  %50 = trunc i32 %49 to i8
  %51 = getelementptr inbounds i8, ptr %.02932.us, i64 1
  store i8 %50, ptr %.02932.us, align 1, !tbaa !15
  %52 = getelementptr inbounds i8, ptr %.02833.us, i64 %22
  %53 = add nuw nsw i32 %.03031.us, 1
  %exitcond.not = icmp eq i32 %53, %15
  br i1 %exitcond.not, label %._crit_edge.us, label %33, !llvm.loop !16

._crit_edge.us:                                   ; preds = %33
  %54 = add nuw nsw i32 %.034.us, 1
  %exitcond39.not = icmp eq i32 %54, %13
  br i1 %exitcond39.not, label %._crit_edge37, label %.lr.ph.us, !llvm.loop !18

._crit_edge37:                                    ; preds = %._crit_edge.us, %.lr.ph36, %11
  ret void
}

declare zeroext i1 @px_is_valid_rgb_image(ptr noundef) local_unnamed_addr #1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

declare zeroext i1 @px_is_valid_gray_image(ptr noundef) local_unnamed_addr #1

declare zeroext i1 @px_image_equal_in_size(ptr noundef, ptr noundef) local_unnamed_addr #1

declare i32 @px_clamp(i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

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 = { "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 #2 = { noreturn nounwind "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 }
attributes #4 = { noreturn 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, !10, i64 12}
!6 = !{!"px_image_t", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!6, !10, i64 16}
!13 = !{!6, !7, i64 0}
!14 = !{!6, !10, i64 20}
!15 = !{!8, !8, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}


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