; 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] }

@.str = external hidden unnamed_addr constant [10 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @merry_prediction_wrong(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %3 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  %4 = load ptr, ptr %2, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.MerryPredictor, ptr %4, i32 0, i32 0
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.MerryPredictor, ptr %6, i32 0, i32 1
  %8 = load i8, ptr %7, align 8, !tbaa !9
  %9 = zext i8 %8 to i32
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.MerryPredictor, ptr %10, i32 0, i32 2
  %12 = load i8, ptr %11, align 1, !tbaa !12
  %13 = zext i8 %12 to i32
  %14 = icmp eq i32 %13, 1
  %15 = zext i1 %14 to i32
  %16 = add nsw i32 %9, %15
  %17 = icmp ne i32 %16, 0
  %18 = zext i1 %17 to i64
  %19 = select i1 %17, i32 1, i32 -1
  %20 = and i32 %19, 7
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [8 x %struct._LocalIndex_], ptr %5, i64 0, i64 %21
  %23 = getelementptr inbounds %struct._LocalIndex_, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.MerryPredictor, ptr %24, i32 0, i32 3
  %26 = load i64, ptr %25, align 8, !tbaa !13
  %27 = and i64 %26, 7
  %28 = getelementptr inbounds [8 x i8], ptr %23, i64 0, i64 %27
  %29 = load i8, ptr %28, align 1, !tbaa !14
  %30 = add i8 %29, 1
  store i8 %30, ptr %28, align 1, !tbaa !14
  ret void
}

declare i32 @printf(ptr noundef, ...) #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" }

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

%struct._LocalIndex_ = type { [8 x i8] }

@.str = external hidden unnamed_addr constant [10 x i8], align 1

; Function Attrs: nofree nounwind uwtable
define dso_local void @merry_prediction_wrong(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  %3 = getelementptr inbounds i8, ptr %0, i64 64
  %4 = load i8, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 65
  %6 = load i8, ptr %5, align 1, !tbaa !10
  %7 = icmp eq i8 %6, 1
  %8 = zext i1 %7 to i8
  %9 = or i8 %4, %8
  %.not = icmp eq i8 %9, 0
  %10 = select i1 %.not, i64 7, i64 1
  %11 = getelementptr inbounds [8 x %struct._LocalIndex_], ptr %0, i64 0, i64 %10
  %12 = getelementptr inbounds i8, ptr %0, i64 72
  %13 = load i64, ptr %12, align 8, !tbaa !11
  %14 = and i64 %13, 7
  %15 = getelementptr inbounds [8 x i8], ptr %11, i64 0, i64 %14
  %16 = load i8, ptr %15, align 1, !tbaa !12
  %17 = add i8 %16, 1
  store i8 %17, ptr %15, align 1, !tbaa !12
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

attributes #0 = { nofree 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 = { nofree nounwind "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 = !{!6, !7, i64 65}
!11 = !{!6, !9, i64 72}
!12 = !{!7, !7, i64 0}


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