; LLVM IR from Aeternum-Veritas/Merry :: legacy/decoder/src (merry_branch_predictor)

; 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.MerryPredictor = type { [8 x %struct._LocalIndex_], i8, i8, i64 }
%struct._LocalIndex_ = type { [8 x i8] }

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @merry_predict_branch(ptr noundef %0, i64 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i8, align 1
  %6 = alloca i8, align 1
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #2
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.MerryPredictor, ptr %7, i32 0, i32 0
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.MerryPredictor, ptr %9, i32 0, i32 1
  %11 = load i8, ptr %10, align 8, !tbaa !11
  %12 = zext i8 %11 to i32
  %13 = and i32 %12, 7
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [8 x %struct._LocalIndex_], ptr %8, i64 0, i64 %14
  %16 = getelementptr inbounds %struct._LocalIndex_, ptr %15, i32 0, i32 0
  %17 = load i64, ptr %4, align 8, !tbaa !9
  %18 = and i64 %17, 7
  %19 = getelementptr inbounds [8 x i8], ptr %16, i64 0, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !13
  store i8 %20, ptr %5, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #2
  store i8 1, ptr %6, align 1, !tbaa !13
  %21 = load i8, ptr %5, align 1, !tbaa !13
  %22 = zext i8 %21 to i32
  %23 = and i32 %22, 3
  %24 = icmp eq i32 %23, 3
  br i1 %24, label %25, label %30

25:                                               ; preds = %2
  store i8 0, ptr %6, align 1, !tbaa !13
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.MerryPredictor, ptr %26, i32 0, i32 1
  %28 = load i8, ptr %27, align 8, !tbaa !11
  %29 = add i8 %28, -1
  store i8 %29, ptr %27, align 8, !tbaa !11
  br label %35

30:                                               ; preds = %2
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.MerryPredictor, ptr %31, i32 0, i32 1
  %33 = load i8, ptr %32, align 8, !tbaa !11
  %34 = add i8 %33, 1
  store i8 %34, ptr %32, align 8, !tbaa !11
  br label %35

35:                                               ; preds = %30, %25
  %36 = load i64, ptr %4, align 8, !tbaa !9
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.MerryPredictor, ptr %37, i32 0, i32 3
  store i64 %36, ptr %38, align 8, !tbaa !14
  %39 = load i8, ptr %6, align 1, !tbaa !13
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.MerryPredictor, ptr %40, i32 0, i32 2
  store i8 %39, ptr %41, align 1, !tbaa !15
  %42 = load i8, ptr %6, align 1, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #2
  ret i8 %42
}

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

; 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 = { 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, !7, i64 64}
!12 = !{!"MerryPredictor", !7, i64 0, !7, i64 64, !7, i64 65, !10, i64 72}
!13 = !{!7, !7, i64 0}
!14 = !{!12, !10, i64 72}
!15 = !{!12, !7, i64 65}


; 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._LocalIndex_ = type { [8 x i8] }

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) uwtable
define dso_local zeroext range(i8 0, 2) i8 @merry_predict_branch(ptr nocapture noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 64
  %4 = load i8, ptr %3, align 8, !tbaa !5
  %5 = and i8 %4, 7
  %6 = zext nneg i8 %5 to i64
  %7 = getelementptr inbounds [8 x %struct._LocalIndex_], ptr %0, i64 0, i64 %6
  %8 = and i64 %1, 7
  %9 = getelementptr inbounds [8 x i8], ptr %7, i64 0, i64 %8
  %10 = load i8, ptr %9, align 1, !tbaa !10
  %11 = and i8 %10, 3
  %12 = icmp ne i8 %11, 3
  %storemerge.v = select i1 %12, i8 1, i8 -1
  %storemerge = add i8 %storemerge.v, %4
  %.0 = zext i1 %12 to i8
  store i8 %storemerge, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %0, i64 72
  store i64 %1, ptr %13, align 8, !tbaa !11
  %14 = getelementptr inbounds i8, ptr %0, i64 65
  store i8 %.0, ptr %14, align 1, !tbaa !12
  ret i8 %.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) 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, !7, i64 64}
!6 = !{!"MerryPredictor", !7, i64 0, !7, i64 64, !7, i64 65, !9, i64 72}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"long", !7, i64 0}
!10 = !{!7, !7, i64 0}
!11 = !{!6, !9, i64 72}
!12 = !{!6, !7, i64 65}


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