; LLVM IR from brewlin/garbage-collect :: copying (copying)

; 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_heap = type { ptr, i64 }
%struct.header = type { i64, i64, i64, i64, ptr, ptr }

@from = external dso_local global %struct.gc_heap, align 8

; 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: nounwind uwtable
define dso_local ptr @get_header_by_from(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %10 = load ptr, ptr @from, align 8, !tbaa !9
  store ptr %10, ptr %5, align 8, !tbaa !5
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = icmp ugt ptr %11, %12
  br i1 %13, label %14, label %21

14:                                               ; preds = %1
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = load i64, ptr getelementptr inbounds (%struct.gc_heap, ptr @from, i32 0, i32 1), align 8, !tbaa !12
  %17 = getelementptr inbounds i8, ptr %15, i64 %16
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = icmp ule ptr %17, %18
  br i1 %19, label %20, label %21

20:                                               ; preds = %14
  store ptr null, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %55

21:                                               ; preds = %14, %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = load i64, ptr getelementptr inbounds (%struct.gc_heap, ptr @from, i32 0, i32 1), align 8, !tbaa !12
  %24 = getelementptr inbounds i8, ptr %22, i64 %23
  store ptr %24, ptr %8, align 8, !tbaa !5
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %25, ptr %7, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %51, %21
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  %29 = icmp ult ptr %27, %28
  br i1 %29, label %30, label %53

30:                                               ; preds = %26
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.header, ptr %31, i64 1
  %33 = ptrtoint ptr %32 to i64
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.header, ptr %34, i32 0, i32 2
  %36 = load i64, ptr %35, align 8, !tbaa !13
  %37 = sub i64 %36, 48
  %38 = add i64 %33, %37
  %39 = inttoptr i64 %38 to ptr
  store ptr %39, ptr %9, align 8, !tbaa !5
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.header, ptr %40, i64 1
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = icmp ule ptr %41, %42
  br i1 %43, label %44, label %50

44:                                               ; preds = %30
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = load ptr, ptr %9, align 8, !tbaa !5
  %47 = icmp ult ptr %45, %46
  br i1 %47, label %48, label %50

48:                                               ; preds = %44
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %49, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %54

50:                                               ; preds = %44, %30
  br label %51

51:                                               ; preds = %50
  %52 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %52, ptr %7, align 8, !tbaa !5
  br label %26, !llvm.loop !15

53:                                               ; preds = %26
  store ptr null, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %54

54:                                               ; preds = %53, %48
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  br label %55

55:                                               ; preds = %54, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %56 = load ptr, ptr %2, align 8
  ret ptr %56
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { 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/C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"gc_heap", !6, i64 0, !11, i64 8}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!14, !11, i64 16}
!14 = !{!"header", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !6, i64 32, !6, i64 40}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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

@from = external dso_local local_unnamed_addr global %struct.gc_heap, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local ptr @get_header_by_from(ptr noundef readnone %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @from, align 8, !tbaa !5
  %3 = icmp ule ptr %2, %0
  %4 = load i64, ptr getelementptr inbounds (i8, ptr @from, i64 8), align 8
  %5 = getelementptr inbounds i8, ptr %2, i64 %4
  %.not = icmp ugt ptr %5, %0
  %or.cond = select i1 %3, i1 true, i1 %.not
  br i1 %or.cond, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %1, %7
  %.017 = phi ptr [ %14, %7 ], [ %2, %1 ]
  %6 = icmp ult ptr %.017, %5
  br i1 %6, label %7, label %.loopexit

7:                                                ; preds = %.preheader
  %8 = getelementptr inbounds i8, ptr %.017, i64 48
  %9 = ptrtoint ptr %8 to i64
  %10 = getelementptr inbounds i8, ptr %.017, i64 16
  %11 = load i64, ptr %10, align 8, !tbaa !11
  %12 = add i64 %9, -48
  %13 = add i64 %12, %11
  %14 = inttoptr i64 %13 to ptr
  %.not21 = icmp ule ptr %8, %0
  %15 = icmp ugt ptr %14, %0
  %or.cond23 = select i1 %.not21, i1 %15, i1 false
  br i1 %or.cond23, label %.loopexit, label %.preheader, !llvm.loop !13

.loopexit:                                        ; preds = %7, %.preheader, %1
  %.1 = phi ptr [ null, %1 ], [ null, %.preheader ], [ %.017, %7 ]
  ret ptr %.1
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) 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, !7, i64 0}
!6 = !{!"gc_heap", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!12, !10, i64 16}
!12 = !{!"header", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !7, i64 32, !7, i64 40}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass