; LLVM IR from Simple-XX/SimpleKernel :: src/drv/multiboot2/multiboot2.ll (ZN10MULTIBOOT210get_memoryEPKNS_11iter_data_tEPv)

; 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.MULTIBOOT2::iter_data_t" = type { i32, i32 }
%struct.resource_t = type <{ i8, [7 x i8], ptr, %struct.anon, i8, [7 x i8] }>
%struct.anon = type { i64, i64 }
%"struct.MULTIBOOT2::multiboot_tag_mmap_t" = type { %"struct.MULTIBOOT2::multiboot_tag_t", i32, i32, [0 x %"struct.MULTIBOOT2::multiboot_mmap_entry_t"] }
%"struct.MULTIBOOT2::multiboot_tag_t" = type { i32, i32 }
%"struct.MULTIBOOT2::multiboot_mmap_entry_t" = type { i64, i64, i32, i32 }

@.str.4 = external hidden unnamed_addr constant [21 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: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_ZN10MULTIBOOT210get_memoryEPKNS_11iter_data_tEPv(ptr noundef %0, ptr noundef %1) #1 align 2 {
  %3 = alloca i1, align 1
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = getelementptr inbounds %"struct.MULTIBOOT2::iter_data_t", ptr %8, i32 0, i32 0
  %10 = load i32, ptr %9, align 4, !tbaa !9
  %11 = icmp ne i32 %10, 6
  br i1 %11, label %12, label %13

12:                                               ; preds = %2
  store i1 false, ptr %3, align 1
  br label %69

13:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %14, ptr %6, align 8, !tbaa !5
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.resource_t, ptr %15, i32 0, i32 0
  %17 = load i8, ptr %16, align 8, !tbaa !12
  %18 = zext i8 %17 to i32
  %19 = or i32 %18, 1
  %20 = trunc i32 %19 to i8
  store i8 %20, ptr %16, align 8, !tbaa !12
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.resource_t, ptr %21, i32 0, i32 2
  store ptr @.str.4, ptr %22, align 8, !tbaa !16
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.resource_t, ptr %23, i32 0, i32 3
  %25 = getelementptr inbounds %struct.anon, ptr %24, i32 0, i32 0
  store i64 0, ptr %25, align 8, !tbaa !17
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.resource_t, ptr %26, i32 0, i32 3
  %28 = getelementptr inbounds %struct.anon, ptr %27, i32 0, i32 1
  store i64 0, ptr %28, align 8, !tbaa !18
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %"struct.MULTIBOOT2::multiboot_tag_mmap_t", ptr %29, i32 0, i32 3
  %31 = getelementptr inbounds [0 x %"struct.MULTIBOOT2::multiboot_mmap_entry_t"], ptr %30, i64 0, i64 0
  store ptr %31, ptr %7, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %61, %13
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %"struct.MULTIBOOT2::iter_data_t", ptr %35, i32 0, i32 1
  %37 = load i32, ptr %36, align 4, !tbaa !19
  %38 = zext i32 %37 to i64
  %39 = getelementptr inbounds i8, ptr %34, i64 %38
  %40 = icmp ult ptr %33, %39
  br i1 %40, label %41, label %68

41:                                               ; preds = %32
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = getelementptr inbounds %"struct.MULTIBOOT2::multiboot_mmap_entry_t", ptr %42, i32 0, i32 2
  %44 = load i32, ptr %43, align 8, !tbaa !20
  %45 = icmp eq i32 %44, 1
  br i1 %45, label %51, label %46

46:                                               ; preds = %41
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = getelementptr inbounds %"struct.MULTIBOOT2::multiboot_mmap_entry_t", ptr %47, i32 0, i32 0
  %49 = load i64, ptr %48, align 8, !tbaa !22
  %50 = icmp ult i64 %49, 1048576
  br i1 %50, label %51, label %60

51:                                               ; preds = %46, %41
  %52 = load ptr, ptr %7, align 8, !tbaa !5
  %53 = getelementptr inbounds %"struct.MULTIBOOT2::multiboot_mmap_entry_t", ptr %52, i32 0, i32 1
  %54 = load i64, ptr %53, align 8, !tbaa !23
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.resource_t, ptr %55, i32 0, i32 3
  %57 = getelementptr inbounds %struct.anon, ptr %56, i32 0, i32 1
  %58 = load i64, ptr %57, align 8, !tbaa !18
  %59 = add i64 %58, %54
  store i64 %59, ptr %57, align 8, !tbaa !18
  br label %60

60:                                               ; preds = %51, %46
  br label %61

61:                                               ; preds = %60
  %62 = load ptr, ptr %7, align 8, !tbaa !5
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  %64 = getelementptr inbounds %"struct.MULTIBOOT2::multiboot_tag_mmap_t", ptr %63, i32 0, i32 1
  %65 = load i32, ptr %64, align 8, !tbaa !24
  %66 = zext i32 %65 to i64
  %67 = getelementptr inbounds i8, ptr %62, i64 %66
  store ptr %67, ptr %7, align 8, !tbaa !5
  br label %32, !llvm.loop !27

68:                                               ; preds = %32
  store i1 true, ptr %3, align 1
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %69

69:                                               ; preds = %68, %12
  %70 = load i1, ptr %3, align 1
  ret i1 %70
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"_ZTSN10MULTIBOOT211iter_data_tE", !11, i64 0, !11, i64 4}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !7, i64 0}
!13 = !{!"_ZTS10resource_t", !7, i64 0, !6, i64 8, !14, i64 16, !7, i64 32}
!14 = !{!"_ZTSN10resource_tUt0_E", !15, i64 0, !15, i64 8}
!15 = !{!"long", !7, i64 0}
!16 = !{!13, !6, i64 8}
!17 = !{!13, !15, i64 16}
!18 = !{!13, !15, i64 24}
!19 = !{!10, !11, i64 4}
!20 = !{!21, !11, i64 16}
!21 = !{!"_ZTSN10MULTIBOOT222multiboot_mmap_entry_tE", !15, i64 0, !15, i64 8, !11, i64 16, !11, i64 20}
!22 = !{!21, !15, i64 0}
!23 = !{!21, !15, i64 8}
!24 = !{!25, !11, i64 8}
!25 = !{!"_ZTSN10MULTIBOOT220multiboot_tag_mmap_tE", !26, i64 0, !11, i64 8, !11, i64 12, !7, i64 16}
!26 = !{!"_ZTSN10MULTIBOOT215multiboot_tag_tE", !11, i64 0, !11, i64 4}
!27 = distinct !{!27, !28}
!28 = !{!"llvm.loop.mustprogress"}


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

@.str.4 = external hidden unnamed_addr constant [21 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i1 @_ZN10MULTIBOOT210get_memoryEPKNS_11iter_data_tEPv(ptr noundef readonly %0, ptr nocapture noundef %1) local_unnamed_addr #0 align 2 {
  %3 = load i32, ptr %0, align 4, !tbaa !5
  %.not = icmp eq i32 %3, 6
  br i1 %.not, label %4, label %.loopexit

4:                                                ; preds = %2
  %5 = load i8, ptr %1, align 8, !tbaa !10
  %6 = or i8 %5, 1
  store i8 %6, ptr %1, align 8, !tbaa !10
  %7 = getelementptr inbounds i8, ptr %1, i64 8
  store ptr @.str.4, ptr %7, align 8, !tbaa !15
  %8 = getelementptr inbounds i8, ptr %1, i64 16
  %9 = getelementptr inbounds i8, ptr %1, i64 24
  %10 = getelementptr inbounds i8, ptr %0, i64 4
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %8, i8 0, i64 16, i1 false)
  %11 = load i32, ptr %10, align 4, !tbaa !16
  %12 = zext i32 %11 to i64
  %13 = getelementptr inbounds i8, ptr %0, i64 %12
  %14 = icmp ugt i32 %11, 16
  br i1 %14, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %4
  %15 = getelementptr inbounds i8, ptr %0, i64 16
  %16 = getelementptr inbounds i8, ptr %0, i64 8
  %17 = load i32, ptr %16, align 8, !tbaa !17
  %18 = zext i32 %17 to i64
  br label %19

19:                                               ; preds = %.lr.ph, %31
  %20 = phi i64 [ 0, %.lr.ph ], [ %32, %31 ]
  %.018 = phi ptr [ %15, %.lr.ph ], [ %33, %31 ]
  %21 = getelementptr inbounds i8, ptr %.018, i64 16
  %22 = load i32, ptr %21, align 8, !tbaa !20
  %23 = icmp eq i32 %22, 1
  br i1 %23, label %27, label %24

24:                                               ; preds = %19
  %25 = load i64, ptr %.018, align 8, !tbaa !22
  %26 = icmp ult i64 %25, 1048576
  br i1 %26, label %27, label %31

27:                                               ; preds = %24, %19
  %28 = getelementptr inbounds i8, ptr %.018, i64 8
  %29 = load i64, ptr %28, align 8, !tbaa !23
  %30 = add i64 %20, %29
  store i64 %30, ptr %9, align 8, !tbaa !24
  br label %31

31:                                               ; preds = %24, %27
  %32 = phi i64 [ %20, %24 ], [ %30, %27 ]
  %33 = getelementptr inbounds i8, ptr %.018, i64 %18
  %34 = icmp ult ptr %33, %13
  br i1 %34, label %19, label %.loopexit, !llvm.loop !25

.loopexit:                                        ; preds = %31, %4, %2
  ret i1 %.not
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(read, argmem: 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" }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!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 = !{!"_ZTSN10MULTIBOOT211iter_data_tE", !7, i64 0, !7, i64 4}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!11, !8, i64 0}
!11 = !{!"_ZTS10resource_t", !8, i64 0, !12, i64 8, !13, i64 16, !8, i64 32}
!12 = !{!"any pointer", !8, i64 0}
!13 = !{!"_ZTSN10resource_tUt0_E", !14, i64 0, !14, i64 8}
!14 = !{!"long", !8, i64 0}
!15 = !{!11, !12, i64 8}
!16 = !{!6, !7, i64 4}
!17 = !{!18, !7, i64 8}
!18 = !{!"_ZTSN10MULTIBOOT220multiboot_tag_mmap_tE", !19, i64 0, !7, i64 8, !7, i64 12, !8, i64 16}
!19 = !{!"_ZTSN10MULTIBOOT215multiboot_tag_tE", !7, i64 0, !7, i64 4}
!20 = !{!21, !7, i64 16}
!21 = !{!"_ZTSN10MULTIBOOT222multiboot_mmap_entry_tE", !14, i64 0, !14, i64 8, !7, i64 16, !7, i64 20}
!22 = !{!21, !14, i64 0}
!23 = !{!21, !14, i64 8}
!24 = !{!11, !14, i64 24}
!25 = distinct !{!25, !26}
!26 = !{!"llvm.loop.mustprogress"}


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