; LLVM IR from ibr-ds/STANlite :: Enclave/sqlite (btree)

; 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.Btree = type { ptr, ptr, i8, i8, i8, i8, i32, i32, i32, ptr, ptr, %struct.BtLock }
%struct.BtLock = type { ptr, i32, i8, ptr }
%struct.BtShared = type { ptr, ptr, ptr, ptr, i8, i8, i8, i8, i8, i8, i16, i16, i16, i16, i16, i32, i32, i32, i32, ptr, ptr, ptr, ptr, i32, ptr, ptr, ptr, ptr }
%struct.MemPage = type { i8, i8, i8, i8, i32, i8, i8, i8, i8, i8, i16, i16, i16, i16, i16, i16, [4 x i16], [4 x ptr], ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }

; 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

declare i32 @sqlite3Get4byte(ptr noundef) #1

declare void @sqlite3BtreeEnter(ptr noundef) #1

declare void @sqlite3BtreeLeave(ptr noundef) #1

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

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

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

11:                                               ; preds = %3
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.Btree, ptr %12, i32 0, i32 2
  %14 = load i8, ptr %13, align 8, !tbaa !11
  %15 = zext i8 %14 to i32
  %16 = icmp eq i32 %15, 2
  br i1 %16, label %17, label %67

17:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.Btree, ptr %18, i32 0, i32 1
  %20 = load ptr, ptr %19, align 8, !tbaa !14
  store ptr %20, ptr %8, align 8, !tbaa !5
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  call void @sqlite3BtreeEnter(ptr noundef %21)
  %22 = load i32, ptr %5, align 4, !tbaa !9
  %23 = icmp eq i32 %22, 2
  br i1 %23, label %24, label %27

24:                                               ; preds = %17
  %25 = load ptr, ptr %8, align 8, !tbaa !5
  %26 = call i32 @saveAllCursors(ptr noundef %25, i32 noundef 0, ptr noundef null)
  store i32 %26, ptr %7, align 4, !tbaa !9
  br label %27

27:                                               ; preds = %24, %17
  %28 = load i32, ptr %7, align 4, !tbaa !9
  %29 = icmp eq i32 %28, 0
  br i1 %29, label %30, label %37

30:                                               ; preds = %27
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.BtShared, ptr %31, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !15
  %34 = load i32, ptr %5, align 4, !tbaa !9
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = call i32 @sqlite3PagerSavepoint(ptr noundef %33, i32 noundef %34, i32 noundef %35)
  store i32 %36, ptr %7, align 4, !tbaa !9
  br label %37

37:                                               ; preds = %30, %27
  %38 = load i32, ptr %7, align 4, !tbaa !9
  %39 = icmp eq i32 %38, 0
  br i1 %39, label %40, label %65

40:                                               ; preds = %37
  %41 = load i32, ptr %6, align 4, !tbaa !9
  %42 = icmp slt i32 %41, 0
  br i1 %42, label %43, label %53

43:                                               ; preds = %40
  %44 = load ptr, ptr %8, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.BtShared, ptr %44, i32 0, i32 10
  %46 = load i16, ptr %45, align 2, !tbaa !18
  %47 = zext i16 %46 to i32
  %48 = and i32 %47, 8
  %49 = icmp ne i32 %48, 0
  br i1 %49, label %50, label %53

50:                                               ; preds = %43
  %51 = load ptr, ptr %8, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.BtShared, ptr %51, i32 0, i32 18
  store i32 0, ptr %52, align 4, !tbaa !19
  br label %53

53:                                               ; preds = %50, %43, %40
  %54 = load ptr, ptr %8, align 8, !tbaa !5
  %55 = call i32 @newDatabase(ptr noundef %54)
  store i32 %55, ptr %7, align 4, !tbaa !9
  %56 = load ptr, ptr %8, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.BtShared, ptr %56, i32 0, i32 3
  %58 = load ptr, ptr %57, align 8, !tbaa !20
  %59 = getelementptr inbounds %struct.MemPage, ptr %58, i32 0, i32 19
  %60 = load ptr, ptr %59, align 8, !tbaa !21
  %61 = getelementptr inbounds i8, ptr %60, i64 28
  %62 = call i32 @sqlite3Get4byte(ptr noundef %61)
  %63 = load ptr, ptr %8, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.BtShared, ptr %63, i32 0, i32 18
  store i32 %62, ptr %64, align 4, !tbaa !19
  br label %65

65:                                               ; preds = %53, %37
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  call void @sqlite3BtreeLeave(ptr noundef %66)
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %67

67:                                               ; preds = %65, %11, %3
  %68 = load i32, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret i32 %68
}

declare i32 @sqlite3PagerSavepoint(ptr noundef, i32 noundef, i32 noundef) #1

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

!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, !7, i64 16}
!12 = !{!"Btree", !6, i64 0, !6, i64 8, !7, i64 16, !7, i64 17, !7, i64 18, !7, i64 19, !10, i64 20, !10, i64 24, !10, i64 28, !6, i64 32, !6, i64 40, !13, i64 48}
!13 = !{!"BtLock", !6, i64 0, !10, i64 8, !7, i64 12, !6, i64 16}
!14 = !{!12, !6, i64 8}
!15 = !{!16, !6, i64 0}
!16 = !{!"BtShared", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !7, i64 32, !7, i64 33, !7, i64 34, !7, i64 35, !7, i64 36, !7, i64 37, !17, i64 38, !17, i64 40, !17, i64 42, !17, i64 44, !17, i64 46, !10, i64 48, !10, i64 52, !10, i64 56, !10, i64 60, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !10, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128}
!17 = !{!"short", !7, i64 0}
!18 = !{!16, !17, i64 38}
!19 = !{!16, !10, i64 60}
!20 = !{!16, !6, i64 24}
!21 = !{!22, !6, i64 80}
!22 = !{!"MemPage", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !10, i64 4, !7, i64 8, !7, i64 9, !7, i64 10, !7, i64 11, !7, i64 12, !17, i64 14, !17, i64 16, !17, i64 18, !17, i64 20, !17, i64 22, !17, i64 24, !7, i64 26, !7, i64 40, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128}


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

declare i32 @sqlite3Get4byte(ptr noundef) local_unnamed_addr #0

declare void @sqlite3BtreeEnter(ptr noundef) local_unnamed_addr #0

declare void @sqlite3BtreeLeave(ptr noundef) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @sqlite3BtreeSavepoint(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %35, label %4

4:                                                ; preds = %3
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  %6 = load i8, ptr %5, align 8, !tbaa !5
  %7 = icmp eq i8 %6, 2
  br i1 %7, label %8, label %35

8:                                                ; preds = %4
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  tail call void @sqlite3BtreeEnter(ptr noundef nonnull %0) #2
  %11 = icmp eq i32 %1, 2
  br i1 %11, label %12, label %.thread

12:                                               ; preds = %8
  %13 = tail call i32 @saveAllCursors(ptr noundef %10, i32 noundef 0, ptr noundef null)
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %.thread, label %.thread21

.thread:                                          ; preds = %8, %12
  %15 = load ptr, ptr %10, align 8, !tbaa !13
  %16 = tail call i32 @sqlite3PagerSavepoint(ptr noundef %15, i32 noundef %1, i32 noundef %2) #2
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %.thread21

18:                                               ; preds = %.thread
  %19 = icmp slt i32 %2, 0
  br i1 %19, label %20, label %26

20:                                               ; preds = %18
  %21 = getelementptr inbounds i8, ptr %10, i64 38
  %22 = load i16, ptr %21, align 2, !tbaa !16
  %23 = and i16 %22, 8
  %.not19 = icmp eq i16 %23, 0
  br i1 %.not19, label %26, label %24

24:                                               ; preds = %20
  %25 = getelementptr inbounds i8, ptr %10, i64 60
  store i32 0, ptr %25, align 4, !tbaa !17
  br label %26

26:                                               ; preds = %24, %20, %18
  %27 = tail call i32 @newDatabase(ptr noundef nonnull %10)
  %28 = getelementptr inbounds i8, ptr %10, i64 24
  %29 = load ptr, ptr %28, align 8, !tbaa !18
  %30 = getelementptr inbounds i8, ptr %29, i64 80
  %31 = load ptr, ptr %30, align 8, !tbaa !19
  %32 = getelementptr inbounds i8, ptr %31, i64 28
  %33 = tail call i32 @sqlite3Get4byte(ptr noundef nonnull %32) #2
  %34 = getelementptr inbounds i8, ptr %10, i64 60
  store i32 %33, ptr %34, align 4, !tbaa !17
  br label %.thread21

.thread21:                                        ; preds = %12, %26, %.thread
  %.2 = phi i32 [ %27, %26 ], [ %16, %.thread ], [ %13, %12 ]
  tail call void @sqlite3BtreeLeave(ptr noundef nonnull %0) #2
  br label %35

35:                                               ; preds = %.thread21, %4, %3
  %.3 = phi i32 [ %.2, %.thread21 ], [ 0, %4 ], [ 0, %3 ]
  ret i32 %.3
}

declare i32 @sqlite3PagerSavepoint(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

attributes #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 = { 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, !8, i64 16}
!6 = !{!"Btree", !7, i64 0, !7, i64 8, !8, i64 16, !8, i64 17, !8, i64 18, !8, i64 19, !10, i64 20, !10, i64 24, !10, i64 28, !7, i64 32, !7, i64 40, !11, i64 48}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"BtLock", !7, i64 0, !10, i64 8, !8, i64 12, !7, i64 16}
!12 = !{!6, !7, i64 8}
!13 = !{!14, !7, i64 0}
!14 = !{!"BtShared", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !8, i64 32, !8, i64 33, !8, i64 34, !8, i64 35, !8, i64 36, !8, i64 37, !15, i64 38, !15, i64 40, !15, i64 42, !15, i64 44, !15, i64 46, !10, i64 48, !10, i64 52, !10, i64 56, !10, i64 60, !7, i64 64, !7, i64 72, !7, i64 80, !7, i64 88, !10, i64 96, !7, i64 104, !7, i64 112, !7, i64 120, !7, i64 128}
!15 = !{!"short", !8, i64 0}
!16 = !{!14, !15, i64 38}
!17 = !{!14, !10, i64 60}
!18 = !{!14, !7, i64 24}
!19 = !{!20, !7, i64 80}
!20 = !{!"MemPage", !8, i64 0, !8, i64 1, !8, i64 2, !8, i64 3, !10, i64 4, !8, i64 8, !8, i64 9, !8, i64 10, !8, i64 11, !8, i64 12, !15, i64 14, !15, i64 16, !15, i64 18, !15, i64 20, !15, i64 22, !15, i64 24, !8, i64 26, !8, i64 40, !7, i64 72, !7, i64 80, !7, i64 88, !7, i64 96, !7, i64 104, !7, i64 112, !7, i64 120, !7, i64 128}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,TailCallElimPass