; LLVM IR from Simple-XX/SimpleKernel :: src/libcxx/src/cxxabi.ll_ (aeabi_atexit)

; 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.atexit_func_entry_t = type { ptr, ptr, ptr }

@__atexit_funcs = external dso_local global [128 x %struct.atexit_func_entry_t], align 16
@__atexit_func_count = external dso_local global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local i32 @__aeabi_atexit(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  %8 = load i32, ptr @__atexit_func_count, align 4, !tbaa !9
  %9 = icmp uge i32 %8, 128
  br i1 %9, label %10, label %11

10:                                               ; preds = %3
  store i32 -1, ptr %4, align 4
  br label %29

11:                                               ; preds = %3
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = load i32, ptr @__atexit_func_count, align 4, !tbaa !9
  %14 = zext i32 %13 to i64
  %15 = getelementptr inbounds [128 x %struct.atexit_func_entry_t], ptr @__atexit_funcs, i64 0, i64 %14
  %16 = getelementptr inbounds %struct.atexit_func_entry_t, ptr %15, i32 0, i32 0
  store ptr %12, ptr %16, align 8, !tbaa !11
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = load i32, ptr @__atexit_func_count, align 4, !tbaa !9
  %19 = zext i32 %18 to i64
  %20 = getelementptr inbounds [128 x %struct.atexit_func_entry_t], ptr @__atexit_funcs, i64 0, i64 %19
  %21 = getelementptr inbounds %struct.atexit_func_entry_t, ptr %20, i32 0, i32 1
  store ptr %17, ptr %21, align 8, !tbaa !13
  %22 = load ptr, ptr %7, align 8, !tbaa !5
  %23 = load i32, ptr @__atexit_func_count, align 4, !tbaa !9
  %24 = zext i32 %23 to i64
  %25 = getelementptr inbounds [128 x %struct.atexit_func_entry_t], ptr @__atexit_funcs, i64 0, i64 %24
  %26 = getelementptr inbounds %struct.atexit_func_entry_t, ptr %25, i32 0, i32 2
  store ptr %22, ptr %26, align 8, !tbaa !14
  %27 = load i32, ptr @__atexit_func_count, align 4, !tbaa !9
  %28 = add i32 %27, 1
  store i32 %28, ptr @__atexit_func_count, align 4, !tbaa !9
  store i32 0, ptr %4, align 4
  br label %29

29:                                               ; preds = %11, %10
  %30 = load i32, ptr %4, align 4
  ret i32 %30
}

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

!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 0}
!12 = !{!"_ZTS19atexit_func_entry_t", !6, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!12, !6, i64 8}
!14 = !{!12, !6, i64 16}


; 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.atexit_func_entry_t = type { ptr, ptr, ptr }

@__atexit_funcs = external dso_local local_unnamed_addr global [128 x %struct.atexit_func_entry_t], align 16
@__atexit_func_count = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local range(i32 -1, 1) i32 @__aeabi_atexit(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = load i32, ptr @__atexit_func_count, align 4, !tbaa !5
  %5 = icmp ugt i32 %4, 127
  br i1 %5, label %12, label %6

6:                                                ; preds = %3
  %7 = zext nneg i32 %4 to i64
  %8 = getelementptr inbounds [128 x %struct.atexit_func_entry_t], ptr @__atexit_funcs, i64 0, i64 %7
  store ptr %0, ptr %8, align 8, !tbaa !9
  %9 = getelementptr inbounds [128 x %struct.atexit_func_entry_t], ptr @__atexit_funcs, i64 0, i64 %7, i32 1
  store ptr %1, ptr %9, align 8, !tbaa !12
  %10 = getelementptr inbounds [128 x %struct.atexit_func_entry_t], ptr @__atexit_funcs, i64 0, i64 %7, i32 2
  store ptr %2, ptr %10, align 8, !tbaa !13
  %11 = add nuw nsw i32 %4, 1
  store i32 %11, ptr @__atexit_func_count, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %3, %6
  %.0 = phi i32 [ 0, %6 ], [ -1, %3 ]
  ret i32 %.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: none, 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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"_ZTS19atexit_func_entry_t", !11, i64 0, !11, i64 8, !11, i64 16}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!10, !11, i64 16}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass