; LLVM IR from chdb-io/chdb :: base/poco/Foundation/src/pcre_ord2utf8.ll (pcre_ord2utf)

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

@_pcre_utf8_table1_size = external constant i32, align 4
@_pcre_utf8_table1 = external constant [0 x i32], align 4
@_pcre_utf8_table2 = external constant [0 x i32], align 4

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

7:                                                ; preds = %20, %2
  %8 = load i32, ptr %5, align 4, !tbaa !5
  %9 = load i32, ptr @_pcre_utf8_table1_size, align 4, !tbaa !5
  %10 = icmp slt i32 %8, %9
  br i1 %10, label %11, label %23

11:                                               ; preds = %7
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [0 x i32], ptr @_pcre_utf8_table1, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = icmp sle i32 %12, %16
  br i1 %17, label %18, label %19

18:                                               ; preds = %11
  br label %23

19:                                               ; preds = %11
  br label %20

20:                                               ; preds = %19
  %21 = load i32, ptr %5, align 4, !tbaa !5
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr %5, align 4, !tbaa !5
  br label %7, !llvm.loop !11

23:                                               ; preds = %18, %7
  %24 = load i32, ptr %5, align 4, !tbaa !5
  %25 = load ptr, ptr %4, align 8, !tbaa !9
  %26 = sext i32 %24 to i64
  %27 = getelementptr inbounds i8, ptr %25, i64 %26
  store ptr %27, ptr %4, align 8, !tbaa !9
  %28 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %28, ptr %6, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %41, %23
  %30 = load i32, ptr %6, align 4, !tbaa !5
  %31 = icmp sgt i32 %30, 0
  br i1 %31, label %32, label %44

32:                                               ; preds = %29
  %33 = load i32, ptr %3, align 4, !tbaa !5
  %34 = and i32 %33, 63
  %35 = or i32 128, %34
  %36 = trunc i32 %35 to i8
  %37 = load ptr, ptr %4, align 8, !tbaa !9
  %38 = getelementptr inbounds i8, ptr %37, i32 -1
  store ptr %38, ptr %4, align 8, !tbaa !9
  store i8 %36, ptr %37, align 1, !tbaa !13
  %39 = load i32, ptr %3, align 4, !tbaa !5
  %40 = lshr i32 %39, 6
  store i32 %40, ptr %3, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %32
  %42 = load i32, ptr %6, align 4, !tbaa !5
  %43 = add nsw i32 %42, -1
  store i32 %43, ptr %6, align 4, !tbaa !5
  br label %29, !llvm.loop !14

44:                                               ; preds = %29
  %45 = load i32, ptr %5, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [0 x i32], ptr @_pcre_utf8_table2, i64 0, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !5
  %49 = load i32, ptr %3, align 4, !tbaa !5
  %50 = or i32 %48, %49
  %51 = trunc i32 %50 to i8
  %52 = load ptr, ptr %4, align 8, !tbaa !9
  store i8 %51, ptr %52, align 1, !tbaa !13
  %53 = load i32, ptr %5, align 4, !tbaa !5
  %54 = add nsw i32 %53, 1
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i32 %54
}

; 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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !12}


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

@_pcre_utf8_table1_size = external local_unnamed_addr constant i32, align 4
@_pcre_utf8_table1 = external local_unnamed_addr constant [0 x i32], align 4
@_pcre_utf8_table2 = external local_unnamed_addr constant [0 x i32], align 4

; Function Attrs: nofree norecurse nosync nounwind memory(write, inaccessiblemem: none) uwtable
define dso_local range(i32 -2147483647, -2147483648) i32 @_pcre_ord2utf(i32 noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #0 {
  %3 = load i32, ptr @_pcre_utf8_table1_size, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph.preheader, label %._crit_edge26

.lr.ph.preheader:                                 ; preds = %2
  %wide.trip.count = zext nneg i32 %3 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %7
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %7 ]
  %5 = getelementptr inbounds [0 x i32], ptr @_pcre_utf8_table1, i64 0, i64 %indvars.iv
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %.not = icmp slt i32 %6, %0
  br i1 %.not, label %7, label %._crit_edge.split.loop.exit34

7:                                                ; preds = %.lr.ph
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9

._crit_edge.split.loop.exit34:                    ; preds = %.lr.ph
  %8 = trunc nuw nsw i64 %indvars.iv to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %7, %._crit_edge.split.loop.exit34
  %.015.lcssa = phi i32 [ %8, %._crit_edge.split.loop.exit34 ], [ %3, %7 ]
  %9 = zext nneg i32 %.015.lcssa to i64
  %10 = getelementptr inbounds i8, ptr %1, i64 %9
  %11 = icmp sgt i32 %.015.lcssa, 0
  br i1 %11, label %.lr.ph25.preheader, label %._crit_edge26

.lr.ph25.preheader:                               ; preds = %._crit_edge
  %xtraiter = and i32 %.015.lcssa, 3
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph25.prol.loopexit, label %.lr.ph25.prol

.lr.ph25.prol:                                    ; preds = %.lr.ph25.preheader, %.lr.ph25.prol
  %.023.prol = phi i32 [ %17, %.lr.ph25.prol ], [ %.015.lcssa, %.lr.ph25.preheader ]
  %.01622.prol = phi ptr [ %15, %.lr.ph25.prol ], [ %10, %.lr.ph25.preheader ]
  %.01721.prol = phi i32 [ %16, %.lr.ph25.prol ], [ %0, %.lr.ph25.preheader ]
  %prol.iter = phi i32 [ %prol.iter.next, %.lr.ph25.prol ], [ 0, %.lr.ph25.preheader ]
  %12 = trunc i32 %.01721.prol to i8
  %13 = and i8 %12, 63
  %14 = or disjoint i8 %13, -128
  %15 = getelementptr inbounds i8, ptr %.01622.prol, i64 -1
  store i8 %14, ptr %.01622.prol, align 1, !tbaa !11
  %16 = lshr i32 %.01721.prol, 6
  %17 = add nsw i32 %.023.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 %.lr.ph25.prol.loopexit, label %.lr.ph25.prol, !llvm.loop !12

.lr.ph25.prol.loopexit:                           ; preds = %.lr.ph25.prol, %.lr.ph25.preheader
  %.023.unr = phi i32 [ %.015.lcssa, %.lr.ph25.preheader ], [ %17, %.lr.ph25.prol ]
  %.01622.unr = phi ptr [ %10, %.lr.ph25.preheader ], [ %15, %.lr.ph25.prol ]
  %.01721.unr = phi i32 [ %0, %.lr.ph25.preheader ], [ %16, %.lr.ph25.prol ]
  %.lcssa38.unr = phi ptr [ poison, %.lr.ph25.preheader ], [ %15, %.lr.ph25.prol ]
  %.lcssa.unr = phi i32 [ poison, %.lr.ph25.preheader ], [ %16, %.lr.ph25.prol ]
  %18 = icmp ult i32 %.015.lcssa, 4
  br i1 %18, label %._crit_edge26, label %.lr.ph25

.lr.ph25:                                         ; preds = %.lr.ph25.prol.loopexit, %.lr.ph25
  %.023 = phi i32 [ %39, %.lr.ph25 ], [ %.023.unr, %.lr.ph25.prol.loopexit ]
  %.01622 = phi ptr [ %37, %.lr.ph25 ], [ %.01622.unr, %.lr.ph25.prol.loopexit ]
  %.01721 = phi i32 [ %38, %.lr.ph25 ], [ %.01721.unr, %.lr.ph25.prol.loopexit ]
  %19 = trunc i32 %.01721 to i8
  %20 = and i8 %19, 63
  %21 = or disjoint i8 %20, -128
  %22 = getelementptr inbounds i8, ptr %.01622, i64 -1
  store i8 %21, ptr %.01622, align 1, !tbaa !11
  %23 = lshr i32 %.01721, 6
  %24 = trunc i32 %23 to i8
  %25 = and i8 %24, 63
  %26 = or disjoint i8 %25, -128
  %27 = getelementptr inbounds i8, ptr %.01622, i64 -2
  store i8 %26, ptr %22, align 1, !tbaa !11
  %28 = lshr i32 %.01721, 12
  %29 = trunc i32 %28 to i8
  %30 = and i8 %29, 63
  %31 = or disjoint i8 %30, -128
  %32 = getelementptr inbounds i8, ptr %.01622, i64 -3
  store i8 %31, ptr %27, align 1, !tbaa !11
  %33 = lshr i32 %.01721, 18
  %34 = trunc i32 %33 to i8
  %35 = and i8 %34, 63
  %36 = or disjoint i8 %35, -128
  %37 = getelementptr inbounds i8, ptr %.01622, i64 -4
  store i8 %36, ptr %32, align 1, !tbaa !11
  %38 = lshr i32 %.01721, 24
  %39 = add nsw i32 %.023, -4
  %40 = add i32 %.023, -5
  %41 = icmp ult i32 %40, -2
  br i1 %41, label %.lr.ph25, label %._crit_edge26, !llvm.loop !14

._crit_edge26:                                    ; preds = %.lr.ph25.prol.loopexit, %.lr.ph25, %2, %._crit_edge
  %42 = phi i64 [ %9, %._crit_edge ], [ 0, %2 ], [ %9, %.lr.ph25 ], [ %9, %.lr.ph25.prol.loopexit ]
  %.015.lcssa32 = phi i32 [ %.015.lcssa, %._crit_edge ], [ 0, %2 ], [ %.015.lcssa, %.lr.ph25 ], [ %.015.lcssa, %.lr.ph25.prol.loopexit ]
  %.017.lcssa = phi i32 [ %0, %._crit_edge ], [ %0, %2 ], [ %.lcssa.unr, %.lr.ph25.prol.loopexit ], [ %38, %.lr.ph25 ]
  %.016.lcssa = phi ptr [ %10, %._crit_edge ], [ %1, %2 ], [ %.lcssa38.unr, %.lr.ph25.prol.loopexit ], [ %37, %.lr.ph25 ]
  %43 = getelementptr inbounds [0 x i32], ptr @_pcre_utf8_table2, i64 0, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !5
  %45 = or i32 %44, %.017.lcssa
  %46 = trunc i32 %45 to i8
  store i8 %46, ptr %.016.lcssa, align 1, !tbaa !11
  %47 = add nuw nsw i32 %.015.lcssa32, 1
  ret i32 %47
}

attributes #0 = { nofree norecurse nosync nounwind memory(write, inaccessiblemem: 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.unroll.disable"}
!14 = distinct !{!14, !10}


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