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

; 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 @_ZN2rw12StreamMemory5read8EPvj(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 %51

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 %30

24:                                               ; preds = %15
  %25 = getelementptr inbounds %"class.rw::StreamMemory", ptr %9, i32 0, i32 2
  %26 = load i32, ptr %25, align 8, !tbaa !16
  %27 = getelementptr inbounds %"class.rw::StreamMemory", ptr %9, i32 0, i32 4
  %28 = load i32, ptr %27, align 8, !tbaa !13
  %29 = sub i32 %26, %28
  store i32 %29, ptr %8, align 4, !tbaa !9
  br label %30

30:                                               ; preds = %24, %15
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = getelementptr inbounds %"class.rw::StreamMemory", ptr %9, i32 0, i32 1
  %33 = load ptr, ptr %32, align 8, !tbaa !17
  %34 = getelementptr inbounds %"class.rw::StreamMemory", ptr %9, i32 0, i32 4
  %35 = load i32, ptr %34, align 8, !tbaa !13
  %36 = zext i32 %35 to i64
  %37 = getelementptr inbounds i8, ptr %33, i64 %36
  %38 = load i32, ptr %8, align 4, !tbaa !9
  %39 = zext i32 %38 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %31, ptr align 1 %37, i64 %39, i1 false)
  %40 = load i32, ptr %8, align 4, !tbaa !9
  %41 = getelementptr inbounds %"class.rw::StreamMemory", ptr %9, i32 0, i32 4
  %42 = load i32, ptr %41, align 8, !tbaa !13
  %43 = add i32 %42, %40
  store i32 %43, ptr %41, align 8, !tbaa !13
  %44 = load i32, ptr %7, align 4, !tbaa !9
  %45 = load i32, ptr %8, align 4, !tbaa !9
  %46 = icmp ne i32 %44, %45
  br i1 %46, label %47, label %49

47:                                               ; preds = %30
  %48 = getelementptr inbounds %"class.rw::StreamMemory", ptr %9, i32 0, i32 4
  store i32 -1, ptr %48, align 8, !tbaa !13
  br label %49

49:                                               ; preds = %47, %30
  %50 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %50, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %51

51:                                               ; preds = %49, %14
  %52 = load i32, ptr %4, align 4
  ret i32 %52
}

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, !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 @_ZN2rw12StreamMemory5read8EPvj(ptr noundef nonnull align 8 dereferenceable(28) %0, ptr nocapture noundef writeonly %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 %23, 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
  %15 = sub i32 %13, %10
  %spec.select = select i1 %14, i32 %15, i32 %2
  %16 = getelementptr inbounds i8, ptr %0, i64 8
  %17 = load ptr, ptr %16, align 8, !tbaa !15
  %18 = zext i32 %10 to i64
  %19 = getelementptr inbounds i8, ptr %17, i64 %18
  %20 = zext i32 %spec.select to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %1, ptr align 1 %19, i64 %20, i1 false)
  %21 = load i32, ptr %9, align 8, !tbaa !8
  %22 = add i32 %21, %spec.select
  %.not = icmp eq i32 %spec.select, %2
  %storemerge = select i1 %.not, i32 %22, i32 -1
  store i32 %storemerge, ptr %9, align 8, !tbaa !8
  br label %23

23:                                               ; preds = %3, %8
  %.09 = phi i32 [ %spec.select, %8 ], [ 0, %3 ]
  ret i32 %.09
}

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, !11, i64 8}


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