; 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_read(ptr noundef %0, i64 noundef %1, ptr noundef %2) #2 {
  %4 = alloca i8, align 1
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, 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 ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %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 %83

34:                                               ; preds = %3
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.MerryMemory, ptr %35, i32 0, i32 0
  %37 = load ptr, ptr %36, align 8, !tbaa !18
  %38 = getelementptr inbounds %struct.MerryAddress, ptr %8, i32 0, i32 0
  %39 = load i32, ptr %38, align 4, !tbaa !11
  %40 = zext i32 %39 to i64
  %41 = getelementptr inbounds ptr, ptr %37, i64 %40
  %42 = load ptr, ptr %41, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.MerryMemPage, ptr %42, i32 0, i32 0
  %44 = load ptr, ptr %43, align 8, !tbaa !19
  %45 = icmp ne ptr %44, null
  br i1 %45, label %66, label %46

46:                                               ; preds = %34
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.MerryMemory, ptr %47, i32 0, i32 4
  %49 = load ptr, ptr %48, align 8, !tbaa !21
  call void @merry_mutex_lock(ptr noundef %49)
  %50 = getelementptr inbounds %struct.MerryAddress, ptr %8, i32 0, i32 0
  %51 = load i32, ptr %50, align 4, !tbaa !11
  %52 = zext i32 %51 to i64
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.MerryMemory, ptr %53, i32 0, i32 3
  %55 = load ptr, ptr %54, align 8, !tbaa !22
  %56 = call zeroext i8 @merry_requestHdlr_push_request(i64 noundef 0, i64 noundef %52, ptr noundef %55)
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.MerryMemory, ptr %57, i32 0, i32 4
  %59 = load ptr, ptr %58, align 8, !tbaa !21
  call void @merry_mutex_unlock(ptr noundef %59)
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.MerryMemory, ptr %60, i32 0, i32 2
  %62 = load i64, ptr %61, align 8, !tbaa !17
  %63 = icmp eq i64 %62, 52
  br i1 %63, label %64, label %65

64:                                               ; preds = %46
  store i8 1, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %83

65:                                               ; preds = %46
  br label %66

66:                                               ; preds = %65, %34
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.MerryMemory, ptr %67, i32 0, i32 0
  %69 = load ptr, ptr %68, align 8, !tbaa !18
  %70 = getelementptr inbounds %struct.MerryAddress, ptr %8, i32 0, i32 0
  %71 = load i32, ptr %70, align 4, !tbaa !11
  %72 = zext i32 %71 to i64
  %73 = getelementptr inbounds ptr, ptr %69, i64 %72
  %74 = load ptr, ptr %73, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.MerryMemPage, ptr %74, i32 0, i32 0
  %76 = load ptr, ptr %75, align 8, !tbaa !19
  %77 = getelementptr inbounds %struct.MerryAddress, ptr %8, i32 0, i32 1
  %78 = load i32, ptr %77, align 4, !tbaa !14
  %79 = zext i32 %78 to i64
  %80 = getelementptr inbounds i64, ptr %76, i64 %79
  %81 = load i64, ptr %80, align 8, !tbaa !9
  %82 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %81, ptr %82, align 8, !tbaa !9
  store i8 0, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %83

83:                                               ; preds = %66, %64, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %84 = load i8, ptr %4, align 1
  ret i8 %84
}

declare void @merry_mutex_lock(ptr noundef) #3

declare zeroext i8 @merry_requestHdlr_push_request(i64 noundef, i64 noundef, ptr noundef) #3

declare void @merry_mutex_unlock(ptr noundef) #3

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 = { "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 #4 = { 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}
!21 = !{!16, !6, i64 32}
!22 = !{!16, !6, i64 24}


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

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

11:                                               ; preds = %3
  %12 = load ptr, ptr %0, align 8, !tbaa !13
  %13 = getelementptr inbounds ptr, ptr %12, i64 %6
  %14 = load ptr, ptr %13, align 8, !tbaa !14
  %15 = load ptr, ptr %14, align 8, !tbaa !15
  %.not15 = icmp eq ptr %15, null
  br i1 %.not15, label %16, label %26

16:                                               ; preds = %11
  %17 = getelementptr inbounds i8, ptr %0, i64 32
  %18 = load ptr, ptr %17, align 8, !tbaa !17
  tail call void @merry_mutex_lock(ptr noundef %18) #2
  %19 = getelementptr inbounds i8, ptr %0, i64 24
  %20 = load ptr, ptr %19, align 8, !tbaa !18
  %21 = tail call zeroext i8 @merry_requestHdlr_push_request(i64 noundef 0, i64 noundef %6, ptr noundef %20) #2
  %22 = load ptr, ptr %17, align 8, !tbaa !17
  tail call void @merry_mutex_unlock(ptr noundef %22) #2
  %23 = getelementptr inbounds i8, ptr %0, i64 16
  %24 = load i64, ptr %23, align 8, !tbaa !12
  %25 = icmp eq i64 %24, 52
  br i1 %25, label %30, label %._crit_edge

._crit_edge:                                      ; preds = %16
  %.pre = load ptr, ptr %0, align 8, !tbaa !13
  %.phi.trans.insert = getelementptr inbounds ptr, ptr %.pre, i64 %6
  %.pre16 = load ptr, ptr %.phi.trans.insert, align 8, !tbaa !14
  %.pre17 = load ptr, ptr %.pre16, align 8, !tbaa !15
  br label %26

26:                                               ; preds = %._crit_edge, %11
  %27 = phi ptr [ %.pre17, %._crit_edge ], [ %15, %11 ]
  %28 = getelementptr inbounds i64, ptr %27, i64 %5
  %29 = load i64, ptr %28, align 8, !tbaa !19
  store i64 %29, ptr %2, align 8, !tbaa !19
  br label %30

30:                                               ; preds = %16, %26, %9
  %.0 = phi i8 [ 1, %9 ], [ 0, %26 ], [ 1, %16 ]
  ret i8 %.0
}

declare void @merry_mutex_lock(ptr noundef) local_unnamed_addr #1

declare zeroext i8 @merry_requestHdlr_push_request(i64 noundef, i64 noundef, ptr noundef) local_unnamed_addr #1

declare void @merry_mutex_unlock(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, !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 = !{!6, !7, i64 32}
!18 = !{!6, !7, i64 24}
!19 = !{!10, !10, i64 0}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LowerExpectIntrinsicPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass