; LLVM IR from aap/librw :: src/image.ll (ZN2rw10ColorQuant11makePaletteEiPNS_4RGBAE)

; 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.rw::ColorQuant" = type { ptr, %"struct.rw::LinkList" }
%"struct.rw::LinkList" = type { %"struct.rw::LLLink" }
%"struct.rw::LLLink" = type { ptr, ptr }
%"struct.rw::ColorQuant::Node" = type { i32, i32, i32, i32, i32, ptr, [16 x ptr], %"struct.rw::LLLink" }
%"struct.rw::RGBA" = type { i8, i8, i8, i8 }

; 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: mustprogress uwtable
define dso_local void @_ZN2rw10ColorQuant11makePaletteEiPNS_4RGBAE(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %1, ptr noundef %2) #1 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  %12 = load ptr, ptr %4, align 8
  br label %13

13:                                               ; preds = %18, %3
  %14 = getelementptr inbounds %"struct.rw::ColorQuant", ptr %12, i32 0, i32 1
  %15 = call noundef i32 @_ZN2rw8LinkList5countEv(ptr noundef nonnull align 8 dereferenceable(16) %14)
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = icmp sgt i32 %15, %16
  br i1 %17, label %18, label %27

18:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %19 = getelementptr inbounds %"struct.rw::ColorQuant", ptr %12, i32 0, i32 1
  %20 = getelementptr inbounds %"struct.rw::LinkList", ptr %19, i32 0, i32 0
  %21 = getelementptr inbounds %"struct.rw::LLLink", ptr %20, i32 0, i32 0
  %22 = load ptr, ptr %21, align 8, !tbaa !11
  %23 = getelementptr inbounds i8, ptr %22, i64 -160
  store ptr %23, ptr %7, align 8, !tbaa !5
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = getelementptr inbounds %"struct.rw::ColorQuant::Node", ptr %24, i32 0, i32 5
  %26 = load ptr, ptr %25, align 8, !tbaa !15
  call void @_ZN2rw10ColorQuant10reduceNodeEPNS0_4NodeE(ptr noundef nonnull align 8 dereferenceable(24) %12, ptr noundef %26)
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %13, !llvm.loop !17

27:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  store ptr null, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %28 = getelementptr inbounds %"struct.rw::ColorQuant", ptr %12, i32 0, i32 1
  %29 = getelementptr inbounds %"struct.rw::LinkList", ptr %28, i32 0, i32 0
  %30 = getelementptr inbounds %"struct.rw::LLLink", ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %30, align 8, !tbaa !11
  store ptr %31, ptr %10, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %112, %27
  %33 = load ptr, ptr %10, align 8, !tbaa !5
  %34 = getelementptr inbounds %"struct.rw::LLLink", ptr %33, i32 0, i32 0
  %35 = load ptr, ptr %34, align 8, !tbaa !19
  store ptr %35, ptr %9, align 8, !tbaa !5
  %36 = load ptr, ptr %10, align 8, !tbaa !5
  %37 = getelementptr inbounds %"struct.rw::ColorQuant", ptr %12, i32 0, i32 1
  %38 = call noundef ptr @_ZN2rw8LinkList3endEv(ptr noundef nonnull align 8 dereferenceable(16) %37)
  %39 = icmp ne ptr %36, %38
  br i1 %39, label %41, label %40

40:                                               ; preds = %32
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %114

41:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %42 = load ptr, ptr %10, align 8, !tbaa !5
  %43 = getelementptr inbounds i8, ptr %42, i64 -160
  store ptr %43, ptr %11, align 8, !tbaa !5
  %44 = load ptr, ptr %11, align 8, !tbaa !5
  %45 = getelementptr inbounds %"struct.rw::ColorQuant::Node", ptr %44, i32 0, i32 4
  %46 = load i32, ptr %45, align 8, !tbaa !20
  %47 = load ptr, ptr %11, align 8, !tbaa !5
  %48 = getelementptr inbounds %"struct.rw::ColorQuant::Node", ptr %47, i32 0, i32 0
  %49 = load i32, ptr %48, align 8, !tbaa !21
  %50 = udiv i32 %49, %46
  store i32 %50, ptr %48, align 8, !tbaa !21
  %51 = load ptr, ptr %11, align 8, !tbaa !5
  %52 = getelementptr inbounds %"struct.rw::ColorQuant::Node", ptr %51, i32 0, i32 4
  %53 = load i32, ptr %52, align 8, !tbaa !20
  %54 = load ptr, ptr %11, align 8, !tbaa !5
  %55 = getelementptr inbounds %"struct.rw::ColorQuant::Node", ptr %54, i32 0, i32 1
  %56 = load i32, ptr %55, align 4, !tbaa !22
  %57 = udiv i32 %56, %53
  store i32 %57, ptr %55, align 4, !tbaa !22
  %58 = load ptr, ptr %11, align 8, !tbaa !5
  %59 = getelementptr inbounds %"struct.rw::ColorQuant::Node", ptr %58, i32 0, i32 4
  %60 = load i32, ptr %59, align 8, !tbaa !20
  %61 = load ptr, ptr %11, align 8, !tbaa !5
  %62 = getelementptr inbounds %"struct.rw::ColorQuant::Node", ptr %61, i32 0, i32 2
  %63 = load i32, ptr %62, align 8, !tbaa !23
  %64 = udiv i32 %63, %60
  store i32 %64, ptr %62, align 8, !tbaa !23
  %65 = load ptr, ptr %11, align 8, !tbaa !5
  %66 = getelementptr inbounds %"struct.rw::ColorQuant::Node", ptr %65, i32 0, i32 4
  %67 = load i32, ptr %66, align 8, !tbaa !20
  %68 = load ptr, ptr %11, align 8, !tbaa !5
  %69 = getelementptr inbounds %"struct.rw::ColorQuant::Node", ptr %68, i32 0, i32 3
  %70 = load i32, ptr %69, align 4, !tbaa !24
  %71 = udiv i32 %70, %67
  store i32 %71, ptr %69, align 4, !tbaa !24
  %72 = load ptr, ptr %11, align 8, !tbaa !5
  %73 = getelementptr inbounds %"struct.rw::ColorQuant::Node", ptr %72, i32 0, i32 0
  %74 = load i32, ptr %73, align 8, !tbaa !21
  %75 = trunc i32 %74 to i8
  %76 = load ptr, ptr %6, align 8, !tbaa !5
  %77 = load i32, ptr %8, align 4, !tbaa !9
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds %"struct.rw::RGBA", ptr %76, i64 %78
  %80 = getelementptr inbounds %"struct.rw::RGBA", ptr %79, i32 0, i32 0
  store i8 %75, ptr %80, align 1, !tbaa !25
  %81 = load ptr, ptr %11, align 8, !tbaa !5
  %82 = getelementptr inbounds %"struct.rw::ColorQuant::Node", ptr %81, i32 0, i32 1
  %83 = load i32, ptr %82, align 4, !tbaa !22
  %84 = trunc i32 %83 to i8
  %85 = load ptr, ptr %6, align 8, !tbaa !5
  %86 = load i32, ptr %8, align 4, !tbaa !9
  %87 = sext i32 %86 to i64
  %88 = getelementptr inbounds %"struct.rw::RGBA", ptr %85, i64 %87
  %89 = getelementptr inbounds %"struct.rw::RGBA", ptr %88, i32 0, i32 1
  store i8 %84, ptr %89, align 1, !tbaa !27
  %90 = load ptr, ptr %11, align 8, !tbaa !5
  %91 = getelementptr inbounds %"struct.rw::ColorQuant::Node", ptr %90, i32 0, i32 2
  %92 = load i32, ptr %91, align 8, !tbaa !23
  %93 = trunc i32 %92 to i8
  %94 = load ptr, ptr %6, align 8, !tbaa !5
  %95 = load i32, ptr %8, align 4, !tbaa !9
  %96 = sext i32 %95 to i64
  %97 = getelementptr inbounds %"struct.rw::RGBA", ptr %94, i64 %96
  %98 = getelementptr inbounds %"struct.rw::RGBA", ptr %97, i32 0, i32 2
  store i8 %93, ptr %98, align 1, !tbaa !28
  %99 = load ptr, ptr %11, align 8, !tbaa !5
  %100 = getelementptr inbounds %"struct.rw::ColorQuant::Node", ptr %99, i32 0, i32 3
  %101 = load i32, ptr %100, align 4, !tbaa !24
  %102 = trunc i32 %101 to i8
  %103 = load ptr, ptr %6, align 8, !tbaa !5
  %104 = load i32, ptr %8, align 4, !tbaa !9
  %105 = sext i32 %104 to i64
  %106 = getelementptr inbounds %"struct.rw::RGBA", ptr %103, i64 %105
  %107 = getelementptr inbounds %"struct.rw::RGBA", ptr %106, i32 0, i32 3
  store i8 %102, ptr %107, align 1, !tbaa !29
  %108 = load i32, ptr %8, align 4, !tbaa !9
  %109 = add nsw i32 %108, 1
  store i32 %109, ptr %8, align 4, !tbaa !9
  %110 = load ptr, ptr %11, align 8, !tbaa !5
  %111 = getelementptr inbounds %"struct.rw::ColorQuant::Node", ptr %110, i32 0, i32 4
  store i32 %108, ptr %111, align 8, !tbaa !20
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  br label %112

112:                                              ; preds = %41
  %113 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %113, ptr %10, align 8, !tbaa !5
  br label %32, !llvm.loop !30

114:                                              ; preds = %40
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  ret void
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN2rw10ColorQuant10reduceNodeEPNS0_4NodeE(ptr noundef nonnull align 8 dereferenceable(24), ptr noundef) #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN2rw8LinkList5countEv(ptr noundef nonnull align 8 dereferenceable(16)) #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN2rw8LinkList3endEv(ptr noundef nonnull align 8 dereferenceable(16)) #2 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !6, i64 8}
!12 = !{!"_ZTSN2rw10ColorQuantE", !6, i64 0, !13, i64 8}
!13 = !{!"_ZTSN2rw8LinkListE", !14, i64 0}
!14 = !{!"_ZTSN2rw6LLLinkE", !6, i64 0, !6, i64 8}
!15 = !{!16, !6, i64 24}
!16 = !{!"_ZTSN2rw10ColorQuant4NodeE", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !6, i64 24, !7, i64 32, !14, i64 160}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!14, !6, i64 0}
!20 = !{!16, !10, i64 16}
!21 = !{!16, !10, i64 0}
!22 = !{!16, !10, i64 4}
!23 = !{!16, !10, i64 8}
!24 = !{!16, !10, i64 12}
!25 = !{!26, !7, i64 0}
!26 = !{!"_ZTSN2rw4RGBAE", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3}
!27 = !{!26, !7, i64 1}
!28 = !{!26, !7, i64 2}
!29 = !{!26, !7, i64 3}
!30 = distinct !{!30, !18}


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

%"struct.rw::RGBA" = type { i8, i8, i8, i8 }

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN2rw10ColorQuant11makePaletteEiPNS_4RGBAE(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 align 2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = tail call noundef i32 @_ZN2rw8LinkList5countEv(ptr noundef nonnull align 8 dereferenceable(16) %4)
  %6 = icmp sgt i32 %5, %1
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3, %.lr.ph
  %7 = load ptr, ptr %4, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %7, i64 -136
  %9 = load ptr, ptr %8, align 8, !tbaa !12
  tail call void @_ZN2rw10ColorQuant10reduceNodeEPNS0_4NodeE(ptr noundef nonnull align 8 dereferenceable(24) %0, ptr noundef %9)
  %10 = tail call noundef i32 @_ZN2rw8LinkList5countEv(ptr noundef nonnull align 8 dereferenceable(16) %4)
  %11 = icmp sgt i32 %10, %1
  br i1 %11, label %.lr.ph, label %._crit_edge, !llvm.loop !15

._crit_edge:                                      ; preds = %.lr.ph, %3
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = load ptr, ptr %12, align 8, !tbaa !17
  %14 = tail call noundef ptr @_ZN2rw8LinkList3endEv(ptr noundef nonnull align 8 dereferenceable(16) %4)
  %.not29 = icmp eq ptr %12, %14
  br i1 %.not29, label %._crit_edge34, label %.lr.ph33

._crit_edge34:                                    ; preds = %.lr.ph33, %._crit_edge
  ret void

.lr.ph33:                                         ; preds = %._crit_edge, %.lr.ph33
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph33 ], [ 0, %._crit_edge ]
  %15 = phi ptr [ %39, %.lr.ph33 ], [ %13, %._crit_edge ]
  %.02830 = phi ptr [ %15, %.lr.ph33 ], [ %12, %._crit_edge ]
  %16 = getelementptr inbounds i8, ptr %.02830, i64 -160
  %17 = getelementptr inbounds i8, ptr %.02830, i64 -144
  %18 = load i32, ptr %17, align 8, !tbaa !18
  %19 = load i32, ptr %16, align 8, !tbaa !19
  %20 = udiv i32 %19, %18
  store i32 %20, ptr %16, align 8, !tbaa !19
  %21 = getelementptr inbounds i8, ptr %.02830, i64 -156
  %22 = load i32, ptr %21, align 4, !tbaa !20
  %23 = udiv i32 %22, %18
  store i32 %23, ptr %21, align 4, !tbaa !20
  %24 = getelementptr inbounds i8, ptr %.02830, i64 -152
  %25 = load i32, ptr %24, align 8, !tbaa !21
  %26 = udiv i32 %25, %18
  store i32 %26, ptr %24, align 8, !tbaa !21
  %27 = getelementptr inbounds i8, ptr %.02830, i64 -148
  %28 = load i32, ptr %27, align 4, !tbaa !22
  %29 = udiv i32 %28, %18
  store i32 %29, ptr %27, align 4, !tbaa !22
  %30 = trunc i32 %20 to i8
  %31 = getelementptr inbounds %"struct.rw::RGBA", ptr %2, i64 %indvars.iv
  store i8 %30, ptr %31, align 1, !tbaa !23
  %32 = trunc i32 %23 to i8
  %33 = getelementptr inbounds i8, ptr %31, i64 1
  store i8 %32, ptr %33, align 1, !tbaa !25
  %34 = trunc i32 %26 to i8
  %35 = getelementptr inbounds i8, ptr %31, i64 2
  store i8 %34, ptr %35, align 1, !tbaa !26
  %36 = trunc i32 %29 to i8
  %37 = getelementptr inbounds i8, ptr %31, i64 3
  store i8 %36, ptr %37, align 1, !tbaa !27
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %38 = trunc nuw nsw i64 %indvars.iv to i32
  store i32 %38, ptr %17, align 8, !tbaa !18
  %39 = load ptr, ptr %15, align 8, !tbaa !17
  %40 = tail call noundef ptr @_ZN2rw8LinkList3endEv(ptr noundef nonnull align 8 dereferenceable(16) %4)
  %.not = icmp eq ptr %15, %40
  br i1 %.not, label %._crit_edge34, label %.lr.ph33, !llvm.loop !28
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN2rw10ColorQuant10reduceNodeEPNS0_4NodeE(ptr noundef nonnull align 8 dereferenceable(24), ptr noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN2rw8LinkList5countEv(ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN2rw8LinkList3endEv(ptr noundef nonnull align 8 dereferenceable(16)) local_unnamed_addr #1 align 2

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 = !{!6, !7, i64 8}
!6 = !{!"_ZTSN2rw10ColorQuantE", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"_ZTSN2rw8LinkListE", !11, i64 0}
!11 = !{!"_ZTSN2rw6LLLinkE", !7, i64 0, !7, i64 8}
!12 = !{!13, !7, i64 24}
!13 = !{!"_ZTSN2rw10ColorQuant4NodeE", !14, i64 0, !14, i64 4, !14, i64 8, !14, i64 12, !14, i64 16, !7, i64 24, !8, i64 32, !11, i64 160}
!14 = !{!"int", !8, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!11, !7, i64 0}
!18 = !{!13, !14, i64 16}
!19 = !{!13, !14, i64 0}
!20 = !{!13, !14, i64 4}
!21 = !{!13, !14, i64 8}
!22 = !{!13, !14, i64 12}
!23 = !{!24, !8, i64 0}
!24 = !{!"_ZTSN2rw4RGBAE", !8, i64 0, !8, i64 1, !8, i64 2, !8, i64 3}
!25 = !{!24, !8, i64 1}
!26 = !{!24, !8, i64 2}
!27 = !{!24, !8, i64 3}
!28 = distinct !{!28, !16}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass