; 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.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 }
%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

; Function Attrs: nounwind uwtable
define dso_local i64 @sqlite3BtreeRowCountEst(ptr noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i8, align 1
  %6 = alloca i32, align 4
  store ptr %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 1, ptr %5) #2
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.BtCursor, ptr %7, i32 0, i32 12
  %9 = load i8, ptr %8, align 2, !tbaa !9
  %10 = zext i8 %9 to i32
  %11 = icmp ne i32 %10, 1
  br i1 %11, label %12, label %13

12:                                               ; preds = %1
  store i64 -1, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %53

13:                                               ; preds = %1
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.BtCursor, ptr %14, i32 0, i32 19
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.BtCursor, ptr %16, i32 0, i32 14
  %18 = load i8, ptr %17, align 8, !tbaa !15
  %19 = sext i8 %18 to i64
  %20 = getelementptr inbounds [20 x ptr], ptr %15, i64 0, i64 %19
  %21 = load ptr, ptr %20, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.MemPage, ptr %21, i32 0, i32 5
  %23 = load i8, ptr %22, align 8, !tbaa !16
  %24 = zext i8 %23 to i32
  %25 = icmp eq i32 %24, 0
  br i1 %25, label %26, label %27

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

27:                                               ; preds = %13
  store i64 1, ptr %4, align 8, !tbaa !18
  store i8 0, ptr %5, align 1, !tbaa !19
  br label %28

28:                                               ; preds = %48, %27
  %29 = load i8, ptr %5, align 1, !tbaa !19
  %30 = zext i8 %29 to i32
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.BtCursor, ptr %31, i32 0, i32 14
  %33 = load i8, ptr %32, align 8, !tbaa !15
  %34 = sext i8 %33 to i32
  %35 = icmp sle i32 %30, %34
  br i1 %35, label %36, label %51

36:                                               ; preds = %28
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.BtCursor, ptr %37, i32 0, i32 19
  %39 = load i8, ptr %5, align 1, !tbaa !19
  %40 = zext i8 %39 to i64
  %41 = getelementptr inbounds [20 x ptr], ptr %38, i64 0, i64 %40
  %42 = load ptr, ptr %41, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.MemPage, ptr %42, i32 0, i32 14
  %44 = load i16, ptr %43, align 2, !tbaa !20
  %45 = zext i16 %44 to i64
  %46 = load i64, ptr %4, align 8, !tbaa !18
  %47 = mul nsw i64 %46, %45
  store i64 %47, ptr %4, align 8, !tbaa !18
  br label %48

48:                                               ; preds = %36
  %49 = load i8, ptr %5, align 1, !tbaa !19
  %50 = add i8 %49, 1
  store i8 %50, ptr %5, align 1, !tbaa !19
  br label %28, !llvm.loop !21

51:                                               ; preds = %28
  %52 = load i64, ptr %4, align 8, !tbaa !18
  store i64 %52, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %53

53:                                               ; preds = %51, %26, %12
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %54 = load i64, ptr %2, align 8
  ret i64 %54
}

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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !7, i64 86}
!10 = !{!"BtCursor", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !12, i64 56, !6, i64 64, !13, i64 72, !13, i64 76, !13, 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}
!11 = !{!"CellInfo", !12, i64 0, !6, i64 8, !13, i64 16, !14, i64 20, !14, i64 22}
!12 = !{!"long long", !7, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = !{!10, !7, i64 88}
!16 = !{!17, !7, i64 8}
!17 = !{!"MemPage", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !13, i64 4, !7, i64 8, !7, i64 9, !7, i64 10, !7, i64 11, !7, i64 12, !14, i64 14, !14, i64 16, !14, i64 18, !14, i64 20, !14, i64 22, !14, 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}
!18 = !{!12, !12, i64 0}
!19 = !{!7, !7, i64 0}
!20 = !{!17, !14, i64 22}
!21 = distinct !{!21, !22}
!22 = !{!"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: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local i64 @sqlite3BtreeRowCountEst(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 86
  %3 = load i8, ptr %2, align 2, !tbaa !5
  %.not = icmp eq i8 %3, 1
  br i1 %.not, label %4, label %.loopexit

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 152
  %6 = getelementptr inbounds i8, ptr %0, i64 88
  %7 = load i8, ptr %6, align 8, !tbaa !14
  %8 = sext i8 %7 to i64
  %9 = getelementptr inbounds [20 x ptr], ptr %5, i64 0, i64 %8
  %10 = load ptr, ptr %9, align 8, !tbaa !15
  %11 = getelementptr inbounds i8, ptr %10, i64 8
  %12 = load i8, ptr %11, align 8, !tbaa !16
  %13 = icmp eq i8 %12, 0
  br i1 %13, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %4
  %.not1213 = icmp slt i8 %7, 0
  br i1 %.not1213, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %narrow = add nuw i8 %7, 1
  %wide.trip.count = zext i8 %narrow to i64
  %xtraiter = and i64 %wide.trip.count, 3
  %14 = icmp ult i8 %7, 3
  br i1 %14, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 252
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.3, %.lr.ph ]
  %.01014 = phi i64 [ 1, %.lr.ph.preheader.new ], [ %38, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %15 = getelementptr inbounds [20 x ptr], ptr %5, i64 0, i64 %indvars.iv
  %16 = load ptr, ptr %15, align 8, !tbaa !15
  %17 = getelementptr inbounds i8, ptr %16, i64 22
  %18 = load i16, ptr %17, align 2, !tbaa !18
  %19 = zext i16 %18 to i64
  %20 = mul nuw nsw i64 %.01014, %19
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %21 = getelementptr inbounds [20 x ptr], ptr %5, i64 0, i64 %indvars.iv.next
  %22 = load ptr, ptr %21, align 8, !tbaa !15
  %23 = getelementptr inbounds i8, ptr %22, i64 22
  %24 = load i16, ptr %23, align 2, !tbaa !18
  %25 = zext i16 %24 to i64
  %26 = mul nuw nsw i64 %20, %25
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %27 = getelementptr inbounds [20 x ptr], ptr %5, i64 0, i64 %indvars.iv.next.1
  %28 = load ptr, ptr %27, align 8, !tbaa !15
  %29 = getelementptr inbounds i8, ptr %28, i64 22
  %30 = load i16, ptr %29, align 2, !tbaa !18
  %31 = zext i16 %30 to i64
  %32 = mul nuw nsw i64 %26, %31
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %33 = getelementptr inbounds [20 x ptr], ptr %5, i64 0, i64 %indvars.iv.next.2
  %34 = load ptr, ptr %33, align 8, !tbaa !15
  %35 = getelementptr inbounds i8, ptr %34, i64 22
  %36 = load i16, ptr %35, align 2, !tbaa !18
  %37 = zext i16 %36 to i64
  %38 = mul nuw nsw i64 %32, %37
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !19

.loopexit.loopexit.unr-lcssa:                     ; preds = %.lr.ph, %.lr.ph.preheader
  %.lcssa.ph = phi i64 [ poison, %.lr.ph.preheader ], [ %38, %.lr.ph ]
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next.3, %.lr.ph ]
  %.01014.unr = phi i64 [ 1, %.lr.ph.preheader ], [ %38, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.loopexit.loopexit.unr-lcssa, %.lr.ph.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph.epil ], [ %indvars.iv.unr, %.loopexit.loopexit.unr-lcssa ]
  %.01014.epil = phi i64 [ %44, %.lr.ph.epil ], [ %.01014.unr, %.loopexit.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %.loopexit.loopexit.unr-lcssa ]
  %39 = getelementptr inbounds [20 x ptr], ptr %5, i64 0, i64 %indvars.iv.epil
  %40 = load ptr, ptr %39, align 8, !tbaa !15
  %41 = getelementptr inbounds i8, ptr %40, i64 22
  %42 = load i16, ptr %41, align 2, !tbaa !18
  %43 = zext i16 %42 to i64
  %44 = mul nuw nsw i64 %.01014.epil, %43
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %.loopexit, label %.lr.ph.epil, !llvm.loop !21

.loopexit:                                        ; preds = %.loopexit.loopexit.unr-lcssa, %.lr.ph.epil, %.preheader, %4, %1
  %.011 = phi i64 [ -1, %1 ], [ -1, %4 ], [ 1, %.preheader ], [ %.lcssa.ph, %.loopexit.loopexit.unr-lcssa ], [ %44, %.lr.ph.epil ]
  ret i64 %.011
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) 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" }

!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 86}
!6 = !{!"BtCursor", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32, !11, i64 56, !7, i64 64, !12, i64 72, !12, i64 76, !12, 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}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"CellInfo", !11, i64 0, !7, i64 8, !12, i64 16, !13, i64 20, !13, i64 22}
!11 = !{!"long long", !8, i64 0}
!12 = !{!"int", !8, i64 0}
!13 = !{!"short", !8, i64 0}
!14 = !{!6, !8, i64 88}
!15 = !{!7, !7, i64 0}
!16 = !{!17, !8, i64 8}
!17 = !{!"MemPage", !8, i64 0, !8, i64 1, !8, i64 2, !8, i64 3, !12, i64 4, !8, i64 8, !8, i64 9, !8, i64 10, !8, i64 11, !8, i64 12, !13, i64 14, !13, i64 16, !13, i64 18, !13, i64 20, !13, i64 22, !13, 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}
!18 = !{!17, !13, i64 22}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.unroll.disable"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass