; 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.BtCursor = type { ptr, ptr, ptr, ptr, %struct.CellInfo, i64, ptr, i32, i32, i32, i8, i8, i8, i8, i8, i8, ptr, ptr, [20 x i16], [20 x ptr] }
%struct.CellInfo = type { i64, ptr, i32, i16, i16 }

declare void @sqlite3_free(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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

declare i32 @sqlite3PagerClose(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @sqlite3BtreeClose(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.Btree, ptr %6, i32 0, i32 1
  %8 = load ptr, ptr %7, align 8, !tbaa !9
  store ptr %8, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  call void @sqlite3BtreeEnter(ptr noundef %9)
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.BtShared, ptr %10, i32 0, i32 2
  %12 = load ptr, ptr %11, align 8, !tbaa !13
  store ptr %12, ptr %4, align 8, !tbaa !5
  br label %13

13:                                               ; preds = %29, %1
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = icmp ne ptr %14, null
  br i1 %15, label %16, label %30

16:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %17, ptr %5, align 8, !tbaa !5
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.BtCursor, ptr %18, i32 0, i32 2
  %20 = load ptr, ptr %19, align 8, !tbaa !16
  store ptr %20, ptr %4, align 8, !tbaa !5
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.BtCursor, ptr %21, i32 0, i32 0
  %23 = load ptr, ptr %22, align 8, !tbaa !20
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = icmp eq ptr %23, %24
  br i1 %25, label %26, label %29

26:                                               ; preds = %16
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = call i32 @sqlite3BtreeCloseCursor(ptr noundef %27)
  br label %29

29:                                               ; preds = %26, %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  br label %13, !llvm.loop !21

30:                                               ; preds = %13
  %31 = load ptr, ptr %2, align 8, !tbaa !5
  %32 = call i32 @sqlite3BtreeRollback(ptr noundef %31, i32 noundef 0, i32 noundef 0)
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  call void @sqlite3BtreeLeave(ptr noundef %33)
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.Btree, ptr %34, i32 0, i32 3
  %36 = load i8, ptr %35, align 1, !tbaa !23
  %37 = icmp ne i8 %36, 0
  br i1 %37, label %38, label %42

38:                                               ; preds = %30
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = call i32 @removeFromSharingList(ptr noundef %39)
  %41 = icmp ne i32 %40, 0
  br i1 %41, label %42, label %72

42:                                               ; preds = %38, %30
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.BtShared, ptr %43, i32 0, i32 0
  %45 = load ptr, ptr %44, align 8, !tbaa !24
  %46 = load ptr, ptr %2, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.Btree, ptr %46, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8, !tbaa !25
  %49 = call i32 @sqlite3PagerClose(ptr noundef %45, ptr noundef %48)
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.BtShared, ptr %50, i32 0, i32 20
  %52 = load ptr, ptr %51, align 8, !tbaa !26
  %53 = icmp ne ptr %52, null
  br i1 %53, label %54, label %66

54:                                               ; preds = %42
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.BtShared, ptr %55, i32 0, i32 19
  %57 = load ptr, ptr %56, align 8, !tbaa !27
  %58 = icmp ne ptr %57, null
  br i1 %58, label %59, label %66

59:                                               ; preds = %54
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.BtShared, ptr %60, i32 0, i32 20
  %62 = load ptr, ptr %61, align 8, !tbaa !26
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.BtShared, ptr %63, i32 0, i32 19
  %65 = load ptr, ptr %64, align 8, !tbaa !27
  call void %62(ptr noundef %65)
  br label %66

66:                                               ; preds = %59, %54, %42
  %67 = load ptr, ptr %3, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.BtShared, ptr %67, i32 0, i32 19
  %69 = load ptr, ptr %68, align 8, !tbaa !27
  call void @sqlite3DbFree(ptr noundef null, ptr noundef %69)
  %70 = load ptr, ptr %3, align 8, !tbaa !5
  call void @freeTempSpace(ptr noundef %70)
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  call void @sqlite3_free(ptr noundef %71)
  br label %72

72:                                               ; preds = %66, %38
  %73 = load ptr, ptr %2, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.Btree, ptr %73, i32 0, i32 10
  %75 = load ptr, ptr %74, align 8, !tbaa !28
  %76 = icmp ne ptr %75, null
  br i1 %76, label %77, label %85

77:                                               ; preds = %72
  %78 = load ptr, ptr %2, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.Btree, ptr %78, i32 0, i32 9
  %80 = load ptr, ptr %79, align 8, !tbaa !29
  %81 = load ptr, ptr %2, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.Btree, ptr %81, i32 0, i32 10
  %83 = load ptr, ptr %82, align 8, !tbaa !28
  %84 = getelementptr inbounds %struct.Btree, ptr %83, i32 0, i32 9
  store ptr %80, ptr %84, align 8, !tbaa !29
  br label %85

85:                                               ; preds = %77, %72
  %86 = load ptr, ptr %2, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.Btree, ptr %86, i32 0, i32 9
  %88 = load ptr, ptr %87, align 8, !tbaa !29
  %89 = icmp ne ptr %88, null
  br i1 %89, label %90, label %98

90:                                               ; preds = %85
  %91 = load ptr, ptr %2, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.Btree, ptr %91, i32 0, i32 10
  %93 = load ptr, ptr %92, align 8, !tbaa !28
  %94 = load ptr, ptr %2, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.Btree, ptr %94, i32 0, i32 9
  %96 = load ptr, ptr %95, align 8, !tbaa !29
  %97 = getelementptr inbounds %struct.Btree, ptr %96, i32 0, i32 10
  store ptr %93, ptr %97, align 8, !tbaa !28
  br label %98

98:                                               ; preds = %90, %85
  %99 = load ptr, ptr %2, align 8, !tbaa !5
  call void @sqlite3_free(ptr noundef %99)
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret i32 0
}

declare void @sqlite3BtreeEnter(ptr noundef) #0

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

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

declare void @sqlite3BtreeLeave(ptr noundef) #0

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

declare void @sqlite3DbFree(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @freeTempSpace(ptr noundef) #2

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 8}
!10 = !{!"Btree", !6, i64 0, !6, i64 8, !7, i64 16, !7, i64 17, !7, i64 18, !7, i64 19, !11, i64 20, !11, i64 24, !11, i64 28, !6, i64 32, !6, i64 40, !12, i64 48}
!11 = !{!"int", !7, i64 0}
!12 = !{!"BtLock", !6, i64 0, !11, i64 8, !7, i64 12, !6, i64 16}
!13 = !{!14, !6, i64 16}
!14 = !{!"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, !15, i64 38, !15, i64 40, !15, i64 42, !15, i64 44, !15, i64 46, !11, i64 48, !11, i64 52, !11, i64 56, !11, i64 60, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !11, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128}
!15 = !{!"short", !7, i64 0}
!16 = !{!17, !6, i64 16}
!17 = !{!"BtCursor", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !18, i64 32, !19, i64 56, !6, i64 64, !11, i64 72, !11, i64 76, !11, i64 80, !7, i64 84, !7, i64 85, !7, i64 86, !7, i64 87, !7, i64 88, !7, i64 89, !6, i64 96, !6, i64 104, !7, i64 112, !7, i64 152}
!18 = !{!"CellInfo", !19, i64 0, !6, i64 8, !11, i64 16, !15, i64 20, !15, i64 22}
!19 = !{!"long long", !7, i64 0}
!20 = !{!17, !6, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = !{!10, !7, i64 17}
!24 = !{!14, !6, i64 0}
!25 = !{!10, !6, i64 0}
!26 = !{!14, !6, i64 72}
!27 = !{!14, !6, i64 64}
!28 = !{!10, !6, i64 40}
!29 = !{!10, !6, i64 32}


; 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 void @sqlite3_free(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @sqlite3BtreeClose(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  tail call void @sqlite3BtreeEnter(ptr noundef %0) #2
  %4 = getelementptr inbounds i8, ptr %3, i64 16
  %5 = load ptr, ptr %4, align 8, !tbaa !12
  %.not39 = icmp eq ptr %5, null
  br i1 %.not39, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1, %12
  %.040 = phi ptr [ %7, %12 ], [ %5, %1 ]
  %6 = getelementptr inbounds i8, ptr %.040, i64 16
  %7 = load ptr, ptr %6, align 8, !tbaa !15
  %8 = load ptr, ptr %.040, align 8, !tbaa !19
  %9 = icmp eq ptr %8, %0
  br i1 %9, label %10, label %12

10:                                               ; preds = %.lr.ph
  %11 = tail call i32 @sqlite3BtreeCloseCursor(ptr noundef nonnull %.040)
  br label %12

12:                                               ; preds = %10, %.lr.ph
  %.not = icmp eq ptr %7, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !20

._crit_edge:                                      ; preds = %12, %1
  %13 = tail call i32 @sqlite3BtreeRollback(ptr noundef %0, i32 noundef 0, i32 noundef 0)
  tail call void @sqlite3BtreeLeave(ptr noundef %0) #2
  %14 = getelementptr inbounds i8, ptr %0, i64 17
  %15 = load i8, ptr %14, align 1, !tbaa !22
  %.not33 = icmp eq i8 %15, 0
  br i1 %.not33, label %18, label %16

16:                                               ; preds = %._crit_edge
  %17 = tail call i32 @removeFromSharingList(ptr noundef %3)
  %.not34 = icmp eq i32 %17, 0
  br i1 %.not34, label %31, label %18

18:                                               ; preds = %16, %._crit_edge
  %19 = load ptr, ptr %3, align 8, !tbaa !23
  %20 = load ptr, ptr %0, align 8, !tbaa !24
  %21 = tail call i32 @sqlite3PagerClose(ptr noundef %19, ptr noundef %20) #2
  %22 = getelementptr inbounds i8, ptr %3, i64 72
  %23 = load ptr, ptr %22, align 8, !tbaa !25
  %.not35 = icmp eq ptr %23, null
  br i1 %.not35, label %28, label %24

24:                                               ; preds = %18
  %25 = getelementptr inbounds i8, ptr %3, i64 64
  %26 = load ptr, ptr %25, align 8, !tbaa !26
  %.not36 = icmp eq ptr %26, null
  br i1 %.not36, label %28, label %27

27:                                               ; preds = %24
  tail call void %23(ptr noundef nonnull %26) #2
  br label %28

28:                                               ; preds = %27, %24, %18
  %29 = getelementptr inbounds i8, ptr %3, i64 64
  %30 = load ptr, ptr %29, align 8, !tbaa !26
  tail call void @sqlite3DbFree(ptr noundef null, ptr noundef %30) #2
  tail call void @freeTempSpace(ptr noundef nonnull %3)
  tail call void @sqlite3_free(ptr noundef nonnull %3) #2
  br label %31

31:                                               ; preds = %28, %16
  %32 = getelementptr inbounds i8, ptr %0, i64 40
  %33 = load ptr, ptr %32, align 8, !tbaa !27
  %.not37 = icmp eq ptr %33, null
  %.phi.trans.insert = getelementptr inbounds i8, ptr %0, i64 32
  %.pre = load ptr, ptr %.phi.trans.insert, align 8, !tbaa !28
  br i1 %.not37, label %._crit_edge41, label %34

34:                                               ; preds = %31
  %35 = getelementptr inbounds i8, ptr %33, i64 32
  store ptr %.pre, ptr %35, align 8, !tbaa !28
  br label %._crit_edge41

._crit_edge41:                                    ; preds = %31, %34
  %.not38 = icmp eq ptr %.pre, null
  br i1 %.not38, label %38, label %36

36:                                               ; preds = %._crit_edge41
  %37 = getelementptr inbounds i8, ptr %.pre, i64 40
  store ptr %33, ptr %37, align 8, !tbaa !27
  br label %38

38:                                               ; preds = %36, %._crit_edge41
  tail call void @sqlite3_free(ptr noundef nonnull %0) #2
  ret i32 0
}

declare void @sqlite3BtreeEnter(ptr noundef) local_unnamed_addr #0

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

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

declare void @sqlite3BtreeLeave(ptr noundef) local_unnamed_addr #0

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

declare void @sqlite3DbFree(ptr noundef, ptr noundef) local_unnamed_addr #0

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

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, !7, i64 8}
!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 = !{!13, !7, i64 16}
!13 = !{!"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, !14, i64 38, !14, i64 40, !14, i64 42, !14, i64 44, !14, 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}
!14 = !{!"short", !8, i64 0}
!15 = !{!16, !7, i64 16}
!16 = !{!"BtCursor", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !17, i64 32, !18, i64 56, !7, i64 64, !10, i64 72, !10, i64 76, !10, i64 80, !8, i64 84, !8, i64 85, !8, i64 86, !8, i64 87, !8, i64 88, !8, i64 89, !7, i64 96, !7, i64 104, !8, i64 112, !8, i64 152}
!17 = !{!"CellInfo", !18, i64 0, !7, i64 8, !10, i64 16, !14, i64 20, !14, i64 22}
!18 = !{!"long long", !8, i64 0}
!19 = !{!16, !7, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!6, !8, i64 17}
!23 = !{!13, !7, i64 0}
!24 = !{!6, !7, i64 0}
!25 = !{!13, !7, i64 72}
!26 = !{!13, !7, i64 64}
!27 = !{!6, !7, i64 40}
!28 = !{!6, !7, i64 32}


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