; LLVM IR from ryanlayer/giggle :: src (giggle_index)

; 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.leaf_data_result = type { i64, ptr, ptr }
%struct.query_filter = type { ptr, i8, i32, %union.metadata_data }
%union.metadata_data = type { i64 }
%struct.giggle_index = type { ptr, i32, i32, ptr, ptr, ptr, ptr, ptr, ptr }

@stderr = external global ptr, align 8
@.str.37 = external hidden unnamed_addr constant [52 x i8], align 1
@.str.38 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.39 = external hidden unnamed_addr constant [1 x i8], align 1
@.str.40 = external hidden unnamed_addr constant [51 x i8], align 1

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

; Function Attrs: noreturn
declare void @err(i32 noundef, ptr noundef, ...) #1

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

; Function Attrs: nounwind
declare void @free(ptr noundef) #2

; Function Attrs: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #3

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

; Function Attrs: nounwind uwtable
define dso_local void @apply_query_filter_to_results(ptr noundef %0, ptr noundef %1, ptr noundef %2) #4 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  %14 = alloca i8, align 1
  %15 = alloca ptr, align 8
  %16 = alloca ptr, align 8
  %17 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #6
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = load ptr, ptr %18, align 8, !tbaa !5
  store ptr %19, ptr %7, align 8, !tbaa !5
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = icmp ne ptr %20, null
  br i1 %21, label %23, label %22

22:                                               ; preds = %3
  store i32 1, ptr %8, align 4
  br label %100

23:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #6
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.leaf_data_result, ptr %24, i32 0, i32 0
  %26 = load i64, ptr %25, align 8, !tbaa !9
  store i64 %26, ptr %10, align 8, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #6
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.leaf_data_result, ptr %27, i32 0, i32 1
  %29 = load ptr, ptr %28, align 8, !tbaa !13
  store ptr %29, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #6
  store i64 0, ptr %12, align 8, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #6
  call void @llvm.lifetime.start.p0(i64 1, ptr %14) #6
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.query_filter, ptr %30, i32 0, i32 1
  %32 = load i8, ptr %31, align 8, !tbaa !14
  store i8 %32, ptr %14, align 1, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #6
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.giggle_index, ptr %33, i32 0, i32 6
  %35 = load ptr, ptr %34, align 8, !tbaa !18
  store ptr %35, ptr %15, align 8, !tbaa !5
  store i64 0, ptr %9, align 8, !tbaa !12
  br label %36

36:                                               ; preds = %64, %23
  %37 = load i64, ptr %9, align 8, !tbaa !12
  %38 = load i64, ptr %10, align 8, !tbaa !12
  %39 = icmp ult i64 %37, %38
  br i1 %39, label %40, label %67

40:                                               ; preds = %36
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #6
  %41 = load ptr, ptr %15, align 8, !tbaa !5
  %42 = load ptr, ptr %11, align 8, !tbaa !5
  %43 = load i64, ptr %9, align 8, !tbaa !12
  %44 = getelementptr inbounds i64, ptr %42, i64 %43
  %45 = load i64, ptr %44, align 8, !tbaa !12
  %46 = load i8, ptr %14, align 1, !tbaa !17
  %47 = call ptr @read_metadata_item_by_column_id(ptr noundef %41, i64 noundef %45, i8 noundef zeroext %46)
  store ptr %47, ptr %16, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #6
  %48 = load ptr, ptr %16, align 8, !tbaa !5
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = call i32 @filter_metadata_row_by_item(ptr noundef %48, ptr noundef %49)
  store i32 %50, ptr %17, align 4, !tbaa !20
  %51 = load ptr, ptr %16, align 8, !tbaa !5
  call void @metadata_item_destroy(ptr noundef %51)
  %52 = load i32, ptr %17, align 4, !tbaa !20
  %53 = icmp ne i32 %52, 0
  br i1 %53, label %54, label %63

54:                                               ; preds = %40
  %55 = load ptr, ptr %11, align 8, !tbaa !5
  %56 = load i64, ptr %9, align 8, !tbaa !12
  %57 = getelementptr inbounds i64, ptr %55, i64 %56
  %58 = load i64, ptr %57, align 8, !tbaa !12
  %59 = load ptr, ptr %11, align 8, !tbaa !5
  %60 = load i64, ptr %12, align 8, !tbaa !12
  %61 = add i64 %60, 1
  store i64 %61, ptr %12, align 8, !tbaa !12
  %62 = getelementptr inbounds i64, ptr %59, i64 %60
  store i64 %58, ptr %62, align 8, !tbaa !12
  br label %63

63:                                               ; preds = %54, %40
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #6
  br label %64

64:                                               ; preds = %63
  %65 = load i64, ptr %9, align 8, !tbaa !12
  %66 = add i64 %65, 1
  store i64 %66, ptr %9, align 8, !tbaa !12
  br label %36, !llvm.loop !21

67:                                               ; preds = %36
  %68 = load i64, ptr %10, align 8, !tbaa !12
  %69 = load i64, ptr %12, align 8, !tbaa !12
  %70 = sub i64 %68, %69
  store i64 %70, ptr %13, align 8, !tbaa !12
  %71 = load ptr, ptr @stderr, align 8, !tbaa !5
  %72 = load i64, ptr %13, align 8, !tbaa !12
  %73 = load i64, ptr %13, align 8, !tbaa !12
  %74 = icmp ugt i64 %73, 1
  %75 = zext i1 %74 to i64
  %76 = select i1 %74, ptr @.str.38, ptr @.str.39
  %77 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %71, ptr noundef @.str.37, i64 noundef %72, ptr noundef %76) #6
  %78 = load i64, ptr %12, align 8, !tbaa !12
  %79 = icmp eq i64 %78, 0
  br i1 %79, label %80, label %84

80:                                               ; preds = %67
  %81 = load ptr, ptr %11, align 8, !tbaa !5
  call void @free(ptr noundef %81) #6
  %82 = load ptr, ptr %7, align 8, !tbaa !5
  call void @free(ptr noundef %82) #6
  %83 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr null, ptr %83, align 8, !tbaa !5
  br label %99

84:                                               ; preds = %67
  %85 = load ptr, ptr %11, align 8, !tbaa !5
  %86 = load i64, ptr %12, align 8, !tbaa !12
  %87 = mul i64 8, %86
  %88 = call ptr @realloc(ptr noundef %85, i64 noundef %87) #7
  store ptr %88, ptr %11, align 8, !tbaa !5
  %89 = load ptr, ptr %11, align 8, !tbaa !5
  %90 = icmp eq ptr %89, null
  br i1 %90, label %91, label %92

91:                                               ; preds = %84
  call void (i32, ptr, ...) @err(i32 noundef 1, ptr noundef @.str.40) #8
  unreachable

92:                                               ; preds = %84
  %93 = load i64, ptr %12, align 8, !tbaa !12
  %94 = load ptr, ptr %7, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.leaf_data_result, ptr %94, i32 0, i32 0
  store i64 %93, ptr %95, align 8, !tbaa !9
  %96 = load ptr, ptr %11, align 8, !tbaa !5
  %97 = load ptr, ptr %7, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.leaf_data_result, ptr %97, i32 0, i32 1
  store ptr %96, ptr %98, align 8, !tbaa !13
  br label %99

99:                                               ; preds = %92, %80
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #6
  call void @llvm.lifetime.end.p0(i64 1, ptr %14) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #6
  store i32 0, ptr %8, align 4
  br label %100

100:                                              ; preds = %99, %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #6
  %101 = load i32, ptr %8, align 4
  switch i32 %101, label %103 [
    i32 0, label %102
    i32 1, label %102
  ]

102:                                              ; preds = %100, %100
  ret void

103:                                              ; preds = %100
  unreachable
}

declare ptr @read_metadata_item_by_column_id(ptr noundef, i64 noundef, i8 noundef zeroext) #5

declare i32 @filter_metadata_row_by_item(ptr noundef, ptr noundef) #5

declare void @metadata_item_destroy(ptr noundef) #5

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { noreturn "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 "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 allocsize(1) "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 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 #5 = { "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 #6 = { nounwind }
attributes #7 = { nounwind allocsize(1) }
attributes #8 = { noreturn }

!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, !11, i64 0}
!10 = !{!"leaf_data_result", !11, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!"long", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!10, !6, i64 8}
!14 = !{!15, !7, i64 8}
!15 = !{!"query_filter", !6, i64 0, !7, i64 8, !16, i64 12, !7, i64 16}
!16 = !{!"int", !7, i64 0}
!17 = !{!7, !7, i64 0}
!18 = !{!19, !6, i64 40}
!19 = !{!"giggle_index", !6, i64 0, !16, i64 8, !16, i64 12, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56}
!20 = !{!16, !16, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"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"

@stderr = external local_unnamed_addr global ptr, align 8
@.str.37 = external hidden unnamed_addr constant [52 x i8], align 1
@.str.38 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.39 = external hidden unnamed_addr constant [1 x i8], align 1
@.str.40 = external hidden unnamed_addr constant [51 x i8], align 1

; Function Attrs: noreturn
declare void @err(i32 noundef, ptr noundef, ...) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local void @apply_query_filter_to_results(ptr nocapture noundef readonly %0, ptr noundef %1, ptr nocapture noundef %2) local_unnamed_addr #4 {
  %4 = load ptr, ptr %2, align 8, !tbaa !5
  %.not = icmp eq ptr %4, null
  br i1 %.not, label %36, label %5

5:                                                ; preds = %3
  %6 = load i64, ptr %4, align 8, !tbaa !9
  %7 = getelementptr inbounds i8, ptr %4, i64 8
  %8 = load ptr, ptr %7, align 8, !tbaa !12
  %9 = getelementptr inbounds i8, ptr %1, i64 8
  %10 = load i8, ptr %9, align 8, !tbaa !13
  %11 = getelementptr inbounds i8, ptr %0, i64 40
  %12 = load ptr, ptr %11, align 8, !tbaa !16
  %.not42 = icmp eq i64 %6, 0
  br i1 %.not42, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %5, %21
  %.041 = phi i64 [ %22, %21 ], [ 0, %5 ]
  %.03540 = phi i64 [ %.1, %21 ], [ 0, %5 ]
  %13 = getelementptr inbounds i64, ptr %8, i64 %.041
  %14 = load i64, ptr %13, align 8, !tbaa !18
  %15 = tail call ptr @read_metadata_item_by_column_id(ptr noundef %12, i64 noundef %14, i8 noundef zeroext %10) #6
  %16 = tail call i32 @filter_metadata_row_by_item(ptr noundef %15, ptr noundef %1) #6
  tail call void @metadata_item_destroy(ptr noundef %15) #6
  %.not39 = icmp eq i32 %16, 0
  br i1 %.not39, label %21, label %17

17:                                               ; preds = %.lr.ph
  %18 = load i64, ptr %13, align 8, !tbaa !18
  %19 = add i64 %.03540, 1
  %20 = getelementptr inbounds i64, ptr %8, i64 %.03540
  store i64 %18, ptr %20, align 8, !tbaa !18
  br label %21

21:                                               ; preds = %17, %.lr.ph
  %.1 = phi i64 [ %19, %17 ], [ %.03540, %.lr.ph ]
  %22 = add nuw i64 %.041, 1
  %exitcond.not = icmp eq i64 %22, %6
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !19

._crit_edge:                                      ; preds = %21, %5
  %.035.lcssa = phi i64 [ 0, %5 ], [ %.1, %21 ]
  %23 = sub i64 %6, %.035.lcssa
  %24 = load ptr, ptr @stderr, align 8, !tbaa !5
  %25 = icmp ugt i64 %23, 1
  %26 = select i1 %25, ptr @.str.38, ptr @.str.39
  %27 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %24, ptr noundef nonnull @.str.37, i64 noundef %23, ptr noundef nonnull %26) #7
  %28 = icmp eq i64 %.035.lcssa, 0
  br i1 %28, label %29, label %30

29:                                               ; preds = %._crit_edge
  tail call void @free(ptr noundef %8) #6
  tail call void @free(ptr noundef %4) #6
  store ptr null, ptr %2, align 8, !tbaa !5
  br label %36

30:                                               ; preds = %._crit_edge
  %31 = shl i64 %.035.lcssa, 3
  %32 = tail call ptr @realloc(ptr noundef %8, i64 noundef %31) #8
  %33 = icmp eq ptr %32, null
  br i1 %33, label %34, label %35

34:                                               ; preds = %30
  tail call void (i32, ptr, ...) @err(i32 noundef 1, ptr noundef nonnull @.str.40) #9
  unreachable

35:                                               ; preds = %30
  store i64 %.035.lcssa, ptr %4, align 8, !tbaa !9
  store ptr %32, ptr %7, align 8, !tbaa !12
  br label %36

36:                                               ; preds = %29, %35, %3
  ret void
}

declare ptr @read_metadata_item_by_column_id(ptr noundef, i64 noundef, i8 noundef zeroext) local_unnamed_addr #5

declare i32 @filter_metadata_row_by_item(ptr noundef, ptr noundef) local_unnamed_addr #5

declare void @metadata_item_destroy(ptr noundef) local_unnamed_addr #5

attributes #0 = { noreturn "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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { 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 #4 = { 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 #5 = { "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 #6 = { nounwind }
attributes #7 = { cold nounwind }
attributes #8 = { nounwind allocsize(1) }
attributes #9 = { noreturn 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, !11, i64 0}
!10 = !{!"leaf_data_result", !11, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = !{!14, !7, i64 8}
!14 = !{!"query_filter", !6, i64 0, !7, i64 8, !15, i64 12, !7, i64 16}
!15 = !{!"int", !7, i64 0}
!16 = !{!17, !6, i64 40}
!17 = !{!"giggle_index", !6, i64 0, !15, i64 8, !15, i64 12, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56}
!18 = !{!11, !11, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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