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

; 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.log_msg_part_s = type { ptr, i64, i64 }
%struct.logging_input_s = type { ptr, ptr, ptr, i32, i64 }

@g_num_log_msg_handlers = external hidden global i64, align 8
@g_handlers = external hidden global [1024 x %struct.log_msg_part_s], align 16

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @rcutils_logging_format_message(ptr noundef %0, i32 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, ptr noundef %5) #1 {
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca %struct.logging_input_s, align 8
  %15 = alloca i64, align 8
  %16 = alloca i32, align 4
  store ptr %0, ptr %8, align 8, !tbaa !5
  store i32 %1, ptr %9, align 4, !tbaa !9
  store ptr %2, ptr %10, align 8, !tbaa !5
  store i64 %3, ptr %11, align 8, !tbaa !11
  store ptr %4, ptr %12, align 8, !tbaa !5
  store ptr %5, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 40, ptr %14) #2
  %17 = getelementptr inbounds %struct.logging_input_s, ptr %14, i32 0, i32 0
  %18 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %18, ptr %17, align 8, !tbaa !13
  %19 = getelementptr inbounds %struct.logging_input_s, ptr %14, i32 0, i32 1
  %20 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %20, ptr %19, align 8, !tbaa !15
  %21 = getelementptr inbounds %struct.logging_input_s, ptr %14, i32 0, i32 2
  %22 = load ptr, ptr %12, align 8, !tbaa !5
  store ptr %22, ptr %21, align 8, !tbaa !16
  %23 = getelementptr inbounds %struct.logging_input_s, ptr %14, i32 0, i32 3
  %24 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %24, ptr %23, align 8, !tbaa !17
  %25 = getelementptr inbounds %struct.logging_input_s, ptr %14, i32 0, i32 4
  %26 = load i64, ptr %11, align 8, !tbaa !11
  store i64 %26, ptr %25, align 8, !tbaa !18
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #2
  store i64 0, ptr %15, align 8, !tbaa !11
  br label %27

27:                                               ; preds = %50, %6
  %28 = load i64, ptr %15, align 8, !tbaa !11
  %29 = load i64, ptr @g_num_log_msg_handlers, align 8, !tbaa !11
  %30 = icmp ult i64 %28, %29
  br i1 %30, label %32, label %31

31:                                               ; preds = %27
  store i32 2, ptr %16, align 4
  br label %53

32:                                               ; preds = %27
  %33 = load i64, ptr %15, align 8, !tbaa !11
  %34 = getelementptr inbounds [1024 x %struct.log_msg_part_s], ptr @g_handlers, i64 0, i64 %33
  %35 = getelementptr inbounds %struct.log_msg_part_s, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !19
  %37 = load ptr, ptr %13, align 8, !tbaa !5
  %38 = load i64, ptr %15, align 8, !tbaa !11
  %39 = getelementptr inbounds [1024 x %struct.log_msg_part_s], ptr @g_handlers, i64 0, i64 %38
  %40 = getelementptr inbounds %struct.log_msg_part_s, ptr %39, i32 0, i32 1
  %41 = load i64, ptr %40, align 8, !tbaa !21
  %42 = load i64, ptr %15, align 8, !tbaa !11
  %43 = getelementptr inbounds [1024 x %struct.log_msg_part_s], ptr @g_handlers, i64 0, i64 %42
  %44 = getelementptr inbounds %struct.log_msg_part_s, ptr %43, i32 0, i32 2
  %45 = load i64, ptr %44, align 8, !tbaa !22
  %46 = call ptr %36(ptr noundef %14, ptr noundef %37, i64 noundef %41, i64 noundef %45)
  %47 = icmp eq ptr %46, null
  br i1 %47, label %48, label %49

48:                                               ; preds = %32
  store i32 2, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %53

49:                                               ; preds = %32
  br label %50

50:                                               ; preds = %49
  %51 = load i64, ptr %15, align 8, !tbaa !11
  %52 = add i64 %51, 1
  store i64 %52, ptr %15, align 8, !tbaa !11
  br label %27, !llvm.loop !23

53:                                               ; preds = %48, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #2
  %54 = load i32, ptr %16, align 4
  switch i32 %54, label %56 [
    i32 2, label %55
  ]

55:                                               ; preds = %53
  store i32 0, ptr %7, align 4
  store i32 1, ptr %16, align 4
  br label %56

56:                                               ; preds = %55, %53
  call void @llvm.lifetime.end.p0(i64 40, ptr %14) #2
  %57 = load i32, ptr %7, align 4
  ret i32 %57
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"logging_input_s", !6, i64 0, !6, i64 8, !6, i64 16, !10, i64 24, !12, i64 32}
!15 = !{!14, !6, i64 8}
!16 = !{!14, !6, i64 16}
!17 = !{!14, !10, i64 24}
!18 = !{!14, !12, i64 32}
!19 = !{!20, !6, i64 0}
!20 = !{!"log_msg_part_s", !6, i64 0, !12, i64 8, !12, i64 16}
!21 = !{!20, !12, i64 8}
!22 = !{!20, !12, i64 16}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}


; 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.log_msg_part_s = type { ptr, i64, i64 }
%struct.logging_input_s = type { ptr, ptr, ptr, i32, i64 }

@g_num_log_msg_handlers = external hidden local_unnamed_addr global i64, align 8
@g_handlers = external hidden local_unnamed_addr global [1024 x %struct.log_msg_part_s], align 16

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 3) i32 @rcutils_logging_format_message(ptr noundef %0, i32 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, ptr noundef %5) local_unnamed_addr #1 {
  %7 = alloca %struct.logging_input_s, align 8
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %7) #2
  store ptr %2, ptr %7, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %7, i64 8
  store ptr %0, ptr %8, align 8, !tbaa !12
  %9 = getelementptr inbounds i8, ptr %7, i64 16
  store ptr %4, ptr %9, align 8, !tbaa !13
  %10 = getelementptr inbounds i8, ptr %7, i64 24
  store i32 %1, ptr %10, align 8, !tbaa !14
  %11 = getelementptr inbounds i8, ptr %7, i64 32
  store i64 %3, ptr %11, align 8, !tbaa !15
  %12 = load i64, ptr @g_num_log_msg_handlers, align 8, !tbaa !16
  %.not14.not = icmp eq i64 %12, 0
  br i1 %.not14.not, label %._crit_edge, label %.lr.ph

13:                                               ; preds = %.lr.ph
  %14 = add nuw i64 %.01215, 1
  %15 = load i64, ptr @g_num_log_msg_handlers, align 8, !tbaa !16
  %.not = icmp ult i64 %14, %15
  br i1 %.not, label %.lr.ph, label %._crit_edge, !llvm.loop !17

.lr.ph:                                           ; preds = %6, %13
  %.01215 = phi i64 [ %14, %13 ], [ 0, %6 ]
  %16 = getelementptr inbounds [1024 x %struct.log_msg_part_s], ptr @g_handlers, i64 0, i64 %.01215
  %17 = load ptr, ptr %16, align 8, !tbaa !19
  %18 = getelementptr inbounds i8, ptr %16, i64 8
  %19 = load i64, ptr %18, align 8, !tbaa !21
  %20 = getelementptr inbounds i8, ptr %16, i64 16
  %21 = load i64, ptr %20, align 8, !tbaa !22
  %22 = call ptr %17(ptr noundef nonnull %7, ptr noundef %5, i64 noundef %19, i64 noundef %21) #2
  %23 = icmp eq ptr %22, null
  br i1 %23, label %._crit_edge, label %13

._crit_edge:                                      ; preds = %13, %.lr.ph, %6
  %spec.select = phi i32 [ 0, %6 ], [ 2, %.lr.ph ], [ 0, %13 ]
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %7) #2
  ret i32 %spec.select
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{!"logging_input_s", !7, i64 0, !7, i64 8, !7, i64 16, !10, i64 24, !11, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!6, !7, i64 8}
!13 = !{!6, !7, i64 16}
!14 = !{!6, !10, i64 24}
!15 = !{!6, !11, i64 32}
!16 = !{!11, !11, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!20, !7, i64 0}
!20 = !{!"log_msg_part_s", !7, i64 0, !11, i64 8, !11, i64 16}
!21 = !{!20, !11, i64 8}
!22 = !{!20, !11, i64 16}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass