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

; 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
declare hidden i32 @pager_wait_on_lock(ptr noundef, i32 noundef) #1

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

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

14:                                               ; preds = %3
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.Pager, ptr %15, i32 0, i32 26
  %17 = load i32, ptr %16, align 8, !tbaa !11
  store i32 %17, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %100

18:                                               ; preds = %3
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = trunc i32 %19 to i8
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.Pager, ptr %21, i32 0, i32 19
  store i8 %20, ptr %22, align 2, !tbaa !15
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.Pager, ptr %23, i32 0, i32 14
  %25 = load i8, ptr %24, align 1, !tbaa !16
  %26 = zext i8 %25 to i32
  %27 = icmp eq i32 %26, 1
  br i1 %27, label %28, label %98

28:                                               ; preds = %18
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.Pager, ptr %29, i32 0, i32 60
  %31 = load ptr, ptr %30, align 8, !tbaa !17
  %32 = icmp ne ptr %31, null
  br i1 %32, label %33, label %62

33:                                               ; preds = %28
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.Pager, ptr %34, i32 0, i32 1
  %36 = load i8, ptr %35, align 8, !tbaa !18
  %37 = zext i8 %36 to i32
  %38 = icmp ne i32 %37, 0
  br i1 %38, label %39, label %57

39:                                               ; preds = %33
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.Pager, ptr %40, i32 0, i32 60
  %42 = load ptr, ptr %41, align 8, !tbaa !17
  %43 = call i32 @sqlite3WalExclusiveMode(ptr noundef %42, i32 noundef -1)
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %45, label %57

45:                                               ; preds = %39
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = call i32 @pagerLockDb(ptr noundef %46, i32 noundef 4)
  store i32 %47, ptr %8, align 4, !tbaa !9
  %48 = load i32, ptr %8, align 4, !tbaa !9
  %49 = icmp ne i32 %48, 0
  br i1 %49, label %50, label %52

50:                                               ; preds = %45
  %51 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %51, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %100

52:                                               ; preds = %45
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.Pager, ptr %53, i32 0, i32 60
  %55 = load ptr, ptr %54, align 8, !tbaa !17
  %56 = call i32 @sqlite3WalExclusiveMode(ptr noundef %55, i32 noundef 1)
  br label %57

57:                                               ; preds = %52, %39, %33
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.Pager, ptr %58, i32 0, i32 60
  %60 = load ptr, ptr %59, align 8, !tbaa !17
  %61 = call i32 @sqlite3WalBeginWriteTransaction(ptr noundef %60)
  store i32 %61, ptr %8, align 4, !tbaa !9
  br label %74

62:                                               ; preds = %28
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = call i32 @pagerLockDb(ptr noundef %63, i32 noundef 2)
  store i32 %64, ptr %8, align 4, !tbaa !9
  %65 = load i32, ptr %8, align 4, !tbaa !9
  %66 = icmp eq i32 %65, 0
  br i1 %66, label %67, label %73

67:                                               ; preds = %62
  %68 = load i32, ptr %6, align 4, !tbaa !9
  %69 = icmp ne i32 %68, 0
  br i1 %69, label %70, label %73

70:                                               ; preds = %67
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  %72 = call i32 @pager_wait_on_lock(ptr noundef %71, i32 noundef 4)
  store i32 %72, ptr %8, align 4, !tbaa !9
  br label %73

73:                                               ; preds = %70, %67, %62
  br label %74

74:                                               ; preds = %73, %57
  %75 = load i32, ptr %8, align 4, !tbaa !9
  %76 = icmp eq i32 %75, 0
  br i1 %76, label %77, label %97

77:                                               ; preds = %74
  %78 = load ptr, ptr %5, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.Pager, ptr %78, i32 0, i32 14
  store i8 2, ptr %79, align 1, !tbaa !16
  %80 = load ptr, ptr %5, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.Pager, ptr %80, i32 0, i32 22
  %82 = load i32, ptr %81, align 8, !tbaa !19
  %83 = load ptr, ptr %5, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.Pager, ptr %83, i32 0, i32 25
  store i32 %82, ptr %84, align 4, !tbaa !20
  %85 = load ptr, ptr %5, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.Pager, ptr %85, i32 0, i32 22
  %87 = load i32, ptr %86, align 8, !tbaa !19
  %88 = load ptr, ptr %5, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.Pager, ptr %88, i32 0, i32 24
  store i32 %87, ptr %89, align 8, !tbaa !21
  %90 = load ptr, ptr %5, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.Pager, ptr %90, i32 0, i32 22
  %92 = load i32, ptr %91, align 8, !tbaa !19
  %93 = load ptr, ptr %5, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.Pager, ptr %93, i32 0, i32 23
  store i32 %92, ptr %94, align 4, !tbaa !22
  %95 = load ptr, ptr %5, align 8, !tbaa !5
  %96 = getelementptr inbounds %struct.Pager, ptr %95, i32 0, i32 34
  store i64 0, ptr %96, align 8, !tbaa !23
  br label %97

97:                                               ; preds = %77, %74
  br label %98

98:                                               ; preds = %97, %18
  %99 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %99, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %100

100:                                              ; preds = %98, %50, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %101 = load i32, ptr %4, align 4
  ret i32 %101
}

declare i32 @sqlite3WalExclusiveMode(ptr noundef, i32 noundef) #2

declare i32 @sqlite3WalBeginWriteTransaction(ptr noundef) #2

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, !7, i64 26}
!16 = !{!12, !7, i64 21}
!17 = !{!12, !6, i64 288}
!18 = !{!12, !7, i64 8}
!19 = !{!12, !10, i64 32}
!20 = !{!12, !10, i64 44}
!21 = !{!12, !10, i64 40}
!22 = !{!12, !10, i64 36}
!23 = !{!12, !13, i64 96}


; 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: nounwind uwtable
declare hidden i32 @pager_wait_on_lock(ptr noundef, i32 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local i32 @sqlite3PagerBegin(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
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %6, label %43

6:                                                ; preds = %3
  %7 = trunc i32 %2 to i8
  %8 = getelementptr inbounds i8, ptr %0, i64 26
  store i8 %7, ptr %8, align 2, !tbaa !13
  %9 = getelementptr inbounds i8, ptr %0, i64 21
  %10 = load i8, ptr %9, align 1, !tbaa !14
  %11 = icmp eq i8 %10, 1
  br i1 %11, label %12, label %43

12:                                               ; preds = %6
  %13 = getelementptr inbounds i8, ptr %0, i64 288
  %14 = load ptr, ptr %13, align 8, !tbaa !15
  %.not32 = icmp eq ptr %14, null
  br i1 %.not32, label %28, label %15

15:                                               ; preds = %12
  %16 = getelementptr inbounds i8, ptr %0, i64 8
  %17 = load i8, ptr %16, align 8, !tbaa !16
  %.not33 = icmp eq i8 %17, 0
  br i1 %.not33, label %25, label %18

18:                                               ; preds = %15
  %19 = tail call i32 @sqlite3WalExclusiveMode(ptr noundef nonnull %14, i32 noundef -1) #2
  %.not34 = icmp eq i32 %19, 0
  br i1 %.not34, label %25, label %20

20:                                               ; preds = %18
  %21 = tail call i32 @pagerLockDb(ptr noundef nonnull %0, i32 noundef 4)
  %.not35 = icmp eq i32 %21, 0
  br i1 %.not35, label %22, label %43

22:                                               ; preds = %20
  %23 = load ptr, ptr %13, align 8, !tbaa !15
  %24 = tail call i32 @sqlite3WalExclusiveMode(ptr noundef %23, i32 noundef 1) #2
  br label %25

25:                                               ; preds = %22, %18, %15
  %26 = load ptr, ptr %13, align 8, !tbaa !15
  %27 = tail call i32 @sqlite3WalBeginWriteTransaction(ptr noundef %26) #2
  br label %34

28:                                               ; preds = %12
  %29 = tail call i32 @pagerLockDb(ptr noundef nonnull %0, i32 noundef 2)
  %30 = icmp eq i32 %29, 0
  %31 = icmp ne i32 %1, 0
  %or.cond = and i1 %31, %30
  br i1 %or.cond, label %32, label %34

32:                                               ; preds = %28
  %33 = tail call i32 @pager_wait_on_lock(ptr noundef nonnull %0, i32 noundef 4)
  br label %34

34:                                               ; preds = %28, %32, %25
  %.0 = phi i32 [ %27, %25 ], [ %33, %32 ], [ %29, %28 ]
  %35 = icmp eq i32 %.0, 0
  br i1 %35, label %36, label %43

36:                                               ; preds = %34
  store i8 2, ptr %9, align 1, !tbaa !14
  %37 = getelementptr inbounds i8, ptr %0, i64 32
  %38 = load i32, ptr %37, align 8, !tbaa !17
  %39 = getelementptr inbounds i8, ptr %0, i64 44
  store i32 %38, ptr %39, align 4, !tbaa !18
  %40 = getelementptr inbounds i8, ptr %0, i64 40
  store i32 %38, ptr %40, align 8, !tbaa !19
  %41 = getelementptr inbounds i8, ptr %0, i64 36
  store i32 %38, ptr %41, align 4, !tbaa !20
  %42 = getelementptr inbounds i8, ptr %0, i64 96
  store i64 0, ptr %42, align 8, !tbaa !21
  br label %43

43:                                               ; preds = %6, %36, %34, %20, %3
  %.028 = phi i32 [ %5, %3 ], [ %21, %20 ], [ 0, %36 ], [ %.0, %34 ], [ 0, %6 ]
  ret i32 %.028
}

declare i32 @sqlite3WalExclusiveMode(ptr noundef, i32 noundef) local_unnamed_addr #1

declare i32 @sqlite3WalBeginWriteTransaction(ptr noundef) local_unnamed_addr #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 = { "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, !8, i64 26}
!14 = !{!6, !8, i64 21}
!15 = !{!6, !7, i64 288}
!16 = !{!6, !8, i64 8}
!17 = !{!6, !10, i64 32}
!18 = !{!6, !10, i64 44}
!19 = !{!6, !10, i64 40}
!20 = !{!6, !10, i64 36}
!21 = !{!6, !11, i64 96}


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