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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

declare void @sqlite3OsClose(ptr noundef) #2

declare void @sqlite3_free(ptr noundef) #2

; 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 @sqlite3WalClose(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3, ptr noundef %4) #1 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  store ptr %4, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !9
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = icmp ne ptr %14, null
  br i1 %15, label %16, label %81

16:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  store i32 0, ptr %12, align 4, !tbaa !9
  %17 = load ptr, ptr %10, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %60

19:                                               ; preds = %16
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.Wal, ptr %20, i32 0, i32 1
  %22 = load ptr, ptr %21, align 8, !tbaa !11
  %23 = call i32 @sqlite3OsLock(ptr noundef %22, i32 noundef 4)
  store i32 %23, ptr %11, align 4, !tbaa !9
  %24 = icmp eq i32 0, %23
  br i1 %24, label %25, label %60

25:                                               ; preds = %19
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.Wal, ptr %26, i32 0, i32 11
  %28 = load i8, ptr %27, align 1, !tbaa !16
  %29 = zext i8 %28 to i32
  %30 = icmp eq i32 %29, 0
  br i1 %30, label %31, label %34

31:                                               ; preds = %25
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.Wal, ptr %32, i32 0, i32 11
  store i8 1, ptr %33, align 1, !tbaa !16
  br label %34

34:                                               ; preds = %31, %25
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = load i32, ptr %8, align 4, !tbaa !9
  %38 = load i32, ptr %9, align 4, !tbaa !9
  %39 = load ptr, ptr %10, align 8, !tbaa !5
  %40 = call i32 @sqlite3WalCheckpoint(ptr noundef %35, ptr noundef %36, i32 noundef 0, ptr noundef null, ptr noundef null, i32 noundef %37, i32 noundef %38, ptr noundef %39, ptr noundef null, ptr noundef null)
  store i32 %40, ptr %11, align 4, !tbaa !9
  %41 = load i32, ptr %11, align 4, !tbaa !9
  %42 = icmp eq i32 %41, 0
  br i1 %42, label %43, label %59

43:                                               ; preds = %34
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  store i32 -1, ptr %13, align 4, !tbaa !9
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.Wal, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !11
  call void @sqlite3OsFileControlHint(ptr noundef %46, i32 noundef 10, ptr noundef %13)
  %47 = load i32, ptr %13, align 4, !tbaa !9
  %48 = icmp ne i32 %47, 1
  br i1 %48, label %49, label %50

49:                                               ; preds = %43
  store i32 1, ptr %12, align 4, !tbaa !9
  br label %58

50:                                               ; preds = %43
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.Wal, ptr %51, i32 0, i32 4
  %53 = load i64, ptr %52, align 8, !tbaa !17
  %54 = icmp sge i64 %53, 0
  br i1 %54, label %55, label %57

55:                                               ; preds = %50
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  call void @walLimitSize(ptr noundef %56, i64 noundef 0)
  br label %57

57:                                               ; preds = %55, %50
  br label %58

58:                                               ; preds = %57, %49
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  br label %59

59:                                               ; preds = %58, %34
  br label %60

60:                                               ; preds = %59, %19, %16
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = load i32, ptr %12, align 4, !tbaa !9
  call void @walIndexClose(ptr noundef %61, i32 noundef %62)
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.Wal, ptr %63, i32 0, i32 2
  %65 = load ptr, ptr %64, align 8, !tbaa !18
  call void @sqlite3OsClose(ptr noundef %65)
  %66 = load i32, ptr %12, align 4, !tbaa !9
  %67 = icmp ne i32 %66, 0
  br i1 %67, label %68, label %76

68:                                               ; preds = %60
  call void @sqlite3BeginBenignMalloc()
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.Wal, ptr %69, i32 0, i32 0
  %71 = load ptr, ptr %70, align 8, !tbaa !19
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.Wal, ptr %72, i32 0, i32 21
  %74 = load ptr, ptr %73, align 8, !tbaa !20
  %75 = call i32 @sqlite3OsDelete(ptr noundef %71, ptr noundef %74, i32 noundef 0)
  call void @sqlite3EndBenignMalloc()
  br label %76

76:                                               ; preds = %68, %60
  %77 = load ptr, ptr %6, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.Wal, ptr %77, i32 0, i32 7
  %79 = load ptr, ptr %78, align 8, !tbaa !21
  call void @sqlite3_free(ptr noundef %79)
  %80 = load ptr, ptr %6, align 8, !tbaa !5
  call void @sqlite3_free(ptr noundef %80)
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %81

81:                                               ; preds = %76, %5
  %82 = load i32, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  ret i32 %82
}

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

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

declare void @sqlite3OsFileControlHint(ptr noundef, i32 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @walLimitSize(ptr noundef, i64 noundef) #1

declare void @sqlite3BeginBenignMalloc() #2

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

declare void @sqlite3EndBenignMalloc() #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, !6, i64 8}
!12 = !{!"Wal", !6, i64 0, !6, i64 8, !6, i64 16, !10, i64 24, !13, i64 32, !10, i64 40, !10, i64 44, !6, i64 48, !10, i64 56, !14, i64 60, !7, i64 62, !7, i64 63, !7, i64 64, !7, i64 65, !7, i64 66, !7, i64 67, !7, i64 68, !7, i64 69, !15, i64 72, !10, i64 120, !10, i64 124, !6, i64 128, !10, i64 136}
!13 = !{!"long long", !7, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = !{!"WalIndexHdr", !10, i64 0, !10, i64 4, !10, i64 8, !7, i64 12, !7, i64 13, !14, i64 14, !10, i64 16, !10, i64 20, !7, i64 24, !7, i64 32, !7, i64 40}
!16 = !{!12, !7, i64 63}
!17 = !{!12, !13, i64 32}
!18 = !{!12, !6, i64 16}
!19 = !{!12, !6, i64 0}
!20 = !{!12, !6, i64 128}
!21 = !{!12, !6, i64 48}


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

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

declare void @sqlite3OsClose(ptr noundef) local_unnamed_addr #2

declare void @sqlite3_free(ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress 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 @sqlite3WalClose(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3, ptr noundef %4) local_unnamed_addr #1 {
  %6 = alloca i32, align 4
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %41, label %7

7:                                                ; preds = %5
  %.not29 = icmp eq ptr %4, null
  br i1 %.not29, label %30, label %8

8:                                                ; preds = %7
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !5
  %11 = tail call i32 @sqlite3OsLock(ptr noundef %10, i32 noundef 4) #3
  %12 = icmp eq i32 %11, 0
  br i1 %12, label %13, label %30

13:                                               ; preds = %8
  %14 = getelementptr inbounds i8, ptr %0, i64 63
  %15 = load i8, ptr %14, align 1, !tbaa !14
  %16 = icmp eq i8 %15, 0
  br i1 %16, label %17, label %18

17:                                               ; preds = %13
  store i8 1, ptr %14, align 1, !tbaa !14
  br label %18

18:                                               ; preds = %17, %13
  %19 = tail call i32 @sqlite3WalCheckpoint(ptr noundef nonnull %0, ptr noundef %1, i32 noundef 0, ptr noundef null, ptr noundef null, i32 noundef %2, i32 noundef %3, ptr noundef nonnull %4, ptr noundef null, ptr noundef null)
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %30

21:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %6) #3
  store i32 -1, ptr %6, align 4, !tbaa !15
  %22 = load ptr, ptr %9, align 8, !tbaa !5
  call void @sqlite3OsFileControlHint(ptr noundef %22, i32 noundef 10, ptr noundef nonnull %6) #3
  %23 = load i32, ptr %6, align 4, !tbaa !15
  %.not30 = icmp eq i32 %23, 1
  br i1 %.not30, label %24, label %29

24:                                               ; preds = %21
  %25 = getelementptr inbounds i8, ptr %0, i64 32
  %26 = load i64, ptr %25, align 8, !tbaa !16
  %27 = icmp sgt i64 %26, -1
  br i1 %27, label %28, label %29

28:                                               ; preds = %24
  call void @walLimitSize(ptr noundef nonnull %0, i64 noundef 0)
  br label %29

29:                                               ; preds = %21, %24, %28
  %.0 = phi i32 [ 0, %28 ], [ 0, %24 ], [ 1, %21 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %6) #3
  br label %30

30:                                               ; preds = %18, %29, %8, %7
  %.022 = phi i32 [ 0, %29 ], [ %19, %18 ], [ %11, %8 ], [ 0, %7 ]
  %.1 = phi i32 [ %.0, %29 ], [ 0, %18 ], [ 0, %8 ], [ 0, %7 ]
  call void @walIndexClose(ptr noundef nonnull %0, i32 noundef %.1)
  %31 = getelementptr inbounds i8, ptr %0, i64 16
  %32 = load ptr, ptr %31, align 8, !tbaa !17
  call void @sqlite3OsClose(ptr noundef %32) #3
  %.not31 = icmp eq i32 %.1, 0
  br i1 %.not31, label %38, label %33

33:                                               ; preds = %30
  call void @sqlite3BeginBenignMalloc() #3
  %34 = load ptr, ptr %0, align 8, !tbaa !18
  %35 = getelementptr inbounds i8, ptr %0, i64 128
  %36 = load ptr, ptr %35, align 8, !tbaa !19
  %37 = call i32 @sqlite3OsDelete(ptr noundef %34, ptr noundef %36, i32 noundef 0) #3
  call void @sqlite3EndBenignMalloc() #3
  br label %38

38:                                               ; preds = %33, %30
  %39 = getelementptr inbounds i8, ptr %0, i64 48
  %40 = load ptr, ptr %39, align 8, !tbaa !20
  call void @sqlite3_free(ptr noundef %40) #3
  call void @sqlite3_free(ptr noundef nonnull %0) #3
  br label %41

41:                                               ; preds = %38, %5
  %.123 = phi i32 [ %.022, %38 ], [ 0, %5 ]
  ret i32 %.123
}

declare i32 @sqlite3OsLock(ptr noundef, i32 noundef) local_unnamed_addr #2

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

declare void @sqlite3OsFileControlHint(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

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

declare void @sqlite3BeginBenignMalloc() local_unnamed_addr #2

declare i32 @sqlite3OsDelete(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

declare void @sqlite3EndBenignMalloc() local_unnamed_addr #2

attributes #0 = { mustprogress 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, !7, i64 8}
!6 = !{!"Wal", !7, i64 0, !7, i64 8, !7, i64 16, !10, i64 24, !11, i64 32, !10, i64 40, !10, i64 44, !7, i64 48, !10, i64 56, !12, i64 60, !8, i64 62, !8, i64 63, !8, i64 64, !8, i64 65, !8, i64 66, !8, i64 67, !8, i64 68, !8, i64 69, !13, i64 72, !10, i64 120, !10, i64 124, !7, i64 128, !10, i64 136}
!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 = !{!"WalIndexHdr", !10, i64 0, !10, i64 4, !10, i64 8, !8, i64 12, !8, i64 13, !12, i64 14, !10, i64 16, !10, i64 20, !8, i64 24, !8, i64 32, !8, i64 40}
!14 = !{!6, !8, i64 63}
!15 = !{!10, !10, i64 0}
!16 = !{!6, !11, i64 32}
!17 = !{!6, !7, i64 16}
!18 = !{!6, !7, i64 0}
!19 = !{!6, !7, i64 128}
!20 = !{!6, !7, i64 48}


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