; LLVM IR from chdb-io/chdb :: base/glibc-compatibility/musl (strsignal)

; 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"

@strings = external hidden constant [671 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local ptr @strsignal(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  store ptr @strings, ptr %3, align 8, !tbaa !9
  %4 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %4, ptr %2, align 4, !tbaa !5
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = sub i32 %5, 1
  %7 = icmp uge i32 %6, 64
  br i1 %7, label %8, label %9

8:                                                ; preds = %1
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %8, %1
  br label %10

10:                                               ; preds = %24, %9
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = add nsw i32 %11, -1
  store i32 %12, ptr %2, align 4, !tbaa !5
  %13 = icmp ne i32 %11, 0
  br i1 %13, label %14, label %27

14:                                               ; preds = %10
  br label %15

15:                                               ; preds = %20, %14
  %16 = load ptr, ptr %3, align 8, !tbaa !9
  %17 = load i8, ptr %16, align 1, !tbaa !11
  %18 = icmp ne i8 %17, 0
  br i1 %18, label %19, label %23

19:                                               ; preds = %15
  br label %20

20:                                               ; preds = %19
  %21 = load ptr, ptr %3, align 8, !tbaa !9
  %22 = getelementptr inbounds i8, ptr %21, i32 1
  store ptr %22, ptr %3, align 8, !tbaa !9
  br label %15, !llvm.loop !12

23:                                               ; preds = %15
  br label %24

24:                                               ; preds = %23
  %25 = load ptr, ptr %3, align 8, !tbaa !9
  %26 = getelementptr inbounds i8, ptr %25, i32 1
  store ptr %26, ptr %3, align 8, !tbaa !9
  br label %10, !llvm.loop !14

27:                                               ; preds = %10
  %28 = load ptr, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret ptr %28
}

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

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

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 willreturn memory(argmem: readwrite) }
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 = !{!"int", !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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


; 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"

@strings = external hidden constant [671 x i8], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(none) uwtable
define dso_local nonnull ptr @strsignal(i32 noundef %0) local_unnamed_addr #0 {
  %2 = add i32 %0, -65
  %3 = icmp ult i32 %2, -64
  br i1 %3, label %select.unfold._crit_edge, label %.preheader.preheader

.preheader.preheader:                             ; preds = %1
  %4 = add nsw i32 %0, -1
  %xtraiter = and i32 %0, 3
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader.prol.loopexit, label %.preheader.prol

.preheader.prol:                                  ; preds = %.preheader.preheader, %select.unfold.prol
  %.in.prol = phi i32 [ %8, %select.unfold.prol ], [ %0, %.preheader.preheader ]
  %.09.prol = phi ptr [ %7, %select.unfold.prol ], [ @strings, %.preheader.preheader ]
  %prol.iter = phi i32 [ %prol.iter.next, %select.unfold.prol ], [ 0, %.preheader.preheader ]
  br label %5

5:                                                ; preds = %5, %.preheader.prol
  %.1.prol = phi ptr [ %7, %5 ], [ %.09.prol, %.preheader.prol ]
  %6 = load i8, ptr %.1.prol, align 1, !tbaa !5
  %.not7.prol = icmp eq i8 %6, 0
  %7 = getelementptr inbounds i8, ptr %.1.prol, i64 1
  br i1 %.not7.prol, label %select.unfold.prol, label %5, !llvm.loop !8

select.unfold.prol:                               ; preds = %5
  %8 = add nsw i32 %.in.prol, -1
  %prol.iter.next = add i32 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i32 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.preheader.prol.loopexit, label %.preheader.prol, !llvm.loop !10

.preheader.prol.loopexit:                         ; preds = %select.unfold.prol, %.preheader.preheader
  %.lcssa.lcssa.unr = phi ptr [ poison, %.preheader.preheader ], [ %7, %select.unfold.prol ]
  %.in.unr = phi i32 [ %0, %.preheader.preheader ], [ %8, %select.unfold.prol ]
  %.09.unr = phi ptr [ @strings, %.preheader.preheader ], [ %7, %select.unfold.prol ]
  %9 = icmp ult i32 %4, 3
  br i1 %9, label %select.unfold._crit_edge, label %.preheader

.preheader:                                       ; preds = %.preheader.prol.loopexit, %select.unfold.3
  %.in = phi i32 [ %19, %select.unfold.3 ], [ %.in.unr, %.preheader.prol.loopexit ]
  %.09 = phi ptr [ %18, %select.unfold.3 ], [ %.09.unr, %.preheader.prol.loopexit ]
  br label %10

10:                                               ; preds = %10, %.preheader
  %.1 = phi ptr [ %12, %10 ], [ %.09, %.preheader ]
  %11 = load i8, ptr %.1, align 1, !tbaa !5
  %.not7 = icmp eq i8 %11, 0
  %12 = getelementptr inbounds i8, ptr %.1, i64 1
  br i1 %.not7, label %select.unfold, label %10, !llvm.loop !8

select.unfold:                                    ; preds = %10, %select.unfold
  %.1.1 = phi ptr [ %14, %select.unfold ], [ %12, %10 ]
  %13 = load i8, ptr %.1.1, align 1, !tbaa !5
  %.not7.1 = icmp eq i8 %13, 0
  %14 = getelementptr inbounds i8, ptr %.1.1, i64 1
  br i1 %.not7.1, label %select.unfold.1, label %select.unfold, !llvm.loop !8

select.unfold.1:                                  ; preds = %select.unfold, %select.unfold.1
  %.1.2 = phi ptr [ %16, %select.unfold.1 ], [ %14, %select.unfold ]
  %15 = load i8, ptr %.1.2, align 1, !tbaa !5
  %.not7.2 = icmp eq i8 %15, 0
  %16 = getelementptr inbounds i8, ptr %.1.2, i64 1
  br i1 %.not7.2, label %select.unfold.2, label %select.unfold.1, !llvm.loop !8

select.unfold.2:                                  ; preds = %select.unfold.1, %select.unfold.2
  %.1.3 = phi ptr [ %18, %select.unfold.2 ], [ %16, %select.unfold.1 ]
  %17 = load i8, ptr %.1.3, align 1, !tbaa !5
  %.not7.3 = icmp eq i8 %17, 0
  %18 = getelementptr inbounds i8, ptr %.1.3, i64 1
  br i1 %.not7.3, label %select.unfold.3, label %select.unfold.2, !llvm.loop !8

select.unfold.3:                                  ; preds = %select.unfold.2
  %19 = add nsw i32 %.in, -4
  %.not.3 = icmp eq i32 %19, 0
  br i1 %.not.3, label %select.unfold._crit_edge, label %.preheader, !llvm.loop !12

select.unfold._crit_edge:                         ; preds = %.preheader.prol.loopexit, %select.unfold.3, %1
  %.0.lcssa = phi ptr [ @strings, %1 ], [ %.lcssa.lcssa.unr, %.preheader.prol.loopexit ], [ %18, %select.unfold.3 ]
  ret ptr %.0.lcssa
}

attributes #0 = { nofree norecurse nosync nounwind memory(none) 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" }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.unroll.disable"}
!12 = distinct !{!12, !9}


; Effective O3 passes: GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass