; LLVM IR from buzz-lang/Buzz :: src/buzz (buzzdebug)

; 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.buzzvm_s = type { ptr, i32, i32, i32, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i16, ptr, ptr, ptr, ptr, i32, i32, ptr, i16, ptr, i32 }
%struct.buzzdarray_s = type { ptr, i64, i32, i32, ptr }
%struct.buzzint_t = type { i16, i16, i32 }

@.str.19 = external hidden unnamed_addr constant [16 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: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #1

; Function Attrs: nounwind uwtable
declare dso_local void @buzzdebug_backtrace_entry(i32 noundef, ptr noundef, i32 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @buzzdebug_backtrace(ptr noundef %0, ptr noundef %1, ptr noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.buzzvm_s, ptr %9, i32 0, i32 5
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  %12 = getelementptr inbounds %struct.buzzdarray_s, ptr %11, i32 0, i32 1
  %13 = load i64, ptr %12, align 8, !tbaa !13
  %14 = icmp eq i64 %13, 0
  br i1 %14, label %15, label %18

15:                                               ; preds = %3
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %16, ptr noundef @.str.19) #3
  br label %66

18:                                               ; preds = %3
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.buzzvm_s, ptr %20, i32 0, i32 2
  %22 = load i32, ptr %21, align 4, !tbaa !16
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  call void @buzzdebug_backtrace_entry(i32 noundef 1, ptr noundef %19, i32 noundef %22, ptr noundef %23)
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.buzzvm_s, ptr %24, i32 0, i32 5
  %26 = load ptr, ptr %25, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.buzzdarray_s, ptr %26, i32 0, i32 1
  %28 = load i64, ptr %27, align 8, !tbaa !13
  %29 = sub nsw i64 %28, 2
  store i64 %29, ptr %7, align 8, !tbaa !17
  br label %30

30:                                               ; preds = %63, %18
  %31 = load i64, ptr %7, align 8, !tbaa !17
  %32 = icmp sge i64 %31, 0
  br i1 %32, label %34, label %33

33:                                               ; preds = %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %66

34:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.buzzvm_s, ptr %35, i32 0, i32 4
  %37 = load ptr, ptr %36, align 8, !tbaa !18
  %38 = getelementptr inbounds %struct.buzzdarray_s, ptr %37, i32 0, i32 0
  %39 = load ptr, ptr %38, align 8, !tbaa !19
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.buzzvm_s, ptr %40, i32 0, i32 4
  %42 = load ptr, ptr %41, align 8, !tbaa !18
  %43 = getelementptr inbounds %struct.buzzdarray_s, ptr %42, i32 0, i32 1
  %44 = load i64, ptr %43, align 8, !tbaa !13
  %45 = sub nsw i64 %44, 1
  %46 = getelementptr inbounds ptr, ptr %39, i64 %45
  %47 = load ptr, ptr %46, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.buzzint_t, ptr %47, i32 0, i32 2
  %49 = load i32, ptr %48, align 4, !tbaa !20
  store i32 %49, ptr %8, align 4, !tbaa !21
  %50 = load i32, ptr %8, align 4, !tbaa !21
  %51 = sub nsw i32 %50, 1
  store i32 %51, ptr %8, align 4, !tbaa !21
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.buzzvm_s, ptr %52, i32 0, i32 5
  %54 = load ptr, ptr %53, align 8, !tbaa !9
  %55 = getelementptr inbounds %struct.buzzdarray_s, ptr %54, i32 0, i32 1
  %56 = load i64, ptr %55, align 8, !tbaa !13
  %57 = load i64, ptr %7, align 8, !tbaa !17
  %58 = sub nsw i64 %56, %57
  %59 = trunc i64 %58 to i32
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = load i32, ptr %8, align 4, !tbaa !21
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  call void @buzzdebug_backtrace_entry(i32 noundef %59, ptr noundef %60, i32 noundef %61, ptr noundef %62)
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %63

63:                                               ; preds = %34
  %64 = load i64, ptr %7, align 8, !tbaa !17
  %65 = add nsw i64 %64, -1
  store i64 %65, ptr %7, align 8, !tbaa !17
  br label %30, !llvm.loop !22

66:                                               ; preds = %33, %15
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { 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, !6, i64 32}
!10 = !{!"buzzvm_s", !6, i64 0, !11, i64 8, !11, i64 12, !11, 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, !12, i64 112, !6, i64 120, !6, i64 128, !6, i64 136, !6, i64 144, !11, i64 152, !11, i64 156, !6, i64 160, !12, i64 168, !6, i64 176, !11, i64 184}
!11 = !{!"int", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!14, !15, i64 8}
!14 = !{!"buzzdarray_s", !6, i64 0, !15, i64 8, !11, i64 16, !11, i64 20, !6, i64 24}
!15 = !{!"long", !7, i64 0}
!16 = !{!10, !11, i64 12}
!17 = !{!15, !15, i64 0}
!18 = !{!10, !6, i64 24}
!19 = !{!14, !6, i64 0}
!20 = !{!7, !7, i64 0}
!21 = !{!11, !11, i64 0}
!22 = distinct !{!22, !23}
!23 = !{!"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.19 = external hidden unnamed_addr constant [16 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @buzzdebug_backtrace_entry(i32 noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @buzzdebug_backtrace(ptr nocapture noundef readonly %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #1 {
  %4 = getelementptr inbounds i8, ptr %0, i64 32
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %5, i64 8
  %7 = load i64, ptr %6, align 8, !tbaa !12
  %8 = icmp eq i64 %7, 0
  br i1 %8, label %9, label %11

9:                                                ; preds = %3
  %10 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %2, ptr noundef nonnull @.str.19) #2
  br label %.loopexit

11:                                               ; preds = %3
  %12 = getelementptr inbounds i8, ptr %0, i64 12
  %13 = load i32, ptr %12, align 4, !tbaa !15
  tail call void @buzzdebug_backtrace_entry(i32 noundef 1, ptr noundef %1, i32 noundef %13, ptr noundef %2)
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %14, i64 8
  %16 = load i64, ptr %15, align 8, !tbaa !12
  %17 = icmp sgt i64 %16, 1
  br i1 %17, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %11
  %18 = add nsw i64 %16, -2
  %19 = getelementptr inbounds i8, ptr %0, i64 24
  br label %20

20:                                               ; preds = %.lr.ph, %20
  %.015 = phi i64 [ %18, %.lr.ph ], [ %36, %20 ]
  %21 = load ptr, ptr %19, align 8, !tbaa !16
  %22 = load ptr, ptr %21, align 8, !tbaa !17
  %23 = getelementptr inbounds i8, ptr %21, i64 8
  %24 = load i64, ptr %23, align 8, !tbaa !12
  %25 = getelementptr ptr, ptr %22, i64 %24
  %26 = getelementptr i8, ptr %25, i64 -8
  %27 = load ptr, ptr %26, align 8, !tbaa !18
  %28 = getelementptr inbounds i8, ptr %27, i64 4
  %29 = load i32, ptr %28, align 4, !tbaa !19
  %30 = add nsw i32 %29, -1
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %31, i64 8
  %33 = load i64, ptr %32, align 8, !tbaa !12
  %34 = sub nsw i64 %33, %.015
  %35 = trunc i64 %34 to i32
  tail call void @buzzdebug_backtrace_entry(i32 noundef %35, ptr noundef %1, i32 noundef %30, ptr noundef %2)
  %36 = add nsw i64 %.015, -1
  %.not = icmp eq i64 %.015, 0
  br i1 %.not, label %.loopexit, label %20, !llvm.loop !20

.loopexit:                                        ; preds = %20, %11, %9
  ret void
}

attributes #0 = { nofree 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 #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 = { 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, !7, i64 32}
!6 = !{!"buzzvm_s", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !7, i64 88, !7, i64 96, !7, i64 104, !11, i64 112, !7, i64 120, !7, i64 128, !7, i64 136, !7, i64 144, !10, i64 152, !10, i64 156, !7, i64 160, !11, i64 168, !7, i64 176, !10, i64 184}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"short", !8, i64 0}
!12 = !{!13, !14, i64 8}
!13 = !{!"buzzdarray_s", !7, i64 0, !14, i64 8, !10, i64 16, !10, i64 20, !7, i64 24}
!14 = !{!"long", !8, i64 0}
!15 = !{!6, !10, i64 12}
!16 = !{!6, !7, i64 24}
!17 = !{!13, !7, i64 0}
!18 = !{!7, !7, i64 0}
!19 = !{!8, !8, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


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