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

; 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.Pager = type { ptr, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i32, i32, i32, i32, i32, i32, i32, i32, ptr, ptr, ptr, ptr, i64, i64, ptr, ptr, i32, i32, [16 x i8], i32, i64, ptr, i16, i16, i32, i32, i32, i32, i64, ptr, ptr, ptr, ptr, [3 x i32], ptr, ptr, ptr, ptr, ptr, ptr }
%struct.PagerSavepoint = type { i64, i64, ptr, i32, i32, [4 x i32] }
%struct.sqlite3_file = type { 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 i32 @sqlite3PagerSavepoint(ptr noundef %0, i32 noundef %1, i32 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = 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
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.Pager, ptr %11, i32 0, i32 26
  %13 = load i32, ptr %12, align 8, !tbaa !11
  store i32 %13, ptr %7, align 4, !tbaa !9
  %14 = load i32, ptr %7, align 4, !tbaa !9
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %16, label %110

16:                                               ; preds = %3
  %17 = load i32, ptr %6, align 4, !tbaa !9
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.Pager, ptr %18, i32 0, i32 38
  %20 = load i32, ptr %19, align 8, !tbaa !15
  %21 = icmp slt i32 %17, %20
  br i1 %21, label %22, label %110

22:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %23 = load i32, ptr %6, align 4, !tbaa !9
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = icmp eq i32 %24, 1
  %26 = zext i1 %25 to i64
  %27 = select i1 %25, i32 0, i32 1
  %28 = add nsw i32 %23, %27
  store i32 %28, ptr %9, align 4, !tbaa !9
  %29 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %29, ptr %8, align 4, !tbaa !9
  br label %30

30:                                               ; preds = %45, %22
  %31 = load i32, ptr %8, align 4, !tbaa !9
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.Pager, ptr %32, i32 0, i32 38
  %34 = load i32, ptr %33, align 8, !tbaa !15
  %35 = icmp slt i32 %31, %34
  br i1 %35, label %36, label %48

36:                                               ; preds = %30
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.Pager, ptr %37, i32 0, i32 37
  %39 = load ptr, ptr %38, align 8, !tbaa !16
  %40 = load i32, ptr %8, align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds %struct.PagerSavepoint, ptr %39, i64 %41
  %43 = getelementptr inbounds %struct.PagerSavepoint, ptr %42, i32 0, i32 2
  %44 = load ptr, ptr %43, align 8, !tbaa !17
  call void @sqlite3BitvecDestroy(ptr noundef %44)
  br label %45

45:                                               ; preds = %36
  %46 = load i32, ptr %8, align 4, !tbaa !9
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %8, align 4, !tbaa !9
  br label %30, !llvm.loop !19

48:                                               ; preds = %30
  %49 = load i32, ptr %9, align 4, !tbaa !9
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.Pager, ptr %50, i32 0, i32 38
  store i32 %49, ptr %51, align 8, !tbaa !15
  %52 = load i32, ptr %5, align 4, !tbaa !9
  %53 = icmp eq i32 %52, 1
  br i1 %53, label %54, label %79

54:                                               ; preds = %48
  %55 = load i32, ptr %9, align 4, !tbaa !9
  %56 = icmp eq i32 %55, 0
  br i1 %56, label %57, label %78

57:                                               ; preds = %54
  %58 = load ptr, ptr %4, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.Pager, ptr %58, i32 0, i32 33
  %60 = load ptr, ptr %59, align 8, !tbaa !21
  %61 = getelementptr inbounds %struct.sqlite3_file, ptr %60, i32 0, i32 0
  %62 = load ptr, ptr %61, align 8, !tbaa !22
  %63 = icmp ne ptr %62, null
  br i1 %63, label %64, label %78

64:                                               ; preds = %57
  %65 = load ptr, ptr %4, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.Pager, ptr %65, i32 0, i32 33
  %67 = load ptr, ptr %66, align 8, !tbaa !21
  %68 = call i32 @sqlite3JournalIsInMemory(ptr noundef %67)
  %69 = icmp ne i32 %68, 0
  br i1 %69, label %70, label %75

70:                                               ; preds = %64
  %71 = load ptr, ptr %4, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.Pager, ptr %71, i32 0, i32 33
  %73 = load ptr, ptr %72, align 8, !tbaa !21
  %74 = call i32 @sqlite3OsTruncate(ptr noundef %73, i64 noundef 0)
  store i32 %74, ptr %7, align 4, !tbaa !9
  br label %75

75:                                               ; preds = %70, %64
  %76 = load ptr, ptr %4, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.Pager, ptr %76, i32 0, i32 29
  store i32 0, ptr %77, align 4, !tbaa !24
  br label %78

78:                                               ; preds = %75, %57, %54
  br label %109

79:                                               ; preds = %48
  %80 = load ptr, ptr %4, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.Pager, ptr %80, i32 0, i32 60
  %82 = load ptr, ptr %81, align 8, !tbaa !25
  %83 = icmp ne ptr %82, null
  br i1 %83, label %91, label %84

84:                                               ; preds = %79
  %85 = load ptr, ptr %4, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.Pager, ptr %85, i32 0, i32 32
  %87 = load ptr, ptr %86, align 8, !tbaa !26
  %88 = getelementptr inbounds %struct.sqlite3_file, ptr %87, i32 0, i32 0
  %89 = load ptr, ptr %88, align 8, !tbaa !22
  %90 = icmp ne ptr %89, null
  br i1 %90, label %91, label %108

91:                                               ; preds = %84, %79
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %92 = load i32, ptr %9, align 4, !tbaa !9
  %93 = icmp eq i32 %92, 0
  br i1 %93, label %94, label %95

94:                                               ; preds = %91
  br label %103

95:                                               ; preds = %91
  %96 = load ptr, ptr %4, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.Pager, ptr %96, i32 0, i32 37
  %98 = load ptr, ptr %97, align 8, !tbaa !16
  %99 = load i32, ptr %9, align 4, !tbaa !9
  %100 = sub nsw i32 %99, 1
  %101 = sext i32 %100 to i64
  %102 = getelementptr inbounds %struct.PagerSavepoint, ptr %98, i64 %101
  br label %103

103:                                              ; preds = %95, %94
  %104 = phi ptr [ null, %94 ], [ %102, %95 ]
  store ptr %104, ptr %10, align 8, !tbaa !5
  %105 = load ptr, ptr %4, align 8, !tbaa !5
  %106 = load ptr, ptr %10, align 8, !tbaa !5
  %107 = call i32 @pagerPlaybackSavepoint(ptr noundef %105, ptr noundef %106)
  store i32 %107, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %108

108:                                              ; preds = %103, %84
  br label %109

109:                                              ; preds = %108, %78
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %110

110:                                              ; preds = %109, %16, %3
  %111 = load i32, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret i32 %111
}

declare void @sqlite3BitvecDestroy(ptr noundef) #2

declare i32 @sqlite3JournalIsInMemory(ptr noundef) #2

declare i32 @sqlite3OsTruncate(ptr noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @pagerPlaybackSavepoint(ptr noundef, ptr noundef) #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 = { "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, !10, i64 48}
!12 = !{!"Pager", !6, i64 0, !7, i64 8, !7, i64 9, !7, i64 10, !7, i64 11, !7, i64 12, !7, i64 13, !7, i64 14, !7, i64 15, !7, i64 16, !7, i64 17, !7, i64 18, !7, i64 19, !7, i64 20, !7, i64 21, !7, i64 22, !7, i64 23, !7, i64 24, !7, i64 25, !7, i64 26, !7, i64 27, !7, i64 28, !10, i64 32, !10, i64 36, !10, i64 40, !10, i64 44, !10, i64 48, !10, i64 52, !10, i64 56, !10, i64 60, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !13, i64 96, !13, i64 104, !6, i64 112, !6, i64 120, !10, i64 128, !10, i64 132, !7, i64 136, !10, i64 152, !13, i64 160, !6, i64 168, !14, i64 176, !14, i64 178, !10, i64 180, !10, i64 184, !10, i64 188, !10, i64 192, !13, i64 200, !6, i64 208, !6, i64 216, !6, i64 224, !6, i64 232, !7, i64 240, !6, i64 256, !6, i64 264, !6, i64 272, !6, i64 280, !6, i64 288, !6, i64 296}
!13 = !{!"long long", !7, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = !{!12, !10, i64 128}
!16 = !{!12, !6, i64 120}
!17 = !{!18, !6, i64 16}
!18 = !{!"PagerSavepoint", !13, i64 0, !13, i64 8, !6, i64 16, !10, i64 24, !10, i64 28, !7, i64 32}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!12, !6, i64 88}
!22 = !{!23, !6, i64 0}
!23 = !{!"sqlite3_file", !6, i64 0}
!24 = !{!12, !10, i64 60}
!25 = !{!12, !6, i64 288}
!26 = !{!12, !6, i64 80}


; 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.PagerSavepoint = type { i64, i64, ptr, i32, i32, [4 x i32] }

; Function Attrs: nounwind uwtable
define dso_local i32 @sqlite3PagerSavepoint(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 48
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %6 = icmp eq i32 %5, 0
  br i1 %6, label %7, label %58

7:                                                ; preds = %3
  %8 = getelementptr inbounds i8, ptr %0, i64 128
  %9 = load i32, ptr %8, align 8, !tbaa !13
  %10 = icmp sgt i32 %9, %2
  br i1 %10, label %11, label %58

11:                                               ; preds = %7
  %12 = icmp ne i32 %1, 1
  %13 = zext i1 %12 to i32
  %14 = add nsw i32 %13, %2
  %15 = icmp slt i32 %14, %9
  br i1 %15, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %11
  %16 = getelementptr inbounds i8, ptr %0, i64 120
  %17 = sext i32 %2 to i64
  %18 = zext i1 %12 to i64
  %19 = add nsw i64 %17, %18
  br label %20

20:                                               ; preds = %.lr.ph, %20
  %indvars.iv = phi i64 [ %19, %.lr.ph ], [ %indvars.iv.next, %20 ]
  %21 = load ptr, ptr %16, align 8, !tbaa !14
  %22 = getelementptr inbounds %struct.PagerSavepoint, ptr %21, i64 %indvars.iv, i32 2
  %23 = load ptr, ptr %22, align 8, !tbaa !15
  tail call void @sqlite3BitvecDestroy(ptr noundef %23) #2
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %24 = load i32, ptr %8, align 8, !tbaa !13
  %25 = sext i32 %24 to i64
  %26 = icmp slt i64 %indvars.iv.next, %25
  br i1 %26, label %20, label %._crit_edge, !llvm.loop !17

._crit_edge:                                      ; preds = %20, %11
  store i32 %14, ptr %8, align 8, !tbaa !13
  br i1 %12, label %40, label %27

27:                                               ; preds = %._crit_edge
  %28 = icmp eq i32 %14, 0
  br i1 %28, label %29, label %58

29:                                               ; preds = %27
  %30 = getelementptr inbounds i8, ptr %0, i64 88
  %31 = load ptr, ptr %30, align 8, !tbaa !19
  %32 = load ptr, ptr %31, align 8, !tbaa !20
  %.not33 = icmp eq ptr %32, null
  br i1 %.not33, label %58, label %33

33:                                               ; preds = %29
  %34 = tail call i32 @sqlite3JournalIsInMemory(ptr noundef nonnull %31) #2
  %.not34 = icmp eq i32 %34, 0
  br i1 %.not34, label %38, label %35

35:                                               ; preds = %33
  %36 = load ptr, ptr %30, align 8, !tbaa !19
  %37 = tail call i32 @sqlite3OsTruncate(ptr noundef %36, i64 noundef 0) #2
  br label %38

38:                                               ; preds = %35, %33
  %.0 = phi i32 [ %37, %35 ], [ 0, %33 ]
  %39 = getelementptr inbounds i8, ptr %0, i64 60
  store i32 0, ptr %39, align 4, !tbaa !22
  br label %58

40:                                               ; preds = %._crit_edge
  %41 = getelementptr inbounds i8, ptr %0, i64 288
  %42 = load ptr, ptr %41, align 8, !tbaa !23
  %.not = icmp eq ptr %42, null
  br i1 %.not, label %43, label %47

43:                                               ; preds = %40
  %44 = getelementptr inbounds i8, ptr %0, i64 80
  %45 = load ptr, ptr %44, align 8, !tbaa !24
  %46 = load ptr, ptr %45, align 8, !tbaa !20
  %.not32 = icmp eq ptr %46, null
  br i1 %.not32, label %58, label %47

47:                                               ; preds = %43, %40
  %48 = icmp eq i32 %14, 0
  br i1 %48, label %55, label %49

49:                                               ; preds = %47
  %50 = getelementptr inbounds i8, ptr %0, i64 120
  %51 = load ptr, ptr %50, align 8, !tbaa !14
  %52 = sext i32 %14 to i64
  %53 = getelementptr %struct.PagerSavepoint, ptr %51, i64 %52
  %54 = getelementptr i8, ptr %53, i64 -48
  br label %55

55:                                               ; preds = %47, %49
  %56 = phi ptr [ %54, %49 ], [ null, %47 ]
  %57 = tail call i32 @pagerPlaybackSavepoint(ptr noundef nonnull %0, ptr noundef %56)
  br label %58

58:                                               ; preds = %38, %29, %27, %55, %43, %7, %3
  %.2 = phi i32 [ 0, %7 ], [ %5, %3 ], [ %.0, %38 ], [ 0, %29 ], [ 0, %27 ], [ %57, %55 ], [ 0, %43 ]
  ret i32 %.2
}

declare void @sqlite3BitvecDestroy(ptr noundef) local_unnamed_addr #1

declare i32 @sqlite3JournalIsInMemory(ptr noundef) local_unnamed_addr #1

declare i32 @sqlite3OsTruncate(ptr noundef, i64 noundef) local_unnamed_addr #1

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

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 = { "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, !10, i64 48}
!6 = !{!"Pager", !7, i64 0, !8, i64 8, !8, i64 9, !8, i64 10, !8, i64 11, !8, i64 12, !8, i64 13, !8, i64 14, !8, i64 15, !8, i64 16, !8, i64 17, !8, i64 18, !8, i64 19, !8, i64 20, !8, i64 21, !8, i64 22, !8, i64 23, !8, i64 24, !8, i64 25, !8, i64 26, !8, i64 27, !8, i64 28, !10, i64 32, !10, i64 36, !10, i64 40, !10, i64 44, !10, i64 48, !10, i64 52, !10, i64 56, !10, i64 60, !7, i64 64, !7, i64 72, !7, i64 80, !7, i64 88, !11, i64 96, !11, i64 104, !7, i64 112, !7, i64 120, !10, i64 128, !10, i64 132, !8, i64 136, !10, i64 152, !11, i64 160, !7, i64 168, !12, i64 176, !12, i64 178, !10, i64 180, !10, i64 184, !10, i64 188, !10, i64 192, !11, i64 200, !7, i64 208, !7, i64 216, !7, i64 224, !7, i64 232, !8, i64 240, !7, i64 256, !7, i64 264, !7, i64 272, !7, i64 280, !7, i64 288, !7, i64 296}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"long long", !8, i64 0}
!12 = !{!"short", !8, i64 0}
!13 = !{!6, !10, i64 128}
!14 = !{!6, !7, i64 120}
!15 = !{!16, !7, i64 16}
!16 = !{!"PagerSavepoint", !11, i64 0, !11, i64 8, !7, i64 16, !10, i64 24, !10, i64 28, !8, i64 32}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!6, !7, i64 88}
!20 = !{!21, !7, i64 0}
!21 = !{!"sqlite3_file", !7, i64 0}
!22 = !{!6, !10, i64 60}
!23 = !{!6, !7, i64 288}
!24 = !{!6, !7, i64 80}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass