; LLVM IR from yinqiwen/ardb :: src/common/buffer/buffer.ll (ZN4ardb6Buffer6ReadFDEiRi)

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

%struct.iovec = type { ptr, i64 }
%"class.ardb::Buffer" = type <{ ptr, i64, i64, i64, i8, [7 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: inlinehint mustprogress uwtable
declare dso_local noundef i64 @_ZNK4ardb6Buffer14WriteableBytesEv(ptr noundef nonnull align 8 dereferenceable(33)) #1 align 2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i32 @_ZN4ardb6Buffer5WriteEPKvm(ptr noundef nonnull align 8 dereferenceable(33), ptr noundef, i64 noundef) #1 align 2

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN4ardb6Buffer6ReadFDEiRi(ptr noundef nonnull align 8 dereferenceable(33) %0, i32 noundef %1, ptr noundef nonnull align 4 dereferenceable(4) %2) #3 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca [65536 x i8], align 16
  %8 = alloca [2 x %struct.iovec], align 16
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  %11 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 65536, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 32, ptr %8) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %12 = call noundef i64 @_ZNK4ardb6Buffer14WriteableBytesEv(ptr noundef nonnull align 8 dereferenceable(33) %11)
  store i64 %12, ptr %9, align 8, !tbaa !11
  %13 = getelementptr inbounds %"class.ardb::Buffer", ptr %11, i32 0, i32 0
  %14 = load ptr, ptr %13, align 8, !tbaa !13
  %15 = getelementptr inbounds %"class.ardb::Buffer", ptr %11, i32 0, i32 2
  %16 = load i64, ptr %15, align 8, !tbaa !16
  %17 = getelementptr inbounds i8, ptr %14, i64 %16
  %18 = getelementptr inbounds [2 x %struct.iovec], ptr %8, i64 0, i64 0
  %19 = getelementptr inbounds %struct.iovec, ptr %18, i32 0, i32 0
  store ptr %17, ptr %19, align 16, !tbaa !17
  %20 = load i64, ptr %9, align 8, !tbaa !11
  %21 = getelementptr inbounds [2 x %struct.iovec], ptr %8, i64 0, i64 0
  %22 = getelementptr inbounds %struct.iovec, ptr %21, i32 0, i32 1
  store i64 %20, ptr %22, align 8, !tbaa !19
  %23 = getelementptr inbounds [65536 x i8], ptr %7, i64 0, i64 0
  %24 = getelementptr inbounds [2 x %struct.iovec], ptr %8, i64 0, i64 1
  %25 = getelementptr inbounds %struct.iovec, ptr %24, i32 0, i32 0
  store ptr %23, ptr %25, align 16, !tbaa !17
  %26 = getelementptr inbounds [2 x %struct.iovec], ptr %8, i64 0, i64 1
  %27 = getelementptr inbounds %struct.iovec, ptr %26, i32 0, i32 1
  store i64 65536, ptr %27, align 8, !tbaa !19
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  %28 = load i32, ptr %5, align 4, !tbaa !9
  %29 = getelementptr inbounds [2 x %struct.iovec], ptr %8, i64 0, i64 0
  %30 = load i64, ptr %9, align 8, !tbaa !11
  %31 = icmp ugt i64 %30, 65536
  %32 = zext i1 %31 to i64
  %33 = select i1 %31, i32 1, i32 2
  %34 = call i64 @readv(i32 noundef %28, ptr noundef %29, i32 noundef %33)
  %35 = trunc i64 %34 to i32
  store i32 %35, ptr %10, align 4, !tbaa !9
  %36 = load i32, ptr %10, align 4, !tbaa !9
  %37 = icmp slt i32 %36, 0
  br i1 %37, label %38, label %42

38:                                               ; preds = %3
  %39 = call ptr @__errno_location() #6
  %40 = load i32, ptr %39, align 4, !tbaa !9
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 %40, ptr %41, align 4, !tbaa !9
  br label %64

42:                                               ; preds = %3
  %43 = load i32, ptr %10, align 4, !tbaa !9
  %44 = sext i32 %43 to i64
  %45 = load i64, ptr %9, align 8, !tbaa !11
  %46 = icmp ule i64 %44, %45
  br i1 %46, label %47, label %53

47:                                               ; preds = %42
  %48 = load i32, ptr %10, align 4, !tbaa !9
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds %"class.ardb::Buffer", ptr %11, i32 0, i32 2
  %51 = load i64, ptr %50, align 8, !tbaa !16
  %52 = add i64 %51, %49
  store i64 %52, ptr %50, align 8, !tbaa !16
  br label %63

53:                                               ; preds = %42
  %54 = getelementptr inbounds %"class.ardb::Buffer", ptr %11, i32 0, i32 1
  %55 = load i64, ptr %54, align 8, !tbaa !20
  %56 = getelementptr inbounds %"class.ardb::Buffer", ptr %11, i32 0, i32 2
  store i64 %55, ptr %56, align 8, !tbaa !16
  %57 = getelementptr inbounds [65536 x i8], ptr %7, i64 0, i64 0
  %58 = load i32, ptr %10, align 4, !tbaa !9
  %59 = sext i32 %58 to i64
  %60 = load i64, ptr %9, align 8, !tbaa !11
  %61 = sub i64 %59, %60
  %62 = call noundef i32 @_ZN4ardb6Buffer5WriteEPKvm(ptr noundef nonnull align 8 dereferenceable(33) %11, ptr noundef %57, i64 noundef %61)
  br label %63

63:                                               ; preds = %53, %47
  br label %64

64:                                               ; preds = %63, %38
  %65 = load i32, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 32, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 65536, ptr %7) #5
  ret i32 %65
}

declare i64 @readv(i32 noundef, ptr noundef, i32 noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint 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 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 #3 = { 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 #4 = { "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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"_ZTSN4ardb6BufferE", !6, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !15, i64 32}
!15 = !{!"bool", !7, i64 0}
!16 = !{!14, !12, i64 16}
!17 = !{!18, !6, i64 0}
!18 = !{!"_ZTS5iovec", !6, i64 0, !12, i64 8}
!19 = !{!18, !12, i64 8}
!20 = !{!14, !12, i64 8}


; 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.iovec = type { ptr, i64 }

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i64 @_ZNK4ardb6Buffer14WriteableBytesEv(ptr noundef nonnull align 8 dereferenceable(33)) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef i32 @_ZN4ardb6Buffer5WriteEPKvm(ptr noundef nonnull align 8 dereferenceable(33), ptr noundef, i64 noundef) local_unnamed_addr #1 align 2

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN4ardb6Buffer6ReadFDEiRi(ptr noundef nonnull align 8 dereferenceable(33) %0, i32 noundef %1, ptr nocapture noundef nonnull writeonly align 4 dereferenceable(4) %2) local_unnamed_addr #3 align 2 {
  %4 = alloca [65536 x i8], align 16
  %5 = alloca [2 x %struct.iovec], align 16
  call void @llvm.lifetime.start.p0(i64 65536, ptr nonnull %4) #5
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %5) #5
  %6 = tail call noundef i64 @_ZNK4ardb6Buffer14WriteableBytesEv(ptr noundef nonnull align 8 dereferenceable(33) %0)
  %7 = load ptr, ptr %0, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = load i64, ptr %8, align 8, !tbaa !12
  %10 = getelementptr inbounds i8, ptr %7, i64 %9
  store ptr %10, ptr %5, align 16, !tbaa !13
  %11 = getelementptr inbounds i8, ptr %5, i64 8
  store i64 %6, ptr %11, align 8, !tbaa !15
  %12 = getelementptr inbounds i8, ptr %5, i64 16
  store ptr %4, ptr %12, align 16, !tbaa !13
  %13 = getelementptr inbounds i8, ptr %5, i64 24
  store i64 65536, ptr %13, align 8, !tbaa !15
  %14 = icmp ugt i64 %6, 65536
  %15 = select i1 %14, i32 1, i32 2
  %16 = call i64 @readv(i32 noundef %1, ptr noundef nonnull %5, i32 noundef %15)
  %17 = trunc i64 %16 to i32
  %18 = icmp slt i32 %17, 0
  br i1 %18, label %19, label %22

19:                                               ; preds = %3
  %20 = tail call ptr @__errno_location() #6
  %21 = load i32, ptr %20, align 4, !tbaa !16
  store i32 %21, ptr %2, align 4, !tbaa !16
  br label %32

22:                                               ; preds = %3
  %23 = and i64 %16, 2147483647
  %.not = icmp ugt i64 %23, %6
  br i1 %.not, label %27, label %24

24:                                               ; preds = %22
  %25 = load i64, ptr %8, align 8, !tbaa !12
  %26 = add i64 %25, %23
  store i64 %26, ptr %8, align 8, !tbaa !12
  br label %32

27:                                               ; preds = %22
  %28 = getelementptr inbounds i8, ptr %0, i64 8
  %29 = load i64, ptr %28, align 8, !tbaa !18
  store i64 %29, ptr %8, align 8, !tbaa !12
  %30 = sub nsw i64 %23, %6
  %31 = call noundef i32 @_ZN4ardb6Buffer5WriteEPKvm(ptr noundef nonnull align 8 dereferenceable(33) %0, ptr noundef nonnull %4, i64 noundef %30)
  br label %32

32:                                               ; preds = %24, %27, %19
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %5) #5
  call void @llvm.lifetime.end.p0(i64 65536, ptr nonnull %4) #5
  ret i32 %17
}

declare i64 @readv(i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #4

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint 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 = { 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 #3 = { 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 #4 = { "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, !7, i64 0}
!6 = !{!"_ZTSN4ardb6BufferE", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !11, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"bool", !8, i64 0}
!12 = !{!6, !10, i64 16}
!13 = !{!14, !7, i64 0}
!14 = !{!"_ZTS5iovec", !7, i64 0, !10, i64 8}
!15 = !{!14, !10, i64 8}
!16 = !{!17, !17, i64 0}
!17 = !{!"int", !8, i64 0}
!18 = !{!6, !10, i64 8}


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