; LLVM IR from Aeternum-Veritas/Merry :: merry (merry_memory)

; 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.MerryAddress = type { i32, i32 }
%struct.MerryMemory = type { ptr, i64, i64, ptr, ptr }
%struct.MerryMemPage = type { ptr }

; 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: nocallback nofree nosync nounwind willreturn memory(none)
declare i64 @llvm.expect.i64(i64, i64) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @merry_memory_get_address(ptr noundef %0, i64 noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca %struct.MerryAddress, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %8 = getelementptr inbounds %struct.MerryAddress, ptr %6, i32 0, i32 0
  %9 = load i64, ptr %5, align 8, !tbaa !9
  %10 = udiv i64 %9, 131072
  %11 = trunc i64 %10 to i32
  store i32 %11, ptr %8, align 4, !tbaa !11
  %12 = getelementptr inbounds %struct.MerryAddress, ptr %6, i32 0, i32 1
  %13 = load i64, ptr %5, align 8, !tbaa !9
  %14 = urem i64 %13, 131072
  %15 = trunc i64 %14 to i32
  store i32 %15, ptr %12, align 4, !tbaa !14
  %16 = getelementptr inbounds %struct.MerryAddress, ptr %6, i32 0, i32 0
  %17 = load i32, ptr %16, align 4, !tbaa !11
  %18 = zext i32 %17 to i64
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.MerryMemory, ptr %19, i32 0, i32 1
  %21 = load i64, ptr %20, align 8, !tbaa !15
  %22 = icmp uge i64 %18, %21
  %23 = xor i1 %22, true
  %24 = xor i1 %23, true
  %25 = zext i1 %24 to i32
  %26 = sext i32 %25 to i64
  %27 = call i64 @llvm.expect.i64(i64 %26, i64 0)
  %28 = icmp ne i64 %27, 0
  br i1 %28, label %29, label %32

29:                                               ; preds = %2
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.MerryMemory, ptr %30, i32 0, i32 2
  store i64 52, ptr %31, align 8, !tbaa !17
  store ptr null, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %47

32:                                               ; preds = %2
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.MerryMemory, ptr %33, i32 0, i32 0
  %35 = load ptr, ptr %34, align 8, !tbaa !18
  %36 = getelementptr inbounds %struct.MerryAddress, ptr %6, i32 0, i32 0
  %37 = load i32, ptr %36, align 4, !tbaa !11
  %38 = zext i32 %37 to i64
  %39 = getelementptr inbounds ptr, ptr %35, i64 %38
  %40 = load ptr, ptr %39, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.MerryMemPage, ptr %40, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !19
  %43 = getelementptr inbounds %struct.MerryAddress, ptr %6, i32 0, i32 1
  %44 = load i32, ptr %43, align 4, !tbaa !14
  %45 = zext i32 %44 to i64
  %46 = getelementptr inbounds i64, ptr %42, i64 %45
  store ptr %46, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %47

47:                                               ; preds = %32, %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %48 = load ptr, ptr %3, align 8
  ret ptr %48
}

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 = { 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 = !{!"MerryAddress", !13, i64 0, !13, i64 4}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !13, i64 4}
!15 = !{!16, !10, i64 8}
!16 = !{!"MerryMemory", !6, i64 0, !10, i64 8, !10, i64 16, !6, i64 24, !6, i64 32}
!17 = !{!16, !10, i64 16}
!18 = !{!16, !6, i64 0}
!19 = !{!20, !6, i64 0}
!20 = !{!"MerryMemPage", !6, i64 0}


; 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: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local ptr @merry_memory_get_address(ptr nocapture noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = lshr i64 %1, 17
  %4 = and i64 %3, 4294967295
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %.not = icmp ult i64 %4, %6
  br i1 %.not, label %9, label %7, !prof !11

7:                                                ; preds = %2
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  store i64 52, ptr %8, align 8, !tbaa !12
  br label %16

9:                                                ; preds = %2
  %10 = and i64 %1, 131071
  %11 = load ptr, ptr %0, align 8, !tbaa !13
  %12 = getelementptr inbounds ptr, ptr %11, i64 %4
  %13 = load ptr, ptr %12, align 8, !tbaa !14
  %14 = load ptr, ptr %13, align 8, !tbaa !15
  %15 = getelementptr inbounds i64, ptr %14, i64 %10
  br label %16

16:                                               ; preds = %9, %7
  %.0 = phi ptr [ null, %7 ], [ %15, %9 ]
  ret ptr %.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn 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" }

!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, !10, i64 8}
!6 = !{!"MerryMemory", !7, i64 0, !10, i64 8, !10, i64 16, !7, i64 24, !7, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"branch_weights", !"expected", i32 2000, i32 1}
!12 = !{!6, !10, i64 16}
!13 = !{!6, !7, i64 0}
!14 = !{!7, !7, i64 0}
!15 = !{!16, !7, i64 0}
!16 = !{!"MerryMemPage", !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LowerExpectIntrinsicPass,PostOrderFunctionAttrsPass,SROAPass