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

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

@cache = external dso_local global %struct.cache_def, align 8
@giggle_data_handler = external dso_local global %struct.giggle_def, align 8
@.str.18 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.32 = external hidden unnamed_addr constant [21 x i8], align 1
@.str.33 = external hidden unnamed_addr constant [4 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 @uint64_t_ll_giggle_set_data_handler(...) #1

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 void @giggle_index_destroy(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @valid_giggle_index(ptr noundef) #2

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

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

declare i32 @giggle_store(ptr noundef) #1

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

declare i32 @rmrf(ptr noundef) #1

declare void @giggle_write_tree_leaf_data(ptr noundef) #1

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

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

declare void @giggle_query_result_destroy(ptr noundef) #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

; Function Attrs: nounwind uwtable
define dso_local void @test_giggle_query_bug_0() #2 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  %8 = call ptr @giggle_init(i32 noundef 24, ptr noundef @.str.33, i32 noundef 1, ptr noundef @uint64_t_ll_giggle_set_data_handler)
  store ptr %8, ptr %1, align 8, !tbaa !5
  store ptr @giggle_write_tree_leaf_data, ptr getelementptr inbounds (%struct.giggle_def, ptr @giggle_data_handler, i32 0, i32 10), align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  store ptr @.str.32, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %9 = load ptr, ptr %1, align 8, !tbaa !5
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = call i32 @giggle_index_directory(ptr noundef %9, ptr noundef %10, i32 noundef 0)
  store i32 %11, ptr %3, align 4, !tbaa !12
  %12 = load ptr, ptr %1, align 8, !tbaa !5
  call void @valid_giggle_index(ptr noundef %12)
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %13 = load ptr, ptr %1, align 8, !tbaa !5
  %14 = call ptr @giggle_query(ptr noundef %13, ptr noundef @.str.18, i32 noundef 23669300, i32 noundef 23671590, ptr noundef null)
  store ptr %14, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !12
  br label %15

15:                                               ; preds = %27, %0
  %16 = load i32, ptr %6, align 4, !tbaa !12
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.giggle_query_result, ptr %17, i32 0, i32 1
  %19 = load i32, ptr %18, align 8, !tbaa !14
  %20 = icmp ult i32 %16, %19
  br i1 %20, label %21, label %30

21:                                               ; preds = %15
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = load i32, ptr %6, align 4, !tbaa !12
  %24 = call i32 @giggle_get_query_len(ptr noundef %22, i32 noundef %23)
  %25 = load i32, ptr %5, align 4, !tbaa !12
  %26 = add i32 %25, %24
  store i32 %26, ptr %5, align 4, !tbaa !12
  br label %27

27:                                               ; preds = %21
  %28 = load i32, ptr %6, align 4, !tbaa !12
  %29 = add i32 %28, 1
  store i32 %29, ptr %6, align 4, !tbaa !12
  br label %15, !llvm.loop !16

30:                                               ; preds = %15
  call void @giggle_query_result_destroy(ptr noundef %4)
  %31 = load ptr, ptr %1, align 8, !tbaa !5
  %32 = call i32 @giggle_store(ptr noundef %31)
  %33 = zext i32 %32 to i64
  call void @UnityAssertEqualNumber(i64 noundef 0, i64 noundef %33, ptr noundef null, i64 noundef 1731, i32 noundef 148)
  call void @giggle_index_destroy(ptr noundef %1)
  %34 = load ptr, ptr getelementptr inbounds (%struct.cache_def, ptr @cache, i32 0, i32 6), align 8, !tbaa !18
  call void (...) %34()
  %35 = call ptr @giggle_load(ptr noundef @.str.33, ptr noundef @uint64_t_ll_giggle_set_data_handler)
  store ptr %35, ptr %1, 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 !20
  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 !21
  %36 = load ptr, ptr %1, align 8, !tbaa !5
  %37 = call ptr @giggle_query(ptr noundef %36, ptr noundef @.str.18, i32 noundef 23669300, i32 noundef 23671590, ptr noundef null)
  store ptr %37, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !12
  store i32 0, ptr %6, align 4, !tbaa !12
  br label %38

38:                                               ; preds = %50, %30
  %39 = load i32, ptr %6, align 4, !tbaa !12
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.giggle_query_result, ptr %40, i32 0, i32 1
  %42 = load i32, ptr %41, align 8, !tbaa !14
  %43 = icmp ult i32 %39, %42
  br i1 %43, label %44, label %53

44:                                               ; preds = %38
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = load i32, ptr %6, align 4, !tbaa !12
  %47 = call i32 @giggle_get_query_len(ptr noundef %45, i32 noundef %46)
  %48 = load i32, ptr %7, align 4, !tbaa !12
  %49 = add i32 %48, %47
  store i32 %49, ptr %7, align 4, !tbaa !12
  br label %50

50:                                               ; preds = %44
  %51 = load i32, ptr %6, align 4, !tbaa !12
  %52 = add i32 %51, 1
  store i32 %52, ptr %6, align 4, !tbaa !12
  br label %38, !llvm.loop !22

53:                                               ; preds = %38
  %54 = load i32, ptr %5, align 4, !tbaa !12
  %55 = zext i32 %54 to i64
  %56 = load i32, ptr %7, align 4, !tbaa !12
  %57 = zext i32 %56 to i64
  call void @UnityAssertEqualNumber(i64 noundef %55, i64 noundef %57, ptr noundef null, i64 noundef 1754, i32 noundef 148)
  call void @giggle_query_result_destroy(ptr noundef %4)
  call void @giggle_index_destroy(ptr noundef %1)
  %58 = load ptr, ptr getelementptr inbounds (%struct.cache_def, ptr @cache, i32 0, i32 6), align 8, !tbaa !18
  call void (...) %58()
  %59 = call i32 @rmrf(ptr noundef @.str.33)
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  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, !6, i64 112}
!10 = !{!"giggle_def", !11, i64 0, !11, 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}
!11 = !{!"cache_handler", !6, i64 0, !6, i64 8, !6, i64 16}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !13, i64 8}
!15 = !{!"giggle_query_result", !6, i64 0, !13, i64 8, !6, i64 16, !13, i64 24}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!19, !6, i64 48}
!19 = !{!"cache_def", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48}
!20 = !{!10, !6, i64 120}
!21 = !{!10, !6, i64 128}
!22 = distinct !{!22, !17}


; 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.cache_def = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%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 }

@cache = external dso_local local_unnamed_addr global %struct.cache_def, align 8
@giggle_data_handler = external dso_local local_unnamed_addr global %struct.giggle_def, align 8
@.str.18 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.32 = external hidden unnamed_addr constant [21 x i8], align 1
@.str.33 = external hidden unnamed_addr constant [4 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 @uint64_t_ll_giggle_set_data_handler(...) #1

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 void @giggle_index_destroy(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @valid_giggle_index(ptr noundef) local_unnamed_addr #2

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

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

declare i32 @giggle_store(ptr noundef) local_unnamed_addr #1

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

declare i32 @rmrf(ptr noundef) local_unnamed_addr #1

declare void @giggle_write_tree_leaf_data(ptr noundef) #1

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

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

declare void @giggle_query_result_destroy(ptr noundef) local_unnamed_addr #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

; Function Attrs: nounwind uwtable
define dso_local void @test_giggle_query_bug_0() local_unnamed_addr #2 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #3
  %3 = tail call ptr @giggle_init(i32 noundef 24, ptr noundef nonnull @.str.33, i32 noundef 1, ptr noundef nonnull @uint64_t_ll_giggle_set_data_handler) #3
  store ptr %3, ptr %1, align 8, !tbaa !5
  store ptr @giggle_write_tree_leaf_data, ptr getelementptr inbounds (i8, ptr @giggle_data_handler, i64 112), align 8, !tbaa !9
  %4 = tail call i32 @giggle_index_directory(ptr noundef %3, ptr noundef nonnull @.str.32, i32 noundef 0) #3
  tail call void @valid_giggle_index(ptr noundef %3)
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #3
  %5 = tail call ptr @giggle_query(ptr noundef %3, ptr noundef nonnull @.str.18, i32 noundef 23669300, i32 noundef 23671590, ptr noundef null) #3
  store ptr %5, ptr %2, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %5, i64 8
  %7 = load i32, ptr %6, align 8, !tbaa !12
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.01013 = phi i32 [ %10, %.lr.ph ], [ 0, %0 ]
  %.01112 = phi i32 [ %9, %.lr.ph ], [ 0, %0 ]
  %8 = tail call i32 @giggle_get_query_len(ptr noundef nonnull %5, i32 noundef %.01013) #3
  %9 = add i32 %8, %.01112
  %10 = add nuw i32 %.01013, 1
  %11 = load i32, ptr %6, align 8, !tbaa !12
  %12 = icmp ult i32 %10, %11
  br i1 %12, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !15

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %13 = zext i32 %9 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %0
  %.011.lcssa = phi i64 [ 0, %0 ], [ %13, %._crit_edge.loopexit ]
  call void @giggle_query_result_destroy(ptr noundef nonnull %2) #3
  %14 = call i32 @giggle_store(ptr noundef %3) #3
  %15 = zext i32 %14 to i64
  call void @UnityAssertEqualNumber(i64 noundef 0, i64 noundef %15, ptr noundef null, i64 noundef 1731, i32 noundef 148) #3
  call void @giggle_index_destroy(ptr noundef nonnull %1) #3
  %16 = load ptr, ptr getelementptr inbounds (i8, ptr @cache, i64 48), align 8, !tbaa !17
  call void (...) %16() #3
  %17 = call ptr @giggle_load(ptr noundef nonnull @.str.33, ptr noundef nonnull @uint64_t_ll_giggle_set_data_handler) #3
  store ptr %17, ptr %1, align 8, !tbaa !5
  store ptr @giggle_collect_intersection_data_in_block, ptr getelementptr inbounds (i8, ptr @giggle_data_handler, i64 120), align 8, !tbaa !19
  store ptr @leaf_data_map_intersection_to_offset_list, ptr getelementptr inbounds (i8, ptr @giggle_data_handler, i64 128), align 8, !tbaa !20
  %18 = call ptr @giggle_query(ptr noundef %17, ptr noundef nonnull @.str.18, i32 noundef 23669300, i32 noundef 23671590, ptr noundef null) #3
  store ptr %18, ptr %2, align 8, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %18, i64 8
  %20 = load i32, ptr %19, align 8, !tbaa !12
  %.not20 = icmp eq i32 %20, 0
  br i1 %.not20, label %._crit_edge18, label %.lr.ph17

.lr.ph17:                                         ; preds = %._crit_edge, %.lr.ph17
  %21 = phi ptr [ %25, %.lr.ph17 ], [ %18, %._crit_edge ]
  %.015 = phi i32 [ %23, %.lr.ph17 ], [ 0, %._crit_edge ]
  %.114 = phi i32 [ %24, %.lr.ph17 ], [ 0, %._crit_edge ]
  %22 = call i32 @giggle_get_query_len(ptr noundef nonnull %21, i32 noundef %.114) #3
  %23 = add i32 %22, %.015
  %24 = add nuw i32 %.114, 1
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %25, i64 8
  %27 = load i32, ptr %26, align 8, !tbaa !12
  %28 = icmp ult i32 %24, %27
  br i1 %28, label %.lr.ph17, label %._crit_edge18.loopexit, !llvm.loop !21

._crit_edge18.loopexit:                           ; preds = %.lr.ph17
  %29 = zext i32 %23 to i64
  br label %._crit_edge18

._crit_edge18:                                    ; preds = %._crit_edge18.loopexit, %._crit_edge
  %.0.lcssa = phi i64 [ 0, %._crit_edge ], [ %29, %._crit_edge18.loopexit ]
  call void @UnityAssertEqualNumber(i64 noundef %.011.lcssa, i64 noundef %.0.lcssa, ptr noundef null, i64 noundef 1754, 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
  %30 = load ptr, ptr getelementptr inbounds (i8, ptr @cache, i64 48), align 8, !tbaa !17
  call void (...) %30() #3
  %31 = call i32 @rmrf(ptr noundef nonnull @.str.33) #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
  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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 112}
!10 = !{!"giggle_def", !11, i64 0, !11, 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}
!11 = !{!"cache_handler", !6, i64 0, !6, i64 8, !6, i64 16}
!12 = !{!13, !14, i64 8}
!13 = !{!"giggle_query_result", !6, i64 0, !14, i64 8, !6, i64 16, !14, i64 24}
!14 = !{!"int", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!18, !6, i64 48}
!18 = !{!"cache_def", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48}
!19 = !{!10, !6, i64 120}
!20 = !{!10, !6, i64 128}
!21 = distinct !{!21, !16}


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