; 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.giggle_query_result = type { ptr, i32, ptr, i32 }
%struct.giggle_query_iter = type { ptr, i32, i32, i32, ptr, ptr, ptr }
%struct.long_uint_ll = type { i64, ptr, ptr }
%struct.long_uint_ll_node = type { i64, i64, ptr }

@.str.41 = external hidden unnamed_addr constant [39 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: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #1

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

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

declare void @qsort(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #3

; Function Attrs: nounwind uwtable
define dso_local ptr @giggle_get_query_itr(ptr noundef %0, i32 noundef %1) #4 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = 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) #5
  %10 = call noalias ptr @malloc(i64 noundef 48) #6
  store ptr %10, ptr %6, align 8, !tbaa !5
  %11 = load ptr, ptr %6, align 8, !tbaa !5
  %12 = icmp eq ptr %11, null
  br i1 %12, label %13, label %14

13:                                               ; preds = %2
  call void (i32, ptr, ...) @err(i32 noundef 1, ptr noundef @.str.41) #7
  unreachable

14:                                               ; preds = %2
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.giggle_query_result, ptr %15, i32 0, i32 0
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.giggle_query_iter, ptr %18, i32 0, i32 0
  store ptr %17, ptr %19, align 8, !tbaa !13
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.giggle_query_iter, ptr %20, i32 0, i32 2
  store i32 0, ptr %21, align 4, !tbaa !15
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.giggle_query_iter, ptr %22, i32 0, i32 3
  store i32 0, ptr %23, align 8, !tbaa !16
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.giggle_query_iter, ptr %25, i32 0, i32 1
  store i32 %24, ptr %26, align 8, !tbaa !17
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.giggle_query_iter, ptr %27, i32 0, i32 5
  store ptr null, ptr %28, align 8, !tbaa !18
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.giggle_query_iter, ptr %29, i32 0, i32 6
  store ptr null, ptr %30, align 8, !tbaa !19
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.giggle_query_iter, ptr %31, i32 0, i32 4
  store ptr null, ptr %32, align 8, !tbaa !20
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.giggle_query_result, ptr %33, i32 0, i32 2
  %35 = load ptr, ptr %34, align 8, !tbaa !21
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = zext i32 %36 to i64
  %38 = getelementptr inbounds ptr, ptr %35, i64 %37
  %39 = load ptr, ptr %38, align 8, !tbaa !5
  %40 = icmp eq ptr %39, null
  br i1 %40, label %41, label %43

41:                                               ; preds = %14
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %42, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %115

43:                                               ; preds = %14
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.giggle_query_result, ptr %44, i32 0, i32 2
  %46 = load ptr, ptr %45, align 8, !tbaa !21
  %47 = load i32, ptr %5, align 4, !tbaa !9
  %48 = zext i32 %47 to i64
  %49 = getelementptr inbounds ptr, ptr %46, i64 %48
  %50 = load ptr, ptr %49, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.long_uint_ll, ptr %50, i32 0, i32 0
  %52 = load i64, ptr %51, align 8, !tbaa !22
  %53 = trunc i64 %52 to i32
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.giggle_query_iter, ptr %54, i32 0, i32 3
  store i32 %53, ptr %55, align 8, !tbaa !16
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.giggle_query_result, ptr %56, i32 0, i32 2
  %58 = load ptr, ptr %57, align 8, !tbaa !21
  %59 = load i32, ptr %5, align 4, !tbaa !9
  %60 = zext i32 %59 to i64
  %61 = getelementptr inbounds ptr, ptr %58, i64 %60
  %62 = load ptr, ptr %61, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.long_uint_ll, ptr %62, i32 0, i32 0
  %64 = load i64, ptr %63, align 8, !tbaa !22
  %65 = mul i64 %64, 8
  %66 = call noalias ptr @malloc(i64 noundef %65) #6
  %67 = load ptr, ptr %6, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.giggle_query_iter, ptr %67, i32 0, i32 5
  store ptr %66, ptr %68, align 8, !tbaa !18
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.giggle_query_iter, ptr %69, i32 0, i32 5
  %71 = load ptr, ptr %70, align 8, !tbaa !18
  %72 = icmp eq ptr %71, null
  br i1 %72, label %73, label %74

73:                                               ; preds = %43
  call void (i32, ptr, ...) @err(i32 noundef 1, ptr noundef @.str.41) #7
  unreachable

74:                                               ; preds = %43
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %75 = load ptr, ptr %4, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.giggle_query_result, ptr %75, i32 0, i32 2
  %77 = load ptr, ptr %76, align 8, !tbaa !21
  %78 = load i32, ptr %5, align 4, !tbaa !9
  %79 = zext i32 %78 to i64
  %80 = getelementptr inbounds ptr, ptr %77, i64 %79
  %81 = load ptr, ptr %80, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.long_uint_ll, ptr %81, i32 0, i32 1
  %83 = load ptr, ptr %82, align 8, !tbaa !25
  store ptr %83, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %84

84:                                               ; preds = %87, %74
  %85 = load ptr, ptr %8, align 8, !tbaa !5
  %86 = icmp ne ptr %85, null
  br i1 %86, label %87, label %101

87:                                               ; preds = %84
  %88 = load ptr, ptr %8, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.long_uint_ll_node, ptr %88, i32 0, i32 0
  %90 = load i64, ptr %89, align 8, !tbaa !26
  %91 = load ptr, ptr %6, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.giggle_query_iter, ptr %91, i32 0, i32 5
  %93 = load ptr, ptr %92, align 8, !tbaa !18
  %94 = load i32, ptr %9, align 4, !tbaa !9
  %95 = add i32 %94, 1
  store i32 %95, ptr %9, align 4, !tbaa !9
  %96 = zext i32 %94 to i64
  %97 = getelementptr inbounds i64, ptr %93, i64 %96
  store i64 %90, ptr %97, align 8, !tbaa !28
  %98 = load ptr, ptr %8, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.long_uint_ll_node, ptr %98, i32 0, i32 2
  %100 = load ptr, ptr %99, align 8, !tbaa !29
  store ptr %100, ptr %8, align 8, !tbaa !5
  br label %84, !llvm.loop !30

101:                                              ; preds = %84
  %102 = load ptr, ptr %6, align 8, !tbaa !5
  %103 = getelementptr inbounds %struct.giggle_query_iter, ptr %102, i32 0, i32 5
  %104 = load ptr, ptr %103, align 8, !tbaa !18
  %105 = load ptr, ptr %4, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct.giggle_query_result, ptr %105, i32 0, i32 2
  %107 = load ptr, ptr %106, align 8, !tbaa !21
  %108 = load i32, ptr %5, align 4, !tbaa !9
  %109 = zext i32 %108 to i64
  %110 = getelementptr inbounds ptr, ptr %107, i64 %109
  %111 = load ptr, ptr %110, align 8, !tbaa !5
  %112 = getelementptr inbounds %struct.long_uint_ll, ptr %111, i32 0, i32 0
  %113 = load i64, ptr %112, align 8, !tbaa !22
  call void @qsort(ptr noundef %104, i64 noundef %113, i64 noundef 8, ptr noundef @long_cmp)
  %114 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %114, ptr %3, align 8
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  br label %115

115:                                              ; preds = %101, %41
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  %116 = load ptr, ptr %3, align 8
  ret ptr %116
}

declare i32 @long_cmp(ptr noundef, ptr noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind allocsize(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 #2 = { 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 #3 = { "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 = { nounwind }
attributes #6 = { nounwind allocsize(0) }
attributes #7 = { 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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"giggle_query_result", !6, i64 0, !10, i64 8, !6, i64 16, !10, i64 24}
!13 = !{!14, !6, i64 0}
!14 = !{!"giggle_query_iter", !6, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!15 = !{!14, !10, i64 12}
!16 = !{!14, !10, i64 16}
!17 = !{!14, !10, i64 8}
!18 = !{!14, !6, i64 32}
!19 = !{!14, !6, i64 40}
!20 = !{!14, !6, i64 24}
!21 = !{!12, !6, i64 16}
!22 = !{!23, !24, i64 0}
!23 = !{!"long_uint_ll", !24, i64 0, !6, i64 8, !6, i64 16}
!24 = !{!"long", !7, i64 0}
!25 = !{!23, !6, i64 8}
!26 = !{!27, !24, i64 0}
!27 = !{!"long_uint_ll_node", !24, i64 0, !24, i64 8, !6, i64 16}
!28 = !{!24, !24, i64 0}
!29 = !{!27, !6, i64 16}
!30 = distinct !{!30, !31}
!31 = !{!"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"

@.str.41 = external hidden unnamed_addr constant [39 x i8], align 1

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

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

; Function Attrs: nofree
declare void @qsort(ptr noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local noalias noundef ptr @giggle_get_query_itr(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #3 {
  %3 = tail call noalias dereferenceable_or_null(48) ptr @malloc(i64 noundef 48) #6
  %4 = icmp eq ptr %3, null
  br i1 %4, label %5, label %6

5:                                                ; preds = %2
  tail call void (i32, ptr, ...) @err(i32 noundef 1, ptr noundef nonnull @.str.41) #7
  unreachable

6:                                                ; preds = %2
  %7 = load ptr, ptr %0, align 8, !tbaa !5
  store ptr %7, ptr %3, align 8, !tbaa !11
  %8 = getelementptr inbounds i8, ptr %3, i64 12
  store i32 0, ptr %8, align 4, !tbaa !13
  %9 = getelementptr inbounds i8, ptr %3, i64 16
  store i32 0, ptr %9, align 8, !tbaa !14
  %10 = getelementptr inbounds i8, ptr %3, i64 8
  store i32 %1, ptr %10, align 8, !tbaa !15
  %11 = getelementptr inbounds i8, ptr %3, i64 24
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %11, i8 0, i64 24, i1 false)
  %13 = load ptr, ptr %12, align 8, !tbaa !16
  %14 = zext i32 %1 to i64
  %15 = getelementptr inbounds ptr, ptr %13, i64 %14
  %16 = load ptr, ptr %15, align 8, !tbaa !17
  %17 = icmp eq ptr %16, null
  br i1 %17, label %33, label %18

18:                                               ; preds = %6
  %19 = getelementptr inbounds i8, ptr %3, i64 32
  %20 = load i64, ptr %16, align 8, !tbaa !18
  %21 = trunc i64 %20 to i32
  store i32 %21, ptr %9, align 8, !tbaa !14
  %22 = shl i64 %20, 3
  %23 = tail call noalias ptr @malloc(i64 noundef %22) #6
  store ptr %23, ptr %19, align 8, !tbaa !21
  %24 = icmp eq ptr %23, null
  br i1 %24, label %25, label %26

25:                                               ; preds = %18
  tail call void (i32, ptr, ...) @err(i32 noundef 1, ptr noundef nonnull @.str.41) #7
  unreachable

26:                                               ; preds = %18
  %27 = getelementptr inbounds i8, ptr %16, i64 8
  %.03135 = load ptr, ptr %27, align 8, !tbaa !17
  %.not36 = icmp eq ptr %.03135, null
  br i1 %.not36, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %26, %.lr.ph
  %.03138 = phi ptr [ %.031, %.lr.ph ], [ %.03135, %26 ]
  %.037 = phi i32 [ %29, %.lr.ph ], [ 0, %26 ]
  %28 = load i64, ptr %.03138, align 8, !tbaa !22
  %29 = add i32 %.037, 1
  %30 = zext i32 %.037 to i64
  %31 = getelementptr inbounds i64, ptr %23, i64 %30
  store i64 %28, ptr %31, align 8, !tbaa !24
  %32 = getelementptr inbounds i8, ptr %.03138, i64 16
  %.031 = load ptr, ptr %32, align 8, !tbaa !17
  %.not = icmp eq ptr %.031, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !25

._crit_edge:                                      ; preds = %.lr.ph, %26
  tail call void @qsort(ptr noundef nonnull %23, i64 noundef %20, i64 noundef 8, ptr noundef nonnull @long_cmp) #8
  br label %33

33:                                               ; preds = %6, %._crit_edge
  ret ptr %3
}

declare i32 @long_cmp(ptr noundef, ptr noundef) #4

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #5

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(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 #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 = { nofree "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 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 #4 = { "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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #6 = { nounwind allocsize(0) }
attributes #7 = { noreturn nounwind }
attributes #8 = { 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, !7, i64 0}
!6 = !{!"giggle_query_result", !7, i64 0, !10, i64 8, !7, i64 16, !10, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !7, i64 0}
!12 = !{!"giggle_query_iter", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !7, i64 24, !7, i64 32, !7, i64 40}
!13 = !{!12, !10, i64 12}
!14 = !{!12, !10, i64 16}
!15 = !{!12, !10, i64 8}
!16 = !{!6, !7, i64 16}
!17 = !{!7, !7, i64 0}
!18 = !{!19, !20, i64 0}
!19 = !{!"long_uint_ll", !20, i64 0, !7, i64 8, !7, i64 16}
!20 = !{!"long", !8, i64 0}
!21 = !{!12, !7, i64 32}
!22 = !{!23, !20, i64 0}
!23 = !{!"long_uint_ll_node", !20, i64 0, !20, i64 8, !7, i64 16}
!24 = !{!20, !20, i64 0}
!25 = distinct !{!25, !26}
!26 = !{!"llvm.loop.mustprogress"}


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