; 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.file_id_offset_pair = type { i32, i64 }
%struct.leaf_data_result = type { i64, ptr, ptr }
%struct.giggle_query_result = type { ptr, i32, ptr, i32 }
%struct.giggle_index = type { ptr, i32, i32, ptr, ptr, ptr, ptr, ptr, ptr }

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

; 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) #1

; Function Attrs: nounwind uwtable
define dso_local void @leaf_data_map_intersection_to_offset_list(ptr noundef %0, ptr noundef %1, ptr noundef %2) #2 {
  %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 %struct.file_id_offset_pair, align 8
  %10 = alloca ptr, align 8
  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) #4
  %11 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %11, ptr %7, align 8, !tbaa !5
  %12 = load ptr, ptr %7, align 8, !tbaa !5
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %75

14:                                               ; preds = %3
  %15 = load ptr, ptr %7, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.leaf_data_result, ptr %15, i32 0, i32 0
  %17 = load i64, ptr %16, align 8, !tbaa !9
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.giggle_query_result, ptr %18, i32 0, i32 3
  %20 = load i32, ptr %19, align 8, !tbaa !12
  %21 = zext i32 %20 to i64
  %22 = add i64 %21, %17
  %23 = trunc i64 %22 to i32
  store i32 %23, ptr %19, align 8, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !15
  br label %24

24:                                               ; preds = %63, %14
  %25 = load i32, ptr %8, align 4, !tbaa !15
  %26 = zext i32 %25 to i64
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.leaf_data_result, ptr %27, i32 0, i32 0
  %29 = load i64, ptr %28, align 8, !tbaa !9
  %30 = icmp ult i64 %26, %29
  br i1 %30, label %31, label %66

31:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 16, ptr %9) #4
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.giggle_index, ptr %32, i32 0, i32 5
  %34 = load ptr, ptr %33, align 8, !tbaa !16
  %35 = load ptr, ptr %7, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.leaf_data_result, ptr %35, i32 0, i32 1
  %37 = load ptr, ptr %36, align 8, !tbaa !18
  %38 = load i32, ptr %8, align 4, !tbaa !15
  %39 = zext i32 %38 to i64
  %40 = getelementptr inbounds i64, ptr %37, i64 %39
  %41 = load i64, ptr %40, align 8, !tbaa !19
  %42 = call { i32, i64 } @offset_index_get(ptr noundef %34, i64 noundef %41)
  %43 = getelementptr inbounds { i32, i64 }, ptr %9, i32 0, i32 0
  %44 = extractvalue { i32, i64 } %42, 0
  store i32 %44, ptr %43, align 8
  %45 = getelementptr inbounds { i32, i64 }, ptr %9, i32 0, i32 1
  %46 = extractvalue { i32, i64 } %42, 1
  store i64 %46, ptr %45, align 8
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.giggle_query_result, ptr %47, i32 0, i32 2
  %49 = load ptr, ptr %48, align 8, !tbaa !20
  %50 = getelementptr inbounds %struct.file_id_offset_pair, ptr %9, i32 0, i32 0
  %51 = load i32, ptr %50, align 8, !tbaa !21
  %52 = zext i32 %51 to i64
  %53 = getelementptr inbounds ptr, ptr %49, i64 %52
  %54 = getelementptr inbounds %struct.file_id_offset_pair, ptr %9, i32 0, i32 1
  %55 = load i64, ptr %54, align 8, !tbaa !23
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.leaf_data_result, ptr %56, i32 0, i32 1
  %58 = load ptr, ptr %57, align 8, !tbaa !18
  %59 = load i32, ptr %8, align 4, !tbaa !15
  %60 = zext i32 %59 to i64
  %61 = getelementptr inbounds i64, ptr %58, i64 %60
  %62 = load i64, ptr %61, align 8, !tbaa !19
  call void @long_uint_ll_append(ptr noundef %53, i64 noundef %55, i64 noundef %62)
  call void @llvm.lifetime.end.p0(i64 16, ptr %9) #4
  br label %63

63:                                               ; preds = %31
  %64 = load i32, ptr %8, align 4, !tbaa !15
  %65 = add i32 %64, 1
  store i32 %65, ptr %8, align 4, !tbaa !15
  br label %24, !llvm.loop !24

66:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.leaf_data_result, ptr %67, i32 0, i32 2
  %69 = load ptr, ptr %68, align 8, !tbaa !26
  store ptr %69, ptr %10, align 8, !tbaa !5
  %70 = load ptr, ptr %7, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.leaf_data_result, ptr %70, i32 0, i32 1
  %72 = load ptr, ptr %71, align 8, !tbaa !18
  call void @free(ptr noundef %72) #4
  %73 = load ptr, ptr %7, align 8, !tbaa !5
  call void @free(ptr noundef %73) #4
  %74 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %74, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %75

75:                                               ; preds = %66, %3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret void
}

declare { i32, i64 } @offset_index_get(ptr noundef, i64 noundef) #3

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

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

!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 = !{!13, !14, i64 24}
!13 = !{!"giggle_query_result", !6, i64 0, !14, i64 8, !6, i64 16, !14, i64 24}
!14 = !{!"int", !7, i64 0}
!15 = !{!14, !14, i64 0}
!16 = !{!17, !6, i64 32}
!17 = !{!"giggle_index", !6, i64 0, !14, i64 8, !14, i64 12, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56}
!18 = !{!10, !6, i64 8}
!19 = !{!11, !11, i64 0}
!20 = !{!13, !6, i64 16}
!21 = !{!22, !14, i64 0}
!22 = !{!"file_id_offset_pair", !14, i64 0, !11, i64 8}
!23 = !{!22, !11, i64 8}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}
!26 = !{!10, !6, i64 16}


; 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 nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @leaf_data_map_intersection_to_offset_list(ptr nocapture noundef readonly %0, ptr nocapture noundef %1, ptr noundef %2) local_unnamed_addr #1 {
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %34, label %4

4:                                                ; preds = %3
  %5 = load i64, ptr %2, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %1, i64 24
  %7 = load i32, ptr %6, align 8, !tbaa !11
  %8 = trunc i64 %5 to i32
  %9 = add i32 %7, %8
  store i32 %9, ptr %6, align 8, !tbaa !11
  %.not20 = icmp eq i64 %5, 0
  br i1 %.not20, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4
  %10 = getelementptr inbounds i8, ptr %0, i64 32
  %11 = getelementptr inbounds i8, ptr %2, i64 8
  %12 = getelementptr inbounds i8, ptr %1, i64 16
  br label %13

13:                                               ; preds = %.lr.ph, %13
  %14 = phi i64 [ 0, %.lr.ph ], [ %29, %13 ]
  %.019 = phi i32 [ 0, %.lr.ph ], [ %28, %13 ]
  %15 = load ptr, ptr %10, align 8, !tbaa !14
  %16 = load ptr, ptr %11, align 8, !tbaa !16
  %17 = getelementptr inbounds i64, ptr %16, i64 %14
  %18 = load i64, ptr %17, align 8, !tbaa !17
  %19 = tail call { i32, i64 } @offset_index_get(ptr noundef %15, i64 noundef %18) #3
  %20 = extractvalue { i32, i64 } %19, 0
  %21 = extractvalue { i32, i64 } %19, 1
  %22 = load ptr, ptr %12, align 8, !tbaa !18
  %23 = zext i32 %20 to i64
  %24 = getelementptr inbounds ptr, ptr %22, i64 %23
  %25 = load ptr, ptr %11, align 8, !tbaa !16
  %26 = getelementptr inbounds i64, ptr %25, i64 %14
  %27 = load i64, ptr %26, align 8, !tbaa !17
  tail call void @long_uint_ll_append(ptr noundef %24, i64 noundef %21, i64 noundef %27) #3
  %28 = add i32 %.019, 1
  %29 = zext i32 %28 to i64
  %30 = load i64, ptr %2, align 8, !tbaa !5
  %31 = icmp ugt i64 %30, %29
  br i1 %31, label %13, label %._crit_edge, !llvm.loop !19

._crit_edge:                                      ; preds = %13, %4
  %32 = getelementptr inbounds i8, ptr %2, i64 8
  %33 = load ptr, ptr %32, align 8, !tbaa !16
  tail call void @free(ptr noundef %33) #3
  tail call void @free(ptr noundef nonnull %2) #3
  br label %34

34:                                               ; preds = %._crit_edge, %3
  ret void
}

declare { i32, i64 } @offset_index_get(ptr noundef, i64 noundef) local_unnamed_addr #2

declare void @long_uint_ll_append(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #2

attributes #0 = { 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 #1 = { 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 #2 = { "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 }

!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 = !{!"leaf_data_result", !7, i64 0, !10, i64 8, !10, i64 16}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!12, !13, i64 24}
!12 = !{!"giggle_query_result", !10, i64 0, !13, i64 8, !10, i64 16, !13, i64 24}
!13 = !{!"int", !8, i64 0}
!14 = !{!15, !10, i64 32}
!15 = !{!"giggle_index", !10, i64 0, !13, i64 8, !13, i64 12, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56}
!16 = !{!6, !10, i64 8}
!17 = !{!7, !7, i64 0}
!18 = !{!12, !10, i64 16}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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