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

; 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.__va_list_tag = type { i32, i32, ptr, ptr }

; Function Attrs: nounwind uwtable
define dso_local ptr @rcutils_format_string_limit(ptr noundef byval(%struct.rcutils_allocator_s) align 8 %0, i64 noundef %1, ptr noundef %2, ...) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca [1 x %struct.__va_list_tag], align 16
  %8 = alloca [1 x %struct.__va_list_tag], align 16
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  store i64 %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !9
  %13 = load ptr, ptr %6, align 8, !tbaa !9
  %14 = icmp eq ptr null, %13
  br i1 %14, label %15, label %16

15:                                               ; preds = %3
  store ptr null, ptr %4, align 8
  br label %77

16:                                               ; preds = %3
  %17 = call zeroext i1 @rcutils_allocator_is_valid(ptr noundef %0)
  br i1 %17, label %19, label %18

18:                                               ; preds = %16
  store ptr null, ptr %4, align 8
  br label %77

19:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 24, ptr %7) #4
  %20 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %7, i64 0, i64 0
  call void @llvm.va_start.p0(ptr %20)
  call void @llvm.lifetime.start.p0(i64 24, ptr %8) #4
  %21 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %8, i64 0, i64 0
  %22 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %7, i64 0, i64 0
  call void @llvm.va_copy.p0(ptr %21, ptr %22)
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %23 = load ptr, ptr %6, align 8, !tbaa !9
  %24 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %7, i64 0, i64 0
  %25 = call i32 @rcutils_vsnprintf(ptr noundef null, i64 noundef 0, ptr noundef %23, ptr noundef %24)
  %26 = sext i32 %25 to i64
  store i64 %26, ptr %9, align 8, !tbaa !5
  %27 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %7, i64 0, i64 0
  call void @llvm.va_end.p0(ptr %27)
  %28 = load i64, ptr %9, align 8, !tbaa !5
  %29 = icmp eq i64 %28, -1
  br i1 %29, label %30, label %32

30:                                               ; preds = %19
  %31 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %8, i64 0, i64 0
  call void @llvm.va_end.p0(ptr %31)
  store ptr null, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %76

32:                                               ; preds = %19
  %33 = load i64, ptr %9, align 8, !tbaa !5
  %34 = add i64 %33, 1
  %35 = load i64, ptr %5, align 8, !tbaa !5
  %36 = icmp ugt i64 %34, %35
  br i1 %36, label %37, label %40

37:                                               ; preds = %32
  %38 = load i64, ptr %5, align 8, !tbaa !5
  %39 = sub i64 %38, 1
  store i64 %39, ptr %9, align 8, !tbaa !5
  br label %40

40:                                               ; preds = %37, %32
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %41 = getelementptr inbounds %struct.rcutils_allocator_s, ptr %0, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !11
  %43 = load i64, ptr %9, align 8, !tbaa !5
  %44 = add i64 %43, 1
  %45 = getelementptr inbounds %struct.rcutils_allocator_s, ptr %0, i32 0, i32 4
  %46 = load ptr, ptr %45, align 8, !tbaa !13
  %47 = call ptr %42(i64 noundef %44, ptr noundef %46)
  store ptr %47, ptr %11, align 8, !tbaa !9
  %48 = load ptr, ptr %11, align 8, !tbaa !9
  %49 = icmp eq ptr null, %48
  br i1 %49, label %50, label %52

50:                                               ; preds = %40
  %51 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %8, i64 0, i64 0
  call void @llvm.va_end.p0(ptr %51)
  store ptr null, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %75

52:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %53 = load ptr, ptr %11, align 8, !tbaa !9
  %54 = load i64, ptr %9, align 8, !tbaa !5
  %55 = add i64 %54, 1
  %56 = load ptr, ptr %6, align 8, !tbaa !9
  %57 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %8, i64 0, i64 0
  %58 = call i32 @rcutils_vsnprintf(ptr noundef %53, i64 noundef %55, ptr noundef %56, ptr noundef %57)
  store i32 %58, ptr %12, align 4, !tbaa !14
  %59 = load i32, ptr %12, align 4, !tbaa !14
  %60 = icmp sgt i32 0, %59
  br i1 %60, label %61, label %68

61:                                               ; preds = %52
  %62 = getelementptr inbounds %struct.rcutils_allocator_s, ptr %0, i32 0, i32 1
  %63 = load ptr, ptr %62, align 8, !tbaa !16
  %64 = load ptr, ptr %11, align 8, !tbaa !9
  %65 = getelementptr inbounds %struct.rcutils_allocator_s, ptr %0, i32 0, i32 4
  %66 = load ptr, ptr %65, align 8, !tbaa !13
  call void %63(ptr noundef %64, ptr noundef %66)
  %67 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %8, i64 0, i64 0
  call void @llvm.va_end.p0(ptr %67)
  store ptr null, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %74

68:                                               ; preds = %52
  %69 = load ptr, ptr %11, align 8, !tbaa !9
  %70 = load i64, ptr %9, align 8, !tbaa !5
  %71 = getelementptr inbounds i8, ptr %69, i64 %70
  store i8 0, ptr %71, align 1, !tbaa !17
  %72 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %8, i64 0, i64 0
  call void @llvm.va_end.p0(ptr %72)
  %73 = load ptr, ptr %11, align 8, !tbaa !9
  store ptr %73, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %74

74:                                               ; preds = %68, %61
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  br label %75

75:                                               ; preds = %74, %50
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  br label %76

76:                                               ; preds = %75, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 24, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 24, ptr %7) #4
  br label %77

77:                                               ; preds = %76, %18, %15
  %78 = load ptr, ptr %4, align 8
  ret ptr %78
}

declare zeroext i1 @rcutils_allocator_is_valid(ptr noundef) #1

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.va_copy.p0(ptr, ptr) #3

declare i32 @rcutils_vsnprintf(ptr noundef, i64 noundef, ptr noundef, ptr noundef) #1

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

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

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 = { "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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nosync nounwind willreturn }
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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"rcutils_allocator_s", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!13 = !{!12, !10, i64 32}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = !{!12, !10, i64 8}
!17 = !{!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_allocator_s = type { ptr, ptr, ptr, ptr, ptr }
%struct.__va_list_tag = type { i32, i32, ptr, ptr }

; Function Attrs: nounwind uwtable
define dso_local ptr @rcutils_format_string_limit(ptr noundef byval(%struct.rcutils_allocator_s) align 8 %0, i64 noundef %1, ptr noundef %2, ...) local_unnamed_addr #0 {
  %4 = alloca [1 x %struct.__va_list_tag], align 16
  %5 = alloca [1 x %struct.__va_list_tag], align 16
  %6 = icmp eq ptr %2, null
  br i1 %6, label %33, label %7

7:                                                ; preds = %3
  %8 = call zeroext i1 @rcutils_allocator_is_valid(ptr noundef nonnull %0) #4
  br i1 %8, label %9, label %33

9:                                                ; preds = %7
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %4) #4
  call void @llvm.va_start.p0(ptr nonnull %4)
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %5) #4
  call void @llvm.va_copy.p0(ptr nonnull %5, ptr nonnull %4)
  %10 = call i32 @rcutils_vsnprintf(ptr noundef null, i64 noundef 0, ptr noundef nonnull %2, ptr noundef nonnull %4) #4
  call void @llvm.va_end.p0(ptr nonnull %4)
  %11 = icmp eq i32 %10, -1
  br i1 %11, label %32, label %12

12:                                               ; preds = %9
  %13 = sext i32 %10 to i64
  %14 = add nsw i64 %13, 1
  %15 = icmp ugt i64 %14, %1
  %16 = add i64 %1, -1
  %spec.select = select i1 %15, i64 %16, i64 %13
  %17 = load ptr, ptr %0, align 8, !tbaa !5
  %18 = add i64 %spec.select, 1
  %19 = getelementptr inbounds i8, ptr %0, i64 32
  %20 = load ptr, ptr %19, align 8, !tbaa !10
  %21 = call ptr %17(i64 noundef %18, ptr noundef %20) #4
  %22 = icmp eq ptr %21, null
  br i1 %22, label %32, label %23

23:                                               ; preds = %12
  %24 = call i32 @rcutils_vsnprintf(ptr noundef nonnull %21, i64 noundef %18, ptr noundef nonnull %2, ptr noundef nonnull %5) #4
  %25 = icmp slt i32 %24, 0
  br i1 %25, label %26, label %30

26:                                               ; preds = %23
  %27 = getelementptr inbounds i8, ptr %0, i64 8
  %28 = load ptr, ptr %27, align 8, !tbaa !11
  %29 = load ptr, ptr %19, align 8, !tbaa !10
  call void %28(ptr noundef nonnull %21, ptr noundef %29) #4
  br label %32

30:                                               ; preds = %23
  %31 = getelementptr inbounds i8, ptr %21, i64 %spec.select
  store i8 0, ptr %31, align 1, !tbaa !12
  br label %32

32:                                               ; preds = %12, %9, %30, %26
  %.2 = phi ptr [ null, %26 ], [ %21, %30 ], [ null, %9 ], [ null, %12 ]
  call void @llvm.va_end.p0(ptr nonnull %5)
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %5) #4
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %4) #4
  br label %33

33:                                               ; preds = %7, %3, %32
  %.3 = phi ptr [ %.2, %32 ], [ null, %3 ], [ null, %7 ]
  ret ptr %.3
}

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

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

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.va_start.p0(ptr) #3

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.va_copy.p0(ptr, ptr) #3

declare i32 @rcutils_vsnprintf(ptr noundef, i64 noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.va_end.p0(ptr) #3

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

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 = { "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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { mustprogress nocallback nofree nosync nounwind willreturn }
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, !7, i64 0}
!6 = !{!"rcutils_allocator_s", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 32}
!11 = !{!6, !7, i64 8}
!12 = !{!8, !8, i64 0}


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