; LLVM IR from EricZinda/InductorHtn :: src/FXPlatform/Posix/TraceError_Pos.ll (ZN10TraceError12CaptureStackEj)

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

%class.TraceError = type <{ %"class.std::runtime_error", ptr, i32, [4 x i8] }>
%"class.std::runtime_error" = type { %"class.std::exception", %"struct.std::__cow_string" }
%"class.std::exception" = type { ptr }
%"struct.std::__cow_string" = type { %union.anon }
%union.anon = type { ptr }

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN10TraceError12CaptureStackEj(ptr noundef nonnull align 8 dereferenceable(28) %0, i32 noundef %1) #0 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %8 = load ptr, ptr %3, align 8
  %9 = load i32, ptr %4, align 4, !tbaa !9
  %10 = add i32 %9, 1
  %11 = zext i32 %10 to i64
  %12 = call ptr @llvm.stacksave.p0()
  store ptr %12, ptr %5, align 8
  %13 = alloca ptr, i64 %11, align 16
  store i64 %11, ptr %6, align 8
  %14 = mul nuw i64 8, %11
  %15 = udiv i64 %14, 8
  %16 = trunc i64 %15 to i32
  %17 = call i32 @backtrace(ptr noundef %13, i32 noundef %16)
  %18 = getelementptr inbounds %class.TraceError, ptr %8, i32 0, i32 2
  store i32 %17, ptr %18, align 8, !tbaa !11
  %19 = getelementptr inbounds %class.TraceError, ptr %8, i32 0, i32 2
  %20 = load i32, ptr %19, align 8, !tbaa !11
  %21 = icmp eq i32 %20, 0
  br i1 %21, label %22, label %23

22:                                               ; preds = %2
  store i32 1, ptr %7, align 4
  br label %28

23:                                               ; preds = %2
  %24 = getelementptr inbounds %class.TraceError, ptr %8, i32 0, i32 2
  %25 = load i32, ptr %24, align 8, !tbaa !11
  %26 = call ptr @backtrace_symbols(ptr noundef %13, i32 noundef %25) #4
  %27 = getelementptr inbounds %class.TraceError, ptr %8, i32 0, i32 1
  store ptr %26, ptr %27, align 8, !tbaa !16
  store i32 0, ptr %7, align 4
  br label %28

28:                                               ; preds = %23, %22
  %29 = load ptr, ptr %5, align 8
  call void @llvm.stackrestore.p0(ptr %29)
  %30 = load i32, ptr %7, align 4
  switch i32 %30, label %32 [
    i32 0, label %31
    i32 1, label %31
  ]

31:                                               ; preds = %28, %28
  ret void

32:                                               ; preds = %28
  unreachable
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #1

declare i32 @backtrace(ptr noundef, i32 noundef) #2

; Function Attrs: nounwind
declare ptr @backtrace_symbols(ptr noundef, i32 noundef) #3

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn }
attributes #2 = { "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 "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 = { 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 24}
!12 = !{!"_ZTS10TraceError", !13, i64 0, !6, i64 16, !10, i64 24}
!13 = !{!"_ZTSSt13runtime_error", !14, i64 0, !15, i64 8}
!14 = !{!"_ZTSSt9exception"}
!15 = !{!"_ZTSSt12__cow_string", !7, i64 0}
!16 = !{!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"

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN10TraceError12CaptureStackEj(ptr nocapture noundef nonnull writeonly align 8 dereferenceable(28) %0, i32 noundef %1) local_unnamed_addr #0 align 2 {
  %3 = add i32 %1, 1
  %4 = zext i32 %3 to i64
  %5 = alloca ptr, i64 %4, align 16
  %6 = call i32 @backtrace(ptr noundef nonnull %5, i32 noundef %3)
  %7 = getelementptr inbounds i8, ptr %0, i64 24
  store i32 %6, ptr %7, align 8, !tbaa !5
  %8 = icmp eq i32 %6, 0
  br i1 %8, label %12, label %9

9:                                                ; preds = %2
  %10 = call ptr @backtrace_symbols(ptr noundef nonnull %5, i32 noundef %6) #3
  %11 = getelementptr inbounds i8, ptr %0, i64 16
  store ptr %10, ptr %11, align 8, !tbaa !14
  br label %12

12:                                               ; preds = %2, %9
  ret void
}

declare i32 @backtrace(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind
declare ptr @backtrace_symbols(ptr noundef, i32 noundef) local_unnamed_addr #2

attributes #0 = { 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 #1 = { "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 "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, !13, i64 24}
!6 = !{!"_ZTS10TraceError", !7, i64 0, !12, i64 16, !13, i64 24}
!7 = !{!"_ZTSSt13runtime_error", !8, i64 0, !9, i64 8}
!8 = !{!"_ZTSSt9exception"}
!9 = !{!"_ZTSSt12__cow_string", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C++ TBAA"}
!12 = !{!"any pointer", !10, i64 0}
!13 = !{!"int", !10, i64 0}
!14 = !{!6, !12, i64 16}


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