; LLVM IR from IOsonata/IOsonata :: src/storage/diskio_impl.ll (ZN6DiskIO5FlushEv)

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

%class.DiskIO = type { ptr, i32, i32, i32, ptr }
%struct.__DiskIO_Cache_Desc = type { i32, i32, 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: mustprogress uwtable
define dso_local void @_ZN6DiskIO5FlushEv(ptr noundef nonnull align 8 dereferenceable(32) %0) #1 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %4 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %5

5:                                                ; preds = %49, %1
  %6 = load i32, ptr %3, align 4, !tbaa !9
  %7 = getelementptr inbounds %class.DiskIO, ptr %4, i32 0, i32 3
  %8 = load i32, ptr %7, align 8, !tbaa !11
  %9 = icmp slt i32 %6, %8
  br i1 %9, label %11, label %10

10:                                               ; preds = %5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %52

11:                                               ; preds = %5
  %12 = getelementptr inbounds %class.DiskIO, ptr %4, i32 0, i32 4
  %13 = load ptr, ptr %12, align 8, !tbaa !14
  %14 = load i32, ptr %3, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %13, i64 %15
  %17 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %16, i32 0, i32 0
  %18 = load volatile i32, ptr %17, align 4, !tbaa !15
  %19 = and i32 %18, -2147483648
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %21, label %48

21:                                               ; preds = %11
  %22 = getelementptr inbounds %class.DiskIO, ptr %4, i32 0, i32 4
  %23 = load ptr, ptr %22, align 8, !tbaa !14
  %24 = load i32, ptr %3, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %23, i64 %25
  %27 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %26, i32 0, i32 1
  %28 = load i32, ptr %27, align 4, !tbaa !17
  %29 = getelementptr inbounds %class.DiskIO, ptr %4, i32 0, i32 4
  %30 = load ptr, ptr %29, align 8, !tbaa !14
  %31 = load i32, ptr %3, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %30, i64 %32
  %34 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %33, i32 0, i32 2
  %35 = load ptr, ptr %34, align 4, !tbaa !18
  %36 = load ptr, ptr %4, align 8, !tbaa !19
  %37 = getelementptr inbounds ptr, ptr %36, i64 7
  %38 = load ptr, ptr %37, align 8
  %39 = call noundef zeroext i1 %38(ptr noundef nonnull align 8 dereferenceable(32) %4, i32 noundef %28, ptr noundef %35)
  %40 = getelementptr inbounds %class.DiskIO, ptr %4, i32 0, i32 4
  %41 = load ptr, ptr %40, align 8, !tbaa !14
  %42 = load i32, ptr %3, align 4, !tbaa !9
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %41, i64 %43
  %45 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %44, i32 0, i32 0
  %46 = load volatile i32, ptr %45, align 4, !tbaa !15
  %47 = and i32 %46, 2147483647
  store volatile i32 %47, ptr %45, align 4, !tbaa !15
  br label %48

48:                                               ; preds = %21, %11
  br label %49

49:                                               ; preds = %48
  %50 = load i32, ptr %3, align 4, !tbaa !9
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %3, align 4, !tbaa !9
  br label %5, !llvm.loop !21

52:                                               ; preds = %10
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 16}
!12 = !{!"_ZTS6DiskIO", !13, i64 8, !10, i64 12, !10, i64 16, !6, i64 24}
!13 = !{!"_ZTS12__DiskIOType", !7, i64 0}
!14 = !{!12, !6, i64 24}
!15 = !{!16, !10, i64 0}
!16 = !{!"_ZTS19__DiskIO_Cache_Desc", !10, i64 0, !10, i64 4, !6, i64 8}
!17 = !{!16, !10, i64 4}
!18 = !{!16, !6, i64 8}
!19 = !{!20, !20, i64 0}
!20 = !{!"vtable pointer", !8, i64 0}
!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"

%struct.__DiskIO_Cache_Desc = type { i32, i32, ptr }

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN6DiskIO5FlushEv(ptr noundef nonnull align 8 dereferenceable(32) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 16
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 24
  %.pre = load ptr, ptr %5, align 8, !tbaa !12
  br label %6

._crit_edge:                                      ; preds = %24, %1
  ret void

6:                                                ; preds = %.lr.ph, %24
  %7 = phi i32 [ %3, %.lr.ph ], [ %25, %24 ]
  %8 = phi ptr [ %.pre, %.lr.ph ], [ %26, %24 ]
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %24 ]
  %9 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %8, i64 %indvars.iv
  %10 = load volatile i32, ptr %9, align 4, !tbaa !13
  %.not = icmp sgt i32 %10, -1
  br i1 %.not, label %24, label %11

11:                                               ; preds = %6
  %12 = getelementptr inbounds i8, ptr %9, i64 4
  %13 = load i32, ptr %12, align 4, !tbaa !15
  %14 = getelementptr inbounds i8, ptr %9, i64 8
  %15 = load ptr, ptr %14, align 4, !tbaa !16
  %16 = load ptr, ptr %0, align 8, !tbaa !17
  %17 = getelementptr inbounds i8, ptr %16, i64 56
  %18 = load ptr, ptr %17, align 8
  %19 = tail call noundef zeroext i1 %18(ptr noundef nonnull align 8 dereferenceable(32) %0, i32 noundef %13, ptr noundef %15)
  %20 = load ptr, ptr %5, align 8, !tbaa !12
  %21 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %20, i64 %indvars.iv
  %22 = load volatile i32, ptr %21, align 4, !tbaa !13
  %23 = and i32 %22, 2147483647
  store volatile i32 %23, ptr %21, align 4, !tbaa !13
  %.pre8 = load i32, ptr %2, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %6, %11
  %25 = phi i32 [ %7, %6 ], [ %.pre8, %11 ]
  %26 = phi ptr [ %8, %6 ], [ %20, %11 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %27 = sext i32 %25 to i64
  %28 = icmp slt i64 %indvars.iv.next, %27
  br i1 %28, label %6, label %._crit_edge, !llvm.loop !19
}

attributes #0 = { mustprogress 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, !10, i64 16}
!6 = !{!"_ZTS6DiskIO", !7, i64 8, !10, i64 12, !10, i64 16, !11, i64 24}
!7 = !{!"_ZTS12__DiskIOType", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !11, i64 24}
!13 = !{!14, !10, i64 0}
!14 = !{!"_ZTS19__DiskIO_Cache_Desc", !10, i64 0, !10, i64 4, !11, i64 8}
!15 = !{!14, !10, i64 4}
!16 = !{!14, !11, i64 8}
!17 = !{!18, !18, i64 0}
!18 = !{!"vtable pointer", !9, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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