; 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.glob_t = type { i64, ptr, i64, i32, ptr, ptr, ptr, ptr, ptr }

@stderr = external global ptr, align 8
@.str.17 = external hidden unnamed_addr constant [38 x i8], align 1
@.str.18 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.19 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.20 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.21 = external hidden unnamed_addr constant [16 x i8], align 1
@.str.22 = 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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @giggle_index_file(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @giggle_index_directory(ptr noundef %0, ptr noundef %1, i32 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca %struct.glob_t, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 72, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = call i32 @glob(ptr noundef %11, i32 noundef 0, ptr noundef null, ptr noundef %7) #3
  store i32 %12, ptr %8, align 4, !tbaa !9
  %13 = load i32, ptr %8, align 4, !tbaa !9
  %14 = icmp ne i32 %13, 0
  br i1 %14, label %15, label %35

15:                                               ; preds = %3
  %16 = load ptr, ptr @stderr, align 8, !tbaa !5
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = load i32, ptr %8, align 4, !tbaa !9
  %19 = icmp eq i32 %18, 2
  br i1 %19, label %20, label %21

20:                                               ; preds = %15
  br label %32

21:                                               ; preds = %15
  %22 = load i32, ptr %8, align 4, !tbaa !9
  %23 = icmp eq i32 %22, 3
  br i1 %23, label %24, label %25

24:                                               ; preds = %21
  br label %30

25:                                               ; preds = %21
  %26 = load i32, ptr %8, align 4, !tbaa !9
  %27 = icmp eq i32 %26, 1
  %28 = zext i1 %27 to i64
  %29 = select i1 %27, ptr @.str.20, ptr @.str.21
  br label %30

30:                                               ; preds = %25, %24
  %31 = phi ptr [ @.str.19, %24 ], [ %29, %25 ]
  br label %32

32:                                               ; preds = %30, %20
  %33 = phi ptr [ @.str.18, %20 ], [ %31, %30 ]
  %34 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %16, ptr noundef @.str.17, ptr noundef %17, ptr noundef %33) #3
  br label %35

35:                                               ; preds = %32, %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !9
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %36

36:                                               ; preds = %65, %35
  %37 = load i32, ptr %9, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds %struct.glob_t, ptr %7, i32 0, i32 0
  %40 = load i64, ptr %39, align 8, !tbaa !11
  %41 = icmp ult i64 %38, %40
  br i1 %41, label %42, label %68

42:                                               ; preds = %36
  %43 = load i32, ptr %6, align 4, !tbaa !9
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %45, label %54

45:                                               ; preds = %42
  %46 = load ptr, ptr @stderr, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.glob_t, ptr %7, i32 0, i32 1
  %48 = load ptr, ptr %47, align 8, !tbaa !14
  %49 = load i32, ptr %9, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds ptr, ptr %48, i64 %50
  %52 = load ptr, ptr %51, align 8, !tbaa !5
  %53 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %46, ptr noundef @.str.22, ptr noundef %52) #3
  br label %54

54:                                               ; preds = %45, %42
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.glob_t, ptr %7, i32 0, i32 1
  %57 = load ptr, ptr %56, align 8, !tbaa !14
  %58 = load i32, ptr %9, align 4, !tbaa !9
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds ptr, ptr %57, i64 %59
  %61 = load ptr, ptr %60, align 8, !tbaa !5
  %62 = call i32 @giggle_index_file(ptr noundef %55, ptr noundef %61)
  %63 = load i32, ptr %10, align 4, !tbaa !9
  %64 = add i32 %63, %62
  store i32 %64, ptr %10, align 4, !tbaa !9
  br label %65

65:                                               ; preds = %54
  %66 = load i32, ptr %9, align 4, !tbaa !9
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %9, align 4, !tbaa !9
  br label %36, !llvm.loop !15

68:                                               ; preds = %36
  call void @globfree(ptr noundef %7) #3
  %69 = load i32, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 72, ptr %7) #3
  ret i32 %69
}

; Function Attrs: nounwind
declare i32 @glob(ptr noundef, i32 noundef, ptr noundef, ptr noundef) #2

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #2

; Function Attrs: nounwind
declare void @globfree(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #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, !13, i64 0}
!12 = !{!"", !13, i64 0, !6, i64 8, !13, i64 16, !10, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !6, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"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.glob_t = type { i64, ptr, i64, i32, ptr, ptr, ptr, ptr, ptr }

@stderr = external local_unnamed_addr global ptr, align 8
@.str.17 = external hidden unnamed_addr constant [38 x i8], align 1
@.str.18 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.19 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.20 = external hidden unnamed_addr constant [18 x i8], align 1
@.str.21 = external hidden unnamed_addr constant [16 x i8], align 1
@.str.22 = 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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @giggle_index_file(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @giggle_index_directory(ptr noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = alloca %struct.glob_t, align 8
  call void @llvm.lifetime.start.p0(i64 72, ptr nonnull %4) #4
  %5 = call i32 @glob(ptr noundef %1, i32 noundef 0, ptr noundef null, ptr noundef nonnull %4) #4
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %14, label %6

6:                                                ; preds = %3
  %7 = load ptr, ptr @stderr, align 8, !tbaa !5
  switch i32 %5, label %8 [
    i32 2, label %11
    i32 3, label %.fold.split
  ]

8:                                                ; preds = %6
  %9 = icmp eq i32 %5, 1
  %10 = select i1 %9, ptr @.str.20, ptr @.str.21
  br label %11

.fold.split:                                      ; preds = %6
  br label %11

11:                                               ; preds = %6, %.fold.split, %8
  %12 = phi ptr [ @.str.18, %6 ], [ %10, %8 ], [ @.str.19, %.fold.split ]
  %13 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %7, ptr noundef nonnull @.str.17, ptr noundef %1, ptr noundef nonnull %12) #5
  br label %14

14:                                               ; preds = %11, %3
  %15 = load i64, ptr %4, align 8, !tbaa !9
  %.not19 = icmp eq i64 %15, 0
  br i1 %.not19, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %14
  %.not16 = icmp eq i32 %2, 0
  %16 = getelementptr inbounds i8, ptr %4, i64 8
  br i1 %.not16, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph, %.lr.ph.split.us
  %indvars.iv23 = phi i64 [ %indvars.iv.next24, %.lr.ph.split.us ], [ 0, %.lr.ph ]
  %.018.us = phi i32 [ %21, %.lr.ph.split.us ], [ 0, %.lr.ph ]
  %17 = load ptr, ptr %16, align 8, !tbaa !13
  %18 = getelementptr inbounds ptr, ptr %17, i64 %indvars.iv23
  %19 = load ptr, ptr %18, align 8, !tbaa !5
  %20 = call i32 @giggle_index_file(ptr noundef %0, ptr noundef %19)
  %21 = add i32 %20, %.018.us
  %indvars.iv.next24 = add nuw nsw i64 %indvars.iv23, 1
  %22 = load i64, ptr %4, align 8, !tbaa !9
  %23 = icmp ugt i64 %22, %indvars.iv.next24
  br i1 %23, label %.lr.ph.split.us, label %._crit_edge, !llvm.loop !14

.lr.ph.split:                                     ; preds = %.lr.ph, %.lr.ph.split
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph.split ], [ 0, %.lr.ph ]
  %.018 = phi i32 [ %33, %.lr.ph.split ], [ 0, %.lr.ph ]
  %24 = load ptr, ptr @stderr, align 8, !tbaa !5
  %25 = load ptr, ptr %16, align 8, !tbaa !13
  %26 = getelementptr inbounds ptr, ptr %25, i64 %indvars.iv
  %27 = load ptr, ptr %26, align 8, !tbaa !5
  %28 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %24, ptr noundef nonnull @.str.22, ptr noundef %27) #5
  %29 = load ptr, ptr %16, align 8, !tbaa !13
  %30 = getelementptr inbounds ptr, ptr %29, i64 %indvars.iv
  %31 = load ptr, ptr %30, align 8, !tbaa !5
  %32 = call i32 @giggle_index_file(ptr noundef %0, ptr noundef %31)
  %33 = add i32 %32, %.018
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %34 = load i64, ptr %4, align 8, !tbaa !9
  %35 = icmp ugt i64 %34, %indvars.iv.next
  br i1 %35, label %.lr.ph.split, label %._crit_edge, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph.split, %.lr.ph.split.us, %14
  %.0.lcssa = phi i32 [ 0, %14 ], [ %21, %.lr.ph.split.us ], [ %33, %.lr.ph.split ]
  call void @globfree(ptr noundef nonnull %4) #4
  call void @llvm.lifetime.end.p0(i64 72, ptr nonnull %4) #4
  ret i32 %.0.lcssa
}

; Function Attrs: nounwind
declare i32 @glob(ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: nounwind
declare void @globfree(ptr noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #3 = { nofree 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 #4 = { nounwind }
attributes #5 = { cold 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 = !{!"", !11, i64 0, !6, i64 8, !11, i64 16, !12, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64}
!11 = !{!"long", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !6, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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