; LLVM IR from erwincoumans/tiny-differentiable-simulator :: third_party/dear_imgui/imnodes.ll (ZN7imnodes16GetSelectedLinksEPi)

; 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.imnodes::EditorContext" = type { %"struct.imnodes::(anonymous namespace)::ObjectPool", %"struct.imnodes::(anonymous namespace)::ObjectPool.6", %"struct.imnodes::(anonymous namespace)::ObjectPool.8", %struct.ImVector.0, %struct.ImVec2, %struct.ImVector.0, %struct.ImVector.0, i32, %"struct.imnodes::(anonymous namespace)::ClickInteractionState" }
%"struct.imnodes::(anonymous namespace)::ObjectPool" = type { %struct.ImVector.4, %struct.ImVector.5, %struct.ImVector.0, %struct.ImGuiStorage }
%struct.ImVector.4 = type { i32, i32, ptr }
%struct.ImVector.5 = type { i32, i32, ptr }
%struct.ImGuiStorage = type { %struct.ImVector }
%struct.ImVector = type { i32, i32, ptr }
%"struct.imnodes::(anonymous namespace)::ObjectPool.6" = type { %struct.ImVector.7, %struct.ImVector.5, %struct.ImVector.0, %struct.ImGuiStorage }
%struct.ImVector.7 = type { i32, i32, ptr }
%"struct.imnodes::(anonymous namespace)::ObjectPool.8" = type { %struct.ImVector.9, %struct.ImVector.5, %struct.ImVector.0, %struct.ImGuiStorage }
%struct.ImVector.9 = type { i32, i32, ptr }
%struct.ImVec2 = type { float, float }
%struct.ImVector.0 = type { i32, i32, ptr }
%"struct.imnodes::(anonymous namespace)::ClickInteractionState" = type { %struct.anon.10, %struct.anon.11 }
%struct.anon.10 = type { i32, %"struct.imnodes::(anonymous namespace)::OptionalIndex", i32 }
%"struct.imnodes::(anonymous namespace)::OptionalIndex" = type { i32 }
%struct.anon.11 = type { %struct.ImRect }
%struct.ImRect = type { %struct.ImVec2, %struct.ImVec2 }
%"struct.imnodes::(anonymous namespace)::LinkData" = type { i32, i32, i32, %struct.anon.16 }
%struct.anon.16 = type { i32, i32, i32 }

@.str.1 = external hidden unnamed_addr constant [111 x i8], align 1
@.str.18 = external hidden unnamed_addr constant [17 x i8], align 1
@__PRETTY_FUNCTION__._ZN7imnodes16GetSelectedLinksEPi = external hidden unnamed_addr constant [38 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 nounwind uwtable
declare hidden noundef nonnull align 8 dereferenceable(280) ptr @_ZN7imnodes12_GLOBAL__N_118editor_context_getEv() #1

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

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef nonnull align 4 dereferenceable(24) ptr @_ZNK8ImVectorIN7imnodes12_GLOBAL__N_18LinkDataEEixEi(ptr noundef nonnull align 8 dereferenceable(16), i32 noundef) #3 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNK8ImVectorIiEixEi(ptr noundef nonnull align 8 dereferenceable(16), i32 noundef) #3 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN7imnodes16GetSelectedLinksEPi(ptr noundef %0) #4 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = icmp ne ptr %6, null
  br i1 %7, label %8, label %9

8:                                                ; preds = %1
  br label %11

9:                                                ; preds = %1
  call void @__assert_fail(ptr noundef @.str.18, ptr noundef @.str.1, i32 noundef 2476, ptr noundef @__PRETTY_FUNCTION__._ZN7imnodes16GetSelectedLinksEPi) #5
  unreachable

10:                                               ; No predecessors!
  br label %11

11:                                               ; preds = %10, %8
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #6
  %12 = call noundef nonnull align 8 dereferenceable(280) ptr @_ZN7imnodes12_GLOBAL__N_118editor_context_getEv()
  store ptr %12, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #6
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %37, %11
  %14 = load i32, ptr %4, align 4, !tbaa !9
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %"struct.imnodes::EditorContext", ptr %15, i32 0, i32 6
  %17 = call noundef i32 @_ZNK8ImVectorIiE4sizeEv(ptr noundef nonnull align 8 dereferenceable(16) %16)
  %18 = icmp slt i32 %14, %17
  br i1 %18, label %20, label %19

19:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #6
  br label %40

20:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #6
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %"struct.imnodes::EditorContext", ptr %21, i32 0, i32 6
  %23 = load i32, ptr %4, align 4, !tbaa !9
  %24 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNK8ImVectorIiEixEi(ptr noundef nonnull align 8 dereferenceable(16) %22, i32 noundef %23)
  %25 = load i32, ptr %24, align 4, !tbaa !9
  store i32 %25, ptr %5, align 4, !tbaa !9
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %"struct.imnodes::EditorContext", ptr %26, i32 0, i32 2
  %28 = getelementptr inbounds %"struct.imnodes::(anonymous namespace)::ObjectPool.8", ptr %27, i32 0, i32 0
  %29 = load i32, ptr %5, align 4, !tbaa !9
  %30 = call noundef nonnull align 4 dereferenceable(24) ptr @_ZNK8ImVectorIN7imnodes12_GLOBAL__N_18LinkDataEEixEi(ptr noundef nonnull align 8 dereferenceable(16) %28, i32 noundef %29)
  %31 = getelementptr inbounds %"struct.imnodes::(anonymous namespace)::LinkData", ptr %30, i32 0, i32 0
  %32 = load i32, ptr %31, align 4, !tbaa !11
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = load i32, ptr %4, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds i32, ptr %33, i64 %35
  store i32 %32, ptr %36, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #6
  br label %37

37:                                               ; preds = %20
  %38 = load i32, ptr %4, align 4, !tbaa !9
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %4, align 4, !tbaa !9
  br label %13, !llvm.loop !14

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

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 = { 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 = { inlinehint 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 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 #5 = { noreturn nounwind }
attributes #6 = { 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 0}
!12 = !{!"_ZTSN7imnodes12_GLOBAL__N_18LinkDataE", !10, i64 0, !10, i64 4, !10, i64 8, !13, i64 12}
!13 = !{!"_ZTSN7imnodes12_GLOBAL__N_18LinkData3$_6E", !10, i64 0, !10, i64 4, !10, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; 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.1 = external hidden unnamed_addr constant [111 x i8], align 1
@.str.18 = external hidden unnamed_addr constant [17 x i8], align 1
@__PRETTY_FUNCTION__._ZN7imnodes16GetSelectedLinksEPi = external hidden unnamed_addr constant [38 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare hidden noundef nonnull align 8 dereferenceable(280) ptr @_ZN7imnodes12_GLOBAL__N_118editor_context_getEv() local_unnamed_addr #0

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

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef nonnull align 4 dereferenceable(24) ptr @_ZNK8ImVectorIN7imnodes12_GLOBAL__N_18LinkDataEEixEi(ptr noundef nonnull align 8 dereferenceable(16), i32 noundef) local_unnamed_addr #2 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNK8ImVectorIiEixEi(ptr noundef nonnull align 8 dereferenceable(16), i32 noundef) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN7imnodes16GetSelectedLinksEPi(ptr noundef writeonly %0) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %2, label %3

2:                                                ; preds = %1
  tail call void @__assert_fail(ptr noundef nonnull @.str.18, ptr noundef nonnull @.str.1, i32 noundef 2476, ptr noundef nonnull @__PRETTY_FUNCTION__._ZN7imnodes16GetSelectedLinksEPi) #3
  unreachable

3:                                                ; preds = %1
  %4 = tail call noundef nonnull align 8 dereferenceable(280) ptr @_ZN7imnodes12_GLOBAL__N_118editor_context_getEv()
  %5 = getelementptr inbounds i8, ptr %4, i64 232
  %6 = tail call noundef i32 @_ZNK8ImVectorIiE4sizeEv(ptr noundef nonnull align 8 dereferenceable(16) %5)
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3
  %8 = getelementptr inbounds i8, ptr %4, i64 128
  br label %9

._crit_edge:                                      ; preds = %9, %3
  ret void

9:                                                ; preds = %.lr.ph, %9
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %9 ]
  %10 = trunc nuw nsw i64 %indvars.iv to i32
  %11 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNK8ImVectorIiEixEi(ptr noundef nonnull align 8 dereferenceable(16) %5, i32 noundef %10)
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = tail call noundef nonnull align 4 dereferenceable(24) ptr @_ZNK8ImVectorIN7imnodes12_GLOBAL__N_18LinkDataEEixEi(ptr noundef nonnull align 8 dereferenceable(16) %8, i32 noundef %12)
  %14 = load i32, ptr %13, align 4, !tbaa !9
  %15 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  store i32 %14, ptr %15, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %16 = tail call noundef i32 @_ZNK8ImVectorIiE4sizeEv(ptr noundef nonnull align 8 dereferenceable(16) %5)
  %17 = sext i32 %16 to i64
  %18 = icmp slt i64 %indvars.iv.next, %17
  br i1 %18, label %9, label %._crit_edge, !llvm.loop !12
}

attributes #0 = { 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 #1 = { 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 #2 = { inlinehint 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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"_ZTSN7imnodes12_GLOBAL__N_18LinkDataE", !6, i64 0, !6, i64 4, !6, i64 8, !11, i64 12}
!11 = !{!"_ZTSN7imnodes12_GLOBAL__N_18LinkData3$_6E", !6, i64 0, !6, i64 4, !6, i64 8}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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