; LLVM IR from mist64/perfect6502 :: netlist_sim (stabilizeChip)

; 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.state_t = type { i16, i16, i16, i16, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, %struct.list_t, ptr, %struct.list_t, ptr, ptr, i16, ptr, i32 }
%struct.list_t = type { ptr, i16 }

; Function Attrs: nounwind uwtable
declare dso_local void @recalcNodeList(ptr noundef) #0

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

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

; Function Attrs: nounwind uwtable
define dso_local void @stabilizeChip(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i16, align 2
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %3) #3
  store i16 0, ptr %3, align 2, !tbaa !9
  br label %4

4:                                                ; preds = %16, %1
  %5 = load i16, ptr %3, align 2, !tbaa !9
  %6 = zext i16 %5 to i32
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.state_t, ptr %7, i32 0, i32 0
  %9 = load i16, ptr %8, align 8, !tbaa !11
  %10 = zext i16 %9 to i32
  %11 = icmp slt i32 %6, %10
  br i1 %11, label %13, label %12

12:                                               ; preds = %4
  call void @llvm.lifetime.end.p0(i64 2, ptr %3) #3
  br label %19

13:                                               ; preds = %4
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = load i16, ptr %3, align 2, !tbaa !9
  call void @listout_add(ptr noundef %14, i16 noundef zeroext %15)
  br label %16

16:                                               ; preds = %13
  %17 = load i16, ptr %3, align 2, !tbaa !9
  %18 = add i16 %17, 1
  store i16 %18, ptr %3, align 2, !tbaa !9
  br label %4, !llvm.loop !15

19:                                               ; preds = %12
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  call void @recalcNodeList(ptr noundef %20)
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @listout_add(ptr noundef, i16 noundef zeroext) #2

attributes #0 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"", !10, i64 0, !10, i64 2, !10, i64 4, !10, i64 6, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128, !13, i64 136, !6, i64 152, !13, i64 160, !6, i64 176, !6, i64 184, !10, i64 192, !6, i64 200, !14, i64 208}
!13 = !{!"", !6, i64 0, !10, i64 8}
!14 = !{!"int", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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"

; Function Attrs: nounwind uwtable
declare dso_local void @recalcNodeList(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @stabilizeChip(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load i16, ptr %0, align 8, !tbaa !5
  %.not = icmp eq i16 %2, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %1
  tail call void @recalcNodeList(ptr noundef nonnull %0)
  ret void

.lr.ph:                                           ; preds = %1, %.lr.ph
  %.05 = phi i16 [ %3, %.lr.ph ], [ 0, %1 ]
  tail call void @listout_add(ptr noundef nonnull %0, i16 noundef zeroext %.05)
  %3 = add nuw i16 %.05, 1
  %4 = load i16, ptr %0, align 8, !tbaa !5
  %5 = icmp ult i16 %3, %4
  br i1 %5, label %.lr.ph, label %._crit_edge, !llvm.loop !13
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @listout_add(ptr noundef, i16 noundef zeroext) local_unnamed_addr #1

attributes #0 = { 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 = { inlinehint 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 0}
!6 = !{!"", !7, i64 0, !7, i64 2, !7, i64 4, !7, i64 6, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !10, i64 64, !10, i64 72, !10, i64 80, !10, i64 88, !10, i64 96, !10, i64 104, !10, i64 112, !10, i64 120, !10, i64 128, !11, i64 136, !10, i64 152, !11, i64 160, !10, i64 176, !10, i64 184, !7, i64 192, !10, i64 200, !12, i64 208}
!7 = !{!"short", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"", !10, i64 0, !7, i64 8}
!12 = !{!"int", !8, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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