; LLVM IR from chdb-io/chdb :: base/poco/JSON/src (pdjson)

; 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.json_stream = type { i64, ptr, i64, i64, i32, i32, %struct.anon, i64, %struct.json_source, %struct.json_allocator, [128 x i8] }
%struct.anon = type { ptr, i64, i64 }
%struct.json_source = type { ptr, ptr, i64, %union.anon }
%union.anon = type { %struct.anon.2 }
%struct.anon.2 = type { ptr, ptr, ptr }
%struct.json_allocator = type { ptr, ptr, ptr }

@.str = external hidden unnamed_addr constant [15 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [38 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [35 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @read_escaped(ptr noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.json_stream, ptr %8, i32 0, i32 8
  %10 = getelementptr inbounds %struct.json_source, ptr %9, i32 0, i32 0
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.json_stream, ptr %12, i32 0, i32 8
  %14 = call i32 %11(ptr noundef %13)
  store i32 %14, ptr %4, align 4, !tbaa !16
  %15 = load i32, ptr %4, align 4, !tbaa !16
  %16 = icmp eq i32 %15, -1
  br i1 %16, label %17, label %36

17:                                               ; preds = %1
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.json_stream, ptr %18, i32 0, i32 5
  %20 = load i32, ptr %19, align 4, !tbaa !17
  %21 = and i32 %20, 1
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %35, label %23

23:                                               ; preds = %17
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.json_stream, ptr %24, i32 0, i32 5
  %26 = load i32, ptr %25, align 4, !tbaa !17
  %27 = or i32 %26, 1
  store i32 %27, ptr %25, align 4, !tbaa !17
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.json_stream, ptr %28, i32 0, i32 10
  %30 = getelementptr inbounds [128 x i8], ptr %29, i64 0, i64 0
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.json_stream, ptr %31, i32 0, i32 0
  %33 = load i64, ptr %32, align 8, !tbaa !18
  %34 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %30, i64 noundef 128, ptr noundef @.str, i64 noundef %33, ptr noundef @.str.1) #4
  br label %35

35:                                               ; preds = %23, %17
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %90

36:                                               ; preds = %1
  %37 = load i32, ptr %4, align 4, !tbaa !16
  %38 = icmp eq i32 %37, 117
  br i1 %38, label %39, label %45

39:                                               ; preds = %36
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = call i32 @read_unicode(ptr noundef %40)
  %42 = icmp ne i32 %41, 0
  br i1 %42, label %43, label %44

43:                                               ; preds = %39
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %90

44:                                               ; preds = %39
  br label %88

45:                                               ; preds = %36
  %46 = load i32, ptr %4, align 4, !tbaa !16
  switch i32 %46, label %67 [
    i32 92, label %47
    i32 98, label %47
    i32 102, label %47
    i32 110, label %47
    i32 114, label %47
    i32 116, label %47
    i32 47, label %47
    i32 34, label %47
  ]

47:                                               ; preds = %45, %45, %45, %45, %45, %45, %45, %45
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store ptr @.str.2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = load i32, ptr %4, align 4, !tbaa !16
  %50 = call ptr @strchr(ptr noundef %48, i32 noundef %49) #5
  store ptr %50, ptr %7, align 8, !tbaa !5
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = load ptr, ptr %7, align 8, !tbaa !5
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = ptrtoint ptr %52 to i64
  %55 = ptrtoint ptr %53 to i64
  %56 = sub i64 %54, %55
  %57 = getelementptr inbounds [9 x i8], ptr @.str.3, i64 0, i64 %56
  %58 = load i8, ptr %57, align 1, !tbaa !19
  %59 = sext i8 %58 to i32
  %60 = call i32 @pushchar(ptr noundef %51, i32 noundef %59)
  %61 = icmp ne i32 %60, 0
  br i1 %61, label %62, label %63

62:                                               ; preds = %47
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %64

63:                                               ; preds = %47
  store i32 0, ptr %5, align 4
  br label %64

64:                                               ; preds = %63, %62
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  %65 = load i32, ptr %5, align 4
  switch i32 %65, label %90 [
    i32 0, label %66
  ]

66:                                               ; preds = %64
  br label %87

67:                                               ; preds = %45
  %68 = load ptr, ptr %3, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.json_stream, ptr %68, i32 0, i32 5
  %70 = load i32, ptr %69, align 4, !tbaa !17
  %71 = and i32 %70, 1
  %72 = icmp ne i32 %71, 0
  br i1 %72, label %86, label %73

73:                                               ; preds = %67
  %74 = load ptr, ptr %3, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.json_stream, ptr %74, i32 0, i32 5
  %76 = load i32, ptr %75, align 4, !tbaa !17
  %77 = or i32 %76, 1
  store i32 %77, ptr %75, align 4, !tbaa !17
  %78 = load ptr, ptr %3, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.json_stream, ptr %78, i32 0, i32 10
  %80 = getelementptr inbounds [128 x i8], ptr %79, i64 0, i64 0
  %81 = load ptr, ptr %3, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.json_stream, ptr %81, i32 0, i32 0
  %83 = load i64, ptr %82, align 8, !tbaa !18
  %84 = load i32, ptr %4, align 4, !tbaa !16
  %85 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %80, i64 noundef 128, ptr noundef @.str.4, i64 noundef %83, i32 noundef %84) #4
  br label %86

86:                                               ; preds = %73, %67
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %90

87:                                               ; preds = %66
  br label %88

88:                                               ; preds = %87, %44
  br label %89

89:                                               ; preds = %88
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %90

90:                                               ; preds = %89, %86, %64, %43, %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  %91 = load i32, ptr %2, align 4
  ret i32 %91
}

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

; Function Attrs: nounwind
declare i32 @snprintf(ptr noundef, i64 noundef, ptr noundef, ...) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @read_unicode(ptr noundef) #0

; Function Attrs: nounwind willreturn memory(read)
declare ptr @strchr(ptr noundef, i32 noundef) #3

; Function Attrs: nounwind uwtable
declare hidden i32 @pushchar(ptr noundef, i32 noundef) #0

; 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 "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 #3 = { nounwind willreturn memory(read) "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 #4 = { nounwind }
attributes #5 = { nounwind willreturn memory(read) }

!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, !6, i64 72}
!10 = !{!"json_stream", !11, i64 0, !6, i64 8, !11, i64 16, !11, i64 24, !12, i64 32, !12, i64 36, !13, i64 40, !11, i64 64, !14, i64 72, !15, i64 120, !7, i64 144}
!11 = !{!"long", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!"", !6, i64 0, !11, i64 8, !11, i64 16}
!14 = !{!"json_source", !6, i64 0, !6, i64 8, !11, i64 16, !7, i64 24}
!15 = !{!"json_allocator", !6, i64 0, !6, i64 8, !6, i64 16}
!16 = !{!12, !12, i64 0}
!17 = !{!10, !12, i64 36}
!18 = !{!10, !11, i64 0}
!19 = !{!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"

@.str = external hidden unnamed_addr constant [15 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [38 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [35 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @read_escaped(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 72
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = tail call i32 %3(ptr noundef nonnull %2) #3
  switch i32 %4, label %24 [
    i32 -1, label %5
    i32 117, label %14
    i32 92, label %16
    i32 98, label %16
    i32 102, label %16
    i32 110, label %16
    i32 114, label %16
    i32 116, label %16
    i32 47, label %16
    i32 34, label %16
  ]

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 36
  %7 = load i32, ptr %6, align 4, !tbaa !15
  %8 = and i32 %7, 1
  %.not24 = icmp eq i32 %8, 0
  br i1 %.not24, label %9, label %34

9:                                                ; preds = %5
  %10 = or disjoint i32 %7, 1
  store i32 %10, ptr %6, align 4, !tbaa !15
  %11 = getelementptr inbounds i8, ptr %0, i64 144
  %12 = load i64, ptr %0, align 8, !tbaa !16
  %13 = tail call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef nonnull dereferenceable(1) %11, i64 noundef 128, ptr noundef nonnull @.str, i64 noundef %12, ptr noundef nonnull @.str.1) #3
  br label %34

14:                                               ; preds = %1
  %15 = tail call i32 @read_unicode(ptr noundef nonnull %0)
  %.not23 = icmp eq i32 %15, 0
  br i1 %.not23, label %33, label %34

16:                                               ; preds = %1, %1, %1, %1, %1, %1, %1, %1
  %17 = tail call ptr @strchr(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %4) #4
  %18 = ptrtoint ptr %17 to i64
  %19 = sub i64 %18, ptrtoint (ptr @.str.2 to i64)
  %20 = getelementptr inbounds [9 x i8], ptr @.str.3, i64 0, i64 %19
  %21 = load i8, ptr %20, align 1, !tbaa !17
  %22 = sext i8 %21 to i32
  %23 = tail call i32 @pushchar(ptr noundef nonnull %0, i32 noundef %22)
  %.not = icmp eq i32 %23, 0
  br i1 %.not, label %33, label %34

24:                                               ; preds = %1
  %25 = getelementptr inbounds i8, ptr %0, i64 36
  %26 = load i32, ptr %25, align 4, !tbaa !15
  %27 = and i32 %26, 1
  %.not22 = icmp eq i32 %27, 0
  br i1 %.not22, label %28, label %34

28:                                               ; preds = %24
  %29 = or disjoint i32 %26, 1
  store i32 %29, ptr %25, align 4, !tbaa !15
  %30 = getelementptr inbounds i8, ptr %0, i64 144
  %31 = load i64, ptr %0, align 8, !tbaa !16
  %32 = tail call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef nonnull dereferenceable(1) %30, i64 noundef 128, ptr noundef nonnull @.str.4, i64 noundef %31, i32 noundef %4) #3
  br label %34

33:                                               ; preds = %14, %16
  br label %34

34:                                               ; preds = %24, %28, %14, %5, %9, %16, %33
  %.1 = phi i32 [ 0, %33 ], [ -1, %16 ], [ -1, %9 ], [ -1, %5 ], [ -1, %14 ], [ -1, %28 ], [ -1, %24 ]
  ret i32 %.1
}

; Function Attrs: nofree nounwind
declare noundef i32 @snprintf(ptr noalias nocapture noundef writeonly, i64 noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i32 @read_unicode(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare ptr @strchr(ptr noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden i32 @pushchar(ptr noundef, i32 noundef) local_unnamed_addr #0

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 = { nofree nounwind "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 nofree nounwind willreturn memory(argmem: read) "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 #3 = { nounwind }
attributes #4 = { nounwind willreturn memory(read) }

!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, !10, i64 72}
!6 = !{!"json_stream", !7, i64 0, !10, i64 8, !7, i64 16, !7, i64 24, !11, i64 32, !11, i64 36, !12, i64 40, !7, i64 64, !13, i64 72, !14, i64 120, !8, i64 144}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!"", !10, i64 0, !7, i64 8, !7, i64 16}
!13 = !{!"json_source", !10, i64 0, !10, i64 8, !7, i64 16, !8, i64 24}
!14 = !{!"json_allocator", !10, i64 0, !10, i64 8, !10, i64 16}
!15 = !{!6, !11, i64 36}
!16 = !{!6, !7, i64 0}
!17 = !{!8, !8, i64 0}


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