; LLVM IR from ryanlayer/giggle :: test/unit (test_bulk_insert)

; 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_def = type { %struct.cache_handler, %struct.cache_handler, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.cache_handler = type { ptr, ptr, ptr }
%struct.cache_def = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.giggle_query_result = type { ptr, i32, ptr, i32 }

@ORDER = external dso_local global i32, align 4
@.str.4 = external hidden unnamed_addr constant [16 x i8], align 1
@giggle_data_handler = external dso_local global %struct.giggle_def, align 8
@.str.8 = external hidden unnamed_addr constant [2 x i8], align 1
@cache = external dso_local global %struct.cache_def, align 8
@.str.11 = external hidden unnamed_addr constant [29 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

declare void @UnityAssertEqualNumber(i64 noundef, i64 noundef, ptr noundef, i64 noundef, i32 noundef) #1

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

declare i32 @rmrf(ptr noundef) #1

declare void @giggle_index_destroy(ptr noundef) #1

declare i64 @giggle_bulk_insert(ptr noundef, ptr noundef, i32 noundef) #1

declare ptr @giggle_load(ptr noundef, ptr noundef) #1

declare void @uint64_t_ll_giggle_set_data_handler(...) #1

declare ptr @giggle_collect_intersection_data_in_block(i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef) #1

declare void @leaf_data_map_intersection_to_offset_list(ptr noundef, ptr noundef, ptr noundef) #1

declare ptr @giggle_query(ptr noundef, ptr noundef, i32 noundef, i32 noundef, ptr noundef) #1

declare void @giggle_query_result_destroy(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @test_giggle_bulk_insert_fews() #2 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  store ptr @.str.4, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  store ptr @.str.11, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 2, ptr %3, align 4, !tbaa !9
  br label %7

7:                                                ; preds = %25, %0
  %8 = load i32, ptr %3, align 4, !tbaa !9
  %9 = icmp ule i32 %8, 100
  br i1 %9, label %10, label %28

10:                                               ; preds = %7
  %11 = load i32, ptr %3, align 4, !tbaa !9
  store i32 %11, ptr @ORDER, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %12 = load ptr, ptr %1, align 8, !tbaa !5
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = call i64 @giggle_bulk_insert(ptr noundef %12, ptr noundef %13, i32 noundef 1)
  store i64 %14, ptr %4, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = call ptr @giggle_load(ptr noundef %15, ptr noundef @uint64_t_ll_giggle_set_data_handler)
  store ptr %16, ptr %5, align 8, !tbaa !5
  store ptr @giggle_collect_intersection_data_in_block, ptr getelementptr inbounds (%struct.giggle_def, ptr @giggle_data_handler, i32 0, i32 11), align 8, !tbaa !13
  store ptr @leaf_data_map_intersection_to_offset_list, ptr getelementptr inbounds (%struct.giggle_def, ptr @giggle_data_handler, i32 0, i32 12), align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store ptr null, ptr %6, align 8, !tbaa !5
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = call ptr @giggle_query(ptr noundef %17, ptr noundef @.str.8, i32 noundef 20, i32 noundef 50, ptr noundef %18)
  store ptr %19, ptr %6, align 8, !tbaa !5
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.giggle_query_result, ptr %20, i32 0, i32 3
  %22 = load i32, ptr %21, align 8, !tbaa !17
  %23 = zext i32 %22 to i64
  call void @UnityAssertEqualNumber(i64 noundef 8, i64 noundef %23, ptr noundef null, i64 noundef 1953, i32 noundef 148)
  call void @giggle_query_result_destroy(ptr noundef %6)
  call void @giggle_index_destroy(ptr noundef %5)
  %24 = load ptr, ptr getelementptr inbounds (%struct.cache_def, ptr @cache, i32 0, i32 6), align 8, !tbaa !19
  call void (...) %24()
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %25

25:                                               ; preds = %10
  %26 = load i32, ptr %3, align 4, !tbaa !9
  %27 = add i32 %26, 1
  store i32 %27, ptr %3, align 4, !tbaa !9
  br label %7, !llvm.loop !21

28:                                               ; preds = %7
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = call i32 @rmrf(ptr noundef %29)
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #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 #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 = { 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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !6, i64 120}
!14 = !{!"giggle_def", !15, i64 0, !15, i64 24, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128}
!15 = !{!"cache_handler", !6, i64 0, !6, i64 8, !6, i64 16}
!16 = !{!14, !6, i64 128}
!17 = !{!18, !10, i64 24}
!18 = !{!"giggle_query_result", !6, i64 0, !10, i64 8, !6, i64 16, !10, i64 24}
!19 = !{!20, !6, i64 48}
!20 = !{!"cache_def", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48}
!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"

%struct.giggle_def = type { %struct.cache_handler, %struct.cache_handler, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.cache_handler = type { ptr, ptr, ptr }
%struct.cache_def = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr }

@ORDER = external dso_local local_unnamed_addr global i32, align 4
@.str.4 = external hidden unnamed_addr constant [16 x i8], align 1
@giggle_data_handler = external dso_local local_unnamed_addr global %struct.giggle_def, align 8
@.str.8 = external hidden unnamed_addr constant [2 x i8], align 1
@cache = external dso_local local_unnamed_addr global %struct.cache_def, align 8
@.str.11 = external hidden unnamed_addr constant [29 x i8], align 1

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

declare void @UnityAssertEqualNumber(i64 noundef, i64 noundef, ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #1

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

declare i32 @rmrf(ptr noundef) local_unnamed_addr #1

declare void @giggle_index_destroy(ptr noundef) local_unnamed_addr #1

declare i64 @giggle_bulk_insert(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

declare ptr @giggle_load(ptr noundef, ptr noundef) local_unnamed_addr #1

declare void @uint64_t_ll_giggle_set_data_handler(...) #1

declare ptr @giggle_collect_intersection_data_in_block(i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef) #1

declare void @leaf_data_map_intersection_to_offset_list(ptr noundef, ptr noundef, ptr noundef) #1

declare ptr @giggle_query(ptr noundef, ptr noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

declare void @giggle_query_result_destroy(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @test_giggle_bulk_insert_fews() local_unnamed_addr #2 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  br label %3

3:                                                ; preds = %0, %3
  %.06 = phi i32 [ 2, %0 ], [ %11, %3 ]
  store i32 %.06, ptr @ORDER, align 4, !tbaa !5
  %4 = call i64 @giggle_bulk_insert(ptr noundef nonnull @.str.4, ptr noundef nonnull @.str.11, i32 noundef 1) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #3
  %5 = call ptr @giggle_load(ptr noundef nonnull @.str.11, ptr noundef nonnull @uint64_t_ll_giggle_set_data_handler) #3
  store ptr %5, ptr %1, align 8, !tbaa !9
  store ptr @giggle_collect_intersection_data_in_block, ptr getelementptr inbounds (i8, ptr @giggle_data_handler, i64 120), align 8, !tbaa !11
  store ptr @leaf_data_map_intersection_to_offset_list, ptr getelementptr inbounds (i8, ptr @giggle_data_handler, i64 128), align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #3
  store ptr null, ptr %2, align 8, !tbaa !9
  %6 = call ptr @giggle_query(ptr noundef %5, ptr noundef nonnull @.str.8, i32 noundef 20, i32 noundef 50, ptr noundef null) #3
  store ptr %6, ptr %2, align 8, !tbaa !9
  %7 = getelementptr inbounds i8, ptr %6, i64 24
  %8 = load i32, ptr %7, align 8, !tbaa !15
  %9 = zext i32 %8 to i64
  call void @UnityAssertEqualNumber(i64 noundef 8, i64 noundef %9, ptr noundef null, i64 noundef 1953, i32 noundef 148) #3
  call void @giggle_query_result_destroy(ptr noundef nonnull %2) #3
  call void @giggle_index_destroy(ptr noundef nonnull %1) #3
  %10 = load ptr, ptr getelementptr inbounds (i8, ptr @cache, i64 48), align 8, !tbaa !17
  call void (...) %10() #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %1) #3
  %11 = add nuw nsw i32 %.06, 1
  %exitcond.not = icmp eq i32 %11, 101
  br i1 %exitcond.not, label %12, label %3, !llvm.loop !19

12:                                               ; preds = %3
  %13 = call i32 @rmrf(ptr noundef nonnull @.str.11) #3
  ret void
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #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 #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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 120}
!12 = !{!"giggle_def", !13, i64 0, !13, i64 24, !10, i64 48, !10, i64 56, !10, i64 64, !10, i64 72, !10, i64 80, !10, i64 88, !10, i64 96, !10, i64 104, !10, i64 112, !10, i64 120, !10, i64 128}
!13 = !{!"cache_handler", !10, i64 0, !10, i64 8, !10, i64 16}
!14 = !{!12, !10, i64 128}
!15 = !{!16, !6, i64 24}
!16 = !{!"giggle_query_result", !10, i64 0, !6, i64 8, !10, i64 16, !6, i64 24}
!17 = !{!18, !10, i64 48}
!18 = !{!"cache_def", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,SROAPass,SimplifyCFGPass