; LLVM IR from ponylang/ponyc :: src/libponyrt/mem (pagemap)

; 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.pagemap_level_t = type { i32, i32, i64, i64 }

@root = external hidden global ptr, align 8
@level = external hidden constant [3 x %struct.pagemap_level_t], align 16
@.str = external hidden unnamed_addr constant [31 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [76 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [33 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [31 x i8], align 1
@__func__.ponyint_pagemap_get = external hidden unnamed_addr constant [20 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [48 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [50 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

declare void @ponyint_assert_fail(ptr noundef, ptr noundef, i64 noundef, ptr noundef) #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 @ponyint_pagemap_get(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  %16 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store ptr @root, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = load atomic i64, ptr %17 acquire, align 8
  store i64 %18, ptr %8, align 8
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %19, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  store i64 0, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %20

20:                                               ; preds = %98, %2
  %21 = load i64, ptr %10, align 8, !tbaa !9
  %22 = icmp ult i64 %21, 3
  br i1 %22, label %24, label %23

23:                                               ; preds = %20
  store i32 2, ptr %11, align 4
  br label %101

24:                                               ; preds = %20
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = icmp eq ptr %25, null
  br i1 %26, label %27, label %28

27:                                               ; preds = %24
  store ptr null, ptr %3, align 8
  store i32 1, ptr %11, align 4
  br label %101

28:                                               ; preds = %24
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = ptrtoint ptr %29 to i64
  %31 = load i64, ptr %10, align 8, !tbaa !9
  %32 = getelementptr inbounds [3 x %struct.pagemap_level_t], ptr @level, i64 0, i64 %31
  %33 = getelementptr inbounds %struct.pagemap_level_t, ptr %32, i32 0, i32 0
  %34 = load i32, ptr %33, align 8, !tbaa !11
  %35 = zext i32 %34 to i64
  %36 = lshr i64 %30, %35
  %37 = load i64, ptr %10, align 8, !tbaa !9
  %38 = getelementptr inbounds [3 x %struct.pagemap_level_t], ptr @level, i64 0, i64 %37
  %39 = getelementptr inbounds %struct.pagemap_level_t, ptr %38, i32 0, i32 1
  %40 = load i32, ptr %39, align 4, !tbaa !14
  %41 = sext i32 %40 to i64
  %42 = and i64 %36, %41
  store i64 %42, ptr %9, align 8, !tbaa !9
  %43 = load i64, ptr %10, align 8, !tbaa !9
  %44 = icmp eq i64 %43, 2
  br i1 %44, label %45, label %71

45:                                               ; preds = %28
  %46 = load i64, ptr %9, align 8, !tbaa !9
  %47 = mul i64 %46, 2
  store i64 %47, ptr %9, align 8, !tbaa !9
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = load i64, ptr %9, align 8, !tbaa !9
  %50 = getelementptr inbounds ptr, ptr %48, i64 %49
  store ptr %50, ptr %6, align 8, !tbaa !5
  %51 = load i64, ptr %9, align 8, !tbaa !9
  %52 = load i64, ptr %10, align 8, !tbaa !9
  %53 = getelementptr inbounds [3 x %struct.pagemap_level_t], ptr @level, i64 0, i64 %52
  %54 = getelementptr inbounds %struct.pagemap_level_t, ptr %53, i32 0, i32 2
  %55 = load i64, ptr %54, align 8, !tbaa !15
  %56 = icmp ule i64 %51, %55
  br i1 %56, label %57, label %58

57:                                               ; preds = %45
  br label %59

58:                                               ; preds = %45
  call void @ponyint_assert_fail(ptr noundef @.str, ptr noundef @.str.1, i64 noundef 156, ptr noundef @__func__.ponyint_pagemap_get)
  br label %59

59:                                               ; preds = %58, %57
  %60 = load i64, ptr %9, align 8, !tbaa !9
  %61 = udiv i64 %60, 2
  %62 = load i64, ptr %10, align 8, !tbaa !9
  %63 = getelementptr inbounds [3 x %struct.pagemap_level_t], ptr @level, i64 0, i64 %62
  %64 = getelementptr inbounds %struct.pagemap_level_t, ptr %63, i32 0, i32 1
  %65 = load i32, ptr %64, align 4, !tbaa !14
  %66 = sext i32 %65 to i64
  %67 = icmp ule i64 %61, %66
  br i1 %67, label %68, label %69

68:                                               ; preds = %59
  br label %70

69:                                               ; preds = %59
  call void @ponyint_assert_fail(ptr noundef @.str.2, ptr noundef @.str.1, i64 noundef 157, ptr noundef @__func__.ponyint_pagemap_get)
  br label %70

70:                                               ; preds = %69, %68
  br label %97

71:                                               ; preds = %28
  %72 = load ptr, ptr %7, align 8, !tbaa !5
  %73 = load i64, ptr %9, align 8, !tbaa !9
  %74 = getelementptr inbounds ptr, ptr %72, i64 %73
  store ptr %74, ptr %6, align 8, !tbaa !5
  %75 = load ptr, ptr %6, align 8, !tbaa !5
  %76 = load atomic i64, ptr %75 acquire, align 8
  store i64 %76, ptr %12, align 8
  %77 = load ptr, ptr %12, align 8, !tbaa !5
  store ptr %77, ptr %7, align 8, !tbaa !5
  %78 = load i64, ptr %9, align 8, !tbaa !9
  %79 = load i64, ptr %10, align 8, !tbaa !9
  %80 = getelementptr inbounds [3 x %struct.pagemap_level_t], ptr @level, i64 0, i64 %79
  %81 = getelementptr inbounds %struct.pagemap_level_t, ptr %80, i32 0, i32 2
  %82 = load i64, ptr %81, align 8, !tbaa !15
  %83 = icmp ule i64 %78, %82
  br i1 %83, label %84, label %85

84:                                               ; preds = %71
  br label %86

85:                                               ; preds = %71
  call void @ponyint_assert_fail(ptr noundef @.str, ptr noundef @.str.1, i64 noundef 163, ptr noundef @__func__.ponyint_pagemap_get)
  br label %86

86:                                               ; preds = %85, %84
  %87 = load i64, ptr %9, align 8, !tbaa !9
  %88 = load i64, ptr %10, align 8, !tbaa !9
  %89 = getelementptr inbounds [3 x %struct.pagemap_level_t], ptr @level, i64 0, i64 %88
  %90 = getelementptr inbounds %struct.pagemap_level_t, ptr %89, i32 0, i32 1
  %91 = load i32, ptr %90, align 4, !tbaa !14
  %92 = sext i32 %91 to i64
  %93 = icmp ule i64 %87, %92
  br i1 %93, label %94, label %95

94:                                               ; preds = %86
  br label %96

95:                                               ; preds = %86
  call void @ponyint_assert_fail(ptr noundef @.str.3, ptr noundef @.str.1, i64 noundef 164, ptr noundef @__func__.ponyint_pagemap_get)
  br label %96

96:                                               ; preds = %95, %94
  br label %97

97:                                               ; preds = %96, %70
  br label %98

98:                                               ; preds = %97
  %99 = load i64, ptr %10, align 8, !tbaa !9
  %100 = add i64 %99, 1
  store i64 %100, ptr %10, align 8, !tbaa !9
  br label %20, !llvm.loop !16

101:                                              ; preds = %27, %23
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %102 = load i32, ptr %11, align 4
  switch i32 %102, label %129 [
    i32 2, label %103
  ]

103:                                              ; preds = %101
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %104 = load ptr, ptr %6, align 8, !tbaa !5
  %105 = load atomic i64, ptr %104 acquire, align 8
  store i64 %105, ptr %14, align 8
  %106 = load ptr, ptr %14, align 8, !tbaa !5
  store ptr %106, ptr %13, align 8, !tbaa !5
  %107 = load i64, ptr %9, align 8, !tbaa !9
  %108 = add i64 %107, 1
  store i64 %108, ptr %9, align 8, !tbaa !9
  %109 = load i64, ptr %9, align 8, !tbaa !9
  %110 = icmp ule i64 %109, 65536
  br i1 %110, label %111, label %112

111:                                              ; preds = %103
  br label %113

112:                                              ; preds = %103
  call void @ponyint_assert_fail(ptr noundef @.str.4, ptr noundef @.str.1, i64 noundef 170, ptr noundef @__func__.ponyint_pagemap_get)
  br label %113

113:                                              ; preds = %112, %111
  %114 = load i64, ptr %9, align 8, !tbaa !9
  %115 = udiv i64 %114, 2
  %116 = icmp ule i64 %115, 4095
  br i1 %116, label %117, label %118

117:                                              ; preds = %113
  br label %119

118:                                              ; preds = %113
  call void @ponyint_assert_fail(ptr noundef @.str.5, ptr noundef @.str.1, i64 noundef 171, ptr noundef @__func__.ponyint_pagemap_get)
  br label %119

119:                                              ; preds = %118, %117
  %120 = load ptr, ptr %7, align 8, !tbaa !5
  %121 = load i64, ptr %9, align 8, !tbaa !9
  %122 = getelementptr inbounds ptr, ptr %120, i64 %121
  store ptr %122, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #3
  %123 = load ptr, ptr %6, align 8, !tbaa !5
  %124 = load atomic i64, ptr %123 acquire, align 8
  store i64 %124, ptr %16, align 8
  %125 = load ptr, ptr %16, align 8, !tbaa !5
  store ptr %125, ptr %15, align 8, !tbaa !5
  %126 = load ptr, ptr %15, align 8, !tbaa !5
  %127 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %126, ptr %127, align 8, !tbaa !5
  %128 = load ptr, ptr %13, align 8, !tbaa !5
  store ptr %128, ptr %3, align 8
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  br label %129

129:                                              ; preds = %119, %101
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %130 = load ptr, ptr %3, align 8
  ret ptr %130
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"pagemap_level_t", !13, i64 0, !13, i64 4, !10, i64 8, !10, i64 16}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !13, i64 4}
!15 = !{!12, !10, i64 8}
!16 = distinct !{!16, !17}
!17 = !{!"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"

%struct.pagemap_level_t = type { i32, i32, i64, i64 }

@root = external hidden local_unnamed_addr global ptr, align 8
@level = external hidden local_unnamed_addr constant [3 x %struct.pagemap_level_t], align 16
@.str = external hidden unnamed_addr constant [31 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [76 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [33 x i8], align 1
@__func__.ponyint_pagemap_get = external hidden unnamed_addr constant [20 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [48 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [50 x i8], align 1

declare void @ponyint_assert_fail(ptr noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @ponyint_pagemap_get(ptr noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #1 {
  %3 = load atomic i64, ptr @root acquire, align 8
  %4 = ptrtoint ptr %0 to i64
  %5 = icmp eq i64 %3, 0
  br i1 %5, label %.loopexit, label %6

6:                                                ; preds = %2
  %7 = inttoptr i64 %3 to ptr
  %8 = load i32, ptr @level, align 16, !tbaa !5
  %9 = zext nneg i32 %8 to i64
  %10 = lshr i64 %4, %9
  %11 = load i32, ptr getelementptr inbounds (i8, ptr @level, i64 4), align 4, !tbaa !11
  %12 = sext i32 %11 to i64
  %13 = and i64 %10, %12
  %14 = getelementptr inbounds ptr, ptr %7, i64 %13
  %15 = load atomic i64, ptr %14 acquire, align 8
  %16 = inttoptr i64 %15 to ptr
  %17 = load i64, ptr getelementptr inbounds (i8, ptr @level, i64 8), align 8, !tbaa !12
  %.not = icmp ugt i64 %13, %17
  br i1 %.not, label %18, label %19

18:                                               ; preds = %6
  tail call void @ponyint_assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i64 noundef 163, ptr noundef nonnull @__func__.ponyint_pagemap_get) #2
  br label %19

19:                                               ; preds = %18, %6
  %20 = icmp eq i64 %15, 0
  br i1 %20, label %.loopexit, label %21

21:                                               ; preds = %19
  %22 = load i32, ptr getelementptr inbounds (i8, ptr @level, i64 24), align 8, !tbaa !5
  %23 = zext nneg i32 %22 to i64
  %24 = lshr i64 %4, %23
  %25 = load i32, ptr getelementptr inbounds (i8, ptr @level, i64 28), align 4, !tbaa !11
  %26 = sext i32 %25 to i64
  %27 = and i64 %24, %26
  %28 = getelementptr inbounds ptr, ptr %16, i64 %27
  %29 = load atomic i64, ptr %28 acquire, align 8
  %30 = inttoptr i64 %29 to ptr
  %31 = load i64, ptr getelementptr inbounds (i8, ptr @level, i64 32), align 16, !tbaa !12
  %.not.1 = icmp ugt i64 %27, %31
  br i1 %.not.1, label %32, label %33

32:                                               ; preds = %21
  tail call void @ponyint_assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i64 noundef 163, ptr noundef nonnull @__func__.ponyint_pagemap_get) #2
  br label %33

33:                                               ; preds = %32, %21
  %34 = icmp eq i64 %29, 0
  br i1 %34, label %.loopexit, label %35

35:                                               ; preds = %33
  %36 = load i32, ptr getelementptr inbounds (i8, ptr @level, i64 48), align 16, !tbaa !5
  %37 = zext nneg i32 %36 to i64
  %38 = lshr i64 %4, %37
  %39 = load i32, ptr getelementptr inbounds (i8, ptr @level, i64 52), align 4, !tbaa !11
  %40 = sext i32 %39 to i64
  %41 = and i64 %38, %40
  %42 = shl i64 %41, 1
  %43 = getelementptr inbounds ptr, ptr %30, i64 %42
  %44 = load i64, ptr getelementptr inbounds (i8, ptr @level, i64 56), align 8, !tbaa !12
  %.not47.2 = icmp ugt i64 %42, %44
  br i1 %.not47.2, label %45, label %46

45:                                               ; preds = %35
  tail call void @ponyint_assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i64 noundef 156, ptr noundef nonnull @__func__.ponyint_pagemap_get) #2
  br label %46

46:                                               ; preds = %45, %35
  %47 = and i64 %41, 9223372036854775807
  %.not48.2 = icmp ugt i64 %47, %40
  br i1 %.not48.2, label %48, label %.critedge

48:                                               ; preds = %46
  tail call void @ponyint_assert_fail(ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.1, i64 noundef 157, ptr noundef nonnull @__func__.ponyint_pagemap_get) #2
  br label %.critedge

.critedge:                                        ; preds = %48, %46
  %49 = load atomic i64, ptr %43 acquire, align 8
  %50 = inttoptr i64 %49 to ptr
  %51 = or disjoint i64 %42, 1
  %52 = icmp ult i64 %51, 65537
  br i1 %52, label %53, label %.thread

.thread:                                          ; preds = %.critedge
  tail call void @ponyint_assert_fail(ptr noundef nonnull @.str.4, ptr noundef nonnull @.str.1, i64 noundef 170, ptr noundef nonnull @__func__.ponyint_pagemap_get) #2
  br label %55

53:                                               ; preds = %.critedge
  %54 = icmp ult i64 %42, 8192
  br i1 %54, label %56, label %55

55:                                               ; preds = %.thread, %53
  tail call void @ponyint_assert_fail(ptr noundef nonnull @.str.5, ptr noundef nonnull @.str.1, i64 noundef 171, ptr noundef nonnull @__func__.ponyint_pagemap_get) #2
  br label %56

56:                                               ; preds = %53, %55
  %57 = getelementptr inbounds ptr, ptr %30, i64 %51
  %58 = load atomic i64, ptr %57 acquire, align 8
  %59 = inttoptr i64 %58 to ptr
  store ptr %59, ptr %1, align 8, !tbaa !13
  br label %.loopexit

.loopexit:                                        ; preds = %2, %19, %33, %56
  %.1 = phi ptr [ %50, %56 ], [ null, %33 ], [ null, %19 ], [ null, %2 ]
  ret ptr %.1
}

attributes #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 = { 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, !7, i64 0}
!6 = !{!"pagemap_level_t", !7, i64 0, !7, i64 4, !10, i64 8, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !7, i64 4}
!12 = !{!6, !10, i64 8}
!13 = !{!14, !14, i64 0}
!14 = !{!"any pointer", !8, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass