; 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.PgHdr = type { ptr, ptr, ptr, ptr, ptr, i32, i16, i16, ptr, ptr, ptr }
%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
define dso_local i32 @sqlite3PagerWrite(ptr noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.PgHdr, ptr %6, i32 0, i32 4
  %8 = load ptr, ptr %7, align 8, !tbaa !9
  store ptr %8, ptr %4, align 8, !tbaa !5
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.PgHdr, ptr %9, i32 0, i32 6
  %11 = load i16, ptr %10, align 4, !tbaa !13
  %12 = zext i16 %11 to i32
  %13 = and i32 %12, 4
  %14 = icmp ne i32 %13, 0
  br i1 %14, label %15, label %32

15:                                               ; preds = %1
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.Pager, ptr %16, i32 0, i32 22
  %18 = load i32, ptr %17, align 8, !tbaa !14
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.PgHdr, ptr %19, i32 0, i32 5
  %21 = load i32, ptr %20, align 8, !tbaa !17
  %22 = icmp uge i32 %18, %21
  br i1 %22, label %23, label %32

23:                                               ; preds = %15
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.Pager, ptr %24, i32 0, i32 38
  %26 = load i32, ptr %25, align 8, !tbaa !18
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %28, label %31

28:                                               ; preds = %23
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = call i32 @subjournalPageIfRequired(ptr noundef %29)
  store i32 %30, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %55

31:                                               ; preds = %23
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %55

32:                                               ; preds = %15, %1
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.Pager, ptr %33, i32 0, i32 26
  %35 = load i32, ptr %34, align 8, !tbaa !19
  %36 = icmp ne i32 %35, 0
  br i1 %36, label %37, label %41

37:                                               ; preds = %32
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.Pager, ptr %38, i32 0, i32 26
  %40 = load i32, ptr %39, align 8, !tbaa !19
  store i32 %40, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %55

41:                                               ; preds = %32
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.Pager, ptr %42, i32 0, i32 47
  %44 = load i32, ptr %43, align 8, !tbaa !20
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.Pager, ptr %45, i32 0, i32 48
  %47 = load i32, ptr %46, align 4, !tbaa !21
  %48 = icmp ugt i32 %44, %47
  br i1 %48, label %49, label %52

49:                                               ; preds = %41
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = call i32 @pagerWriteLargeSector(ptr noundef %50)
  store i32 %51, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %55

52:                                               ; preds = %41
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  %54 = call i32 @pager_write(ptr noundef %53)
  store i32 %54, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %55

55:                                               ; preds = %52, %49, %37, %31, %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %56 = load i32, ptr %2, align 4
  ret i32 %56
}

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

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

; Function Attrs: nounwind uwtable
declare hidden i32 @pager_write(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 = { noinline 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 32}
!10 = !{!"PgHdr", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !11, i64 40, !12, i64 44, !12, i64 46, !6, i64 48, !6, i64 56, !6, i64 64}
!11 = !{!"int", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!10, !12, i64 44}
!14 = !{!15, !11, i64 32}
!15 = !{!"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, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !11, i64 48, !11, i64 52, !11, i64 56, !11, i64 60, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !16, i64 96, !16, i64 104, !6, i64 112, !6, i64 120, !11, i64 128, !11, i64 132, !7, i64 136, !11, i64 152, !16, i64 160, !6, i64 168, !12, i64 176, !12, i64 178, !11, i64 180, !11, i64 184, !11, i64 188, !11, i64 192, !16, 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}
!16 = !{!"long long", !7, i64 0}
!17 = !{!10, !11, i64 40}
!18 = !{!15, !11, i64 128}
!19 = !{!15, !11, i64 48}
!20 = !{!15, !11, i64 184}
!21 = !{!15, !11, i64 188}


; 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
define dso_local i32 @sqlite3PagerWrite(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 32
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 44
  %5 = load i16, ptr %4, align 4, !tbaa !12
  %6 = and i16 %5, 4
  %.not = icmp eq i16 %6, 0
  br i1 %.not, label %17, label %7

7:                                                ; preds = %1
  %8 = getelementptr inbounds i8, ptr %3, i64 32
  %9 = load i32, ptr %8, align 8, !tbaa !13
  %10 = getelementptr inbounds i8, ptr %0, i64 40
  %11 = load i32, ptr %10, align 8, !tbaa !16
  %.not13 = icmp ult i32 %9, %11
  br i1 %.not13, label %17, label %12

12:                                               ; preds = %7
  %13 = getelementptr inbounds i8, ptr %3, i64 128
  %14 = load i32, ptr %13, align 8, !tbaa !17
  %.not15 = icmp eq i32 %14, 0
  br i1 %.not15, label %30, label %15

15:                                               ; preds = %12
  %16 = tail call i32 @subjournalPageIfRequired(ptr noundef nonnull %0)
  br label %30

17:                                               ; preds = %7, %1
  %18 = getelementptr inbounds i8, ptr %3, i64 48
  %19 = load i32, ptr %18, align 8, !tbaa !18
  %.not14 = icmp eq i32 %19, 0
  br i1 %.not14, label %20, label %30

20:                                               ; preds = %17
  %21 = getelementptr inbounds i8, ptr %3, i64 184
  %22 = load i32, ptr %21, align 8, !tbaa !19
  %23 = getelementptr inbounds i8, ptr %3, i64 188
  %24 = load i32, ptr %23, align 4, !tbaa !20
  %25 = icmp ugt i32 %22, %24
  br i1 %25, label %26, label %28

26:                                               ; preds = %20
  %27 = tail call i32 @pagerWriteLargeSector(ptr noundef nonnull %0)
  br label %30

28:                                               ; preds = %20
  %29 = tail call i32 @pager_write(ptr noundef nonnull %0)
  br label %30

30:                                               ; preds = %17, %12, %28, %26, %15
  %.0 = phi i32 [ %16, %15 ], [ %27, %26 ], [ %29, %28 ], [ 0, %12 ], [ %19, %17 ]
  ret i32 %.0
}

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

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

; Function Attrs: nounwind uwtable
declare hidden i32 @pager_write(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 = { noinline 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" }

!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 32}
!6 = !{!"PgHdr", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !10, i64 40, !11, i64 44, !11, i64 46, !7, i64 48, !7, i64 56, !7, i64 64}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"short", !8, i64 0}
!12 = !{!6, !11, i64 44}
!13 = !{!14, !10, i64 32}
!14 = !{!"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, !15, i64 96, !15, i64 104, !7, i64 112, !7, i64 120, !10, i64 128, !10, i64 132, !8, i64 136, !10, i64 152, !15, i64 160, !7, i64 168, !11, i64 176, !11, i64 178, !10, i64 180, !10, i64 184, !10, i64 188, !10, i64 192, !15, 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}
!15 = !{!"long long", !8, i64 0}
!16 = !{!6, !10, i64 40}
!17 = !{!14, !10, i64 128}
!18 = !{!14, !10, i64 48}
!19 = !{!14, !10, i64 184}
!20 = !{!14, !10, i64 188}


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