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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN6DiskIO5WriteEjjPhj(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 %90

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 true)
  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 %58

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 i32, ptr %9, align 4, !tbaa !9
  %47 = zext i32 %46 to i64
  %48 = getelementptr inbounds i8, ptr %40, i64 %47
  %49 = load ptr, ptr %10, align 8, !tbaa !5
  %50 = load i32, ptr %13, align 4, !tbaa !9
  %51 = zext i32 %50 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %48, ptr align 1 %49, i64 %51, i1 false)
  %52 = load i32, ptr %8, align 4, !tbaa !9
  %53 = load ptr, ptr %17, align 8, !tbaa !11
  %54 = getelementptr inbounds ptr, ptr %53, i64 7
  %55 = load ptr, ptr %54, align 8
  %56 = call noundef zeroext i1 %55(ptr noundef nonnull align 8 dereferenceable(32) %17, i32 noundef %52, ptr noundef %40)
  %57 = load ptr, ptr %15, align 8
  call void @llvm.stackrestore.p0(ptr %57)
  br label %88

58:                                               ; preds = %21
  %59 = getelementptr inbounds %class.DiskIO, ptr %17, i32 0, i32 4
  %60 = load ptr, ptr %59, align 8, !tbaa !13
  %61 = load i32, ptr %14, align 4, !tbaa !9
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %60, i64 %62
  %64 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %63, i32 0, i32 2
  %65 = load ptr, ptr %64, align 4, !tbaa !16
  %66 = load i32, ptr %9, align 4, !tbaa !9
  %67 = zext i32 %66 to i64
  %68 = getelementptr inbounds i8, ptr %65, i64 %67
  %69 = load ptr, ptr %10, align 8, !tbaa !5
  %70 = load i32, ptr %13, align 4, !tbaa !9
  %71 = zext i32 %70 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %68, ptr align 1 %69, i64 %71, i1 false)
  %72 = getelementptr inbounds %class.DiskIO, ptr %17, i32 0, i32 4
  %73 = load ptr, ptr %72, align 8, !tbaa !13
  %74 = load i32, ptr %14, align 4, !tbaa !9
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %73, i64 %75
  %77 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %76, i32 0, i32 0
  %78 = load volatile i32, ptr %77, align 4, !tbaa !18
  %79 = or i32 %78, -2147483648
  store volatile i32 %79, ptr %77, align 4, !tbaa !18
  %80 = getelementptr inbounds %class.DiskIO, ptr %17, i32 0, i32 4
  %81 = load ptr, ptr %80, align 8, !tbaa !13
  %82 = load i32, ptr %14, align 4, !tbaa !9
  %83 = sext i32 %82 to i64
  %84 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %81, i64 %83
  %85 = getelementptr inbounds %struct.__DiskIO_Cache_Desc, ptr %84, i32 0, i32 0
  %86 = load volatile i32, ptr %85, align 4, !tbaa !18
  %87 = add nsw i32 %86, -1
  store volatile i32 %87, ptr %85, align 4, !tbaa !18
  br label %88

88:                                               ; preds = %58, %36
  %89 = load i32, ptr %13, align 4, !tbaa !9
  store i32 %89, 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 %90

90:                                               ; preds = %88, %20
  %91 = load i32, ptr %6, align 4
  ret i32 %91
}

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: 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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN6DiskIO5WriteEjjPhj(ptr noundef nonnull align 8 dereferenceable(32) %0, i32 noundef %1, i32 noundef %2, ptr noundef readonly %3, i32 noundef %4) unnamed_addr #0 align 2 {
  %6 = icmp eq ptr %3, null
  br i1 %6, label %47, 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 true)
  %16 = icmp slt i32 %15, 0
  br i1 %16, label %17, label %32

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 = zext i32 %14 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %26, ptr nonnull align 1 %3, i64 %27, i1 false)
  %28 = load ptr, ptr %0, align 8, !tbaa !5
  %29 = getelementptr inbounds i8, ptr %28, i64 56
  %30 = load ptr, ptr %29, align 8
  %31 = call noundef zeroext i1 %30(ptr noundef nonnull align 8 dereferenceable(32) %0, i32 noundef %1, ptr noundef nonnull %20)
  call void @llvm.stackrestore.p0(ptr %19)
  br label %47

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

47:                                               ; preds = %17, %32, %5
  %.0 = phi i32 [ -1, %5 ], [ %14, %32 ], [ %14, %17 ]
  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" }
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