; LLVM IR from humz2k/sugaryc :: ext/bdwgc (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"

@GC_all_interior_pointers = external global i32, align 4

; 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(none)
declare i64 @llvm.expect.i64(i64, i64) #1

; 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 @GC_generic_malloc_inner(i64 noundef %0, i32 noundef %1, i32 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  store i64 %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %10 = load i64, ptr %5, align 8, !tbaa !5
  %11 = icmp ule i64 %10, 2047
  %12 = zext i1 %11 to i32
  %13 = sext i32 %12 to i64
  %14 = call i64 @llvm.expect.i64(i64 %13, i64 1)
  %15 = icmp ne i64 %14, 0
  br i1 %15, label %22, label %16

16:                                               ; preds = %3
  %17 = load i64, ptr %5, align 8, !tbaa !5
  %18 = load i32, ptr @GC_all_interior_pointers, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = sub i64 2048, %19
  %21 = icmp ule i64 %17, %20
  br i1 %21, label %22, label %26

22:                                               ; preds = %16, %3
  %23 = load i64, ptr %5, align 8, !tbaa !5
  %24 = load i32, ptr %6, align 4, !tbaa !9
  %25 = call ptr @GC_generic_malloc_inner_small(i64 noundef %23, i32 noundef %24)
  store ptr %25, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %58

26:                                               ; preds = %16
  %27 = load i32, ptr %7, align 4, !tbaa !9
  %28 = and i32 %27, 1
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %35

30:                                               ; preds = %26
  %31 = load i64, ptr %5, align 8, !tbaa !5
  %32 = icmp uge i64 %31, 4096
  br i1 %32, label %33, label %35

33:                                               ; preds = %30
  %34 = load i64, ptr %5, align 8, !tbaa !5
  store i64 %34, ptr %8, align 8, !tbaa !5
  br label %53

35:                                               ; preds = %30, %26
  %36 = load i64, ptr %5, align 8, !tbaa !5
  %37 = load i32, ptr @GC_all_interior_pointers, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = sub i64 -1, %38
  %40 = icmp ult i64 %36, %39
  %41 = zext i1 %40 to i32
  %42 = sext i32 %41 to i64
  %43 = call i64 @llvm.expect.i64(i64 %42, i64 1)
  %44 = icmp ne i64 %43, 0
  br i1 %44, label %45, label %50

45:                                               ; preds = %35
  %46 = load i64, ptr %5, align 8, !tbaa !5
  %47 = load i32, ptr @GC_all_interior_pointers, align 4, !tbaa !9
  %48 = sext i32 %47 to i64
  %49 = add i64 %46, %48
  br label %51

50:                                               ; preds = %35
  br label %51

51:                                               ; preds = %50, %45
  %52 = phi i64 [ %49, %45 ], [ -1, %50 ]
  store i64 %52, ptr %8, align 8, !tbaa !5
  br label %53

53:                                               ; preds = %51, %33
  %54 = load i64, ptr %8, align 8, !tbaa !5
  %55 = load i32, ptr %6, align 4, !tbaa !9
  %56 = load i32, ptr %7, align 4, !tbaa !9
  %57 = call ptr @GC_alloc_large_and_clear(i64 noundef %54, i32 noundef %55, i32 noundef %56)
  store ptr %57, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %58

58:                                               ; preds = %53, %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %59 = load ptr, ptr %4, align 8
  ret ptr %59
}

; Function Attrs: nounwind uwtable
declare hidden ptr @GC_generic_malloc_inner_small(i64 noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
declare hidden ptr @GC_alloc_large_and_clear(i64 noundef, i32 noundef, i32 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #2 = { 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 #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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !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"

@GC_all_interior_pointers = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local ptr @GC_generic_malloc_inner(i64 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp ult i64 %0, 2048
  br i1 %4, label %9, label %5, !prof !5

5:                                                ; preds = %3
  %6 = load i32, ptr @GC_all_interior_pointers, align 4
  %7 = sext i32 %6 to i64
  %8 = sub nsw i64 2048, %7
  %.not = icmp ult i64 %8, %0
  br i1 %.not, label %11, label %9

9:                                                ; preds = %5, %3
  %10 = tail call ptr @GC_generic_malloc_inner_small(i64 noundef %0, i32 noundef %1)
  br label %17

11:                                               ; preds = %5
  %12 = and i32 %2, 1
  %13 = icmp ne i32 %12, 0
  %14 = icmp ugt i64 %0, 4095
  %or.cond = and i1 %14, %13
  %15 = tail call i64 @llvm.uadd.sat.i64(i64 %7, i64 %0)
  %.0 = select i1 %or.cond, i64 %0, i64 %15
  %16 = tail call ptr @GC_alloc_large_and_clear(i64 noundef %.0, i32 noundef %1, i32 noundef %2)
  br label %17

17:                                               ; preds = %11, %9
  %.013 = phi ptr [ %10, %9 ], [ %16, %11 ]
  ret ptr %.013
}

; Function Attrs: nounwind uwtable
declare hidden ptr @GC_generic_malloc_inner_small(i64 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden ptr @GC_alloc_large_and_clear(i64 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.uadd.sat.i64(i64, i64) #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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = !{!"branch_weights", !"expected", i32 2000, i32 1}


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