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

; 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
declare dso_local noundef i32 @_ZN6DiskIO12GetCacheSectEjb(ptr noundef nonnull align 8 dereferenceable(32), i32 noundef, i1 noundef zeroext) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN6DiskIO4ReadEjjPhj(ptr noundef nonnull align 8 dereferenceable(32) %0, i32 noundef %1, i32 noundef %2, ptr noundef %3, i32 noundef %4) unnamed_addr #1 align 2 {
  %6 = alloca i32, align 4
  %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
  %14 = alloca i32, align 4
  %15 = alloca ptr, align 8
  %16 = alloca i64, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !9
  store i32 %2, ptr %9, align 4, !tbaa !9
  store ptr %3, ptr %10, align 8, !tbaa !5
  store i32 %4, ptr %11, align 4, !tbaa !9
  %17 = load ptr, ptr %7, align 8
  %18 = load ptr, ptr %10, align 8, !tbaa !5
  %19 = icmp eq ptr %18, null
  br i1 %19, label %20, label %21

20:                                               ; preds = %5
  store i32 -1, ptr %6, align 4
  br label %77

21:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  %22 = load ptr, ptr %17, align 8, !tbaa !11
  %23 = getelementptr inbounds ptr, ptr %22, i64 2
  %24 = load ptr, ptr %23, align 8
  %25 = call noundef zeroext i16 %24(ptr noundef nonnull align 8 dereferenceable(32) %17)
  %26 = zext i16 %25 to i32
  store i32 %26, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #5
  %27 = load i32, ptr %11, align 4, !tbaa !9
  %28 = load i32, ptr %12, align 4, !tbaa !9
  %29 = load i32, ptr %9, align 4, !tbaa !9
  %30 = sub i32 %28, %29
  %31 = call noundef i32 @_ZL3minii(i32 noundef %27, i32 noundef %30)
  store i32 %31, ptr %13, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #5
  %32 = load i32, ptr %8, align 4, !tbaa !9
  %33 = call noundef i32 @_ZN6DiskIO12GetCacheSectEjb(ptr noundef nonnull align 8 dereferenceable(32) %17, i32 noundef %32, i1 noundef zeroext false)
  store i32 %33, ptr %14, align 4, !tbaa !9
  %34 = load i32, ptr %14, align 4, !tbaa !9
  %35 = icmp slt i32 %34, 0
  br i1 %35, label %36, label %53

36:                                               ; preds = %21
  %37 = load i32, ptr %12, align 4, !tbaa !9
  %38 = zext i32 %37 to i64
  %39 = call ptr @llvm.stacksave.p0()
  store ptr %39, ptr %15, align 8
  %40 = alloca i8, i64 %38, align 16
  store i64 %38, ptr %16, align 8
  %41 = load i32, ptr %8, align 4, !tbaa !9
  %42 = load ptr, ptr %17, align 8, !tbaa !11
  %43 = getelementptr inbounds ptr, ptr %42, i64 6
  %44 = load ptr, ptr %43, align 8
  %45 = call noundef zeroext i1 %44(ptr noundef nonnull align 8 dereferenceable(32) %17, i32 noundef %41, ptr noundef %40)
  %46 = load ptr, ptr %10, align 8, !tbaa !5
  %47 = load i32, ptr %9, align 4, !tbaa !9
  %48 = zext i32 %47 to i64
  %49 = getelementptr inbounds i8, ptr %40, i64 %48
  %50 = load i32, ptr %13, align 4, !tbaa !9
  %51 = sext i32 %50 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %46, ptr align 1 %49, i64 %51, i1 false)
  %52 = load ptr, ptr %15, align 8
  call void @llvm.stackrestore.p0(ptr %52)
  br label %75

53:                                               ; preds = %21
  %54 = load ptr, ptr %10, align 8, !tbaa !5
  %55 = getelementptr inbounds %class.DiskIO, ptr %17, i32 0, i32 4
  %56 = load ptr, ptr %55, align 8, !tbaa !13
  %57 = load i32, ptr %14, align 4, !tbaa !9
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %56, i64 %58
  %60 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %59, i32 0, i32 2
  %61 = load ptr, ptr %60, align 4, !tbaa !16
  %62 = load i32, ptr %9, align 4, !tbaa !9
  %63 = zext i32 %62 to i64
  %64 = getelementptr inbounds i8, ptr %61, i64 %63
  %65 = load i32, ptr %13, align 4, !tbaa !9
  %66 = sext i32 %65 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %54, ptr align 1 %64, i64 %66, i1 false)
  %67 = getelementptr inbounds %class.DiskIO, ptr %17, i32 0, i32 4
  %68 = load ptr, ptr %67, align 8, !tbaa !13
  %69 = load i32, ptr %14, align 4, !tbaa !9
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %68, i64 %70
  %72 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %71, i32 0, i32 0
  %73 = load volatile i32, ptr %72, align 4, !tbaa !18
  %74 = add nsw i32 %73, -1
  store volatile i32 %74, ptr %72, align 4, !tbaa !18
  br label %75

75:                                               ; preds = %53, %36
  %76 = load i32, ptr %13, align 4, !tbaa !9
  store i32 %76, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  br label %77

77:                                               ; preds = %75, %20
  %78 = load i32, ptr %6, align 4
  ret i32 %78
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef i32 @_ZL3minii(i32 noundef, i32 noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #3

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #4

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #3

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


; 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
declare dso_local noundef i32 @_ZN6DiskIO12GetCacheSectEjb(ptr noundef nonnull align 8 dereferenceable(32), i32 noundef, i1 noundef zeroext) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN6DiskIO4ReadEjjPhj(ptr noundef nonnull align 8 dereferenceable(32) %0, i32 noundef %1, i32 noundef %2, ptr noundef writeonly %3, i32 noundef %4) unnamed_addr #0 align 2 {
  %6 = icmp eq ptr %3, null
  br i1 %6, label %41, label %7

7:                                                ; preds = %5
  %8 = load ptr, ptr %0, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %8, i64 16
  %10 = load ptr, ptr %9, align 8
  %11 = tail call noundef zeroext i16 %10(ptr noundef nonnull align 8 dereferenceable(32) %0)
  %12 = zext i16 %11 to i32
  %13 = sub i32 %12, %2
  %14 = tail call noundef i32 @_ZL3minii(i32 noundef %4, i32 noundef %13)
  %15 = tail call noundef i32 @_ZN6DiskIO12GetCacheSectEjb(ptr noundef nonnull align 8 dereferenceable(32) %0, i32 noundef %1, i1 noundef zeroext false)
  %16 = icmp slt i32 %15, 0
  br i1 %16, label %17, label %28

17:                                               ; preds = %7
  %18 = zext i16 %11 to i64
  %19 = tail call ptr @llvm.stacksave.p0()
  %20 = alloca i8, i64 %18, align 16
  %21 = load ptr, ptr %0, align 8, !tbaa !5
  %22 = getelementptr inbounds i8, ptr %21, i64 48
  %23 = load ptr, ptr %22, align 8
  %24 = call noundef zeroext i1 %23(ptr noundef nonnull align 8 dereferenceable(32) %0, i32 noundef %1, ptr noundef nonnull %20)
  %25 = zext i32 %2 to i64
  %26 = getelementptr inbounds i8, ptr %20, i64 %25
  %27 = sext i32 %14 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %3, ptr nonnull align 1 %26, i64 %27, i1 false)
  call void @llvm.stackrestore.p0(ptr %19)
  br label %41

28:                                               ; preds = %7
  %29 = getelementptr inbounds i8, ptr %0, i64 24
  %30 = load ptr, ptr %29, align 8, !tbaa !8
  %31 = zext nneg i32 %15 to i64
  %32 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %30, i64 %31, i32 2
  %33 = load ptr, ptr %32, align 4, !tbaa !14
  %34 = zext i32 %2 to i64
  %35 = getelementptr inbounds i8, ptr %33, i64 %34
  %36 = sext i32 %14 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %3, ptr align 1 %35, i64 %36, i1 false)
  %37 = load ptr, ptr %29, align 8, !tbaa !8
  %38 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %37, i64 %31
  %39 = load volatile i32, ptr %38, align 4, !tbaa !16
  %40 = add nsw i32 %39, -1
  store volatile i32 %40, ptr %38, align 4, !tbaa !16
  br label %41

41:                                               ; preds = %17, %28, %5
  %.0 = phi i32 [ -1, %5 ], [ %14, %28 ], [ %14, %17 ]
  ret i32 %.0
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef i32 @_ZL3minii(i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #2

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" }
attributes #1 = { inlinehint mustprogress 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 = { mustprogress nocallback nofree nosync nounwind willreturn }
attributes #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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 = !{!"vtable pointer", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !13, i64 24}
!9 = !{!"_ZTS6DiskIO", !10, i64 8, !12, i64 12, !12, i64 16, !13, i64 24}
!10 = !{!"_ZTS12__DiskIOType", !11, i64 0}
!11 = !{!"omnipotent char", !7, i64 0}
!12 = !{!"int", !11, i64 0}
!13 = !{!"any pointer", !11, i64 0}
!14 = !{!15, !13, i64 8}
!15 = !{!"_ZTS19__DiskIO_Cache_Desc", !12, i64 0, !12, i64 4, !13, i64 8}
!16 = !{!15, !12, i64 0}


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