; LLVM IR from adventure-db/adventure :: tests/db (btree_tests_perf)

; 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.bt_cur = type { i64, i16 }

@s = external hidden global ptr, align 8
@root = external hidden global i64, align 8
@n = external hidden global i32, align 4
@.str.8 = external hidden unnamed_addr constant [27 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [27 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [31 x i8], align 1

; Function Attrs: nounwind
declare void @srand(i32 noundef) #0

; Function Attrs: nounwind
declare i64 @time(ptr noundef) #0

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

; Function Attrs: nounwind
declare i64 @clock() #0

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

declare i32 @printf(ptr noundef, ...) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @test_perf_query_rand() #3 {
  %1 = alloca i64, align 8
  %2 = alloca i64, align 8
  %3 = alloca %struct.bt_cur, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca %struct.bt_cur, align 8
  %7 = alloca i32, align 4
  %8 = alloca double, align 8
  %9 = call i64 @time(ptr noundef null) #5
  %10 = trunc i64 %9 to i32
  call void @srand(i32 noundef %10) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #5
  %11 = call i64 @clock() #5
  store i64 %11, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #5
  call void @llvm.lifetime.start.p0(i64 16, ptr %3) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %28, %0
  %13 = load i32, ptr %4, align 4, !tbaa !9
  %14 = load i32, ptr @n, align 4, !tbaa !9
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  br label %31

17:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  %18 = call i32 @rand() #5
  store i32 %18, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr %6) #5
  %19 = load ptr, ptr @s, align 8, !tbaa !11
  %20 = load i64, ptr @root, align 8, !tbaa !5
  %21 = load i32, ptr %5, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = call { i64, i16 } @btree_find(ptr noundef %19, i64 noundef %20, i64 noundef %22)
  %24 = getelementptr inbounds { i64, i16 }, ptr %6, i32 0, i32 0
  %25 = extractvalue { i64, i16 } %23, 0
  store i64 %25, ptr %24, align 8
  %26 = getelementptr inbounds { i64, i16 }, ptr %6, i32 0, i32 1
  %27 = extractvalue { i64, i16 } %23, 1
  store i16 %27, ptr %26, align 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %3, ptr align 8 %6, i64 16, i1 false), !tbaa.struct !13
  call void @llvm.lifetime.end.p0(i64 16, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  br label %28

28:                                               ; preds = %17
  %29 = load i32, ptr %4, align 4, !tbaa !9
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %4, align 4, !tbaa !9
  br label %12, !llvm.loop !16

31:                                               ; preds = %16
  %32 = call i64 @clock() #5
  %33 = load i64, ptr %1, align 8, !tbaa !5
  %34 = sub nsw i64 %32, %33
  store i64 %34, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  %35 = load i64, ptr %2, align 8, !tbaa !5
  %36 = mul nsw i64 %35, 1000
  %37 = sdiv i64 %36, 1000000
  %38 = trunc i64 %37 to i32
  store i32 %38, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %39 = load i32, ptr @n, align 4, !tbaa !9
  %40 = mul nsw i32 %39, 1000
  %41 = sitofp i32 %40 to double
  store double %41, ptr %8, align 8, !tbaa !18
  %42 = load i32, ptr %7, align 4, !tbaa !9
  %43 = sitofp i32 %42 to double
  %44 = load double, ptr %8, align 8, !tbaa !18
  %45 = fdiv double %44, %43
  store double %45, ptr %8, align 8, !tbaa !18
  %46 = load i32, ptr @n, align 4, !tbaa !9
  %47 = load i32, ptr %7, align 4, !tbaa !9
  %48 = call i32 (ptr, ...) @printf(ptr noundef @.str.10, i32 noundef %46, i32 noundef %47)
  %49 = load i32, ptr %7, align 4, !tbaa !9
  %50 = mul nsw i32 1000, %49
  %51 = load i32, ptr @n, align 4, !tbaa !9
  %52 = sdiv i32 %50, %51
  %53 = call i32 (ptr, ...) @printf(ptr noundef @.str.8, i32 noundef %52)
  %54 = load double, ptr %8, align 8, !tbaa !18
  %55 = call i32 (ptr, ...) @printf(ptr noundef @.str.9, double noundef %54)
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #5
  ret ptr null
}

; Function Attrs: nounwind
declare i32 @rand() #0

declare { i64, i16 } @btree_find(ptr noundef, i64 noundef, i64 noundef) #2

; 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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 = !{!"long", !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, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{i64 0, i64 8, !5, i64 8, i64 2, !14}
!14 = !{!15, !15, i64 0}
!15 = !{!"short", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!19, !19, i64 0}
!19 = !{!"double", !7, i64 0}


; 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"

@s = external hidden local_unnamed_addr global ptr, align 8
@root = external hidden local_unnamed_addr global i64, align 8
@n = external hidden local_unnamed_addr global i32, align 4
@.str.8 = external hidden unnamed_addr constant [27 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [27 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [31 x i8], align 1

; Function Attrs: nounwind
declare void @srand(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind
declare i64 @time(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind
declare i64 @clock() local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local noalias noundef ptr @test_perf_query_rand() local_unnamed_addr #2 {
  %1 = tail call i64 @time(ptr noundef null) #4
  %2 = trunc i64 %1 to i32
  tail call void @srand(i32 noundef %2) #4
  %3 = tail call i64 @clock() #4
  %4 = load i32, ptr @n, align 4, !tbaa !5
  %5 = icmp sgt i32 %4, 0
  br i1 %5, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %0
  %6 = tail call i64 @clock() #4
  %7 = sub nsw i64 %6, %3
  %8 = sdiv i64 %7, 1000
  %9 = trunc i64 %8 to i32
  %10 = load i32, ptr @n, align 4, !tbaa !5
  %11 = mul nsw i32 %10, 1000
  %12 = sitofp i32 %11 to double
  %13 = sitofp i32 %9 to double
  %14 = fdiv double %12, %13
  %15 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.10, i32 noundef %10, i32 noundef %9)
  %16 = mul nsw i32 %9, 1000
  %17 = load i32, ptr @n, align 4, !tbaa !5
  %18 = sdiv i32 %16, %17
  %19 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.8, i32 noundef %18)
  %20 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.9, double noundef %14)
  ret ptr null

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.011 = phi i32 [ %26, %.lr.ph ], [ 0, %0 ]
  %21 = tail call i32 @rand() #4
  %22 = load ptr, ptr @s, align 8, !tbaa !9
  %23 = load i64, ptr @root, align 8, !tbaa !11
  %24 = sext i32 %21 to i64
  %25 = tail call { i64, i16 } @btree_find(ptr noundef %22, i64 noundef %23, i64 noundef %24) #4
  %26 = add nuw nsw i32 %.011, 1
  %27 = load i32, ptr @n, align 4, !tbaa !5
  %28 = icmp slt i32 %26, %27
  br i1 %28, label %.lr.ph, label %._crit_edge, !llvm.loop !13
}

; Function Attrs: nounwind
declare i32 @rand() local_unnamed_addr #0

declare { i64, i16 } @btree_find(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #3

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

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass