; LLVM IR from KoishiDB/Koishi-DB :: src/disk/writable_file.ll (ZN8koishidb12WritableFile15WriteUnbufferedEPKcm)

; 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.koishidb::Status" = type { ptr }
%"class.koishidb::WritableFile" = type { %"class.std::__cxx11::basic_string", %"class.std::__cxx11::basic_string", i32, i64, [65536 x i8] }
%"class.std::__cxx11::basic_string" = type { %"struct.std::__cxx11::basic_string<char>::_Alloc_hider", i64, %union.anon }
%"struct.std::__cxx11::basic_string<char>::_Alloc_hider" = type { ptr }
%union.anon = type { i64, [8 x i8] }

; 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 nounwind uwtable
declare dso_local void @_ZN8koishidb6Status2OKEv(ptr dead_on_unwind noalias writable sret(%"class.koishidb::Status") align 8) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN8koishidb12WritableFile15WriteUnbufferedEPKcm(ptr dead_on_unwind noalias writable sret(%"class.koishidb::Status") align 8 %0, ptr noundef nonnull align 8 dereferenceable(65616) %1, ptr noundef %2, i64 noundef %3) #2 align 2 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  store i64 %3, ptr %8, align 8, !tbaa !9
  %11 = load ptr, ptr %6, align 8
  br label %12

12:                                               ; preds = %41, %39, %4
  %13 = load i64, ptr %8, align 8, !tbaa !9
  %14 = icmp ugt i64 %13, 0
  br i1 %14, label %15, label %42

15:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %16 = getelementptr inbounds %"class.koishidb::WritableFile", ptr %11, i32 0, i32 2
  %17 = load i32, ptr %16, align 8, !tbaa !11
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = load i64, ptr %8, align 8, !tbaa !9
  %20 = call i64 @write(i32 noundef %17, ptr noundef %18, i64 noundef %19)
  store i64 %20, ptr %9, align 8, !tbaa !9
  %21 = load i64, ptr %9, align 8, !tbaa !9
  %22 = icmp slt i64 %21, 0
  br i1 %22, label %23, label %32

23:                                               ; preds = %15
  %24 = call ptr @__errno_location() #6
  %25 = load i32, ptr %24, align 4, !tbaa !16
  %26 = icmp eq i32 %25, 4
  br i1 %26, label %27, label %28

27:                                               ; preds = %23
  store i32 2, ptr %10, align 4
  br label %39, !llvm.loop !17

28:                                               ; preds = %23
  %29 = getelementptr inbounds %"class.koishidb::WritableFile", ptr %11, i32 0, i32 1
  %30 = call ptr @__errno_location() #6
  %31 = load i32, ptr %30, align 4, !tbaa !16
  call void @_ZN8koishidb12WritableFile10PosixErrorERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi(ptr dead_on_unwind writable sret(%"class.koishidb::Status") align 8 %0, ptr noundef nonnull align 8 dereferenceable(32) %29, i32 noundef %31)
  store i32 1, ptr %10, align 4
  br label %39

32:                                               ; preds = %15
  %33 = load i64, ptr %9, align 8, !tbaa !9
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = getelementptr inbounds i8, ptr %34, i64 %33
  store ptr %35, ptr %7, align 8, !tbaa !5
  %36 = load i64, ptr %9, align 8, !tbaa !9
  %37 = load i64, ptr %8, align 8, !tbaa !9
  %38 = sub i64 %37, %36
  store i64 %38, ptr %8, align 8, !tbaa !9
  store i32 0, ptr %10, align 4
  br label %39

39:                                               ; preds = %32, %28, %27
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  %40 = load i32, ptr %10, align 4
  switch i32 %40, label %44 [
    i32 0, label %41
    i32 2, label %12
    i32 1, label %43
  ]

41:                                               ; preds = %39
  br label %12, !llvm.loop !17

42:                                               ; preds = %12
  call void @_ZN8koishidb6Status2OKEv(ptr dead_on_unwind writable sret(%"class.koishidb::Status") align 8 %0)
  br label %43

43:                                               ; preds = %42, %39
  ret void

44:                                               ; preds = %39
  unreachable
}

declare i64 @write(i32 noundef, ptr noundef, i64 noundef) #3

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #4

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN8koishidb12WritableFile10PosixErrorERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi(ptr dead_on_unwind noalias writable sret(%"class.koishidb::Status") align 8, ptr noundef nonnull align 8 dereferenceable(32), i32 noundef) #2 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 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 = { "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 #4 = { nounwind willreturn memory(none) "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 #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(none) }

!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 = !{!"long", !7, i64 0}
!11 = !{!12, !15, i64 64}
!12 = !{!"_ZTSN8koishidb12WritableFileE", !13, i64 0, !13, i64 32, !15, i64 64, !10, i64 72, !7, i64 80}
!13 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", !14, i64 0, !10, i64 8, !7, i64 16}
!14 = !{!"_ZTSNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_Alloc_hiderE", !6, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = !{!15, !15, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"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"

%"class.koishidb::Status" = type { ptr }

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN8koishidb6Status2OKEv(ptr dead_on_unwind noalias writable sret(%"class.koishidb::Status") align 8) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN8koishidb12WritableFile15WriteUnbufferedEPKcm(ptr dead_on_unwind noalias writable sret(%"class.koishidb::Status") align 8 %0, ptr noundef nonnull align 8 dereferenceable(65616) %1, ptr nocapture noundef readonly %2, i64 noundef %3) local_unnamed_addr #1 align 2 {
  %.not19 = icmp eq i64 %3, 0
  br i1 %.not19, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4
  %5 = getelementptr inbounds i8, ptr %1, i64 64
  br label %6

6:                                                ; preds = %.lr.ph, %18
  %.0921 = phi ptr [ %2, %.lr.ph ], [ %.1, %18 ]
  %.01020 = phi i64 [ %3, %.lr.ph ], [ %.111, %18 ]
  %7 = load i32, ptr %5, align 8, !tbaa !5
  %8 = tail call i64 @write(i32 noundef %7, ptr noundef %.0921, i64 noundef %.01020)
  %9 = icmp slt i64 %8, 0
  br i1 %9, label %10, label %15

10:                                               ; preds = %6
  %11 = tail call ptr @__errno_location() #4
  %12 = load i32, ptr %11, align 4, !tbaa !14
  %13 = icmp eq i32 %12, 4
  br i1 %13, label %18, label %.thread, !llvm.loop !15

.thread:                                          ; preds = %10
  %14 = getelementptr inbounds i8, ptr %1, i64 32
  tail call void @_ZN8koishidb12WritableFile10PosixErrorERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi(ptr dead_on_unwind writable sret(%"class.koishidb::Status") align 8 %0, ptr noundef nonnull align 8 dereferenceable(32) %14, i32 noundef %12)
  br label %19

15:                                               ; preds = %6
  %16 = getelementptr inbounds i8, ptr %.0921, i64 %8
  %17 = sub i64 %.01020, %8
  br label %18

18:                                               ; preds = %10, %15
  %.111 = phi i64 [ %17, %15 ], [ %.01020, %10 ]
  %.1 = phi ptr [ %16, %15 ], [ %.0921, %10 ]
  %.not = icmp eq i64 %.111, 0
  br i1 %.not, label %._crit_edge, label %6

._crit_edge:                                      ; preds = %18, %4
  tail call void @_ZN8koishidb6Status2OKEv(ptr dead_on_unwind writable sret(%"class.koishidb::Status") align 8 %0)
  br label %19

19:                                               ; preds = %.thread, %._crit_edge
  ret void
}

; Function Attrs: nofree
declare noundef i64 @write(i32 noundef, ptr nocapture noundef readonly, i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #3

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN8koishidb12WritableFile10PosixErrorERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi(ptr dead_on_unwind noalias writable sret(%"class.koishidb::Status") align 8, ptr noundef nonnull align 8 dereferenceable(32), i32 noundef) local_unnamed_addr #1 align 2

attributes #0 = { 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 #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 = { nofree "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 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 #4 = { nounwind willreturn memory(none) }

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


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