; LLVM IR from chdb-io/chdb :: base/poco/Foundation/src (pcre_refcount)

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

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

11:                                               ; preds = %2
  store i32 -2, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %62

12:                                               ; preds = %2
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.real_pcre8_or_16, ptr %13, i32 0, i32 0
  %15 = load i32, ptr %14, align 8, !tbaa !11
  %16 = zext i32 %15 to i64
  %17 = icmp ne i64 %16, 1346589253
  br i1 %17, label %18, label %19

18:                                               ; preds = %12
  store i32 -4, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %62

19:                                               ; preds = %12
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.real_pcre8_or_16, ptr %20, i32 0, i32 3
  %22 = load i32, ptr %21, align 4, !tbaa !14
  %23 = and i32 %22, 1
  %24 = icmp eq i32 %23, 0
  br i1 %24, label %25, label %26

25:                                               ; preds = %19
  store i32 -28, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %62

26:                                               ; preds = %19
  %27 = load i32, ptr %5, align 4, !tbaa !9
  %28 = sub nsw i32 0, %27
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.real_pcre8_or_16, ptr %29, i32 0, i32 14
  %31 = load i16, ptr %30, align 8, !tbaa !15
  %32 = zext i16 %31 to i32
  %33 = icmp sgt i32 %28, %32
  br i1 %33, label %34, label %35

34:                                               ; preds = %26
  br label %53

35:                                               ; preds = %26
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.real_pcre8_or_16, ptr %37, i32 0, i32 14
  %39 = load i16, ptr %38, align 8, !tbaa !15
  %40 = zext i16 %39 to i32
  %41 = add nsw i32 %36, %40
  %42 = icmp sgt i32 %41, 65535
  br i1 %42, label %43, label %44

43:                                               ; preds = %35
  br label %51

44:                                               ; preds = %35
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.real_pcre8_or_16, ptr %45, i32 0, i32 14
  %47 = load i16, ptr %46, align 8, !tbaa !15
  %48 = zext i16 %47 to i32
  %49 = load i32, ptr %5, align 4, !tbaa !9
  %50 = add nsw i32 %48, %49
  br label %51

51:                                               ; preds = %44, %43
  %52 = phi i32 [ 65535, %43 ], [ %50, %44 ]
  br label %53

53:                                               ; preds = %51, %34
  %54 = phi i32 [ 0, %34 ], [ %52, %51 ]
  %55 = trunc i32 %54 to i16
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.real_pcre8_or_16, ptr %56, i32 0, i32 14
  store i16 %55, ptr %57, align 8, !tbaa !15
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.real_pcre8_or_16, ptr %58, i32 0, i32 14
  %60 = load i16, ptr %59, align 8, !tbaa !15
  %61 = zext i16 %60 to i32
  store i32 %61, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %62

62:                                               ; preds = %53, %25, %18, %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %63 = load i32, ptr %3, align 4
  ret i32 %63
}

; 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 = !{!"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, !10, i64 0}
!12 = !{!"real_pcre8_or_16", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !13, i64 24, !13, i64 26, !13, i64 28, !13, i64 30, !13, i64 32, !13, i64 34, !13, i64 36, !13, i64 38, !13, i64 40, !13, i64 42, !13, i64 44, !13, i64 46, !6, i64 48, !6, i64 56}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !10, i64 12}
!15 = !{!12, !13, i64 40}


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

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) uwtable
define dso_local range(i32 -28, 65536) i32 @pcre_refcount(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %20, label %4

4:                                                ; preds = %2
  %5 = load i32, ptr %0, align 8, !tbaa !5
  %.not = icmp eq i32 %5, 1346589253
  br i1 %.not, label %6, label %20

6:                                                ; preds = %4
  %7 = getelementptr inbounds i8, ptr %0, i64 12
  %8 = load i32, ptr %7, align 4, !tbaa !12
  %9 = and i32 %8, 1
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %20, label %11

11:                                               ; preds = %6
  %12 = sub nsw i32 0, %1
  %13 = getelementptr inbounds i8, ptr %0, i64 40
  %14 = load i16, ptr %13, align 8, !tbaa !13
  %15 = zext i16 %14 to i32
  %16 = icmp sgt i32 %12, %15
  %17 = add nsw i32 %15, %1
  %spec.select15 = tail call i32 @llvm.smin.i32(i32 %17, i32 65535)
  %spec.select = trunc i32 %spec.select15 to i16
  %18 = select i1 %16, i16 0, i16 %spec.select
  store i16 %18, ptr %13, align 8, !tbaa !13
  %19 = zext i16 %18 to i32
  br label %20

20:                                               ; preds = %6, %4, %2, %11
  %.0 = phi i32 [ %19, %11 ], [ -2, %2 ], [ -4, %4 ], [ -28, %6 ]
  ret i32 %.0
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) 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 speculatable willreturn memory(none) }

!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 = !{!"real_pcre8_or_16", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !7, i64 20, !10, i64 24, !10, i64 26, !10, i64 28, !10, i64 30, !10, i64 32, !10, i64 34, !10, i64 36, !10, i64 38, !10, i64 40, !10, i64 42, !10, i64 44, !10, i64 46, !11, i64 48, !11, i64 56}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !7, i64 12}
!13 = !{!6, !10, i64 40}


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