; LLVM IR from krig/LISP :: komplott (gc_protect)

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

@numroots = external dso_local global i64, align 8
@roots = external dso_local global [500 x ptr], align 16
@roottop = external dso_local global i64, align 8
@rootstack = external dso_local global [50 x i64], align 16

; 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: nounwind uwtable
define dso_local void @gc_protect(ptr noundef %0, ...) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca [1 x %struct.__va_list_tag], align 16
  %4 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 24, ptr %3) #3
  %5 = load i64, ptr @numroots, align 8, !tbaa !9
  %6 = load i64, ptr @roottop, align 8, !tbaa !9
  %7 = add i64 %6, 1
  store i64 %7, ptr @roottop, align 8, !tbaa !9
  %8 = getelementptr inbounds [50 x i64], ptr @rootstack, i64 0, i64 %6
  store i64 %5, ptr %8, align 8, !tbaa !9
  %9 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %3, i64 0, i64 0
  call void @llvm.va_start.p0(ptr %9)
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %10, ptr %4, align 8, !tbaa !5
  br label %11

11:                                               ; preds = %34, %1
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = icmp ne ptr %12, null
  br i1 %13, label %15, label %14

14:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %37

15:                                               ; preds = %11
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = load i64, ptr @numroots, align 8, !tbaa !9
  %18 = add i64 %17, 1
  store i64 %18, ptr @numroots, align 8, !tbaa !9
  %19 = getelementptr inbounds [500 x ptr], ptr @roots, i64 0, i64 %17
  store ptr %16, ptr %19, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %15
  %21 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %3, i64 0, i64 0
  %22 = getelementptr inbounds %struct.__va_list_tag, ptr %21, i32 0, i32 0
  %23 = load i32, ptr %22, align 16
  %24 = icmp ule i32 %23, 40
  br i1 %24, label %25, label %30

25:                                               ; preds = %20
  %26 = getelementptr inbounds %struct.__va_list_tag, ptr %21, i32 0, i32 3
  %27 = load ptr, ptr %26, align 16
  %28 = getelementptr i8, ptr %27, i32 %23
  %29 = add i32 %23, 8
  store i32 %29, ptr %22, align 16
  br label %34

30:                                               ; preds = %20
  %31 = getelementptr inbounds %struct.__va_list_tag, ptr %21, i32 0, i32 2
  %32 = load ptr, ptr %31, align 8
  %33 = getelementptr i8, ptr %32, i32 8
  store ptr %33, ptr %31, align 8
  br label %34

34:                                               ; preds = %30, %25
  %35 = phi ptr [ %28, %25 ], [ %32, %30 ]
  %36 = load ptr, ptr %35, align 8, !tbaa !5
  store ptr %36, ptr %4, align 8, !tbaa !5
  br label %11, !llvm.loop !11

37:                                               ; preds = %14
  %38 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %3, i64 0, i64 0
  call void @llvm.va_end.p0(ptr %38)
  call void @llvm.lifetime.end.p0(i64 24, ptr %3) #3
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.va_start.p0(ptr) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.va_end.p0(ptr) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { nocallback nofree nosync nounwind willreturn }
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 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

%struct.__va_list_tag = type { i32, i32, ptr, ptr }

@numroots = external dso_local local_unnamed_addr global i64, align 8
@roots = external dso_local local_unnamed_addr global [500 x ptr], align 16
@roottop = external dso_local local_unnamed_addr global i64, align 8
@rootstack = external dso_local local_unnamed_addr global [50 x i64], align 16

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

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

; Function Attrs: nofree norecurse nosync nounwind uwtable
define dso_local void @gc_protect(ptr noundef %0, ...) local_unnamed_addr #1 {
  %2 = alloca [1 x %struct.__va_list_tag], align 16
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %2) #3
  %3 = load i64, ptr @numroots, align 8, !tbaa !5
  %4 = load i64, ptr @roottop, align 8, !tbaa !5
  %5 = add i64 %4, 1
  store i64 %5, ptr @roottop, align 8, !tbaa !5
  %6 = getelementptr inbounds [50 x i64], ptr @rootstack, i64 0, i64 %4
  store i64 %3, ptr %6, align 8, !tbaa !5
  call void @llvm.va_start.p0(ptr nonnull %2)
  %.not4 = icmp eq ptr %0, null
  br i1 %.not4, label %10, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %.promoted = load i32, ptr %2, align 16
  %numroots.promoted = load i64, ptr @numroots, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %2, i64 8
  %8 = getelementptr inbounds i8, ptr %2, i64 16
  %9 = load ptr, ptr %8, align 16
  %.promoted6 = load ptr, ptr %7, align 8
  br label %11

._crit_edge:                                      ; preds = %24
  store i64 %15, ptr @numroots, align 8, !tbaa !5
  br label %10

10:                                               ; preds = %._crit_edge, %1
  call void @llvm.va_end.p0(ptr nonnull %2)
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %2) #3
  ret void

11:                                               ; preds = %.lr.ph, %24
  %12 = phi ptr [ %.promoted6, %.lr.ph ], [ %25, %24 ]
  %.05 = phi ptr [ %0, %.lr.ph ], [ %28, %24 ]
  %13 = phi i64 [ %numroots.promoted, %.lr.ph ], [ %15, %24 ]
  %14 = phi i32 [ %.promoted, %.lr.ph ], [ %26, %24 ]
  %15 = add i64 %13, 1
  %16 = getelementptr inbounds [500 x ptr], ptr @roots, i64 0, i64 %13
  store ptr %.05, ptr %16, align 8, !tbaa !9
  %17 = icmp ult i32 %14, 41
  br i1 %17, label %18, label %22

18:                                               ; preds = %11
  %19 = zext nneg i32 %14 to i64
  %20 = getelementptr i8, ptr %9, i64 %19
  %21 = add nuw nsw i32 %14, 8
  store i32 %21, ptr %2, align 16
  br label %24

22:                                               ; preds = %11
  %23 = getelementptr i8, ptr %12, i64 8
  store ptr %23, ptr %7, align 8
  br label %24

24:                                               ; preds = %22, %18
  %25 = phi ptr [ %12, %18 ], [ %23, %22 ]
  %26 = phi i32 [ %21, %18 ], [ %14, %22 ]
  %27 = phi ptr [ %20, %18 ], [ %12, %22 ]
  %28 = load ptr, ptr %27, align 8, !tbaa !9
  %.not = icmp eq ptr %28, null
  br i1 %.not, label %._crit_edge, label %11, !llvm.loop !11
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.va_start.p0(ptr) #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.va_end.p0(ptr) #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nofree norecurse nosync 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 = { mustprogress nocallback nofree nosync nounwind willreturn }
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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass