; LLVM IR from recrsn/tinydb :: database (filter)

; 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.table = type { %struct.table_info, i8, ptr, ptr }
%struct.table_info = type { [32 x i8], i32, [32 x %struct.field], i64 }
%struct.field = type { [32 x i8], i64, i32 }

; 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 willreturn memory(read)
declare i32 @strcmp(ptr noundef, ptr noundef) #1

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

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @read_field(ptr noundef, ptr noundef, i64 noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @filter(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) #2 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca [2048 x i8], align 16
  %10 = alloca [2048 x i8], align 16
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2048, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 2048, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.table, ptr %13, i32 0, i32 0
  %15 = getelementptr inbounds %struct.table_info, ptr %14, i32 0, i32 2
  %16 = load i32, ptr %7, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [32 x %struct.field], ptr %15, i64 0, i64 %17
  %19 = getelementptr inbounds %struct.field, ptr %18, i32 0, i32 2
  %20 = load i32, ptr %19, align 8, !tbaa !11
  store i32 %20, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %21

21:                                               ; preds = %54, %4
  %22 = load i32, ptr %12, align 4, !tbaa !9
  %23 = sext i32 %22 to i64
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.table, ptr %24, i32 0, i32 0
  %26 = getelementptr inbounds %struct.table_info, ptr %25, i32 0, i32 3
  %27 = load i64, ptr %26, align 8, !tbaa !14
  %28 = icmp ult i64 %23, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %57

30:                                               ; preds = %21
  %31 = getelementptr inbounds [2048 x i8], ptr %10, i64 0, i64 0
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = load i32, ptr %12, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = load i32, ptr %7, align 4, !tbaa !9
  %36 = call zeroext i1 @read_field(ptr noundef %31, ptr noundef %32, i64 noundef %34, i32 noundef %35)
  %37 = getelementptr inbounds [2048 x i8], ptr %9, i64 0, i64 0
  %38 = getelementptr inbounds [2048 x i8], ptr %10, i64 0, i64 0
  %39 = load i32, ptr %11, align 4, !tbaa !9
  call void @decode_field(ptr noundef %37, ptr noundef %38, i32 noundef %39)
  %40 = getelementptr inbounds [2048 x i8], ptr %9, i64 0, i64 0
  %41 = load ptr, ptr %8, align 8, !tbaa !5
  %42 = call i32 @strcmp(ptr noundef %40, ptr noundef %41) #4
  %43 = icmp eq i32 %42, 0
  %44 = zext i1 %43 to i32
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = load i8, ptr %45, align 1, !tbaa !18, !range !19, !noundef !20
  %47 = trunc i8 %46 to i1
  %48 = zext i1 %47 to i32
  %49 = and i32 %48, %44
  %50 = icmp ne i32 %49, 0
  %51 = zext i1 %50 to i8
  store i8 %51, ptr %45, align 1, !tbaa !18
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = getelementptr inbounds i8, ptr %52, i32 1
  store ptr %53, ptr %5, align 8, !tbaa !5
  br label %54

54:                                               ; preds = %30
  %55 = load i32, ptr %12, align 4, !tbaa !9
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %12, align 4, !tbaa !9
  br label %21, !llvm.loop !21

57:                                               ; preds = %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 2048, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 2048, ptr %9) #3
  ret void
}

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

!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, !10, i64 40}
!12 = !{!"", !7, i64 0, !13, i64 32, !10, i64 40}
!13 = !{!"long", !7, i64 0}
!14 = !{!15, !13, i64 1576}
!15 = !{!"", !16, i64 0, !17, i64 1584, !6, i64 1592, !6, i64 1600}
!16 = !{!"", !7, i64 0, !10, i64 32, !7, i64 40, !13, i64 1576}
!17 = !{!"_Bool", !7, i64 0}
!18 = !{!17, !17, i64 0}
!19 = !{i8 0, i8 2}
!20 = !{}
!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.field = type { [32 x i8], i64, i32 }

; 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: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i32 @strcmp(ptr nocapture noundef, ptr nocapture noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @read_field(ptr noundef, ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local void @filter(ptr nocapture noundef %0, ptr noundef %1, i32 noundef %2, ptr nocapture noundef readonly %3) local_unnamed_addr #2 {
  %5 = alloca [2048 x i8], align 16
  %6 = alloca [2048 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 2048, ptr nonnull %5) #3
  call void @llvm.lifetime.start.p0(i64 2048, ptr nonnull %6) #3
  %7 = getelementptr inbounds i8, ptr %1, i64 40
  %8 = sext i32 %2 to i64
  %9 = getelementptr inbounds [32 x %struct.field], ptr %7, i64 0, i64 %8, i32 2
  %10 = load i32, ptr %9, align 8, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %1, i64 1576
  %12 = load i64, ptr %11, align 8, !tbaa !11
  %.not = icmp eq i64 %12, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %4
  call void @llvm.lifetime.end.p0(i64 2048, ptr nonnull %6) #3
  call void @llvm.lifetime.end.p0(i64 2048, ptr nonnull %5) #3
  ret void

.lr.ph:                                           ; preds = %4, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %4 ]
  %.01113 = phi ptr [ %20, %.lr.ph ], [ %0, %4 ]
  %13 = call zeroext i1 @read_field(ptr noundef nonnull %6, ptr noundef nonnull %1, i64 noundef %indvars.iv, i32 noundef %2)
  call void @decode_field(ptr noundef nonnull %5, ptr noundef nonnull %6, i32 noundef %10)
  %14 = call i32 @strcmp(ptr noundef nonnull dereferenceable(1) %5, ptr noundef nonnull dereferenceable(1) %3) #4
  %15 = icmp eq i32 %14, 0
  %16 = load i8, ptr %.01113, align 1, !tbaa !16, !range !17, !noundef !18
  %17 = icmp ne i8 %16, 0
  %18 = and i1 %15, %17
  %19 = zext i1 %18 to i8
  store i8 %19, ptr %.01113, align 1, !tbaa !16
  %20 = getelementptr inbounds i8, ptr %.01113, i64 1
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %21 = load i64, ptr %11, align 8, !tbaa !11
  %22 = icmp ugt i64 %21, %indvars.iv.next
  br i1 %22, label %.lr.ph, label %._crit_edge, !llvm.loop !19
}

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

!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 40}
!6 = !{!"", !7, i64 0, !9, i64 32, !10, i64 40}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"long", !7, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !9, i64 1576}
!12 = !{!"", !13, i64 0, !14, i64 1584, !15, i64 1592, !15, i64 1600}
!13 = !{!"", !7, i64 0, !10, i64 32, !7, i64 40, !9, i64 1576}
!14 = !{!"_Bool", !7, i64 0}
!15 = !{!"any pointer", !7, i64 0}
!16 = !{!14, !14, i64 0}
!17 = !{i8 0, i8 2}
!18 = !{}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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