; LLVM IR from ros2/rcutils :: src (error_handling)

; 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.rcutils_error_string_s = type { [1024 x i8] }
%struct.rcutils_error_state_s = type { [768 x i8], [229 x i8], i64 }

@gtls_rcutils_error_string_is_formatted = external dso_local thread_local global i8, align 1
@gtls_rcutils_error_is_set = external dso_local thread_local global i8, align 1
@gtls_rcutils_error_string = external dso_local thread_local global %struct.rcutils_error_string_s, align 1
@gtls_rcutils_error_state = external dso_local thread_local global %struct.rcutils_error_state_s, align 8
@.str.6 = external hidden unnamed_addr constant [1024 x i8], align 1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #0

; Function Attrs: nounwind uwtable
define dso_local void @rcutils_get_error_string(ptr dead_on_unwind noalias writable sret(%struct.rcutils_error_string_s) align 1 %0) #1 {
  %2 = call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @gtls_rcutils_error_is_set)
  %3 = load i8, ptr %2, align 1, !tbaa !5, !range !9, !noundef !10
  %4 = trunc i8 %3 to i1
  br i1 %4, label %7, label %5

5:                                                ; preds = %1
  %6 = getelementptr inbounds %struct.rcutils_error_string_s, ptr %0, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %6, ptr align 1 @.str.6, i64 1024, i1 false), !tbaa.struct !11
  br label %17

7:                                                ; preds = %1
  %8 = call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @gtls_rcutils_error_string_is_formatted)
  %9 = load i8, ptr %8, align 1, !tbaa !5, !range !9, !noundef !10
  %10 = trunc i8 %9 to i1
  br i1 %10, label %15, label %11

11:                                               ; preds = %7
  %12 = call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @gtls_rcutils_error_string)
  %13 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @gtls_rcutils_error_state)
  call void @__rcutils_format_error_string(ptr noundef %12, ptr noundef %13)
  %14 = call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @gtls_rcutils_error_string_is_formatted)
  store i8 1, ptr %14, align 1, !tbaa !5
  br label %15

15:                                               ; preds = %11, %7
  %16 = call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @gtls_rcutils_error_string)
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %0, ptr align 1 %16, i64 1024, i1 false), !tbaa.struct !11
  br label %17

17:                                               ; preds = %15, %5
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
declare hidden void @__rcutils_format_error_string(ptr noundef, ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 nounwind willreturn memory(argmem: readwrite) }

!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 = !{!"_Bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{i8 0, i8 2}
!10 = !{}
!11 = !{i64 0, i64 1024, !12}
!12 = !{!7, !7, i64 0}


; 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.rcutils_error_string_s = type { [1024 x i8] }
%struct.rcutils_error_state_s = type { [768 x i8], [229 x i8], i64 }

@gtls_rcutils_error_string_is_formatted = external dso_local thread_local local_unnamed_addr global i8, align 1
@gtls_rcutils_error_is_set = external dso_local thread_local local_unnamed_addr global i8, align 1
@gtls_rcutils_error_string = external dso_local thread_local global %struct.rcutils_error_string_s, align 1
@gtls_rcutils_error_state = external dso_local thread_local global %struct.rcutils_error_state_s, align 8
@.str.6 = external hidden unnamed_addr constant [1024 x i8], align 1

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #0

; Function Attrs: nounwind uwtable
define dso_local void @rcutils_get_error_string(ptr dead_on_unwind noalias nocapture writable writeonly sret(%struct.rcutils_error_string_s) align 1 %0) local_unnamed_addr #1 {
  %2 = tail call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @gtls_rcutils_error_is_set)
  %3 = load i8, ptr %2, align 1, !tbaa !5, !range !9, !noundef !10
  %4 = trunc nuw i8 %3 to i1
  br i1 %4, label %5, label %._crit_edge

5:                                                ; preds = %1
  %6 = tail call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @gtls_rcutils_error_string_is_formatted)
  %7 = load i8, ptr %6, align 1, !tbaa !5, !range !9, !noundef !10
  %8 = trunc nuw i8 %7 to i1
  %.pre = tail call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @gtls_rcutils_error_string)
  br i1 %8, label %._crit_edge, label %9

9:                                                ; preds = %5
  %10 = tail call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @gtls_rcutils_error_state)
  tail call void @__rcutils_format_error_string(ptr noundef nonnull %.pre, ptr noundef nonnull %10)
  store i8 1, ptr %6, align 1, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %9, %5, %1
  %.pre.sink = phi ptr [ @.str.6, %1 ], [ %.pre, %5 ], [ %.pre, %9 ]
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(1024) %0, ptr noundef nonnull align 1 dereferenceable(1024) %.pre.sink, i64 1024, i1 false)
  ret void
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
declare hidden void @__rcutils_format_error_string(ptr noundef, ptr noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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 = !{!"_Bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{i8 0, i8 2}
!10 = !{}


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