; LLVM IR from eProsima/Fast-DDS :: src/cpp/rtps/persistence (sqlite3)

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

@mem0 = external hidden global %struct.Mem0Global, align 8

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

; Function Attrs: nounwind uwtable
declare dso_local void @sqlite3_mutex_enter(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @sqlite3_mutex_leave(ptr noundef) #1

; 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 @sqlite3_initialize() #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @sqlite3_release_memory(i32 noundef) #1

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

13:                                               ; preds = %1
  store i64 -1, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %60

14:                                               ; preds = %1
  %15 = load ptr, ptr @mem0, align 8, !tbaa !11
  call void @sqlite3_mutex_enter(ptr noundef %15)
  %16 = load i64, ptr getelementptr inbounds (%struct.Mem0Global, ptr @mem0, i32 0, i32 1), align 8, !tbaa !14
  store i64 %16, ptr %4, align 8, !tbaa !5
  %17 = load i64, ptr %3, align 8, !tbaa !5
  %18 = icmp slt i64 %17, 0
  br i1 %18, label %19, label %22

19:                                               ; preds = %14
  %20 = load ptr, ptr @mem0, align 8, !tbaa !11
  call void @sqlite3_mutex_leave(ptr noundef %20)
  %21 = load i64, ptr %4, align 8, !tbaa !5
  store i64 %21, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %60

22:                                               ; preds = %14
  %23 = load i64, ptr getelementptr inbounds (%struct.Mem0Global, ptr @mem0, i32 0, i32 2), align 8, !tbaa !15
  %24 = icmp sgt i64 %23, 0
  br i1 %24, label %25, label %34

25:                                               ; preds = %22
  %26 = load i64, ptr %3, align 8, !tbaa !5
  %27 = load i64, ptr getelementptr inbounds (%struct.Mem0Global, ptr @mem0, i32 0, i32 2), align 8, !tbaa !15
  %28 = icmp sgt i64 %26, %27
  br i1 %28, label %32, label %29

29:                                               ; preds = %25
  %30 = load i64, ptr %3, align 8, !tbaa !5
  %31 = icmp eq i64 %30, 0
  br i1 %31, label %32, label %34

32:                                               ; preds = %29, %25
  %33 = load i64, ptr getelementptr inbounds (%struct.Mem0Global, ptr @mem0, i32 0, i32 2), align 8, !tbaa !15
  store i64 %33, ptr %3, align 8, !tbaa !5
  br label %34

34:                                               ; preds = %32, %29, %22
  %35 = load i64, ptr %3, align 8, !tbaa !5
  store i64 %35, ptr getelementptr inbounds (%struct.Mem0Global, ptr @mem0, i32 0, i32 1), align 8, !tbaa !14
  %36 = call i64 @sqlite3StatusValue(i32 noundef 0)
  store i64 %36, ptr %6, align 8, !tbaa !5
  %37 = load i64, ptr %3, align 8, !tbaa !5
  %38 = icmp sgt i64 %37, 0
  br i1 %38, label %39, label %43

39:                                               ; preds = %34
  %40 = load i64, ptr %3, align 8, !tbaa !5
  %41 = load i64, ptr %6, align 8, !tbaa !5
  %42 = icmp sle i64 %40, %41
  br label %43

43:                                               ; preds = %39, %34
  %44 = phi i1 [ false, %34 ], [ %42, %39 ]
  %45 = zext i1 %44 to i32
  store i32 %45, ptr %9, align 4, !tbaa !9
  %46 = load i32, ptr %9, align 4
  store atomic i32 %46, ptr getelementptr inbounds (%struct.Mem0Global, ptr @mem0, i32 0, i32 3) monotonic, align 8
  %47 = load ptr, ptr @mem0, align 8, !tbaa !11
  call void @sqlite3_mutex_leave(ptr noundef %47)
  %48 = call i64 @sqlite3_memory_used()
  %49 = load i64, ptr %3, align 8, !tbaa !5
  %50 = sub nsw i64 %48, %49
  store i64 %50, ptr %5, align 8, !tbaa !5
  %51 = load i64, ptr %5, align 8, !tbaa !5
  %52 = icmp sgt i64 %51, 0
  br i1 %52, label %53, label %58

53:                                               ; preds = %43
  %54 = load i64, ptr %5, align 8, !tbaa !5
  %55 = and i64 %54, 2147483647
  %56 = trunc i64 %55 to i32
  %57 = call i32 @sqlite3_release_memory(i32 noundef %56)
  br label %58

58:                                               ; preds = %53, %43
  %59 = load i64, ptr %4, align 8, !tbaa !5
  store i64 %59, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %60

60:                                               ; preds = %58, %19, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %61 = load i64, ptr %2, align 8
  ret i64 %61
}

; Function Attrs: nounwind uwtable
declare hidden i64 @sqlite3StatusValue(i32 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i64 @sqlite3_memory_used() #1

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 }

!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 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, !13, i64 0}
!12 = !{!"Mem0Global", !13, i64 0, !6, i64 8, !6, i64 16, !10, i64 24}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!12, !6, i64 8}
!15 = !{!12, !6, 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.Mem0Global = type { ptr, i64, i64, i32 }

@mem0 = external hidden local_unnamed_addr global %struct.Mem0Global, align 8

; Function Attrs: nounwind uwtable
declare dso_local void @sqlite3_mutex_enter(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @sqlite3_mutex_leave(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @sqlite3_initialize() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @sqlite3_release_memory(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i64 @sqlite3_soft_heap_limit64(i64 noundef %0) local_unnamed_addr #0 {
  %2 = tail call i32 @sqlite3_initialize()
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %3, label %27

3:                                                ; preds = %1
  %4 = load ptr, ptr @mem0, align 8, !tbaa !5
  tail call void @sqlite3_mutex_enter(ptr noundef %4)
  %5 = load i64, ptr getelementptr inbounds (i8, ptr @mem0, i64 8), align 8, !tbaa !12
  %6 = icmp slt i64 %0, 0
  br i1 %6, label %7, label %9

7:                                                ; preds = %3
  %8 = load ptr, ptr @mem0, align 8, !tbaa !5
  tail call void @sqlite3_mutex_leave(ptr noundef %8)
  br label %27

9:                                                ; preds = %3
  %10 = load i64, ptr getelementptr inbounds (i8, ptr @mem0, i64 16), align 8, !tbaa !13
  %11 = icmp sgt i64 %10, 0
  %12 = icmp eq i64 %0, 0
  %13 = tail call i64 @llvm.smin.i64(i64 %10, i64 %0)
  %spec.select = select i1 %12, i64 %10, i64 %13
  %.016 = select i1 %11, i64 %spec.select, i64 %0
  store i64 %.016, ptr getelementptr inbounds (i8, ptr @mem0, i64 8), align 8, !tbaa !12
  %14 = tail call i64 @sqlite3StatusValue(i32 noundef 0)
  %15 = icmp sgt i64 %.016, 0
  %16 = icmp sle i64 %.016, %14
  %17 = select i1 %15, i1 %16, i1 false
  %18 = zext i1 %17 to i32
  store atomic i32 %18, ptr getelementptr inbounds (i8, ptr @mem0, i64 24) monotonic, align 8
  %19 = load ptr, ptr @mem0, align 8, !tbaa !5
  tail call void @sqlite3_mutex_leave(ptr noundef %19)
  %20 = tail call i64 @sqlite3_memory_used()
  %21 = sub nsw i64 %20, %.016
  %22 = icmp sgt i64 %21, 0
  br i1 %22, label %23, label %27

23:                                               ; preds = %9
  %24 = trunc i64 %21 to i32
  %25 = and i32 %24, 2147483647
  %26 = tail call i32 @sqlite3_release_memory(i32 noundef %25)
  br label %27

27:                                               ; preds = %9, %23, %1, %7
  %.0 = phi i64 [ %5, %7 ], [ -1, %1 ], [ %5, %23 ], [ %5, %9 ]
  ret i64 %.0
}

; Function Attrs: nounwind uwtable
declare hidden i64 @sqlite3StatusValue(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i64 @sqlite3_memory_used() local_unnamed_addr #0

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smin.i64(i64, i64) #1

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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable 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 0}
!6 = !{!"Mem0Global", !7, i64 0, !10, i64 8, !10, i64 16, !11, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long long", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!6, !10, i64 8}
!13 = !{!6, !10, i64 16}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass