; 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
@__func__.ponyint_pagemap_get_chunk = external hidden unnamed_addr constant [26 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

; Function Attrs: nounwind uwtable
define dso_local ptr @ponyint_pagemap_get_chunk(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  store ptr @root, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = load atomic i64, ptr %13 acquire, align 8
  store i64 %14, ptr %6, align 8
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %15, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store i64 0, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  store i64 0, ptr %8, align 8, !tbaa !9
  br label %16

16:                                               ; preds = %94, %1
  %17 = load i64, ptr %8, align 8, !tbaa !9
  %18 = icmp ult i64 %17, 3
  br i1 %18, label %20, label %19

19:                                               ; preds = %16
  store i32 2, ptr %9, align 4
  br label %97

20:                                               ; preds = %16
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = icmp eq ptr %21, null
  br i1 %22, label %23, label %24

23:                                               ; preds = %20
  store ptr null, ptr %2, align 8
  store i32 1, ptr %9, align 4
  br label %97

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

41:                                               ; preds = %24
  %42 = load i64, ptr %7, align 8, !tbaa !9
  %43 = mul i64 %42, 2
  store i64 %43, ptr %7, align 8, !tbaa !9
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = load i64, ptr %7, align 8, !tbaa !9
  %46 = getelementptr inbounds ptr, ptr %44, i64 %45
  store ptr %46, ptr %4, align 8, !tbaa !5
  %47 = load i64, ptr %7, align 8, !tbaa !9
  %48 = load i64, ptr %8, align 8, !tbaa !9
  %49 = getelementptr inbounds [3 x %struct.pagemap_level_t], ptr @level, i64 0, i64 %48
  %50 = getelementptr inbounds %struct.pagemap_level_t, ptr %49, i32 0, i32 2
  %51 = load i64, ptr %50, align 8, !tbaa !15
  %52 = icmp ule i64 %47, %51
  br i1 %52, label %53, label %54

53:                                               ; preds = %41
  br label %55

54:                                               ; preds = %41
  call void @ponyint_assert_fail(ptr noundef @.str, ptr noundef @.str.1, i64 noundef 109, ptr noundef @__func__.ponyint_pagemap_get_chunk)
  br label %55

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

64:                                               ; preds = %55
  br label %66

65:                                               ; preds = %55
  call void @ponyint_assert_fail(ptr noundef @.str.2, ptr noundef @.str.1, i64 noundef 110, ptr noundef @__func__.ponyint_pagemap_get_chunk)
  br label %66

66:                                               ; preds = %65, %64
  br label %93

67:                                               ; preds = %24
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = load i64, ptr %7, align 8, !tbaa !9
  %70 = getelementptr inbounds ptr, ptr %68, i64 %69
  store ptr %70, ptr %4, align 8, !tbaa !5
  %71 = load ptr, ptr %4, align 8, !tbaa !5
  %72 = load atomic i64, ptr %71 acquire, align 8
  store i64 %72, ptr %10, align 8
  %73 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %73, ptr %5, align 8, !tbaa !5
  %74 = load i64, ptr %7, align 8, !tbaa !9
  %75 = load i64, ptr %8, align 8, !tbaa !9
  %76 = getelementptr inbounds [3 x %struct.pagemap_level_t], ptr @level, i64 0, i64 %75
  %77 = getelementptr inbounds %struct.pagemap_level_t, ptr %76, i32 0, i32 2
  %78 = load i64, ptr %77, align 8, !tbaa !15
  %79 = icmp ule i64 %74, %78
  br i1 %79, label %80, label %81

80:                                               ; preds = %67
  br label %82

81:                                               ; preds = %67
  call void @ponyint_assert_fail(ptr noundef @.str, ptr noundef @.str.1, i64 noundef 116, ptr noundef @__func__.ponyint_pagemap_get_chunk)
  br label %82

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

90:                                               ; preds = %82
  br label %92

91:                                               ; preds = %82
  call void @ponyint_assert_fail(ptr noundef @.str.3, ptr noundef @.str.1, i64 noundef 117, ptr noundef @__func__.ponyint_pagemap_get_chunk)
  br label %92

92:                                               ; preds = %91, %90
  br label %93

93:                                               ; preds = %92, %66
  br label %94

94:                                               ; preds = %93
  %95 = load i64, ptr %8, align 8, !tbaa !9
  %96 = add i64 %95, 1
  store i64 %96, ptr %8, align 8, !tbaa !9
  br label %16, !llvm.loop !16

97:                                               ; preds = %23, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %98 = load i32, ptr %9, align 4
  switch i32 %98, label %104 [
    i32 2, label %99
  ]

99:                                               ; preds = %97
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %100 = load ptr, ptr %4, align 8, !tbaa !5
  %101 = load atomic i64, ptr %100 acquire, align 8
  store i64 %101, ptr %12, align 8
  %102 = load ptr, ptr %12, align 8, !tbaa !5
  store ptr %102, ptr %11, align 8, !tbaa !5
  %103 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr %103, ptr %2, align 8
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  br label %104

104:                                              ; preds = %99, %97
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %105 = load ptr, ptr %2, align 8
  ret ptr %105
}

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

declare void @ponyint_assert_fail(ptr noundef, ptr noundef, i64 noundef, ptr 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, !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
@__func__.ponyint_pagemap_get_chunk = external hidden unnamed_addr constant [26 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [33 x i8], align 1

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

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

17:                                               ; preds = %5
  tail call void @ponyint_assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i64 noundef 116, ptr noundef nonnull @__func__.ponyint_pagemap_get_chunk) #2
  br label %18

18:                                               ; preds = %17, %5
  %19 = icmp eq i64 %14, 0
  br i1 %19, label %.loopexit, label %20

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

31:                                               ; preds = %20
  tail call void @ponyint_assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i64 noundef 116, ptr noundef nonnull @__func__.ponyint_pagemap_get_chunk) #2
  br label %32

32:                                               ; preds = %31, %20
  %33 = icmp eq i64 %28, 0
  br i1 %33, label %.loopexit, label %34

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

44:                                               ; preds = %34
  tail call void @ponyint_assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i64 noundef 109, ptr noundef nonnull @__func__.ponyint_pagemap_get_chunk) #2
  br label %45

45:                                               ; preds = %44, %34
  %46 = and i64 %40, 9223372036854775807
  %.not36.2 = icmp ugt i64 %46, %39
  br i1 %.not36.2, label %47, label %.critedge

47:                                               ; preds = %45
  tail call void @ponyint_assert_fail(ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.1, i64 noundef 110, ptr noundef nonnull @__func__.ponyint_pagemap_get_chunk) #2
  br label %.critedge

.critedge:                                        ; preds = %47, %45
  %48 = load atomic i64, ptr %42 acquire, align 8
  %49 = inttoptr i64 %48 to ptr
  br label %.loopexit

.loopexit:                                        ; preds = %1, %18, %32, %.critedge
  %.1 = phi ptr [ %49, %.critedge ], [ null, %32 ], [ null, %18 ], [ null, %1 ]
  ret ptr %.1
}

declare void @ponyint_assert_fail(ptr noundef, ptr noundef, i64 noundef, 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 = !{!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}


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