; 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
@stderr = external global ptr, align 8
@.str.3 = external hidden unnamed_addr constant [113 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [112 x 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.5 = 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

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

; Function Attrs: nounwind uwtable
define dso_local void @rcutils_set_error_state(ptr noundef %0, ptr noundef %1, i64 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca %struct.rcutils_error_state_s, align 8
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca [4096 x i8], align 16
  %11 = alloca %struct.rcutils_error_string_s, align 1
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1008, ptr %7) #6
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = icmp eq ptr null, %12
  br i1 %13, label %14, label %20

14:                                               ; preds = %3
  br label %15

15:                                               ; preds = %14
  %16 = load ptr, ptr @stderr, align 8, !tbaa !5
  %17 = call i64 @fwrite(ptr noundef @.str.3, i64 noundef 1, i64 noundef 112, ptr noundef %16)
  br label %18

18:                                               ; preds = %15
  br label %19

19:                                               ; preds = %18
  store i32 1, ptr %8, align 4
  br label %75

20:                                               ; preds = %3
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = icmp eq ptr null, %21
  br i1 %22, label %23, label %29

23:                                               ; preds = %20
  br label %24

24:                                               ; preds = %23
  %25 = load ptr, ptr @stderr, align 8, !tbaa !5
  %26 = call i64 @fwrite(ptr noundef @.str.4, i64 noundef 1, i64 noundef 111, ptr noundef %25)
  br label %27

27:                                               ; preds = %24
  br label %28

28:                                               ; preds = %27
  store i32 1, ptr %8, align 4
  br label %75

29:                                               ; preds = %20
  %30 = getelementptr inbounds %struct.rcutils_error_state_s, ptr %7, i32 0, i32 0
  %31 = getelementptr inbounds [768 x i8], ptr %30, i64 0, i64 0
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = call i64 @__rcutils_copy_string(ptr noundef %31, i64 noundef 768, ptr noundef %32)
  %34 = getelementptr inbounds %struct.rcutils_error_state_s, ptr %7, i32 0, i32 1
  %35 = getelementptr inbounds [229 x i8], ptr %34, i64 0, i64 0
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = call i64 @__rcutils_copy_string(ptr noundef %35, i64 noundef 229, ptr noundef %36)
  %38 = load i64, ptr %6, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.rcutils_error_state_s, ptr %7, i32 0, i32 2
  store i64 %38, ptr %39, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #6
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = call i64 @strnlen(ptr noundef %40, i64 noundef 1024) #7
  store i64 %41, ptr %9, align 8, !tbaa !9
  %42 = call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @gtls_rcutils_error_is_set)
  %43 = load i8, ptr %42, align 1, !tbaa !13, !range !15, !noundef !16
  %44 = trunc i8 %43 to i1
  br i1 %44, label %45, label %69

45:                                               ; preds = %29
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @gtls_rcutils_error_string)
  %48 = getelementptr inbounds %struct.rcutils_error_string_s, ptr %47, i32 0, i32 0
  %49 = getelementptr inbounds [1024 x i8], ptr %48, i64 0, i64 0
  %50 = load i64, ptr %9, align 8, !tbaa !9
  %51 = call zeroext i1 @__same_string(ptr noundef %46, ptr noundef %49, i64 noundef %50)
  br i1 %51, label %69, label %52

52:                                               ; preds = %45
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @gtls_rcutils_error_state)
  %55 = getelementptr inbounds %struct.rcutils_error_state_s, ptr %54, i32 0, i32 0
  %56 = getelementptr inbounds [768 x i8], ptr %55, i64 0, i64 0
  %57 = load i64, ptr %9, align 8, !tbaa !9
  %58 = call zeroext i1 @__same_string(ptr noundef %53, ptr noundef %56, i64 noundef %57)
  br i1 %58, label %69, label %59

59:                                               ; preds = %52
  call void @llvm.lifetime.start.p0(i64 4096, ptr %10) #6
  %60 = getelementptr inbounds [4096 x i8], ptr %10, i64 0, i64 0
  call void @__format_overwriting_error_state_message(ptr noundef %60, i64 noundef 4096, ptr noundef %7)
  br label %61

61:                                               ; preds = %59
  %62 = getelementptr inbounds [4096 x i8], ptr %10, i64 0, i64 0
  %63 = getelementptr inbounds [4096 x i8], ptr %10, i64 0, i64 0
  %64 = call i64 @strlen(ptr noundef %63) #7
  %65 = load ptr, ptr @stderr, align 8, !tbaa !5
  %66 = call i64 @fwrite(ptr noundef %62, i64 noundef 1, i64 noundef %64, ptr noundef %65)
  br label %67

67:                                               ; preds = %61
  br label %68

68:                                               ; preds = %67
  call void @llvm.lifetime.end.p0(i64 4096, ptr %10) #6
  br label %69

69:                                               ; preds = %68, %52, %45, %29
  %70 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @gtls_rcutils_error_state)
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %70, ptr align 8 %7, i64 1008, i1 false), !tbaa.struct !17
  %71 = call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @gtls_rcutils_error_string_is_formatted)
  store i8 0, ptr %71, align 1, !tbaa !13
  %72 = call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @gtls_rcutils_error_string)
  %73 = getelementptr inbounds %struct.rcutils_error_string_s, ptr %11, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %73, ptr align 1 @.str.5, i64 1024, i1 false), !tbaa.struct !19
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %72, ptr align 1 %11, i64 1024, i1 false), !tbaa.struct !19
  %74 = call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @gtls_rcutils_error_is_set)
  store i8 1, ptr %74, align 1, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #6
  store i32 0, ptr %8, align 4
  br label %75

75:                                               ; preds = %69, %28, %19
  call void @llvm.lifetime.end.p0(i64 1008, ptr %7) #6
  %76 = load i32, ptr %8, align 4
  switch i32 %76, label %78 [
    i32 0, label %77
    i32 1, label %77
  ]

77:                                               ; preds = %75, %75
  ret void

78:                                               ; preds = %75
  unreachable
}

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

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

; Function Attrs: nounwind uwtable
declare hidden i64 @__rcutils_copy_string(ptr noundef, i64 noundef, ptr noundef) #2

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strnlen(ptr noundef, i64 noundef) #4

; Function Attrs: nounwind uwtable
declare hidden zeroext i1 @__same_string(ptr noundef, ptr noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @__format_overwriting_error_state_message(ptr noundef, i64 noundef, ptr noundef) #2

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #4

; 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) #5

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

!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 = !{!12, !10, i64 1000}
!12 = !{!"rcutils_error_state_s", !7, i64 0, !7, i64 768, !10, i64 1000}
!13 = !{!14, !14, i64 0}
!14 = !{!"_Bool", !7, i64 0}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = !{i64 0, i64 768, !18, i64 768, i64 229, !18, i64 1000, i64 8, !9}
!18 = !{!7, !7, i64 0}
!19 = !{i64 0, i64 1024, !18}


; 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
@stderr = external local_unnamed_addr global ptr, align 8
@.str.3 = external hidden unnamed_addr constant [113 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [112 x 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.5 = 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: nofree nounwind
declare noundef i64 @fwrite(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @rcutils_set_error_state(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #2 {
  %4 = alloca %struct.rcutils_error_state_s, align 8
  %5 = alloca [4096 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 1008, ptr nonnull %4) #6
  %6 = icmp eq ptr %0, null
  br i1 %6, label %7, label %10

7:                                                ; preds = %3
  %8 = load ptr, ptr @stderr, align 8, !tbaa !5
  %9 = tail call i64 @fwrite(ptr noundef nonnull @.str.3, i64 noundef 1, i64 noundef 112, ptr noundef %8) #7
  br label %36

10:                                               ; preds = %3
  %11 = icmp eq ptr %1, null
  br i1 %11, label %12, label %15

12:                                               ; preds = %10
  %13 = load ptr, ptr @stderr, align 8, !tbaa !5
  %14 = tail call i64 @fwrite(ptr noundef nonnull @.str.4, i64 noundef 1, i64 noundef 111, ptr noundef %13) #7
  br label %36

15:                                               ; preds = %10
  %16 = call i64 @__rcutils_copy_string(ptr noundef nonnull %4, i64 noundef 768, ptr noundef nonnull %0)
  %17 = getelementptr inbounds i8, ptr %4, i64 768
  %18 = call i64 @__rcutils_copy_string(ptr noundef nonnull %17, i64 noundef 229, ptr noundef nonnull %1)
  %19 = getelementptr inbounds i8, ptr %4, i64 1000
  store i64 %2, ptr %19, align 8, !tbaa !9
  %20 = call i64 @strnlen(ptr noundef nonnull dereferenceable(1) %0, i64 noundef 1024) #8
  %21 = call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @gtls_rcutils_error_is_set)
  %22 = load i8, ptr %21, align 1, !tbaa !12, !range !14, !noundef !15
  %23 = trunc nuw i8 %22 to i1
  %24 = call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @gtls_rcutils_error_string)
  br i1 %23, label %25, label %._crit_edge

25:                                               ; preds = %15
  %26 = call zeroext i1 @__same_string(ptr noundef nonnull %0, ptr noundef nonnull %24, i64 noundef %20)
  br i1 %26, label %._crit_edge, label %27

27:                                               ; preds = %25
  %28 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @gtls_rcutils_error_state)
  %29 = call zeroext i1 @__same_string(ptr noundef nonnull %0, ptr noundef nonnull %28, i64 noundef %20)
  br i1 %29, label %._crit_edge, label %30

30:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 4096, ptr nonnull %5) #6
  call void @__format_overwriting_error_state_message(ptr noundef nonnull %5, i64 noundef 4096, ptr noundef nonnull %4)
  %31 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %5) #8
  %32 = load ptr, ptr @stderr, align 8, !tbaa !5
  %33 = call i64 @fwrite(ptr noundef nonnull %5, i64 noundef 1, i64 noundef %31, ptr noundef %32) #7
  call void @llvm.lifetime.end.p0(i64 4096, ptr nonnull %5) #6
  br label %._crit_edge

._crit_edge:                                      ; preds = %15, %30, %27, %25
  %34 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @gtls_rcutils_error_state)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(1008) %34, ptr noundef nonnull align 8 dereferenceable(1008) %4, i64 1008, i1 false), !tbaa.struct !16
  %35 = call align 1 ptr @llvm.threadlocal.address.p0(ptr align 1 @gtls_rcutils_error_string_is_formatted)
  store i8 0, ptr %35, align 1, !tbaa !12
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(1024) %24, ptr noundef nonnull align 1 dereferenceable(1024) @.str.5, i64 1024, i1 false), !tbaa.struct !19
  store i8 1, ptr %21, align 1, !tbaa !12
  br label %36

36:                                               ; preds = %._crit_edge, %12, %7
  call void @llvm.lifetime.end.p0(i64 1008, ptr nonnull %4) #6
  ret void
}

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

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

; Function Attrs: nounwind uwtable
declare hidden i64 @__rcutils_copy_string(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strnlen(ptr nocapture noundef, i64 noundef) local_unnamed_addr #4

; Function Attrs: nounwind uwtable
declare hidden zeroext i1 @__same_string(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden void @__format_overwriting_error_state_message(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #4

; 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) #5

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

!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, !11, i64 1000}
!10 = !{!"rcutils_error_state_s", !7, i64 0, !7, i64 768, !11, i64 1000}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"_Bool", !7, i64 0}
!14 = !{i8 0, i8 2}
!15 = !{}
!16 = !{i64 0, i64 768, !17, i64 768, i64 229, !17, i64 1000, i64 8, !18}
!17 = !{!7, !7, i64 0}
!18 = !{!11, !11, i64 0}
!19 = !{i64 0, i64 1024, !17}


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