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

%struct._GC_arrays = type { i64, i64, i64, ptr, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32, i32, i32, i64, i64, i64, ptr, ptr, i64, i32, i64, i64, i64, %struct.dl_hashtbl_s, %struct.dl_hashtbl_s, %struct.fnlz_roots_s, i32, ptr, i64, i64, i32, i64, i64, i64, ptr, [64 x ptr], [8 x i8], [64 x ptr], [129 x ptr], [129 x ptr], [129 x ptr], [2049 x i64], [129 x ptr], [4096 x i8], [4096 x i64], [4096 x i64], ptr, [2048 x %struct.roots], [512 x %struct.exclusion], [2048 x ptr] }
%struct.dl_hashtbl_s = type { ptr, i64, i32 }
%struct.fnlz_roots_s = type { ptr, ptr }
%struct.roots = type { ptr, ptr, ptr, i32 }
%struct.exclusion = type { ptr, ptr }
%struct.obj_kind = type { ptr, ptr, i64, i32, i32 }

@GC_arrays = external global %struct._GC_arrays, align 8
@GC_all_interior_pointers = external global i32, align 4
@GC_obj_kinds = external global [16 x %struct.obj_kind], 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(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
declare dso_local ptr @GC_generic_malloc_aligned(i64 noundef, i32 noundef, i32 noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @GC_malloc_kind_aligned_global(i64 noundef %0, i32 noundef %1, i64 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  store i64 %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !5
  %12 = load i64, ptr %5, align 8, !tbaa !5
  %13 = icmp ule i64 %12, 2047
  %14 = zext i1 %13 to i32
  %15 = sext i32 %14 to i64
  %16 = call i64 @llvm.expect.i64(i64 %15, i64 1)
  %17 = icmp ne i64 %16, 0
  br i1 %17, label %24, label %18

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

24:                                               ; preds = %18, %3
  %25 = load i64, ptr %7, align 8, !tbaa !5
  %26 = icmp ult i64 %25, 2048
  %27 = zext i1 %26 to i32
  %28 = sext i32 %27 to i64
  %29 = call i64 @llvm.expect.i64(i64 %28, i64 1)
  %30 = icmp ne i64 %29, 0
  br i1 %30, label %31, label %88

31:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %32 = load i64, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds [2049 x i64], ptr getelementptr inbounds (%struct._GC_arrays, ptr @GC_arrays, i32 0, i32 53), i64 0, i64 %32
  %34 = load i64, ptr %33, align 8, !tbaa !5
  store i64 %34, ptr %10, align 8, !tbaa !5
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [16 x %struct.obj_kind], ptr @GC_obj_kinds, i64 0, i64 %36
  %38 = getelementptr inbounds %struct.obj_kind, ptr %37, i32 0, i32 0
  %39 = load ptr, ptr %38, align 16, !tbaa !11
  %40 = load i64, ptr %10, align 8, !tbaa !5
  %41 = getelementptr inbounds ptr, ptr %39, i64 %40
  store ptr %41, ptr %9, align 8, !tbaa !14
  %42 = load ptr, ptr %9, align 8, !tbaa !14
  %43 = load ptr, ptr %42, align 8, !tbaa !14
  store ptr %43, ptr %8, align 8, !tbaa !14
  %44 = load i64, ptr %7, align 8, !tbaa !5
  %45 = icmp uge i64 %44, 16
  %46 = zext i1 %45 to i32
  %47 = sext i32 %46 to i64
  %48 = call i64 @llvm.expect.i64(i64 %47, i64 0)
  %49 = icmp ne i64 %48, 0
  br i1 %49, label %50, label %63

50:                                               ; preds = %31
  br label %51

51:                                               ; preds = %59, %50
  %52 = load ptr, ptr %8, align 8, !tbaa !14
  %53 = ptrtoint ptr %52 to i64
  %54 = load i64, ptr %7, align 8, !tbaa !5
  %55 = and i64 %53, %54
  %56 = icmp ne i64 %55, 0
  br i1 %56, label %57, label %62

57:                                               ; preds = %51
  %58 = load ptr, ptr %8, align 8, !tbaa !14
  store ptr %58, ptr %9, align 8, !tbaa !14
  br label %59

59:                                               ; preds = %57
  %60 = load ptr, ptr %9, align 8, !tbaa !14
  %61 = load ptr, ptr %60, align 8, !tbaa !14
  store ptr %61, ptr %8, align 8, !tbaa !14
  br label %51, !llvm.loop !15

62:                                               ; preds = %51
  br label %63

63:                                               ; preds = %62, %31
  %64 = load ptr, ptr %8, align 8, !tbaa !14
  %65 = icmp ne ptr %64, null
  %66 = zext i1 %65 to i32
  %67 = sext i32 %66 to i64
  %68 = call i64 @llvm.expect.i64(i64 %67, i64 1)
  %69 = icmp ne i64 %68, 0
  br i1 %69, label %70, label %84

70:                                               ; preds = %63
  %71 = load ptr, ptr %8, align 8, !tbaa !14
  %72 = load ptr, ptr %71, align 8, !tbaa !14
  %73 = load ptr, ptr %9, align 8, !tbaa !14
  store ptr %72, ptr %73, align 8, !tbaa !14
  %74 = load i32, ptr %6, align 4, !tbaa !9
  %75 = icmp ne i32 %74, 0
  br i1 %75, label %76, label %78

76:                                               ; preds = %70
  %77 = load ptr, ptr %8, align 8, !tbaa !14
  store ptr null, ptr %77, align 8, !tbaa !14
  br label %78

78:                                               ; preds = %76, %70
  %79 = load i64, ptr %10, align 8, !tbaa !5
  %80 = shl i64 %79, 4
  %81 = load i64, ptr getelementptr inbounds (%struct._GC_arrays, ptr @GC_arrays, i32 0, i32 9), align 8, !tbaa !17
  %82 = add i64 %81, %80
  store i64 %82, ptr getelementptr inbounds (%struct._GC_arrays, ptr @GC_arrays, i32 0, i32 9), align 8, !tbaa !17
  %83 = load ptr, ptr %8, align 8, !tbaa !14
  store ptr %83, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %85

84:                                               ; preds = %63
  store i32 0, ptr %11, align 4
  br label %85

85:                                               ; preds = %84, %78
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %86 = load i32, ptr %11, align 4
  switch i32 %86, label %96 [
    i32 0, label %87
    i32 1, label %94
  ]

87:                                               ; preds = %85
  br label %88

88:                                               ; preds = %87, %24, %18
  %89 = load i64, ptr %5, align 8, !tbaa !5
  %90 = load i32, ptr %6, align 4, !tbaa !9
  %91 = load i64, ptr %7, align 8, !tbaa !5
  %92 = call ptr @GC_generic_malloc_aligned(i64 noundef %89, i32 noundef %90, i32 noundef 0, i64 noundef %91)
  %93 = call ptr @GC_clear_stack(ptr noundef %92)
  store ptr %93, ptr %4, align 8
  br label %94

94:                                               ; preds = %88, %85
  %95 = load ptr, ptr %4, align 8
  ret ptr %95

96:                                               ; preds = %85
  unreachable
}

declare ptr @GC_clear_stack(ptr noundef) #3

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 = { "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 #4 = { 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}
!11 = !{!12, !13, i64 0}
!12 = !{!"obj_kind", !13, i64 0, !13, i64 8, !6, i64 16, !10, i64 24, !10, i64 28}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!18, !6, i64 72}
!18 = !{!"_GC_arrays", !6, i64 0, !6, i64 8, !6, i64 16, !13, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !13, i64 112, !13, i64 120, !13, i64 128, !13, i64 136, !13, i64 144, !13, i64 152, !13, i64 160, !13, i64 168, !10, i64 176, !10, i64 180, !10, i64 184, !6, i64 192, !6, i64 200, !6, i64 208, !13, i64 216, !13, i64 224, !6, i64 232, !10, i64 240, !6, i64 248, !6, i64 256, !6, i64 264, !19, i64 272, !19, i64 296, !20, i64 320, !10, i64 336, !13, i64 344, !6, i64 352, !6, i64 360, !10, i64 368, !6, i64 376, !6, i64 384, !6, i64 392, !13, i64 400, !7, i64 408, !7, i64 920, !7, i64 928, !7, i64 1440, !7, i64 2472, !7, i64 3504, !7, i64 4536, !7, i64 20928, !7, i64 21960, !7, i64 26056, !7, i64 58824, !13, i64 91592, !7, i64 91600, !7, i64 157136, !7, i64 165328}
!19 = !{!"dl_hashtbl_s", !13, i64 0, !6, i64 8, !10, i64 16}
!20 = !{!"fnlz_roots_s", !13, i64 0, !13, i64 8}


; 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_arrays = type { i64, i64, i64, ptr, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32, i32, i32, i64, i64, i64, ptr, ptr, i64, i32, i64, i64, i64, %struct.dl_hashtbl_s, %struct.dl_hashtbl_s, %struct.fnlz_roots_s, i32, ptr, i64, i64, i32, i64, i64, i64, ptr, [64 x ptr], [8 x i8], [64 x ptr], [129 x ptr], [129 x ptr], [129 x ptr], [2049 x i64], [129 x ptr], [4096 x i8], [4096 x i64], [4096 x i64], ptr, [2048 x %struct.roots], [512 x %struct.exclusion], [2048 x ptr] }
%struct.dl_hashtbl_s = type { ptr, i64, i32 }
%struct.fnlz_roots_s = type { ptr, ptr }
%struct.roots = type { ptr, ptr, ptr, i32 }
%struct.exclusion = type { ptr, ptr }
%struct.obj_kind = type { ptr, ptr, i64, i32, i32 }

@GC_arrays = external local_unnamed_addr global %struct._GC_arrays, align 8
@GC_all_interior_pointers = external local_unnamed_addr global i32, align 4
@GC_obj_kinds = external local_unnamed_addr global [16 x %struct.obj_kind], align 16

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

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

5:                                                ; preds = %3
  %6 = load i32, ptr @GC_all_interior_pointers, align 4, !tbaa !6
  %7 = sext i32 %6 to i64
  %8 = sub nsw i64 2048, %7
  %.not = icmp uge i64 %8, %0
  %9 = icmp ult i64 %2, 2048
  %or.cond = and i1 %9, %.not
  br i1 %or.cond, label %11, label %.thread, !prof !10

10:                                               ; preds = %3
  %.old = icmp ult i64 %2, 2048
  br i1 %.old, label %11, label %.thread, !prof !5

11:                                               ; preds = %5, %10
  %12 = getelementptr inbounds [2049 x i64], ptr getelementptr inbounds (i8, ptr @GC_arrays, i64 4536), i64 0, i64 %0
  %13 = load i64, ptr %12, align 8, !tbaa !11
  %14 = sext i32 %1 to i64
  %15 = getelementptr inbounds [16 x %struct.obj_kind], ptr @GC_obj_kinds, i64 0, i64 %14
  %16 = load ptr, ptr %15, align 16, !tbaa !13
  %17 = getelementptr inbounds ptr, ptr %16, i64 %13
  %18 = load ptr, ptr %17, align 8, !tbaa !16
  %19 = icmp ugt i64 %2, 15
  br i1 %19, label %.preheader, label %.loopexit, !prof !17

.preheader:                                       ; preds = %11
  %20 = ptrtoint ptr %18 to i64
  %21 = and i64 %20, %2
  %.not3034 = icmp eq i64 %21, 0
  br i1 %.not3034, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.02635 = phi ptr [ %22, %.lr.ph ], [ %18, %.preheader ]
  %22 = load ptr, ptr %.02635, align 8, !tbaa !16
  %23 = ptrtoint ptr %22 to i64
  %24 = and i64 %23, %2
  %.not30 = icmp eq i64 %24, 0
  br i1 %.not30, label %.loopexit, label %.lr.ph, !llvm.loop !18

.loopexit:                                        ; preds = %.lr.ph, %.preheader, %11
  %.127 = phi ptr [ %18, %11 ], [ %18, %.preheader ], [ %22, %.lr.ph ]
  %.125 = phi ptr [ %17, %11 ], [ %17, %.preheader ], [ %.02635, %.lr.ph ]
  %.not31.not = icmp eq ptr %.127, null
  br i1 %.not31.not, label %.thread, label %25, !prof !17

25:                                               ; preds = %.loopexit
  %26 = load ptr, ptr %.127, align 8, !tbaa !16
  store ptr %26, ptr %.125, align 8, !tbaa !16
  %.not32 = icmp eq i32 %1, 0
  br i1 %.not32, label %28, label %27

27:                                               ; preds = %25
  store ptr null, ptr %.127, align 8, !tbaa !16
  br label %28

28:                                               ; preds = %25, %27
  %29 = shl i64 %13, 4
  %30 = load i64, ptr getelementptr inbounds (i8, ptr @GC_arrays, i64 72), align 8, !tbaa !20
  %31 = add i64 %30, %29
  store i64 %31, ptr getelementptr inbounds (i8, ptr @GC_arrays, i64 72), align 8, !tbaa !20
  br label %34

.thread:                                          ; preds = %.loopexit, %10, %5
  %32 = tail call ptr @GC_generic_malloc_aligned(i64 noundef %0, i32 noundef %1, i32 noundef 0, i64 noundef %2)
  %33 = tail call ptr @GC_clear_stack(ptr noundef %32) #2
  br label %34

34:                                               ; preds = %28, %.thread
  %.1 = phi ptr [ %.127, %28 ], [ %33, %.thread ]
  ret ptr %.1
}

declare ptr @GC_clear_stack(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 = { "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 = !{!"branch_weights", !"expected", i32 2000, i32 1}
!6 = !{!7, !7, i64 0}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"branch_weights", i32 2000, i32 2002}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !8, i64 0}
!13 = !{!14, !15, i64 0}
!14 = !{!"obj_kind", !15, i64 0, !15, i64 8, !12, i64 16, !7, i64 24, !7, i64 28}
!15 = !{!"any pointer", !8, i64 0}
!16 = !{!15, !15, i64 0}
!17 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!21, !12, i64 72}
!21 = !{!"_GC_arrays", !12, i64 0, !12, i64 8, !12, i64 16, !15, i64 24, !12, i64 32, !12, i64 40, !12, i64 48, !12, i64 56, !12, i64 64, !12, i64 72, !12, i64 80, !12, i64 88, !12, i64 96, !12, i64 104, !15, i64 112, !15, i64 120, !15, i64 128, !15, i64 136, !15, i64 144, !15, i64 152, !15, i64 160, !15, i64 168, !7, i64 176, !7, i64 180, !7, i64 184, !12, i64 192, !12, i64 200, !12, i64 208, !15, i64 216, !15, i64 224, !12, i64 232, !7, i64 240, !12, i64 248, !12, i64 256, !12, i64 264, !22, i64 272, !22, i64 296, !23, i64 320, !7, i64 336, !15, i64 344, !12, i64 352, !12, i64 360, !7, i64 368, !12, i64 376, !12, i64 384, !12, i64 392, !15, i64 400, !8, i64 408, !8, i64 920, !8, i64 928, !8, i64 1440, !8, i64 2472, !8, i64 3504, !8, i64 4536, !8, i64 20928, !8, i64 21960, !8, i64 26056, !8, i64 58824, !15, i64 91592, !8, i64 91600, !8, i64 157136, !8, i64 165328}
!22 = !{!"dl_hashtbl_s", !15, i64 0, !12, i64 8, !7, i64 16}
!23 = !{!"fnlz_roots_s", !15, i64 0, !15, i64 8}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LowerExpectIntrinsicPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass