; LLVM IR from GPUOpen-LibrariesAndSDKs/TAN :: amf/public/common/DataStreamMemory.ll (ZN3amf23AMFDataStreamMemoryImpl7ReallocEm)

; 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.amf::AMFDataStreamMemoryImpl" = type { %"class.amf::AMFInterfaceImpl", ptr, i64, i64, i64 }
%"class.amf::AMFInterfaceImpl" = type { %"class.amf::AMFDataStream", %"class.amf::AMFInterfaceBase" }
%"class.amf::AMFDataStream" = type { %"class.amf::AMFInterface" }
%"class.amf::AMFInterface" = type { ptr }
%"class.amf::AMFInterfaceBase" = type { ptr, i64 }

declare void @amf_virtual_free(ptr noundef) #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN3amf23AMFDataStreamMemoryImpl7ReallocEm(ptr noundef nonnull align 8 dereferenceable(56) %0, i64 noundef %1) #1 align 2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  %8 = load ptr, ptr %4, align 8
  %9 = load i64, ptr %5, align 8, !tbaa !9
  %10 = getelementptr inbounds %"class.amf::AMFDataStreamMemoryImpl", ptr %8, i32 0, i32 2
  %11 = load i64, ptr %10, align 8, !tbaa !11
  %12 = icmp ugt i64 %9, %11
  br i1 %12, label %13, label %39

13:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %14 = load i64, ptr %5, align 8, !tbaa !9
  %15 = call ptr @amf_virtual_alloc(i64 noundef %14)
  store ptr %15, ptr %6, align 8, !tbaa !5
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = icmp eq ptr %16, null
  br i1 %17, label %18, label %19

18:                                               ; preds = %13
  store i32 6, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %36

19:                                               ; preds = %13
  %20 = load i64, ptr %5, align 8, !tbaa !9
  %21 = getelementptr inbounds %"class.amf::AMFDataStreamMemoryImpl", ptr %8, i32 0, i32 3
  store i64 %20, ptr %21, align 8, !tbaa !17
  %22 = getelementptr inbounds %"class.amf::AMFDataStreamMemoryImpl", ptr %8, i32 0, i32 1
  %23 = load ptr, ptr %22, align 8, !tbaa !18
  %24 = icmp ne ptr %23, null
  br i1 %24, label %25, label %33

25:                                               ; preds = %19
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = getelementptr inbounds %"class.amf::AMFDataStreamMemoryImpl", ptr %8, i32 0, i32 1
  %28 = load ptr, ptr %27, align 8, !tbaa !18
  %29 = getelementptr inbounds %"class.amf::AMFDataStreamMemoryImpl", ptr %8, i32 0, i32 2
  %30 = load i64, ptr %29, align 8, !tbaa !11
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %26, ptr align 1 %28, i64 %30, i1 false)
  %31 = getelementptr inbounds %"class.amf::AMFDataStreamMemoryImpl", ptr %8, i32 0, i32 1
  %32 = load ptr, ptr %31, align 8, !tbaa !18
  call void @amf_virtual_free(ptr noundef %32)
  br label %33

33:                                               ; preds = %25, %19
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = getelementptr inbounds %"class.amf::AMFDataStreamMemoryImpl", ptr %8, i32 0, i32 1
  store ptr %34, ptr %35, align 8, !tbaa !18
  store i32 0, ptr %7, align 4
  br label %36

36:                                               ; preds = %33, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  %37 = load i32, ptr %7, align 4
  switch i32 %37, label %54 [
    i32 0, label %38
    i32 1, label %52
  ]

38:                                               ; preds = %36
  br label %39

39:                                               ; preds = %38, %2
  %40 = load i64, ptr %5, align 8, !tbaa !9
  %41 = getelementptr inbounds %"class.amf::AMFDataStreamMemoryImpl", ptr %8, i32 0, i32 2
  store i64 %40, ptr %41, align 8, !tbaa !11
  %42 = getelementptr inbounds %"class.amf::AMFDataStreamMemoryImpl", ptr %8, i32 0, i32 4
  %43 = load i64, ptr %42, align 8, !tbaa !19
  %44 = getelementptr inbounds %"class.amf::AMFDataStreamMemoryImpl", ptr %8, i32 0, i32 2
  %45 = load i64, ptr %44, align 8, !tbaa !11
  %46 = icmp ugt i64 %43, %45
  br i1 %46, label %47, label %51

47:                                               ; preds = %39
  %48 = getelementptr inbounds %"class.amf::AMFDataStreamMemoryImpl", ptr %8, i32 0, i32 2
  %49 = load i64, ptr %48, align 8, !tbaa !11
  %50 = getelementptr inbounds %"class.amf::AMFDataStreamMemoryImpl", ptr %8, i32 0, i32 4
  store i64 %49, ptr %50, align 8, !tbaa !19
  br label %51

51:                                               ; preds = %47, %39
  store i32 0, ptr %3, align 4
  br label %52

52:                                               ; preds = %51, %36
  %53 = load i32, ptr %3, align 4
  ret i32 %53

54:                                               ; preds = %36
  unreachable
}

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

declare ptr @amf_virtual_alloc(i64 noundef) #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) #3

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

attributes #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 32}
!12 = !{!"_ZTSN3amf23AMFDataStreamMemoryImplE", !13, i64 0, !6, i64 24, !10, i64 32, !10, i64 40, !10, i64 48}
!13 = !{!"_ZTSN3amf16AMFInterfaceImplINS_13AMFDataStreamEiiiEE", !14, i64 0, !16, i64 8}
!14 = !{!"_ZTSN3amf13AMFDataStreamE", !15, i64 0}
!15 = !{!"_ZTSN3amf12AMFInterfaceE"}
!16 = !{!"_ZTSN3amf16AMFInterfaceBaseE", !10, i64 8}
!17 = !{!12, !10, i64 40}
!18 = !{!12, !6, i64 24}
!19 = !{!12, !10, i64 48}


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

declare void @amf_virtual_free(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef range(i32 0, 7) i32 @_ZN3amf23AMFDataStreamMemoryImpl7ReallocEm(ptr nocapture noundef nonnull align 8 dereferenceable(56) %0, i64 noundef %1) local_unnamed_addr #1 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 32
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %5 = icmp ult i64 %4, %1
  br i1 %5, label %6, label %16

6:                                                ; preds = %2
  %7 = tail call ptr @amf_virtual_alloc(i64 noundef %1)
  %8 = icmp eq ptr %7, null
  br i1 %8, label %21, label %9

9:                                                ; preds = %6
  %10 = getelementptr inbounds i8, ptr %0, i64 40
  store i64 %1, ptr %10, align 8, !tbaa !15
  %11 = getelementptr inbounds i8, ptr %0, i64 24
  %12 = load ptr, ptr %11, align 8, !tbaa !16
  %.not = icmp eq ptr %12, null
  br i1 %.not, label %.thread, label %13

13:                                               ; preds = %9
  %14 = load i64, ptr %3, align 8, !tbaa !5
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %7, ptr nonnull align 1 %12, i64 %14, i1 false)
  %15 = load ptr, ptr %11, align 8, !tbaa !16
  tail call void @amf_virtual_free(ptr noundef %15)
  br label %.thread

.thread:                                          ; preds = %9, %13
  store ptr %7, ptr %11, align 8, !tbaa !16
  br label %16

16:                                               ; preds = %.thread, %2
  store i64 %1, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %0, i64 48
  %18 = load i64, ptr %17, align 8, !tbaa !17
  %19 = icmp ugt i64 %18, %1
  br i1 %19, label %20, label %21

20:                                               ; preds = %16
  store i64 %1, ptr %17, align 8, !tbaa !17
  br label %21

21:                                               ; preds = %6, %16, %20
  %.1 = phi i32 [ 0, %20 ], [ 0, %16 ], [ 6, %6 ]
  ret i32 %.1
}

declare ptr @amf_virtual_alloc(i64 noundef) local_unnamed_addr #0

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

attributes #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 = { 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, !11, i64 32}
!6 = !{!"_ZTSN3amf23AMFDataStreamMemoryImplE", !7, i64 0, !14, i64 24, !11, i64 32, !11, i64 40, !11, i64 48}
!7 = !{!"_ZTSN3amf16AMFInterfaceImplINS_13AMFDataStreamEiiiEE", !8, i64 0, !10, i64 8}
!8 = !{!"_ZTSN3amf13AMFDataStreamE", !9, i64 0}
!9 = !{!"_ZTSN3amf12AMFInterfaceE"}
!10 = !{!"_ZTSN3amf16AMFInterfaceBaseE", !11, i64 8}
!11 = !{!"long", !12, i64 0}
!12 = !{!"omnipotent char", !13, i64 0}
!13 = !{!"Simple C++ TBAA"}
!14 = !{!"any pointer", !12, i64 0}
!15 = !{!6, !11, i64 40}
!16 = !{!6, !14, i64 24}
!17 = !{!6, !11, i64 48}


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