; LLVM IR from knizhnik/cppgc :: copygc/gc.ll (ZN2GC15MemoryAllocator5_copyEPNS_6ObjectE)

; 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.GC::MemorySegment" = type { ptr, ptr }
%"class.GC::MemoryAllocator" = type { i64, i64, ptr, ptr, i64, ptr, ptr, i64, i64, ptr, 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: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZNK2GC6Object9getHeaderEv(ptr noundef nonnull align 8 dereferenceable(8)) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN2GC15MemoryAllocator5_copyEPNS_6ObjectE(ptr noundef nonnull align 8 dereferenceable(88) %0, ptr noundef %1) #2 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %8 = load ptr, ptr %4, align 8
  %9 = load ptr, ptr %5, align 8, !tbaa !5
  %10 = icmp ne ptr %9, null
  br i1 %10, label %11, label %60

11:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = call noundef ptr @_ZNK2GC6Object9getHeaderEv(ptr noundef nonnull align 8 dereferenceable(8) %12)
  store ptr %13, ptr %6, align 8, !tbaa !5
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = load i64, ptr %14, align 8, !tbaa !9
  %16 = and i64 %15, 1
  %17 = icmp ne i64 %16, 0
  br i1 %17, label %18, label %23

18:                                               ; preds = %11
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = load i64, ptr %19, align 8, !tbaa !9
  %21 = sub i64 %20, 1
  %22 = inttoptr i64 %21 to ptr
  store ptr %22, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %57

23:                                               ; preds = %11
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = load i64, ptr %24, align 8, !tbaa !9
  %26 = inttoptr i64 %25 to ptr
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = icmp eq ptr %26, %27
  br i1 %28, label %29, label %39

29:                                               ; preds = %23
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = ptrtoint ptr %30 to i64
  %32 = add i64 %31, 1
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  store i64 %32, ptr %33, align 8, !tbaa !9
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = load ptr, ptr %34, align 8, !tbaa !10
  %36 = getelementptr inbounds ptr, ptr %35, i64 0
  %37 = load ptr, ptr %36, align 8
  %38 = call noundef ptr %37(ptr noundef nonnull align 8 dereferenceable(8) %34, ptr noundef %8)
  br label %55

39:                                               ; preds = %23
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = load ptr, ptr %40, align 8, !tbaa !9
  %42 = getelementptr inbounds %"struct.GC::MemorySegment", ptr %41, i32 0, i32 1
  %43 = load ptr, ptr %42, align 8, !tbaa !12
  %44 = icmp eq ptr %43, %8
  br i1 %44, label %45, label %54

45:                                               ; preds = %39
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 9
  store ptr %46, ptr %47, align 8, !tbaa !14
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = load ptr, ptr %48, align 8, !tbaa !10
  %50 = getelementptr inbounds ptr, ptr %49, i64 0
  %51 = load ptr, ptr %50, align 8
  %52 = call noundef ptr %51(ptr noundef nonnull align 8 dereferenceable(8) %48, ptr noundef %8)
  store ptr %52, ptr %5, align 8, !tbaa !5
  %53 = getelementptr inbounds %"class.GC::MemoryAllocator", ptr %8, i32 0, i32 9
  store ptr null, ptr %53, align 8, !tbaa !14
  br label %54

54:                                               ; preds = %45, %39
  br label %55

55:                                               ; preds = %54, %29
  br label %56

56:                                               ; preds = %55
  store i32 0, ptr %7, align 4
  br label %57

57:                                               ; preds = %56, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %58 = load i32, ptr %7, align 4
  switch i32 %58, label %64 [
    i32 0, label %59
    i32 1, label %62
  ]

59:                                               ; preds = %57
  br label %60

60:                                               ; preds = %59, %2
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %61, ptr %3, align 8
  br label %62

62:                                               ; preds = %60, %57
  %63 = load ptr, ptr %3, align 8
  ret ptr %63

64:                                               ; preds = %57
  unreachable
}

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 = { 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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"vtable pointer", !8, i64 0}
!12 = !{!13, !6, i64 8}
!13 = !{!"_ZTSN2GC13MemorySegmentE", !6, i64 0, !6, i64 8}
!14 = !{!15, !6, i64 72}
!15 = !{!"_ZTSN2GC15MemoryAllocatorE", !16, i64 0, !16, i64 8, !6, i64 16, !6, i64 24, !16, i64 32, !6, i64 40, !6, i64 48, !16, i64 56, !16, i64 64, !6, i64 72, !6, i64 80}
!16 = !{!"long", !7, i64 0}


; 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 nounwind uwtable
declare dso_local noundef ptr @_ZNK2GC6Object9getHeaderEv(ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN2GC15MemoryAllocator5_copyEPNS_6ObjectE(ptr noundef nonnull align 8 dereferenceable(88) %0, ptr noundef %1) local_unnamed_addr #1 align 2 {
  %.not = icmp eq ptr %1, null
  br i1 %.not, label %28, label %3

3:                                                ; preds = %2
  %4 = tail call noundef ptr @_ZNK2GC6Object9getHeaderEv(ptr noundef nonnull align 8 dereferenceable(8) %1)
  %5 = load i64, ptr %4, align 8
  %6 = and i64 %5, 1
  %.not21.not = icmp eq i64 %6, 0
  %7 = inttoptr i64 %5 to ptr
  br i1 %.not21.not, label %11, label %8

8:                                                ; preds = %3
  %9 = add nsw i64 %5, -1
  %10 = inttoptr i64 %9 to ptr
  br label %28

11:                                               ; preds = %3
  %12 = icmp eq ptr %7, %1
  br i1 %12, label %13, label %19

13:                                               ; preds = %11
  %14 = ptrtoint ptr %1 to i64
  %15 = add i64 %14, 1
  store i64 %15, ptr %4, align 8, !tbaa !5
  %16 = load ptr, ptr %1, align 8, !tbaa !8
  %17 = load ptr, ptr %16, align 8
  %18 = tail call noundef ptr %17(ptr noundef nonnull align 8 dereferenceable(8) %1, ptr noundef nonnull %0)
  br label %28

19:                                               ; preds = %11
  %20 = getelementptr inbounds i8, ptr %7, i64 8
  %21 = load ptr, ptr %20, align 8, !tbaa !10
  %22 = icmp eq ptr %21, %0
  br i1 %22, label %23, label %28

23:                                               ; preds = %19
  %24 = getelementptr inbounds i8, ptr %0, i64 72
  store ptr %1, ptr %24, align 8, !tbaa !13
  %25 = load ptr, ptr %1, align 8, !tbaa !8
  %26 = load ptr, ptr %25, align 8
  %27 = tail call noundef ptr %26(ptr noundef nonnull align 8 dereferenceable(8) %1, ptr noundef nonnull %0)
  store ptr null, ptr %24, align 8, !tbaa !13
  br label %28

28:                                               ; preds = %8, %13, %23, %19, %2
  %.1 = phi ptr [ null, %2 ], [ %10, %8 ], [ %1, %13 ], [ %27, %23 ], [ %1, %19 ]
  ret ptr %.1
}

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

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"vtable pointer", !7, i64 0}
!10 = !{!11, !12, i64 8}
!11 = !{!"_ZTSN2GC13MemorySegmentE", !12, i64 0, !12, i64 8}
!12 = !{!"any pointer", !6, i64 0}
!13 = !{!14, !12, i64 72}
!14 = !{!"_ZTSN2GC15MemoryAllocatorE", !15, i64 0, !15, i64 8, !12, i64 16, !12, i64 24, !15, i64 32, !12, i64 40, !12, i64 48, !15, i64 56, !15, i64 64, !12, i64 72, !12, i64 80}
!15 = !{!"long", !6, i64 0}


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