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

; 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::Image" = type { i32, i32, i32, i32, i32, i32, ptr, ptr }

; 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 nounwind uwtable
define dso_local void @_ZN2rw5Image15compressPaletteEv(ptr noundef nonnull align 8 dereferenceable(40) %0) #1 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %8 = load ptr, ptr %2, align 8
  %9 = getelementptr inbounds %"struct.rw::Image", ptr %8, i32 0, i32 3
  %10 = load i32, ptr %9, align 4, !tbaa !9
  %11 = icmp ne i32 %10, 8
  br i1 %11, label %12, label %13

12:                                               ; preds = %1
  br label %65

13:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %14 = getelementptr inbounds %"struct.rw::Image", ptr %8, i32 0, i32 6
  %15 = load ptr, ptr %14, align 8, !tbaa !12
  store ptr %15, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !13
  br label %16

16:                                               ; preds = %56, %13
  %17 = load i32, ptr %4, align 4, !tbaa !13
  %18 = getelementptr inbounds %"struct.rw::Image", ptr %8, i32 0, i32 2
  %19 = load i32, ptr %18, align 8, !tbaa !14
  %20 = icmp slt i32 %17, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %16
  store i32 2, ptr %5, align 4
  br label %59

22:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %23, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !13
  br label %24

24:                                               ; preds = %42, %22
  %25 = load i32, ptr %7, align 4, !tbaa !13
  %26 = getelementptr inbounds %"struct.rw::Image", ptr %8, i32 0, i32 1
  %27 = load i32, ptr %26, align 4, !tbaa !15
  %28 = icmp slt i32 %25, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %24
  store i32 5, ptr %5, align 4
  br label %45

30:                                               ; preds = %24
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = load i8, ptr %31, align 1, !tbaa !16
  %33 = zext i8 %32 to i32
  %34 = icmp sgt i32 %33, 15
  br i1 %34, label %35, label %36

35:                                               ; preds = %30
  store i32 1, ptr %5, align 4
  br label %45

36:                                               ; preds = %30
  %37 = getelementptr inbounds %"struct.rw::Image", ptr %8, i32 0, i32 4
  %38 = load i32, ptr %37, align 8, !tbaa !17
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = sext i32 %38 to i64
  %41 = getelementptr inbounds i8, ptr %39, i64 %40
  store ptr %41, ptr %6, align 8, !tbaa !5
  br label %42

42:                                               ; preds = %36
  %43 = load i32, ptr %7, align 4, !tbaa !13
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %7, align 4, !tbaa !13
  br label %24, !llvm.loop !18

45:                                               ; preds = %35, %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  %46 = load i32, ptr %5, align 4
  switch i32 %46, label %53 [
    i32 5, label %47
  ]

47:                                               ; preds = %45
  %48 = getelementptr inbounds %"struct.rw::Image", ptr %8, i32 0, i32 5
  %49 = load i32, ptr %48, align 4, !tbaa !20
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = sext i32 %49 to i64
  %52 = getelementptr inbounds i8, ptr %50, i64 %51
  store ptr %52, ptr %3, align 8, !tbaa !5
  store i32 0, ptr %5, align 4
  br label %53

53:                                               ; preds = %47, %45
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %54 = load i32, ptr %5, align 4
  switch i32 %54, label %59 [
    i32 0, label %55
  ]

55:                                               ; preds = %53
  br label %56

56:                                               ; preds = %55
  %57 = load i32, ptr %4, align 4, !tbaa !13
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %4, align 4, !tbaa !13
  br label %16, !llvm.loop !21

59:                                               ; preds = %53, %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %60 = load i32, ptr %5, align 4
  switch i32 %60, label %63 [
    i32 2, label %61
  ]

61:                                               ; preds = %59
  %62 = getelementptr inbounds %"struct.rw::Image", ptr %8, i32 0, i32 3
  store i32 4, ptr %62, align 4, !tbaa !9
  store i32 0, ptr %5, align 4
  br label %63

63:                                               ; preds = %61, %59
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  %64 = load i32, ptr %5, align 4
  switch i32 %64, label %66 [
    i32 0, label %65
    i32 1, label %65
  ]

65:                                               ; preds = %63, %63, %12
  ret void

66:                                               ; preds = %63
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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" }
attributes #2 = { 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, !11, i64 12}
!10 = !{!"_ZTSN2rw5ImageE", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !6, i64 24, !6, i64 32}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 24}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !11, i64 8}
!15 = !{!10, !11, i64 4}
!16 = !{!7, !7, i64 0}
!17 = !{!10, !11, i64 16}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!10, !11, i64 20}
!21 = distinct !{!21, !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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local void @_ZN2rw5Image15compressPaletteEv(ptr nocapture noundef nonnull align 8 dereferenceable(40) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 12
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %.not = icmp eq i32 %3, 8
  br i1 %.not, label %4, label %.split.us

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load i32, ptr %5, align 8, !tbaa !11
  %.not1926 = icmp sgt i32 %6, 0
  br i1 %.not1926, label %.preheader.lr.ph, label %.critedge21

.preheader.lr.ph:                                 ; preds = %4
  %7 = getelementptr inbounds i8, ptr %0, i64 4
  %8 = load i32, ptr %7, align 4, !tbaa !12
  %.not18.not23 = icmp sgt i32 %8, 0
  %9 = getelementptr inbounds i8, ptr %0, i64 16
  %10 = load i32, ptr %9, align 8
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds i8, ptr %0, i64 20
  %13 = load i32, ptr %12, align 4
  %14 = sext i32 %13 to i64
  br i1 %.not18.not23, label %.preheader.us.preheader, label %.critedge21

.preheader.us.preheader:                          ; preds = %.preheader.lr.ph
  %15 = getelementptr inbounds i8, ptr %0, i64 24
  %16 = load ptr, ptr %15, align 8, !tbaa !13
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us
  %.01528.us = phi i32 [ %24, %._crit_edge.us ], [ 0, %.preheader.us.preheader ]
  %.01627.us = phi ptr [ %23, %._crit_edge.us ], [ %16, %.preheader.us.preheader ]
  br label %17

17:                                               ; preds = %.preheader.us, %20
  %.025.us = phi i32 [ 0, %.preheader.us ], [ %22, %20 ]
  %.01324.us = phi ptr [ %.01627.us, %.preheader.us ], [ %21, %20 ]
  %18 = load i8, ptr %.01324.us, align 1, !tbaa !14
  %19 = icmp ugt i8 %18, 15
  br i1 %19, label %.split.us, label %20

20:                                               ; preds = %17
  %21 = getelementptr inbounds i8, ptr %.01324.us, i64 %11
  %22 = add nuw nsw i32 %.025.us, 1
  %exitcond.not = icmp eq i32 %22, %8
  br i1 %exitcond.not, label %._crit_edge.us, label %17, !llvm.loop !15

._crit_edge.us:                                   ; preds = %20
  %23 = getelementptr inbounds i8, ptr %.01627.us, i64 %14
  %24 = add nuw nsw i32 %.01528.us, 1
  %exitcond30.not = icmp eq i32 %24, %6
  br i1 %exitcond30.not, label %.critedge21, label %.preheader.us, !llvm.loop !17

.critedge21:                                      ; preds = %._crit_edge.us, %.preheader.lr.ph, %4
  store i32 4, ptr %2, align 4, !tbaa !5
  br label %.split.us

.split.us:                                        ; preds = %17, %.critedge21, %1
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) 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 12}
!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 4}
!13 = !{!6, !10, i64 24}
!14 = !{!8, !8, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


; Effective O3 passes: ConstraintEliminationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass