; 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 zeroext i8 @merry_memory_write(ptr noundef %0, i64 noundef %1, i64 noundef %2) #2 {
  %4 = alloca i8, align 1
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca %struct.MerryAddress, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %10 = getelementptr inbounds %struct.MerryAddress, ptr %8, i32 0, i32 0
  %11 = load i64, ptr %6, align 8, !tbaa !9
  %12 = udiv i64 %11, 131072
  %13 = trunc i64 %12 to i32
  store i32 %13, ptr %10, align 4, !tbaa !11
  %14 = getelementptr inbounds %struct.MerryAddress, ptr %8, i32 0, i32 1
  %15 = load i64, ptr %6, align 8, !tbaa !9
  %16 = urem i64 %15, 131072
  %17 = trunc i64 %16 to i32
  store i32 %17, ptr %14, align 4, !tbaa !14
  %18 = getelementptr inbounds %struct.MerryAddress, ptr %8, i32 0, i32 0
  %19 = load i32, ptr %18, align 4, !tbaa !11
  %20 = zext i32 %19 to i64
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.MerryMemory, ptr %21, i32 0, i32 1
  %23 = load i64, ptr %22, align 8, !tbaa !15
  %24 = icmp uge i64 %20, %23
  %25 = xor i1 %24, true
  %26 = xor i1 %25, true
  %27 = zext i1 %26 to i32
  %28 = sext i32 %27 to i64
  %29 = call i64 @llvm.expect.i64(i64 %28, i64 0)
  %30 = icmp ne i64 %29, 0
  br i1 %30, label %31, label %34

31:                                               ; preds = %3
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.MerryMemory, ptr %32, i32 0, i32 2
  store i64 52, ptr %33, align 8, !tbaa !17
  store i8 1, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %50

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

50:                                               ; preds = %34, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %51 = load i8, ptr %4, align 1
  ret i8 %51
}

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(readwrite, inaccessiblemem: none) uwtable
define dso_local zeroext range(i8 0, 2) i8 @merry_memory_write(ptr nocapture noundef %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = lshr i64 %1, 17
  %5 = and i64 %4, 4294967295
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = load i64, ptr %6, align 8, !tbaa !5
  %.not = icmp ult i64 %5, %7
  br i1 %.not, label %10, label %8, !prof !11

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

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

17:                                               ; preds = %10, %8
  %.0 = phi i8 [ 1, %8 ], [ 0, %10 ]
  ret i8 %.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(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}
!17 = !{!10, !10, i64 0}


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