; LLVM IR from humz2k/sugaryc :: ext/bdwgc (dyn_load)

; 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.link_map = type { i64, ptr, ptr, ptr, ptr }
%struct.Elf64_Ehdr = type { [16 x i8], i16, i16, i32, i64, i64, i64, i32, i16, i16, i16, i16, i16, i16 }
%struct.Elf64_Phdr = type { i32, i32, i64, i64, i64, i64, i64, i64 }

; Function Attrs: nounwind uwtable
define dso_local void @GC_register_dynamic_libraries() #0 {
  %1 = alloca ptr, align 8
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  %8 = call i32 @GC_register_dynamic_libraries_dl_iterate_phdr()
  %9 = icmp ne i32 %8, 0
  br i1 %9, label %10, label %11

10:                                               ; preds = %0
  store i32 1, ptr %2, align 4
  br label %73

11:                                               ; preds = %0
  %12 = call ptr @GC_FirstDLOpenedLinkMap()
  store ptr %12, ptr %1, align 8, !tbaa !5
  br label %13

13:                                               ; preds = %68, %11
  %14 = load ptr, ptr %1, align 8, !tbaa !5
  %15 = icmp ne ptr %14, null
  br i1 %15, label %16, label %72

16:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %17 = load ptr, ptr %1, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.link_map, ptr %17, i32 0, i32 0
  %19 = load i64, ptr %18, align 8, !tbaa !9
  %20 = inttoptr i64 %19 to ptr
  store ptr %20, ptr %3, align 8, !tbaa !5
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.Elf64_Ehdr, ptr %22, i32 0, i32 5
  %24 = load i64, ptr %23, align 8, !tbaa !12
  %25 = getelementptr inbounds i8, ptr %21, i64 %24
  store ptr %25, ptr %4, align 8, !tbaa !5
  %26 = load ptr, ptr %1, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.link_map, ptr %26, i32 0, i32 0
  %28 = load i64, ptr %27, align 8, !tbaa !9
  store i64 %28, ptr %5, align 8, !tbaa !16
  store i32 0, ptr %7, align 4, !tbaa !17
  br label %29

29:                                               ; preds = %62, %16
  %30 = load i32, ptr %7, align 4, !tbaa !17
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.Elf64_Ehdr, ptr %31, i32 0, i32 10
  %33 = load i16, ptr %32, align 8, !tbaa !18
  %34 = zext i16 %33 to i32
  %35 = icmp slt i32 %30, %34
  br i1 %35, label %36, label %67

36:                                               ; preds = %29
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.Elf64_Phdr, ptr %37, i32 0, i32 0
  %39 = load i32, ptr %38, align 8, !tbaa !19
  switch i32 %39, label %60 [
    i32 1, label %40
  ]

40:                                               ; preds = %36
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.Elf64_Phdr, ptr %41, i32 0, i32 1
  %43 = load i32, ptr %42, align 4, !tbaa !21
  %44 = and i32 %43, 2
  %45 = icmp ne i32 %44, 0
  br i1 %45, label %47, label %46

46:                                               ; preds = %40
  br label %61

47:                                               ; preds = %40
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.Elf64_Phdr, ptr %48, i32 0, i32 3
  %50 = load i64, ptr %49, align 8, !tbaa !22
  %51 = inttoptr i64 %50 to ptr
  %52 = load i64, ptr %5, align 8, !tbaa !16
  %53 = getelementptr inbounds i8, ptr %51, i64 %52
  store ptr %53, ptr %6, align 8, !tbaa !5
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.Elf64_Phdr, ptr %56, i32 0, i32 6
  %58 = load i64, ptr %57, align 8, !tbaa !23
  %59 = getelementptr inbounds i8, ptr %55, i64 %58
  call void @GC_add_roots_inner(ptr noundef %54, ptr noundef %59, i32 noundef 1)
  br label %61

60:                                               ; preds = %36
  br label %61

61:                                               ; preds = %60, %47, %46
  br label %62

62:                                               ; preds = %61
  %63 = load i32, ptr %7, align 4, !tbaa !17
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %7, align 4, !tbaa !17
  %65 = load ptr, ptr %4, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.Elf64_Phdr, ptr %65, i32 1
  store ptr %66, ptr %4, align 8, !tbaa !5
  br label %29, !llvm.loop !24

67:                                               ; preds = %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  br label %68

68:                                               ; preds = %67
  %69 = load ptr, ptr %1, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.link_map, ptr %69, i32 0, i32 3
  %71 = load ptr, ptr %70, align 8, !tbaa !26
  store ptr %71, ptr %1, align 8, !tbaa !5
  br label %13, !llvm.loop !27

72:                                               ; preds = %13
  store i32 0, ptr %2, align 4
  br label %73

73:                                               ; preds = %72, %10
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  %74 = load i32, ptr %2, align 4
  switch i32 %74, label %76 [
    i32 0, label %75
    i32 1, label %75
  ]

75:                                               ; preds = %73, %73
  ret void

76:                                               ; preds = %73
  unreachable
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
declare hidden i32 @GC_register_dynamic_libraries_dl_iterate_phdr() #0

; Function Attrs: nounwind uwtable
declare hidden ptr @GC_FirstDLOpenedLinkMap() #0

declare void @GC_add_roots_inner(ptr noundef, ptr noundef, i32 noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"link_map", !11, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !11, i64 32}
!13 = !{!"", !7, i64 0, !14, i64 16, !14, i64 18, !15, i64 20, !11, i64 24, !11, i64 32, !11, i64 40, !15, i64 48, !14, i64 52, !14, i64 54, !14, i64 56, !14, i64 58, !14, i64 60, !14, i64 62}
!14 = !{!"short", !7, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = !{!11, !11, i64 0}
!17 = !{!15, !15, i64 0}
!18 = !{!13, !14, i64 56}
!19 = !{!20, !15, i64 0}
!20 = !{!"", !15, i64 0, !15, i64 4, !11, i64 8, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !11, i64 48}
!21 = !{!20, !15, i64 4}
!22 = !{!20, !11, i64 16}
!23 = !{!20, !11, i64 40}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}
!26 = !{!10, !6, i64 24}
!27 = distinct !{!27, !25}


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

; Function Attrs: nounwind uwtable
define dso_local void @GC_register_dynamic_libraries() local_unnamed_addr #0 {
  %1 = tail call i32 @GC_register_dynamic_libraries_dl_iterate_phdr()
  %.not = icmp eq i32 %1, 0
  br i1 %.not, label %2, label %.loopexit

2:                                                ; preds = %0
  %3 = tail call ptr @GC_FirstDLOpenedLinkMap()
  %.not1822 = icmp eq ptr %3, null
  br i1 %.not1822, label %.loopexit, label %.lr.ph25

.lr.ph25:                                         ; preds = %2, %._crit_edge
  %.01623 = phi ptr [ %32, %._crit_edge ], [ %3, %2 ]
  %4 = load i64, ptr %.01623, align 8, !tbaa !5
  %5 = inttoptr i64 %4 to ptr
  %6 = getelementptr inbounds i8, ptr %5, i64 56
  %7 = load i16, ptr %6, align 8, !tbaa !11
  %.not26 = icmp eq i16 %7, 0
  br i1 %.not26, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.lr.ph25
  %8 = getelementptr inbounds i8, ptr %5, i64 32
  %9 = load i64, ptr %8, align 8, !tbaa !15
  %10 = getelementptr inbounds i8, ptr %5, i64 %9
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %25
  %11 = phi i16 [ %26, %25 ], [ %7, %.lr.ph.preheader ]
  %.021 = phi i32 [ %27, %25 ], [ 0, %.lr.ph.preheader ]
  %.01720 = phi ptr [ %28, %25 ], [ %10, %.lr.ph.preheader ]
  %12 = load i32, ptr %.01720, align 8, !tbaa !16
  %cond = icmp eq i32 %12, 1
  br i1 %cond, label %13, label %25

13:                                               ; preds = %.lr.ph
  %14 = getelementptr inbounds i8, ptr %.01720, i64 4
  %15 = load i32, ptr %14, align 4, !tbaa !18
  %16 = and i32 %15, 2
  %.not19 = icmp eq i32 %16, 0
  br i1 %.not19, label %25, label %17

17:                                               ; preds = %13
  %18 = getelementptr inbounds i8, ptr %.01720, i64 16
  %19 = load i64, ptr %18, align 8, !tbaa !19
  %20 = inttoptr i64 %19 to ptr
  %21 = getelementptr inbounds i8, ptr %20, i64 %4
  %22 = getelementptr inbounds i8, ptr %.01720, i64 40
  %23 = load i64, ptr %22, align 8, !tbaa !20
  %24 = getelementptr inbounds i8, ptr %21, i64 %23
  tail call void @GC_add_roots_inner(ptr noundef %21, ptr noundef %24, i32 noundef 1) #2
  %.pre = load i16, ptr %6, align 8, !tbaa !11
  br label %25

25:                                               ; preds = %17, %13, %.lr.ph
  %26 = phi i16 [ %.pre, %17 ], [ %11, %13 ], [ %11, %.lr.ph ]
  %27 = add nuw nsw i32 %.021, 1
  %28 = getelementptr inbounds i8, ptr %.01720, i64 56
  %29 = zext i16 %26 to i32
  %30 = icmp ult i32 %27, %29
  br i1 %30, label %.lr.ph, label %._crit_edge, !llvm.loop !21

._crit_edge:                                      ; preds = %25, %.lr.ph25
  %31 = getelementptr inbounds i8, ptr %.01623, i64 24
  %32 = load ptr, ptr %31, align 8, !tbaa !23
  %.not18 = icmp eq ptr %32, null
  br i1 %.not18, label %.loopexit, label %.lr.ph25, !llvm.loop !24

.loopexit:                                        ; preds = %._crit_edge, %2, %0
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden i32 @GC_register_dynamic_libraries_dl_iterate_phdr() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden ptr @GC_FirstDLOpenedLinkMap() local_unnamed_addr #0

declare void @GC_add_roots_inner(ptr noundef, ptr noundef, i32 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 = !{!6, !7, i64 0}
!6 = !{!"link_map", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!12, !13, i64 56}
!12 = !{!"", !8, i64 0, !13, i64 16, !13, i64 18, !14, i64 20, !7, i64 24, !7, i64 32, !7, i64 40, !14, i64 48, !13, i64 52, !13, i64 54, !13, i64 56, !13, i64 58, !13, i64 60, !13, i64 62}
!13 = !{!"short", !8, i64 0}
!14 = !{!"int", !8, i64 0}
!15 = !{!12, !7, i64 32}
!16 = !{!17, !14, i64 0}
!17 = !{!"", !14, i64 0, !14, i64 4, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48}
!18 = !{!17, !14, i64 4}
!19 = !{!17, !7, i64 16}
!20 = !{!17, !7, i64 40}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = !{!6, !10, i64 24}
!24 = distinct !{!24, !22}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass