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

; 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
@.str = external hidden unnamed_addr constant [37 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [52 x i8], align 1
@__PRETTY_FUNCTION__.ccgc_malloc = external hidden unnamed_addr constant [26 x i8], align 1

; 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 @ccgc_malloc(i64 noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  store ptr @page, ptr %4, align 8, !tbaa !9
  %8 = load ptr, ptr %4, align 8, !tbaa !9
  %9 = getelementptr inbounds %struct.Block, ptr %8, i32 0, i32 0
  %10 = load i64, ptr %9, align 8, !tbaa !11
  %11 = icmp eq i64 %10, 0
  br i1 %11, label %12, label %17

12:                                               ; preds = %1
  %13 = load ptr, ptr %4, align 8, !tbaa !9
  %14 = getelementptr inbounds %struct.Block, ptr %13, i32 0, i32 0
  store i64 65520, ptr %14, align 8, !tbaa !11
  %15 = load ptr, ptr %4, align 8, !tbaa !9
  %16 = getelementptr inbounds %struct.Block, ptr %15, i32 0, i32 1
  store i8 0, ptr %16, align 8, !tbaa !14
  br label %17

17:                                               ; preds = %12, %1
  %18 = load i64, ptr %3, align 8, !tbaa !5
  %19 = icmp eq i64 %18, 0
  br i1 %19, label %20, label %21

20:                                               ; preds = %17
  store ptr null, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %123

21:                                               ; preds = %17
  %22 = load i64, ptr %3, align 8, !tbaa !5
  %23 = udiv i64 %22, 8
  %24 = load i64, ptr %3, align 8, !tbaa !5
  %25 = urem i64 %24, 8
  %26 = icmp ugt i64 %25, 0
  %27 = zext i1 %26 to i32
  %28 = sext i32 %27 to i64
  %29 = add i64 %23, %28
  %30 = mul i64 8, %29
  store i64 %30, ptr %3, align 8, !tbaa !5
  br label %31

31:                                               ; preds = %90, %21
  %32 = load ptr, ptr %4, align 8, !tbaa !9
  %33 = getelementptr inbounds %struct.Block, ptr %32, i32 0, i32 1
  %34 = load i8, ptr %33, align 8, !tbaa !14, !range !15, !noundef !16
  %35 = trunc i8 %34 to i1
  br i1 %35, label %42, label %36

36:                                               ; preds = %31
  %37 = load ptr, ptr %4, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.Block, ptr %37, i32 0, i32 0
  %39 = load i64, ptr %38, align 8, !tbaa !11
  %40 = load i64, ptr %3, align 8, !tbaa !5
  %41 = icmp ult i64 %39, %40
  br label %42

42:                                               ; preds = %36, %31
  %43 = phi i1 [ true, %31 ], [ %41, %36 ]
  br i1 %43, label %44, label %92

44:                                               ; preds = %42
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %45 = load ptr, ptr %4, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.Block, ptr %45, i32 0, i32 4
  %47 = getelementptr inbounds [0 x i8], ptr %46, i64 0, i64 0
  %48 = load ptr, ptr %4, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.Block, ptr %48, i32 0, i32 0
  %50 = load i64, ptr %49, align 8, !tbaa !11
  %51 = getelementptr inbounds i8, ptr %47, i64 %50
  store ptr %51, ptr %6, align 8, !tbaa !9
  %52 = load ptr, ptr %6, align 8, !tbaa !9
  %53 = icmp ult ptr %52, getelementptr inbounds (i8, ptr @page, i64 65536)
  br i1 %53, label %54, label %55

54:                                               ; preds = %44
  br label %56

55:                                               ; preds = %44
  call void @__assert_fail(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 60, ptr noundef @__PRETTY_FUNCTION__.ccgc_malloc) #4
  unreachable

56:                                               ; preds = %54
  br label %57

57:                                               ; preds = %89, %56
  %58 = load ptr, ptr %4, align 8, !tbaa !9
  %59 = getelementptr inbounds %struct.Block, ptr %58, i32 0, i32 1
  %60 = load i8, ptr %59, align 8, !tbaa !14, !range !15, !noundef !16
  %61 = trunc i8 %60 to i1
  br i1 %61, label %68, label %62

62:                                               ; preds = %57
  %63 = load ptr, ptr %6, align 8, !tbaa !9
  %64 = getelementptr inbounds %struct.Block, ptr %63, i32 0, i32 1
  %65 = load i8, ptr %64, align 8, !tbaa !14, !range !15, !noundef !16
  %66 = trunc i8 %65 to i1
  %67 = xor i1 %66, true
  br label %68

68:                                               ; preds = %62, %57
  %69 = phi i1 [ false, %57 ], [ %67, %62 ]
  br i1 %69, label %70, label %90

70:                                               ; preds = %68
  %71 = load ptr, ptr %6, align 8, !tbaa !9
  %72 = getelementptr inbounds %struct.Block, ptr %71, i32 0, i32 0
  %73 = load i64, ptr %72, align 8, !tbaa !11
  %74 = add i64 16, %73
  %75 = load ptr, ptr %4, align 8, !tbaa !9
  %76 = getelementptr inbounds %struct.Block, ptr %75, i32 0, i32 0
  %77 = load i64, ptr %76, align 8, !tbaa !11
  %78 = add i64 %77, %74
  store i64 %78, ptr %76, align 8, !tbaa !11
  %79 = load ptr, ptr %4, align 8, !tbaa !9
  %80 = getelementptr inbounds %struct.Block, ptr %79, i32 0, i32 4
  %81 = getelementptr inbounds [0 x i8], ptr %80, i64 0, i64 0
  %82 = load ptr, ptr %4, align 8, !tbaa !9
  %83 = getelementptr inbounds %struct.Block, ptr %82, i32 0, i32 0
  %84 = load i64, ptr %83, align 8, !tbaa !11
  %85 = getelementptr inbounds i8, ptr %81, i64 %84
  store ptr %85, ptr %6, align 8, !tbaa !9
  %86 = load ptr, ptr %6, align 8, !tbaa !9
  %87 = icmp uge ptr %86, getelementptr inbounds (i8, ptr @page, i64 65536)
  br i1 %87, label %88, label %89

88:                                               ; preds = %70
  br label %90

89:                                               ; preds = %70
  br label %57, !llvm.loop !17

90:                                               ; preds = %88, %68
  %91 = load ptr, ptr %6, align 8, !tbaa !9
  store ptr %91, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %31, !llvm.loop !19

92:                                               ; preds = %42
  %93 = load ptr, ptr %4, align 8, !tbaa !9
  %94 = getelementptr inbounds %struct.Block, ptr %93, i32 0, i32 0
  %95 = load i64, ptr %94, align 8, !tbaa !11
  %96 = load i64, ptr %3, align 8, !tbaa !5
  %97 = add i64 %96, 16
  %98 = icmp ugt i64 %95, %97
  br i1 %98, label %99, label %117

99:                                               ; preds = %92
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %100 = load ptr, ptr %4, align 8, !tbaa !9
  %101 = getelementptr inbounds i8, ptr %100, i64 16
  %102 = load i64, ptr %3, align 8, !tbaa !5
  %103 = getelementptr inbounds i8, ptr %101, i64 %102
  store ptr %103, ptr %7, align 8, !tbaa !9
  %104 = load ptr, ptr %4, align 8, !tbaa !9
  %105 = getelementptr inbounds %struct.Block, ptr %104, i32 0, i32 0
  %106 = load i64, ptr %105, align 8, !tbaa !11
  %107 = load i64, ptr %3, align 8, !tbaa !5
  %108 = sub i64 %106, %107
  %109 = sub i64 %108, 16
  %110 = load ptr, ptr %7, align 8, !tbaa !9
  %111 = getelementptr inbounds %struct.Block, ptr %110, i32 0, i32 0
  store i64 %109, ptr %111, align 8, !tbaa !11
  %112 = load ptr, ptr %7, align 8, !tbaa !9
  %113 = getelementptr inbounds %struct.Block, ptr %112, i32 0, i32 1
  store i8 0, ptr %113, align 8, !tbaa !14
  %114 = load i64, ptr %3, align 8, !tbaa !5
  %115 = load ptr, ptr %4, align 8, !tbaa !9
  %116 = getelementptr inbounds %struct.Block, ptr %115, i32 0, i32 0
  store i64 %114, ptr %116, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %117

117:                                              ; preds = %99, %92
  %118 = load ptr, ptr %4, align 8, !tbaa !9
  %119 = getelementptr inbounds %struct.Block, ptr %118, i32 0, i32 1
  store i8 1, ptr %119, align 8, !tbaa !14
  %120 = load ptr, ptr %4, align 8, !tbaa !9
  %121 = getelementptr inbounds %struct.Block, ptr %120, i32 0, i32 4
  %122 = getelementptr inbounds [0 x i8], ptr %121, i64 0, i64 0
  store ptr %122, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %123

123:                                              ; preds = %117, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %124 = load ptr, ptr %2, align 8
  ret ptr %124
}

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #2

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 = { noreturn nounwind "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 }
attributes #4 = { noreturn 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"", !6, i64 0, !13, i64 8, !13, i64 9, !7, i64 16}
!13 = !{!"_Bool", !7, i64 0}
!14 = !{!12, !13, i64 8}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


; 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 global [65536 x i8], align 16
@.str = external hidden unnamed_addr constant [37 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [52 x i8], align 1
@__PRETTY_FUNCTION__.ccgc_malloc = external hidden unnamed_addr constant [26 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @ccgc_malloc(i64 noundef %0) local_unnamed_addr #0 {
  %2 = load i64, ptr @page, align 16, !tbaa !5
  %3 = icmp eq i64 %2, 0
  br i1 %3, label %4, label %5

4:                                                ; preds = %1
  store i64 65520, ptr @page, align 16, !tbaa !5
  store i8 0, ptr getelementptr inbounds (i8, ptr @page, i64 8), align 8, !tbaa !11
  br label %5

5:                                                ; preds = %4, %1
  %.pre52 = phi i64 [ 65520, %4 ], [ %2, %1 ]
  %6 = icmp eq i64 %0, 0
  br i1 %6, label %40, label %7

7:                                                ; preds = %5
  %8 = and i64 %0, 7
  %.not = icmp eq i64 %8, 0
  %9 = select i1 %.not, i64 0, i64 8
  %10 = add i64 %9, %0
  %11 = and i64 %10, -8
  br label %12

12:                                               ; preds = %.critedge2, %7
  %.pre = phi i64 [ %.pre52, %7 ], [ %.pre.pre, %.critedge2 ]
  %.035.idx = phi i64 [ 0, %7 ], [ %.us-phi, %.critedge2 ]
  %.035.ptr = getelementptr inbounds i8, ptr @page, i64 %.035.idx
  %13 = getelementptr inbounds i8, ptr %.035.ptr, i64 8
  %14 = load i8, ptr %13, align 8, !tbaa !11, !range !12, !noundef !13
  %15 = trunc nuw i8 %14 to i1
  br i1 %15, label %.critedge, label %16

16:                                               ; preds = %12
  %17 = icmp ult i64 %.pre, %11
  br i1 %17, label %.critedge.thread, label %29

.critedge:                                        ; preds = %12
  %.035.add = add nsw i64 %.035.idx, 16
  %.add = add nsw i64 %.pre, %.035.add
  %18 = icmp slt i64 %.add, 65536
  br i1 %18, label %.critedge2, label %20

.critedge.thread:                                 ; preds = %16
  %.035.add53 = add nsw i64 %.035.idx, 16
  %.add54 = add nsw i64 %.pre, %.035.add53
  %19 = icmp slt i64 %.add54, 65536
  br i1 %19, label %.preheader.split, label %20

20:                                               ; preds = %.critedge.thread, %.critedge
  tail call void @__assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 60, ptr noundef nonnull @__PRETTY_FUNCTION__.ccgc_malloc) #2
  unreachable

.preheader.split:                                 ; preds = %.critedge.thread, %25
  %21 = phi i64 [ %28, %25 ], [ %.pre, %.critedge.thread ]
  %.034.idx = phi i64 [ %.add42, %25 ], [ %.add54, %.critedge.thread ]
  %.034.ptr = getelementptr inbounds i8, ptr @page, i64 %.034.idx
  %22 = getelementptr inbounds i8, ptr %.034.ptr, i64 8
  %23 = load i8, ptr %22, align 8, !tbaa !11, !range !12, !noundef !13
  %24 = trunc nuw i8 %23 to i1
  br i1 %24, label %.critedge2, label %25

25:                                               ; preds = %.preheader.split
  %26 = load i64, ptr %.034.ptr, align 8, !tbaa !5
  %27 = add i64 %26, 16
  %28 = add i64 %27, %21
  store i64 %28, ptr %.035.ptr, align 8, !tbaa !5
  %.add42 = add nsw i64 %28, %.035.add53
  %.not45 = icmp slt i64 %.add42, 65536
  br i1 %.not45, label %.preheader.split, label %.critedge2, !llvm.loop !14

.critedge2:                                       ; preds = %.preheader.split, %25, %.critedge
  %.us-phi = phi i64 [ %.add, %.critedge ], [ %.add42, %25 ], [ %.034.idx, %.preheader.split ]
  %.035.ptr.phi.trans.insert = getelementptr inbounds i8, ptr @page, i64 %.us-phi
  %.pre.pre = load i64, ptr %.035.ptr.phi.trans.insert, align 8, !tbaa !5
  br label %12, !llvm.loop !16

29:                                               ; preds = %16
  %30 = getelementptr inbounds i8, ptr %.035.ptr, i64 8
  %31 = add i64 %11, 16
  %32 = icmp ugt i64 %.pre, %31
  br i1 %32, label %33, label %38

33:                                               ; preds = %29
  %34 = getelementptr inbounds i8, ptr %.035.ptr, i64 16
  %35 = getelementptr inbounds i8, ptr %34, i64 %11
  %reass.sub = sub i64 %.pre, %11
  %36 = add i64 %reass.sub, -16
  store i64 %36, ptr %35, align 8, !tbaa !5
  %37 = getelementptr inbounds i8, ptr %35, i64 8
  store i8 0, ptr %37, align 8, !tbaa !11
  store i64 %11, ptr %.035.ptr, align 8, !tbaa !5
  br label %38

38:                                               ; preds = %33, %29
  store i8 1, ptr %30, align 8, !tbaa !11
  %39 = getelementptr inbounds i8, ptr %.035.ptr, i64 16
  br label %40

40:                                               ; preds = %5, %38
  %.0 = phi ptr [ %39, %38 ], [ null, %5 ]
  ret ptr %.0
}

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

attributes #0 = { 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 = { noreturn nounwind "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 = { noreturn 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 = !{!6, !10, i64 8}
!12 = !{i8 0, i8 2}
!13 = !{}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass