; 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_iter = type { ptr, i32, i32, i32, ptr, ptr, ptr }
%struct.giggle_index = type { ptr, i32, i32, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.offset_index = type { ptr, ptr, i32, ptr, i32 }
%struct.file_id_offset_pairs = type { i64, i64, ptr }
%struct.long_uint_pair = type { i64, i32 }

; Function Attrs: nounwind uwtable
define dso_local i32 @giggle_query_next_data(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %6 = load ptr, ptr %4, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.giggle_query_iter, ptr %6, i32 0, i32 3
  %8 = load i32, ptr %7, align 8, !tbaa !9
  %9 = icmp eq i32 %8, 0
  br i1 %9, label %18, label %10

10:                                               ; preds = %2
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.giggle_query_iter, ptr %11, i32 0, i32 2
  %13 = load i32, ptr %12, align 4, !tbaa !12
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.giggle_query_iter, ptr %14, i32 0, i32 3
  %16 = load i32, ptr %15, align 8, !tbaa !9
  %17 = icmp eq i32 %13, %16
  br i1 %17, label %18, label %19

18:                                               ; preds = %10, %2
  store i32 -1, ptr %3, align 4
  br label %67

19:                                               ; preds = %10
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.giggle_query_iter, ptr %20, i32 0, i32 0
  %22 = load ptr, ptr %21, align 8, !tbaa !13
  %23 = getelementptr inbounds %struct.giggle_index, ptr %22, i32 0, i32 5
  %24 = load ptr, ptr %23, align 8, !tbaa !14
  %25 = getelementptr inbounds %struct.offset_index, ptr %24, i32 0, i32 0
  %26 = load ptr, ptr %25, align 8, !tbaa !16
  %27 = getelementptr inbounds %struct.file_id_offset_pairs, ptr %26, i32 0, i32 2
  %28 = load ptr, ptr %27, align 8, !tbaa !18
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.giggle_query_iter, ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %30, align 8, !tbaa !13
  %32 = getelementptr inbounds %struct.giggle_index, ptr %31, i32 0, i32 5
  %33 = load ptr, ptr %32, align 8, !tbaa !14
  %34 = getelementptr inbounds %struct.offset_index, ptr %33, i32 0, i32 4
  %35 = load i32, ptr %34, align 8, !tbaa !21
  %36 = icmp eq i32 %35, 0
  %37 = zext i1 %36 to i64
  %38 = select i1 %36, i64 0, i64 12
  %39 = getelementptr inbounds i8, ptr %28, i64 %38
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.giggle_query_iter, ptr %40, i32 0, i32 6
  %42 = load ptr, ptr %41, align 8, !tbaa !22
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.giggle_query_iter, ptr %43, i32 0, i32 2
  %45 = load i32, ptr %44, align 4, !tbaa !12
  %46 = zext i32 %45 to i64
  %47 = getelementptr inbounds %struct.long_uint_pair, ptr %42, i64 %46
  %48 = getelementptr inbounds %struct.long_uint_pair, ptr %47, i32 0, i32 1
  %49 = load i32, ptr %48, align 8, !tbaa !23
  %50 = zext i32 %49 to i64
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.giggle_query_iter, ptr %51, i32 0, i32 0
  %53 = load ptr, ptr %52, align 8, !tbaa !13
  %54 = getelementptr inbounds %struct.giggle_index, ptr %53, i32 0, i32 5
  %55 = load ptr, ptr %54, align 8, !tbaa !14
  %56 = getelementptr inbounds %struct.offset_index, ptr %55, i32 0, i32 2
  %57 = load i32, ptr %56, align 8, !tbaa !25
  %58 = zext i32 %57 to i64
  %59 = mul i64 %50, %58
  %60 = add i64 %59, 16
  %61 = getelementptr inbounds i8, ptr %39, i64 %60
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %61, ptr %62, align 8, !tbaa !5
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.giggle_query_iter, ptr %63, i32 0, i32 2
  %65 = load i32, ptr %64, align 4, !tbaa !12
  %66 = add i32 %65, 1
  store i32 %66, ptr %64, align 4, !tbaa !12
  store i32 0, ptr %3, align 4
  br label %67

67:                                               ; preds = %19, %18
  %68 = load i32, ptr %3, align 4
  ret i32 %68
}

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

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

%struct.long_uint_pair = type { i64, i32 }

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local range(i32 -1, 1) i32 @giggle_query_next_data(ptr nocapture noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 16
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %35, label %6

6:                                                ; preds = %2
  %7 = getelementptr inbounds i8, ptr %0, i64 12
  %8 = load i32, ptr %7, align 4, !tbaa !11
  %9 = icmp eq i32 %8, %4
  br i1 %9, label %35, label %10

10:                                               ; preds = %6
  %11 = load ptr, ptr %0, align 8, !tbaa !12
  %12 = getelementptr inbounds i8, ptr %11, i64 32
  %13 = load ptr, ptr %12, align 8, !tbaa !13
  %14 = load ptr, ptr %13, align 8, !tbaa !15
  %15 = getelementptr inbounds i8, ptr %14, i64 16
  %16 = load ptr, ptr %15, align 8, !tbaa !17
  %17 = getelementptr inbounds i8, ptr %13, i64 32
  %18 = load i32, ptr %17, align 8, !tbaa !20
  %19 = icmp eq i32 %18, 0
  %20 = select i1 %19, i64 0, i64 12
  %21 = getelementptr inbounds i8, ptr %16, i64 %20
  %22 = getelementptr inbounds i8, ptr %0, i64 40
  %23 = load ptr, ptr %22, align 8, !tbaa !21
  %24 = zext i32 %8 to i64
  %25 = getelementptr inbounds %struct.long_uint_pair, ptr %23, i64 %24, i32 1
  %26 = load i32, ptr %25, align 8, !tbaa !22
  %27 = zext i32 %26 to i64
  %28 = getelementptr inbounds i8, ptr %13, i64 16
  %29 = load i32, ptr %28, align 8, !tbaa !24
  %30 = zext i32 %29 to i64
  %31 = mul nuw i64 %30, %27
  %32 = getelementptr i8, ptr %21, i64 %31
  %33 = getelementptr i8, ptr %32, i64 16
  store ptr %33, ptr %1, align 8, !tbaa !25
  %34 = add i32 %8, 1
  store i32 %34, ptr %7, align 4, !tbaa !11
  br label %35

35:                                               ; preds = %2, %6, %10
  %.0 = phi i32 [ 0, %10 ], [ -1, %6 ], [ -1, %2 ]
  ret i32 %.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: readwrite, inaccessiblemem: none) 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" }

!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, !10, i64 16}
!6 = !{!"giggle_query_iter", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !7, i64 24, !7, i64 32, !7, i64 40}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 12}
!12 = !{!6, !7, i64 0}
!13 = !{!14, !7, i64 32}
!14 = !{!"giggle_index", !7, i64 0, !10, i64 8, !10, i64 12, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56}
!15 = !{!16, !7, i64 0}
!16 = !{!"offset_index", !7, i64 0, !7, i64 8, !10, i64 16, !7, i64 24, !10, i64 32}
!17 = !{!18, !7, i64 16}
!18 = !{!"file_id_offset_pairs", !19, i64 0, !19, i64 8, !7, i64 16}
!19 = !{!"long", !8, i64 0}
!20 = !{!16, !10, i64 32}
!21 = !{!6, !7, i64 40}
!22 = !{!23, !10, i64 8}
!23 = !{!"long_uint_pair", !19, i64 0, !10, i64 8}
!24 = !{!16, !10, i64 16}
!25 = !{!7, !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass