; LLVM IR from IOsonata/IOsonata :: src/storage (ff_diskio)

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

@_ZL11s_pFFDiskIO = external hidden global ptr, align 8

; 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 i32 @disk_read(i8 noundef zeroext %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca i8, align 1
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i64, align 8
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  store i8 %0, ptr %6, align 1, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !8
  store i32 %2, ptr %8, align 4, !tbaa !10
  store i32 %3, ptr %9, align 4, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %16 = load i8, ptr %6, align 1, !tbaa !5
  %17 = zext i8 %16 to i32
  %18 = icmp sgt i32 %17, 0
  br i1 %18, label %19, label %20

19:                                               ; preds = %4
  store i32 4, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %55

20:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  %21 = load i32, ptr %8, align 4, !tbaa !10
  %22 = zext i32 %21 to i64
  %23 = mul i64 %22, 512
  store i64 %23, ptr %13, align 8, !tbaa !12
  br label %24

24:                                               ; preds = %51, %20
  %25 = load i32, ptr %9, align 4, !tbaa !10
  %26 = icmp ugt i32 %25, 0
  br i1 %26, label %27, label %54

27:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #2
  store i32 512, ptr %14, align 4, !tbaa !10
  br label %28

28:                                               ; preds = %31, %27
  %29 = load i32, ptr %14, align 4, !tbaa !10
  %30 = icmp sgt i32 %29, 0
  br i1 %30, label %31, label %51

31:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #2
  %32 = load ptr, ptr @_ZL11s_pFFDiskIO, align 8, !tbaa !8
  %33 = load i64, ptr %13, align 8, !tbaa !12
  %34 = load ptr, ptr %7, align 8, !tbaa !8
  %35 = load i32, ptr %14, align 4, !tbaa !10
  %36 = load ptr, ptr %32, align 8, !tbaa !14
  %37 = getelementptr inbounds ptr, ptr %36, i64 10
  %38 = load ptr, ptr %37, align 8
  %39 = call noundef i32 %38(ptr noundef nonnull align 8 dereferenceable(32) %32, i64 noundef %33, ptr noundef %34, i32 noundef %35)
  store i32 %39, ptr %15, align 4, !tbaa !10
  %40 = load i32, ptr %15, align 4, !tbaa !10
  %41 = load ptr, ptr %7, align 8, !tbaa !8
  %42 = sext i32 %40 to i64
  %43 = getelementptr inbounds i8, ptr %41, i64 %42
  store ptr %43, ptr %7, align 8, !tbaa !8
  %44 = load i32, ptr %15, align 4, !tbaa !10
  %45 = sext i32 %44 to i64
  %46 = load i64, ptr %13, align 8, !tbaa !12
  %47 = add i64 %46, %45
  store i64 %47, ptr %13, align 8, !tbaa !12
  %48 = load i32, ptr %15, align 4, !tbaa !10
  %49 = load i32, ptr %14, align 4, !tbaa !10
  %50 = sub nsw i32 %49, %48
  store i32 %50, ptr %14, align 4, !tbaa !10
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #2
  br label %28, !llvm.loop !16

51:                                               ; preds = %28
  %52 = load i32, ptr %9, align 4, !tbaa !10
  %53 = add i32 %52, -1
  store i32 %53, ptr %9, align 4, !tbaa !10
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #2
  br label %24, !llvm.loop !18

54:                                               ; preds = %24
  store i32 0, ptr %5, align 4
  store i32 1, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  br label %55

55:                                               ; preds = %54, %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  %56 = load i32, ptr %5, align 4
  ret i32 %56
}

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


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

@_ZL11s_pFFDiskIO = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress uwtable
define dso_local range(i32 0, 5) i32 @disk_read(i8 noundef zeroext %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %.not = icmp eq i8 %0, 0
  br i1 %.not, label %5, label %.loopexit

5:                                                ; preds = %4
  %.not1923 = icmp eq i32 %3, 0
  br i1 %.not1923, label %.loopexit, label %.preheader.preheader

.preheader.preheader:                             ; preds = %5
  %6 = zext i32 %2 to i64
  %7 = shl nuw nsw i64 %6, 9
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %19
  %.01526 = phi i64 [ %16, %19 ], [ %7, %.preheader.preheader ]
  %.01625 = phi ptr [ %15, %19 ], [ %1, %.preheader.preheader ]
  %.01824 = phi i32 [ %20, %19 ], [ %3, %.preheader.preheader ]
  br label %8

8:                                                ; preds = %.preheader, %8
  %.01422 = phi i32 [ 512, %.preheader ], [ %17, %8 ]
  %.121 = phi i64 [ %.01526, %.preheader ], [ %16, %8 ]
  %.11720 = phi ptr [ %.01625, %.preheader ], [ %15, %8 ]
  %9 = load ptr, ptr @_ZL11s_pFFDiskIO, align 8, !tbaa !5
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %11 = getelementptr inbounds i8, ptr %10, i64 80
  %12 = load ptr, ptr %11, align 8
  %13 = tail call noundef i32 %12(ptr noundef nonnull align 8 dereferenceable(32) %9, i64 noundef %.121, ptr noundef %.11720, i32 noundef %.01422)
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds i8, ptr %.11720, i64 %14
  %16 = add i64 %.121, %14
  %17 = sub nsw i32 %.01422, %13
  %18 = icmp sgt i32 %17, 0
  br i1 %18, label %8, label %19, !llvm.loop !11

19:                                               ; preds = %8
  %20 = add i32 %.01824, -1
  %.not19 = icmp eq i32 %20, 0
  br i1 %.not19, label %.loopexit, label %.preheader, !llvm.loop !13

.loopexit:                                        ; preds = %19, %5, %4
  %.0 = phi i32 [ 4, %4 ], [ 0, %5 ], [ 0, %19 ]
  ret i32 %.0
}

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, !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 = !{!"vtable pointer", !8, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass