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

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

@.str.2 = external hidden unnamed_addr constant [57 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [11 x i8], align 1
@__PRETTY_FUNCTION__._ZN2rw5Image9palettizeEi = external hidden unnamed_addr constant [33 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [73 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: mustprogress uwtable
declare dso_local void @_ZN2rw5Image4freeEv(ptr noundef nonnull align 8 dereferenceable(40)) #1 align 2

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZN2rw14mustmalloc_LOCEmjPKc(i64 noundef, i32 noundef, ptr noundef) #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN2rw5Image9setPixelsEPh(ptr noundef nonnull align 8 dereferenceable(40), ptr noundef) #3 align 2

; 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) #4

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

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN2rw5Image9palettizeEi(ptr noundef nonnull align 8 dereferenceable(40) %0, i32 noundef %1) #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca [256 x %"struct.rw::RGBA"], align 16
  %6 = alloca %"struct.rw::ColorQuant", align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %9 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 1024, ptr %5) #6
  call void @llvm.lifetime.start.p0(i64 24, ptr %6) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #6
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #6
  call void @_ZN2rw10ColorQuant4initEv(ptr noundef nonnull align 8 dereferenceable(24) %6)
  call void @_ZN2rw10ColorQuant8addImageEPNS_5ImageE(ptr noundef nonnull align 8 dereferenceable(24) %6, ptr noundef %9)
  %10 = load i32, ptr %4, align 4, !tbaa !9
  %11 = icmp sle i32 %10, 8
  br i1 %11, label %12, label %13

12:                                               ; preds = %2
  br label %15

13:                                               ; preds = %2
  call void @__assert_fail(ptr noundef @.str.9, ptr noundef @.str.2, i32 noundef 683, ptr noundef @__PRETTY_FUNCTION__._ZN2rw5Image9palettizeEi) #7
  unreachable

14:                                               ; No predecessors!
  br label %15

15:                                               ; preds = %14, %12
  %16 = load i32, ptr %4, align 4, !tbaa !9
  %17 = shl i32 1, %16
  %18 = getelementptr inbounds [256 x %"struct.rw::RGBA"], ptr %5, i64 0, i64 0
  call void @_ZN2rw10ColorQuant11makePaletteEiPNS_4RGBAE(ptr noundef nonnull align 8 dereferenceable(24) %6, i32 noundef %17, ptr noundef %18)
  %19 = getelementptr inbounds %"struct.rw::Image", ptr %9, i32 0, i32 1
  %20 = load i32, ptr %19, align 4, !tbaa !11
  store i32 %20, ptr %8, align 4, !tbaa !9
  %21 = load i32, ptr %8, align 4, !tbaa !9
  %22 = getelementptr inbounds %"struct.rw::Image", ptr %9, i32 0, i32 2
  %23 = load i32, ptr %22, align 8, !tbaa !13
  %24 = mul i32 %21, %23
  %25 = zext i32 %24 to i64
  %26 = mul i64 %25, 1
  %27 = call noundef ptr @_ZN2rw14mustmalloc_LOCEmjPKc(i64 noundef %26, i32 noundef 196632, ptr noundef @.str.10)
  store ptr %27, ptr %7, align 8, !tbaa !5
  %28 = load ptr, ptr %7, align 8, !tbaa !5
  %29 = load i32, ptr %8, align 4, !tbaa !9
  call void @_ZN2rw10ColorQuant10matchImageEPhjPNS_5ImageE(ptr noundef nonnull align 8 dereferenceable(24) %6, ptr noundef %28, i32 noundef %29, ptr noundef %9)
  call void @_ZN2rw5Image4freeEv(ptr noundef nonnull align 8 dereferenceable(40) %9)
  %30 = load i32, ptr %4, align 4, !tbaa !9
  %31 = getelementptr inbounds %"struct.rw::Image", ptr %9, i32 0, i32 3
  store i32 %30, ptr %31, align 4, !tbaa !14
  %32 = load i32, ptr %4, align 4, !tbaa !9
  %33 = icmp slt i32 %32, 8
  br i1 %33, label %34, label %35

34:                                               ; preds = %15
  br label %38

35:                                               ; preds = %15
  %36 = load i32, ptr %4, align 4, !tbaa !9
  %37 = sdiv i32 %36, 8
  br label %38

38:                                               ; preds = %35, %34
  %39 = phi i32 [ 1, %34 ], [ %37, %35 ]
  %40 = getelementptr inbounds %"struct.rw::Image", ptr %9, i32 0, i32 4
  store i32 %39, ptr %40, align 8, !tbaa !15
  %41 = load i32, ptr %8, align 4, !tbaa !9
  %42 = getelementptr inbounds %"struct.rw::Image", ptr %9, i32 0, i32 5
  store i32 %41, ptr %42, align 4, !tbaa !16
  %43 = getelementptr inbounds %"struct.rw::Image", ptr %9, i32 0, i32 6
  store ptr null, ptr %43, align 8, !tbaa !17
  %44 = getelementptr inbounds %"struct.rw::Image", ptr %9, i32 0, i32 7
  store ptr null, ptr %44, align 8, !tbaa !18
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  call void @_ZN2rw5Image9setPixelsEPh(ptr noundef nonnull align 8 dereferenceable(40) %9, ptr noundef %45)
  call void @_ZN2rw5Image8allocateEv(ptr noundef nonnull align 8 dereferenceable(40) %9)
  %46 = getelementptr inbounds %"struct.rw::Image", ptr %9, i32 0, i32 7
  %47 = load ptr, ptr %46, align 8, !tbaa !18
  %48 = getelementptr inbounds [256 x %"struct.rw::RGBA"], ptr %5, i64 0, i64 0
  %49 = load i32, ptr %4, align 4, !tbaa !9
  %50 = shl i32 1, %49
  %51 = mul nsw i32 4, %50
  %52 = sext i32 %51 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %47, ptr align 16 %48, i64 %52, i1 false)
  call void @_ZN2rw10ColorQuant7destroyEv(ptr noundef nonnull align 8 dereferenceable(24) %6)
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #6
  call void @llvm.lifetime.end.p0(i64 24, ptr %6) #6
  call void @llvm.lifetime.end.p0(i64 1024, ptr %5) #6
  ret void
}

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

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

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

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN2rw10ColorQuant7destroyEv(ptr noundef nonnull align 8 dereferenceable(24)) #1 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 = { inlinehint 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 #3 = { 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 #6 = { nounwind }
attributes #7 = { 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, !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, !10, i64 4}
!12 = !{!"_ZTSN2rw5ImageE", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !6, i64 24, !6, i64 32}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !10, i64 12}
!15 = !{!12, !10, i64 16}
!16 = !{!12, !10, i64 20}
!17 = !{!12, !6, i64 24}
!18 = !{!12, !6, i64 32}


; 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 }
%"struct.rw::ColorQuant" = type { ptr, %"struct.rw::LinkList" }
%"struct.rw::LinkList" = type { %"struct.rw::LLLink" }
%"struct.rw::LLLink" = type { ptr, ptr }

@.str.2 = external hidden unnamed_addr constant [57 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [11 x i8], align 1
@__PRETTY_FUNCTION__._ZN2rw5Image9palettizeEi = external hidden unnamed_addr constant [33 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [73 x i8], align 1

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

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN2rw5Image4freeEv(ptr noundef nonnull align 8 dereferenceable(40)) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN2rw5Image8allocateEv(ptr noundef nonnull align 8 dereferenceable(40)) local_unnamed_addr #1 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZN2rw14mustmalloc_LOCEmjPKc(i64 noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN2rw5Image9setPixelsEPh(ptr noundef nonnull align 8 dereferenceable(40), ptr noundef) local_unnamed_addr #3 align 2

; Function Attrs: mustprogress 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) #4

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

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN2rw5Image9palettizeEi(ptr noundef nonnull align 8 dereferenceable(40) %0, i32 noundef %1) local_unnamed_addr #1 align 2 {
  %3 = alloca [256 x %"struct.rw::RGBA"], align 16
  %4 = alloca %"struct.rw::ColorQuant", align 8
  call void @llvm.lifetime.start.p0(i64 1024, ptr nonnull %3) #7
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %4) #7
  call void @_ZN2rw10ColorQuant4initEv(ptr noundef nonnull align 8 dereferenceable(24) %4)
  call void @_ZN2rw10ColorQuant8addImageEPNS_5ImageE(ptr noundef nonnull align 8 dereferenceable(24) %4, ptr noundef nonnull %0)
  %5 = icmp slt i32 %1, 9
  br i1 %5, label %7, label %6

6:                                                ; preds = %2
  call void @__assert_fail(ptr noundef nonnull @.str.9, ptr noundef nonnull @.str.2, i32 noundef 683, ptr noundef nonnull @__PRETTY_FUNCTION__._ZN2rw5Image9palettizeEi) #8
  unreachable

7:                                                ; preds = %2
  %8 = shl nuw nsw i32 1, %1
  call void @_ZN2rw10ColorQuant11makePaletteEiPNS_4RGBAE(ptr noundef nonnull align 8 dereferenceable(24) %4, i32 noundef %8, ptr noundef nonnull %3)
  %9 = getelementptr inbounds i8, ptr %0, i64 4
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %0, i64 8
  %12 = load i32, ptr %11, align 8, !tbaa !11
  %13 = mul i32 %12, %10
  %14 = zext i32 %13 to i64
  %15 = call noundef ptr @_ZN2rw14mustmalloc_LOCEmjPKc(i64 noundef %14, i32 noundef 196632, ptr noundef nonnull @.str.10)
  call void @_ZN2rw10ColorQuant10matchImageEPhjPNS_5ImageE(ptr noundef nonnull align 8 dereferenceable(24) %4, ptr noundef %15, i32 noundef %10, ptr noundef nonnull %0)
  call void @_ZN2rw5Image4freeEv(ptr noundef nonnull align 8 dereferenceable(40) %0)
  %16 = getelementptr inbounds i8, ptr %0, i64 12
  store i32 %1, ptr %16, align 4, !tbaa !12
  %17 = getelementptr inbounds i8, ptr %0, i64 16
  store i32 1, ptr %17, align 8, !tbaa !13
  %18 = getelementptr inbounds i8, ptr %0, i64 20
  store i32 %10, ptr %18, align 4, !tbaa !14
  %19 = getelementptr inbounds i8, ptr %0, i64 24
  %20 = getelementptr inbounds i8, ptr %0, i64 32
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %19, i8 0, i64 16, i1 false)
  call void @_ZN2rw5Image9setPixelsEPh(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef %15)
  call void @_ZN2rw5Image8allocateEv(ptr noundef nonnull align 8 dereferenceable(40) %0)
  %21 = load ptr, ptr %20, align 8, !tbaa !15
  %22 = shl nuw nsw i32 4, %1
  %23 = zext nneg i32 %22 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(1) %21, ptr noundef nonnull align 16 dereferenceable(1) %3, i64 %23, i1 false)
  call void @_ZN2rw10ColorQuant7destroyEv(ptr noundef nonnull align 8 dereferenceable(24) %4)
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %4) #7
  call void @llvm.lifetime.end.p0(i64 1024, ptr nonnull %3) #7
  ret void
}

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

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

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

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

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #6

attributes #0 = { mustprogress 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 = { inlinehint 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 #3 = { 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 #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 #6 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #7 = { nounwind }
attributes #8 = { 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, !7, i64 4}
!6 = !{!"_ZTSN2rw5ImageE", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !7, i64 20, !10, i64 24, !10, i64 32}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 8}
!12 = !{!6, !7, i64 12}
!13 = !{!6, !7, i64 16}
!14 = !{!6, !7, i64 20}
!15 = !{!6, !10, i64 32}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,MemCpyOptPass,ReassociatePass,SROAPass,SimplifyCFGPass