; LLVM IR from LiuYuguang/supersimplefiledatabase :: filedb (db_search)

; 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.db_s = type { i32, i32, i64, i64, i64, i64, i64, i64, i64, i64, ptr }
%struct.btree_key = type { i64, i64, [0 x i8] }
%struct.btree_node = type { i64, i64, i64, i32 }
%struct.btree_value = type { i64, [0 x i8] }

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #0

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @node_seek(ptr noundef, ptr noundef, i64 noundef) #2

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #3

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #4

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @key_binary_search(ptr noundef, ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @db_search(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) #5 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i64, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i64 %3, ptr %9, align 8, !tbaa !9
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.db_s, ptr %15, i32 0, i32 1
  %17 = load i32, ptr %16, align 4, !tbaa !11
  %18 = icmp eq i32 %17, 0
  br i1 %18, label %19, label %28

19:                                               ; preds = %4
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = call i64 @strlen(ptr noundef %20) #6
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.db_s, ptr %22, i32 0, i32 2
  %24 = load i64, ptr %23, align 8, !tbaa !14
  %25 = icmp uge i64 %21, %24
  br i1 %25, label %26, label %28

26:                                               ; preds = %19
  %27 = call ptr @__errno_location() #7
  store i32 22, ptr %27, align 4, !tbaa !15
  store i32 -1, ptr %5, align 4
  br label %110

28:                                               ; preds = %19, %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #8
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %29, i64 4096
  %31 = getelementptr inbounds i8, ptr %30, i64 0
  store ptr %31, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #8
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #8
  store i64 4096, ptr %12, align 8, !tbaa !9
  br label %32

32:                                               ; preds = %104, %28
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = load ptr, ptr %10, align 8, !tbaa !5
  %35 = load i64, ptr %12, align 8, !tbaa !9
  %36 = call i64 @node_seek(ptr noundef %33, ptr noundef %34, i64 noundef %35)
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = load ptr, ptr %10, align 8, !tbaa !5
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = call i32 @key_binary_search(ptr noundef %37, ptr noundef %38, ptr noundef %39)
  store i32 %40, ptr %11, align 4, !tbaa !15
  %41 = load i32, ptr %11, align 4, !tbaa !15
  %42 = icmp sge i32 %41, 0
  br i1 %42, label %43, label %89

43:                                               ; preds = %32
  %44 = load ptr, ptr %10, align 8, !tbaa !5
  %45 = getelementptr inbounds i8, ptr %44, i64 32
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.db_s, ptr %46, i32 0, i32 3
  %48 = load i64, ptr %47, align 8, !tbaa !16
  %49 = load i32, ptr %11, align 4, !tbaa !15
  %50 = sext i32 %49 to i64
  %51 = mul i64 %48, %50
  %52 = getelementptr inbounds i8, ptr %45, i64 %51
  %53 = getelementptr inbounds %struct.btree_key, ptr %52, i32 0, i32 0
  %54 = load i64, ptr %53, align 8, !tbaa !17
  store i64 %54, ptr %12, align 8, !tbaa !9
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = load ptr, ptr %10, align 8, !tbaa !5
  %57 = load i64, ptr %12, align 8, !tbaa !9
  %58 = sub i64 %57, 4096
  %59 = and i64 %58, -8192
  %60 = add i64 4096, %59
  %61 = call i64 @node_seek(ptr noundef %55, ptr noundef %56, i64 noundef %60)
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #8
  %62 = load ptr, ptr %10, align 8, !tbaa !5
  %63 = load i64, ptr %12, align 8, !tbaa !9
  %64 = load ptr, ptr %10, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.btree_node, ptr %64, i32 0, i32 0
  %66 = load i64, ptr %65, align 8, !tbaa !19
  %67 = sub nsw i64 %63, %66
  %68 = getelementptr inbounds i8, ptr %62, i64 %67
  store ptr %68, ptr %13, align 8, !tbaa !5
  %69 = load ptr, ptr %13, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.btree_value, ptr %69, i32 0, i32 0
  %71 = load i64, ptr %70, align 8, !tbaa !21
  %72 = load i64, ptr %9, align 8, !tbaa !9
  %73 = icmp ugt i64 %71, %72
  br i1 %73, label %74, label %76

74:                                               ; preds = %43
  %75 = call ptr @__errno_location() #7
  store i32 7, ptr %75, align 4, !tbaa !15
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %88

76:                                               ; preds = %43
  %77 = load ptr, ptr %8, align 8, !tbaa !5
  %78 = load ptr, ptr %13, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.btree_value, ptr %78, i32 0, i32 1
  %80 = getelementptr inbounds [0 x i8], ptr %79, i64 0, i64 0
  %81 = load ptr, ptr %13, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.btree_value, ptr %81, i32 0, i32 0
  %83 = load i64, ptr %82, align 8, !tbaa !21
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %77, ptr align 8 %80, i64 %83, i1 false)
  %84 = load ptr, ptr %13, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.btree_value, ptr %84, i32 0, i32 0
  %86 = load i64, ptr %85, align 8, !tbaa !21
  %87 = trunc i64 %86 to i32
  store i32 %87, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %88

88:                                               ; preds = %76, %74
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #8
  br label %109

89:                                               ; preds = %32
  %90 = load i32, ptr %11, align 4, !tbaa !15
  %91 = add nsw i32 %90, 1
  %92 = sub nsw i32 0, %91
  store i32 %92, ptr %11, align 4, !tbaa !15
  %93 = load ptr, ptr %10, align 8, !tbaa !5
  %94 = getelementptr inbounds i8, ptr %93, i64 32
  %95 = load ptr, ptr %6, align 8, !tbaa !5
  %96 = getelementptr inbounds %struct.db_s, ptr %95, i32 0, i32 3
  %97 = load i64, ptr %96, align 8, !tbaa !16
  %98 = load i32, ptr %11, align 4, !tbaa !15
  %99 = sext i32 %98 to i64
  %100 = mul i64 %97, %99
  %101 = getelementptr inbounds i8, ptr %94, i64 %100
  %102 = getelementptr inbounds %struct.btree_key, ptr %101, i32 0, i32 1
  %103 = load i64, ptr %102, align 8, !tbaa !23
  store i64 %103, ptr %12, align 8, !tbaa !9
  br label %104

104:                                              ; preds = %89
  %105 = load i64, ptr %12, align 8, !tbaa !9
  %106 = icmp ne i64 %105, 0
  br i1 %106, label %32, label %107, !llvm.loop !24

107:                                              ; preds = %104
  %108 = call ptr @__errno_location() #7
  store i32 42, ptr %108, align 4, !tbaa !15
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %109

109:                                              ; preds = %107, %88
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #8
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #8
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #8
  br label %110

110:                                              ; preds = %109, %26
  %111 = load i32, ptr %5, align 4
  ret i32 %111
}

attributes #0 = { nounwind willreturn memory(none) "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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 #6 = { nounwind willreturn memory(read) }
attributes #7 = { nounwind willreturn memory(none) }
attributes #8 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 4}
!12 = !{!"db_s", !13, i64 0, !13, i64 4, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !10, i64 64, !6, i64 72}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !10, i64 8}
!15 = !{!13, !13, i64 0}
!16 = !{!12, !10, i64 16}
!17 = !{!18, !10, i64 0}
!18 = !{!"", !10, i64 0, !10, i64 8, !7, i64 16}
!19 = !{!20, !10, i64 0}
!20 = !{!"", !10, i64 0, !10, i64 8, !10, i64 16, !13, i64 24, !13, i64 24, !13, i64 24, !13, i64 24}
!21 = !{!22, !10, i64 0}
!22 = !{!"", !10, i64 0, !7, i64 8}
!23 = !{!18, !10, i64 8}
!24 = distinct !{!24, !25}
!25 = !{!"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"

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @node_seek(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @key_binary_search(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @db_search(ptr noundef %0, ptr noundef %1, ptr nocapture noundef writeonly %2, i64 noundef %3) local_unnamed_addr #4 {
  %5 = getelementptr inbounds i8, ptr %0, i64 4
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = icmp eq i32 %6, 0
  br i1 %7, label %8, label %14

8:                                                ; preds = %4
  %9 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %1) #5
  %10 = getelementptr inbounds i8, ptr %0, i64 8
  %11 = load i64, ptr %10, align 8, !tbaa !12
  %.not = icmp ult i64 %9, %11
  br i1 %.not, label %14, label %12

12:                                               ; preds = %8
  %13 = tail call ptr @__errno_location() #6
  store i32 22, ptr %13, align 4, !tbaa !13
  br label %50

14:                                               ; preds = %8, %4
  %15 = getelementptr inbounds i8, ptr %0, i64 4096
  %16 = getelementptr inbounds i8, ptr %0, i64 16
  %invariant.gep = getelementptr i8, ptr %0, i64 4136
  br label %17

17:                                               ; preds = %43, %14
  %.031 = phi i64 [ 4096, %14 ], [ %47, %43 ]
  %18 = tail call i64 @node_seek(ptr noundef nonnull %0, ptr noundef nonnull %15, i64 noundef %.031)
  %19 = tail call i32 @key_binary_search(ptr noundef nonnull %0, ptr noundef nonnull %15, ptr noundef %1)
  %20 = icmp sgt i32 %19, -1
  %21 = load i64, ptr %16, align 8, !tbaa !14
  br i1 %20, label %22, label %43

22:                                               ; preds = %17
  %23 = getelementptr inbounds i8, ptr %0, i64 4128
  %24 = zext nneg i32 %19 to i64
  %25 = mul i64 %21, %24
  %26 = getelementptr inbounds i8, ptr %23, i64 %25
  %27 = load i64, ptr %26, align 8, !tbaa !15
  %28 = add i64 %27, -4096
  %29 = and i64 %28, -8192
  %30 = or disjoint i64 %29, 4096
  %31 = tail call i64 @node_seek(ptr noundef nonnull %0, ptr noundef nonnull %15, i64 noundef %30)
  %32 = load i64, ptr %15, align 8, !tbaa !17
  %33 = sub nsw i64 %27, %32
  %34 = getelementptr inbounds i8, ptr %15, i64 %33
  %35 = load i64, ptr %34, align 8, !tbaa !19
  %36 = icmp ugt i64 %35, %3
  br i1 %36, label %37, label %39

37:                                               ; preds = %22
  %38 = tail call ptr @__errno_location() #6
  store i32 7, ptr %38, align 4, !tbaa !13
  br label %50

39:                                               ; preds = %22
  %40 = getelementptr inbounds i8, ptr %34, i64 8
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %2, ptr nonnull align 8 %40, i64 %35, i1 false)
  %41 = load i64, ptr %34, align 8, !tbaa !19
  %42 = trunc i64 %41 to i32
  br label %50

43:                                               ; preds = %17
  %44 = xor i32 %19, -1
  %45 = zext nneg i32 %44 to i64
  %46 = mul i64 %21, %45
  %gep = getelementptr i8, ptr %invariant.gep, i64 %46
  %47 = load i64, ptr %gep, align 8, !tbaa !21
  %.not36 = icmp eq i64 %47, 0
  br i1 %.not36, label %48, label %17, !llvm.loop !22

48:                                               ; preds = %43
  %49 = tail call ptr @__errno_location() #6
  store i32 42, ptr %49, align 4, !tbaa !13
  br label %50

50:                                               ; preds = %48, %39, %37, %12
  %.2 = phi i32 [ -1, %12 ], [ -1, %48 ], [ -1, %37 ], [ %42, %39 ]
  ret i32 %.2
}

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

!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, !7, i64 4}
!6 = !{!"db_s", !7, i64 0, !7, i64 4, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !10, i64 64, !11, i64 72}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !10, i64 8}
!13 = !{!7, !7, i64 0}
!14 = !{!6, !10, i64 16}
!15 = !{!16, !10, i64 0}
!16 = !{!"", !10, i64 0, !10, i64 8, !8, i64 16}
!17 = !{!18, !10, i64 0}
!18 = !{!"", !10, i64 0, !10, i64 8, !10, i64 16, !7, i64 24, !7, i64 24, !7, i64 24, !7, i64 24}
!19 = !{!20, !10, i64 0}
!20 = !{!"", !10, i64 0, !8, i64 8}
!21 = !{!16, !10, i64 8}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass