; 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_allocator_s = type { ptr, ptr, ptr, ptr, ptr }
%struct.rcutils_error_string_s = type { [1024 x i8] }

@gtls_rcutils_thread_local_initialized = external dso_local thread_local global i8, align 1
@.str = external hidden unnamed_addr constant [112 x i8], align 1
@stderr = external global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [45 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [67 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @rcutils_initialize_error_handling_thread_local_storage(ptr noundef byval(%struct.rcutils_allocator_s) align 8 %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca %struct.rcutils_error_string_s, align 1
  %4 = call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @gtls_rcutils_thread_local_initialized)
  %5 = load i8, ptr %4, align 1, !tbaa !5, !range !9, !noundef !10
  %6 = trunc i8 %5 to i1
  br i1 %6, label %7, label %8

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

8:                                                ; preds = %1
  %9 = call zeroext i1 @rcutils_allocator_is_valid(ptr noundef %0)
  br i1 %9, label %15, label %10

10:                                               ; preds = %8
  br label %11

11:                                               ; preds = %10
  %12 = load ptr, ptr @stderr, align 8, !tbaa !11
  %13 = call i64 @fwrite(ptr noundef @.str, i64 noundef 1, i64 noundef 111, ptr noundef %12)
  br label %14

14:                                               ; preds = %11
  store i32 11, ptr %2, align 4
  br label %19

15:                                               ; preds = %8
  %16 = call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @gtls_rcutils_thread_local_initialized)
  store i8 1, ptr %16, align 1, !tbaa !5
  call void @rcutils_reset_error()
  br label %17

17:                                               ; preds = %15
  call void @rcutils_set_error_state(ptr noundef @.str.1, ptr noundef @.str.2, i64 noundef 69)
  br label %18

18:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 1024, ptr %3) #4
  call void @rcutils_get_error_string(ptr dead_on_unwind writable sret(%struct.rcutils_error_string_s) align 1 %3)
  call void @rcutils_reset_error()
  store i32 0, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 1024, ptr %3) #4
  br label %19

19:                                               ; preds = %18, %14, %7
  %20 = load i32, ptr %2, align 4
  ret i32 %20
}

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

declare zeroext i1 @rcutils_allocator_is_valid(ptr noundef) #2

declare i64 @fwrite(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @rcutils_reset_error() #0

; Function Attrs: nounwind uwtable
declare dso_local void @rcutils_set_error_state(ptr noundef, ptr noundef, i64 noundef) #0

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

; Function Attrs: nounwind uwtable
declare dso_local void @rcutils_get_error_string(ptr dead_on_unwind noalias writable sret(%struct.rcutils_error_string_s) align 1) #0

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"_Bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{i8 0, i8 2}
!10 = !{}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !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_allocator_s = type { ptr, ptr, ptr, ptr, ptr }
%struct.rcutils_error_string_s = type { [1024 x i8] }

@gtls_rcutils_thread_local_initialized = external dso_local thread_local local_unnamed_addr global i8, align 1
@.str = external hidden unnamed_addr constant [112 x i8], align 1
@stderr = external local_unnamed_addr global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [45 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [67 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 12) i32 @rcutils_initialize_error_handling_thread_local_storage(ptr noundef byval(%struct.rcutils_allocator_s) align 8 %0) local_unnamed_addr #0 {
  %2 = alloca %struct.rcutils_error_string_s, align 1
  %3 = tail call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @gtls_rcutils_thread_local_initialized)
  %4 = load i8, ptr %3, align 1, !tbaa !5, !range !9, !noundef !10
  %5 = trunc nuw i8 %4 to i1
  br i1 %5, label %12, label %6

6:                                                ; preds = %1
  %7 = call zeroext i1 @rcutils_allocator_is_valid(ptr noundef nonnull %0) #5
  br i1 %7, label %11, label %8

8:                                                ; preds = %6
  %9 = load ptr, ptr @stderr, align 8, !tbaa !11
  %10 = call i64 @fwrite(ptr noundef nonnull @.str, i64 noundef 1, i64 noundef 111, ptr noundef %9) #6
  br label %12

11:                                               ; preds = %6
  store i8 1, ptr %3, align 1, !tbaa !5
  call void @rcutils_reset_error()
  call void @rcutils_set_error_state(ptr noundef nonnull @.str.1, ptr noundef nonnull @.str.2, i64 noundef 69)
  call void @llvm.lifetime.start.p0(i64 1024, ptr nonnull %2) #5
  call void @rcutils_get_error_string(ptr dead_on_unwind nonnull writable sret(%struct.rcutils_error_string_s) align 1 %2)
  call void @rcutils_reset_error()
  call void @llvm.lifetime.end.p0(i64 1024, ptr nonnull %2) #5
  br label %12

12:                                               ; preds = %1, %11, %8
  %.0 = phi i32 [ 0, %11 ], [ 11, %8 ], [ 0, %1 ]
  ret i32 %.0
}

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

declare zeroext i1 @rcutils_allocator_is_valid(ptr noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i64 @fwrite(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare dso_local void @rcutils_reset_error() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @rcutils_set_error_state(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare dso_local void @rcutils_get_error_string(ptr dead_on_unwind noalias writable sret(%struct.rcutils_error_string_s) align 1) local_unnamed_addr #0

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

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

!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 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}


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