; LLVM IR from aap/librw :: src/base.ll (ZN2rw12StreamMemory6write8EPKvj)

; 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.rw::StreamMemory" = type <{ %"class.rw::Stream", ptr, i32, i32, i32, [4 x i8] }>
%"class.rw::Stream" = type { 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: 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) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN2rw12StreamMemory6write8EPKvj(ptr noundef nonnull align 8 dereferenceable(28) %0, ptr noundef %1, i32 noundef %2) unnamed_addr #2 align 2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  %9 = load ptr, ptr %5, align 8
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  %11 = getelementptr inbounds ptr, ptr %10, i64 7
  %12 = load ptr, ptr %11, align 8
  %13 = call noundef zeroext i1 %12(ptr noundef nonnull align 8 dereferenceable(28) %9)
  br i1 %13, label %14, label %15

14:                                               ; preds = %3
  store i32 0, ptr %4, align 4
  br label %65

15:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %16 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %16, ptr %8, align 4, !tbaa !9
  %17 = getelementptr inbounds %"class.rw::StreamMemory", ptr %9, i32 0, i32 4
  %18 = load i32, ptr %17, align 8, !tbaa !13
  %19 = load i32, ptr %8, align 4, !tbaa !9
  %20 = add i32 %18, %19
  %21 = getelementptr inbounds %"class.rw::StreamMemory", ptr %9, i32 0, i32 2
  %22 = load i32, ptr %21, align 8, !tbaa !16
  %23 = icmp ugt i32 %20, %22
  br i1 %23, label %24, label %44

24:                                               ; preds = %15
  %25 = getelementptr inbounds %"class.rw::StreamMemory", ptr %9, i32 0, i32 4
  %26 = load i32, ptr %25, align 8, !tbaa !13
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = add i32 %26, %27
  %29 = getelementptr inbounds %"class.rw::StreamMemory", ptr %9, i32 0, i32 3
  %30 = load i32, ptr %29, align 4, !tbaa !17
  %31 = icmp ugt i32 %28, %30
  br i1 %31, label %32, label %38

32:                                               ; preds = %24
  %33 = getelementptr inbounds %"class.rw::StreamMemory", ptr %9, i32 0, i32 3
  %34 = load i32, ptr %33, align 4, !tbaa !17
  %35 = getelementptr inbounds %"class.rw::StreamMemory", ptr %9, i32 0, i32 4
  %36 = load i32, ptr %35, align 8, !tbaa !13
  %37 = sub i32 %34, %36
  store i32 %37, ptr %8, align 4, !tbaa !9
  br label %38

38:                                               ; preds = %32, %24
  %39 = getelementptr inbounds %"class.rw::StreamMemory", ptr %9, i32 0, i32 4
  %40 = load i32, ptr %39, align 8, !tbaa !13
  %41 = load i32, ptr %8, align 4, !tbaa !9
  %42 = add i32 %40, %41
  %43 = getelementptr inbounds %"class.rw::StreamMemory", ptr %9, i32 0, i32 2
  store i32 %42, ptr %43, align 8, !tbaa !16
  br label %44

44:                                               ; preds = %38, %15
  %45 = getelementptr inbounds %"class.rw::StreamMemory", ptr %9, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !18
  %47 = getelementptr inbounds %"class.rw::StreamMemory", ptr %9, i32 0, i32 4
  %48 = load i32, ptr %47, align 8, !tbaa !13
  %49 = zext i32 %48 to i64
  %50 = getelementptr inbounds i8, ptr %46, i64 %49
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = load i32, ptr %8, align 4, !tbaa !9
  %53 = zext i32 %52 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %50, ptr align 1 %51, i64 %53, i1 false)
  %54 = load i32, ptr %8, align 4, !tbaa !9
  %55 = getelementptr inbounds %"class.rw::StreamMemory", ptr %9, i32 0, i32 4
  %56 = load i32, ptr %55, align 8, !tbaa !13
  %57 = add i32 %56, %54
  store i32 %57, ptr %55, align 8, !tbaa !13
  %58 = load i32, ptr %7, align 4, !tbaa !9
  %59 = load i32, ptr %8, align 4, !tbaa !9
  %60 = icmp ne i32 %58, %59
  br i1 %60, label %61, label %63

61:                                               ; preds = %44
  %62 = getelementptr inbounds %"class.rw::StreamMemory", ptr %9, i32 0, i32 4
  store i32 -1, ptr %62, align 8, !tbaa !13
  br label %63

63:                                               ; preds = %61, %44
  %64 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %64, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %65

65:                                               ; preds = %63, %14
  %66 = load i32, ptr %4, align 4
  ret i32 %66
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { 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, !10, i64 24}
!14 = !{!"_ZTSN2rw12StreamMemoryE", !15, i64 0, !6, i64 8, !10, i64 16, !10, i64 20, !10, i64 24}
!15 = !{!"_ZTSN2rw6StreamE"}
!16 = !{!14, !10, i64 16}
!17 = !{!14, !10, i64 20}
!18 = !{!14, !6, 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"

; 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) #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN2rw12StreamMemory6write8EPKvj(ptr noundef nonnull align 8 dereferenceable(28) %0, ptr nocapture noundef readonly %1, i32 noundef %2) unnamed_addr #1 align 2 {
  %4 = load ptr, ptr %0, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %4, i64 56
  %6 = load ptr, ptr %5, align 8
  %7 = tail call noundef zeroext i1 %6(ptr noundef nonnull align 8 dereferenceable(28) %0)
  br i1 %7, label %29, label %8

8:                                                ; preds = %3
  %9 = getelementptr inbounds i8, ptr %0, i64 24
  %10 = load i32, ptr %9, align 8, !tbaa !8
  %11 = add i32 %10, %2
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  %13 = load i32, ptr %12, align 8, !tbaa !14
  %14 = icmp ugt i32 %11, %13
  br i1 %14, label %15, label %21

15:                                               ; preds = %8
  %16 = getelementptr inbounds i8, ptr %0, i64 20
  %17 = load i32, ptr %16, align 4, !tbaa !15
  %18 = icmp ugt i32 %11, %17
  %19 = sub i32 %17, %10
  %spec.select = select i1 %18, i32 %19, i32 %2
  %20 = add i32 %spec.select, %10
  store i32 %20, ptr %12, align 8, !tbaa !14
  br label %21

21:                                               ; preds = %15, %8
  %.1 = phi i32 [ %spec.select, %15 ], [ %2, %8 ]
  %22 = getelementptr inbounds i8, ptr %0, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !16
  %24 = zext i32 %10 to i64
  %25 = getelementptr inbounds i8, ptr %23, i64 %24
  %26 = zext i32 %.1 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %25, ptr align 1 %1, i64 %26, i1 false)
  %27 = load i32, ptr %9, align 8, !tbaa !8
  %28 = add i32 %27, %.1
  %.not = icmp eq i32 %.1, %2
  %spec.select14 = select i1 %.not, i32 %28, i32 -1
  store i32 %spec.select14, ptr %9, align 8, !tbaa !8
  br label %29

29:                                               ; preds = %3, %21
  %.011 = phi i32 [ %.1, %21 ], [ 0, %3 ]
  ret i32 %.011
}

attributes #0 = { mustprogress nocallback nofree 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" }

!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 = !{!"_ZTSN2rw12StreamMemoryE", !10, i64 0, !11, i64 8, !13, i64 16, !13, i64 20, !13, i64 24}
!10 = !{!"_ZTSN2rw6StreamE"}
!11 = !{!"any pointer", !12, i64 0}
!12 = !{!"omnipotent char", !7, i64 0}
!13 = !{!"int", !12, i64 0}
!14 = !{!9, !13, i64 16}
!15 = !{!9, !13, i64 20}
!16 = !{!9, !11, i64 8}


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