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

; 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::Node" = type { i32, i32, i32, i32, i32, ptr, [16 x ptr], %"struct.rw::LLLink" }
%"struct.rw::LLLink" = type { 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 uwtable
declare dso_local noundef ptr @_ZN2rw10ColorQuant10createNodeEi(ptr noundef nonnull align 8 dereferenceable(24), i32 noundef) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN2rw10ColorQuant7getNodeEPNS0_4NodeEji(ptr noundef nonnull align 8 dereferenceable(24) %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) #1 align 2 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  %11 = load ptr, ptr %6, align 8
  %12 = load i32, ptr %9, align 4, !tbaa !9
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %16

14:                                               ; preds = %4
  %15 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %15, ptr %5, align 8
  br label %55

16:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %17 = load i32, ptr %8, align 4, !tbaa !9
  %18 = and i32 %17, 15
  store i32 %18, ptr %10, align 4, !tbaa !9
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  %20 = getelementptr inbounds %"struct.rw::ColorQuant::Node", ptr %19, i32 0, i32 6
  %21 = load i32, ptr %10, align 4, !tbaa !9
  %22 = zext i32 %21 to i64
  %23 = getelementptr inbounds [16 x ptr], ptr %20, i64 0, i64 %22
  %24 = load ptr, ptr %23, align 8, !tbaa !5
  %25 = icmp eq ptr %24, null
  br i1 %25, label %26, label %43

26:                                               ; preds = %16
  %27 = load i32, ptr %9, align 4, !tbaa !9
  %28 = sub nsw i32 %27, 1
  %29 = call noundef ptr @_ZN2rw10ColorQuant10createNodeEi(ptr noundef nonnull align 8 dereferenceable(24) %11, i32 noundef %28)
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = getelementptr inbounds %"struct.rw::ColorQuant::Node", ptr %30, i32 0, i32 6
  %32 = load i32, ptr %10, align 4, !tbaa !9
  %33 = zext i32 %32 to i64
  %34 = getelementptr inbounds [16 x ptr], ptr %31, i64 0, i64 %33
  store ptr %29, ptr %34, align 8, !tbaa !5
  %35 = load ptr, ptr %7, align 8, !tbaa !5
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = getelementptr inbounds %"struct.rw::ColorQuant::Node", ptr %36, i32 0, i32 6
  %38 = load i32, ptr %10, align 4, !tbaa !9
  %39 = zext i32 %38 to i64
  %40 = getelementptr inbounds [16 x ptr], ptr %37, i64 0, i64 %39
  %41 = load ptr, ptr %40, align 8, !tbaa !5
  %42 = getelementptr inbounds %"struct.rw::ColorQuant::Node", ptr %41, i32 0, i32 5
  store ptr %35, ptr %42, align 8, !tbaa !11
  br label %43

43:                                               ; preds = %26, %16
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = getelementptr inbounds %"struct.rw::ColorQuant::Node", ptr %44, i32 0, i32 6
  %46 = load i32, ptr %10, align 4, !tbaa !9
  %47 = zext i32 %46 to i64
  %48 = getelementptr inbounds [16 x ptr], ptr %45, i64 0, i64 %47
  %49 = load ptr, ptr %48, align 8, !tbaa !5
  %50 = load i32, ptr %8, align 4, !tbaa !9
  %51 = lshr i32 %50, 4
  %52 = load i32, ptr %9, align 4, !tbaa !9
  %53 = sub nsw i32 %52, 1
  %54 = call noundef ptr @_ZN2rw10ColorQuant7getNodeEPNS0_4NodeEji(ptr noundef nonnull align 8 dereferenceable(24) %11, ptr noundef %49, i32 noundef %51, i32 noundef %53)
  store ptr %54, ptr %5, align 8
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %55

55:                                               ; preds = %43, %14
  %56 = load ptr, ptr %5, align 8
  ret ptr %56
}

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 = { 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 24}
!12 = !{!"_ZTSN2rw10ColorQuant4NodeE", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !6, i64 24, !7, i64 32, !13, i64 160}
!13 = !{!"_ZTSN2rw6LLLinkE", !6, i64 0, !6, i64 8}


; 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
declare dso_local noundef ptr @_ZN2rw10ColorQuant10createNodeEi(ptr noundef nonnull align 8 dereferenceable(24), i32 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN2rw10ColorQuant7getNodeEPNS0_4NodeEji(ptr noundef nonnull align 8 dereferenceable(24) %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 align 2 {
  %5 = icmp eq i32 %3, 0
  br i1 %5, label %tailrecurse._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4, %tailrecurse
  %.tr1922 = phi i32 [ %12, %tailrecurse ], [ %3, %4 ]
  %.tr1821 = phi i32 [ %17, %tailrecurse ], [ %2, %4 ]
  %.tr1720 = phi ptr [ %16, %tailrecurse ], [ %1, %4 ]
  %6 = and i32 %.tr1821, 15
  %7 = getelementptr inbounds i8, ptr %.tr1720, i64 32
  %8 = zext nneg i32 %6 to i64
  %9 = getelementptr inbounds [16 x ptr], ptr %7, i64 0, i64 %8
  %10 = load ptr, ptr %9, align 8, !tbaa !5
  %11 = icmp eq ptr %10, null
  %12 = add nsw i32 %.tr1922, -1
  br i1 %11, label %13, label %tailrecurse

13:                                               ; preds = %.lr.ph
  %14 = tail call noundef ptr @_ZN2rw10ColorQuant10createNodeEi(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %12)
  store ptr %14, ptr %9, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %14, i64 24
  store ptr %.tr1720, ptr %15, align 8, !tbaa !9
  %.pre = load ptr, ptr %9, align 8, !tbaa !5
  br label %tailrecurse

tailrecurse:                                      ; preds = %.lr.ph, %13
  %16 = phi ptr [ %.pre, %13 ], [ %10, %.lr.ph ]
  %17 = lshr i32 %.tr1821, 4
  %18 = icmp eq i32 %12, 0
  br i1 %18, label %tailrecurse._crit_edge, label %.lr.ph

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %4
  %.tr17.lcssa = phi ptr [ %1, %4 ], [ %16, %tailrecurse ]
  ret ptr %.tr17.lcssa
}

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

!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, !6, i64 24}
!10 = !{!"_ZTSN2rw10ColorQuant4NodeE", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !6, i64 24, !7, i64 32, !12, i64 160}
!11 = !{!"int", !7, i64 0}
!12 = !{!"_ZTSN2rw6LLLinkE", !6, i64 0, !6, i64 8}


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