; LLVM IR from avaxar/ccgc :: ccgc (ccgc_desegment)

; 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.Block = type { i64, i8, i8, [6 x i8], [0 x i8] }

@page = external hidden global [65536 x i8], align 16

; 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 void @ccgc_desegment() #1 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = load i64, ptr @page, align 16, !tbaa !5
  %5 = icmp eq i64 %4, 0
  br i1 %5, label %6, label %7

6:                                                ; preds = %0
  br label %46

7:                                                ; preds = %0
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  store ptr @page, ptr %1, align 8, !tbaa !11
  br label %8

8:                                                ; preds = %44, %7
  br label %9

9:                                                ; preds = %8
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #2
  %10 = load ptr, ptr %1, align 8, !tbaa !11
  %11 = getelementptr inbounds %struct.Block, ptr %10, i32 0, i32 4
  %12 = getelementptr inbounds [0 x i8], ptr %11, i64 0, i64 0
  %13 = load ptr, ptr %1, align 8, !tbaa !11
  %14 = getelementptr inbounds %struct.Block, ptr %13, i32 0, i32 0
  %15 = load i64, ptr %14, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %12, i64 %15
  store ptr %16, ptr %2, align 8, !tbaa !11
  %17 = load ptr, ptr %2, align 8, !tbaa !11
  %18 = icmp uge ptr %17, getelementptr inbounds (i8, ptr @page, i64 65536)
  br i1 %18, label %19, label %20

19:                                               ; preds = %9
  store i32 3, ptr %3, align 4
  br label %42

20:                                               ; preds = %9
  %21 = load ptr, ptr %1, align 8, !tbaa !11
  %22 = getelementptr inbounds %struct.Block, ptr %21, i32 0, i32 1
  %23 = load i8, ptr %22, align 8, !tbaa !13, !range !14, !noundef !15
  %24 = trunc i8 %23 to i1
  br i1 %24, label %30, label %25

25:                                               ; preds = %20
  %26 = load ptr, ptr %2, align 8, !tbaa !11
  %27 = getelementptr inbounds %struct.Block, ptr %26, i32 0, i32 1
  %28 = load i8, ptr %27, align 8, !tbaa !13, !range !14, !noundef !15
  %29 = trunc i8 %28 to i1
  br i1 %29, label %30, label %32

30:                                               ; preds = %25, %20
  %31 = load ptr, ptr %2, align 8, !tbaa !11
  store ptr %31, ptr %1, align 8, !tbaa !11
  br label %41

32:                                               ; preds = %25
  %33 = load ptr, ptr %2, align 8, !tbaa !11
  %34 = getelementptr inbounds %struct.Block, ptr %33, i32 0, i32 0
  %35 = load i64, ptr %34, align 8, !tbaa !5
  %36 = add i64 16, %35
  %37 = load ptr, ptr %1, align 8, !tbaa !11
  %38 = getelementptr inbounds %struct.Block, ptr %37, i32 0, i32 0
  %39 = load i64, ptr %38, align 8, !tbaa !5
  %40 = add i64 %39, %36
  store i64 %40, ptr %38, align 8, !tbaa !5
  br label %41

41:                                               ; preds = %32, %30
  store i32 0, ptr %3, align 4
  br label %42

42:                                               ; preds = %41, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  %43 = load i32, ptr %3, align 4
  switch i32 %43, label %47 [
    i32 0, label %44
    i32 3, label %45
  ]

44:                                               ; preds = %42
  br label %8

45:                                               ; preds = %42
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #2
  br label %46

46:                                               ; preds = %45, %6
  ret void

47:                                               ; preds = %42
  unreachable
}

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, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 8, !10, i64 9, !8, i64 16}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"_Bool", !8, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !8, i64 0}
!13 = !{!6, !10, i64 8}
!14 = !{i8 0, i8 2}
!15 = !{}


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

@page = external hidden local_unnamed_addr global [65536 x i8], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @ccgc_desegment() local_unnamed_addr #0 {
  %1 = load i64, ptr @page, align 16, !tbaa !5
  %2 = icmp ne i64 %1, 0
  %.08.add14 = add i64 %1, 16
  %.not16 = icmp slt i64 %.08.add14, 65536
  %or.cond = and i1 %2, %.not16
  br i1 %or.cond, label %.lr.ph, label %.thread

.lr.ph:                                           ; preds = %0, %15
  %.add19 = phi i64 [ %.add, %15 ], [ %.08.add14, %0 ]
  %3 = phi i64 [ %16, %15 ], [ %1, %0 ]
  %.08.ptr18 = phi ptr [ %.08.ptr, %15 ], [ @page, %0 ]
  %.08.idx17 = phi i64 [ %.2.idx, %15 ], [ 0, %0 ]
  %.ptr1020 = getelementptr inbounds i8, ptr @page, i64 %.add19
  %4 = getelementptr inbounds i8, ptr %.08.ptr18, i64 8
  %5 = load i8, ptr %4, align 8, !tbaa !11, !range !12, !noundef !13
  %6 = trunc nuw i8 %5 to i1
  br i1 %6, label %15, label %7

7:                                                ; preds = %.lr.ph
  %8 = getelementptr inbounds i8, ptr %.ptr1020, i64 8
  %9 = load i8, ptr %8, align 8, !tbaa !11, !range !12, !noundef !13
  %10 = trunc nuw i8 %9 to i1
  br i1 %10, label %15, label %11

11:                                               ; preds = %7
  %12 = load i64, ptr %.ptr1020, align 8, !tbaa !5
  %13 = add i64 %3, 16
  %14 = add i64 %13, %12
  store i64 %14, ptr %.08.ptr18, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %11, %7, %.lr.ph
  %.2.idx = phi i64 [ %.08.idx17, %11 ], [ %.add19, %7 ], [ %.add19, %.lr.ph ]
  %.08.ptr = getelementptr inbounds i8, ptr @page, i64 %.2.idx
  %16 = load i64, ptr %.08.ptr, align 8, !tbaa !5
  %.08.add = add i64 %16, 16
  %.add = add i64 %.08.add, %.2.idx
  %.not = icmp slt i64 %.add, 65536
  br i1 %.not, label %.lr.ph, label %.thread

.thread:                                          ; preds = %15, %0
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, 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 = !{!"", !7, i64 0, !10, i64 8, !10, i64 9, !8, i64 16}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"_Bool", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{i8 0, i8 2}
!13 = !{}


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