; LLVM IR from FrancesoM/Bisc8FX :: sdk_projects/bisc8_platform/ps7_cortexa9_0/standalone_domain/bsp/ps7_cortexa9_0_ (BdRingClone)

; 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.XEmacPs_BdRing = type { i64, i64, i64, i32, i32, i32, ptr, ptr, ptr, ptr, ptr, ptr, i32, i32, i32, i32, i32 }

; 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: nounwind uwtable
define dso_local i64 @XEmacPs_BdRingClone(ptr noundef %0, ptr noundef %1, i8 noundef zeroext %2) #1 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i8, align 1
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i8 %2, ptr %7, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.XEmacPs_BdRing, ptr %11, i32 0, i32 16
  %13 = load i32, ptr %12, align 8, !tbaa !10
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %15, label %16

15:                                               ; preds = %3
  store i64 523, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %78

16:                                               ; preds = %3
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.XEmacPs_BdRing, ptr %17, i32 0, i32 4
  %19 = load i32, ptr %18, align 4, !tbaa !14
  %20 = icmp eq i32 %19, 514
  br i1 %20, label %21, label %22

21:                                               ; preds = %16
  store i64 5, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %78

22:                                               ; preds = %16
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.XEmacPs_BdRing, ptr %23, i32 0, i32 14
  %25 = load i32, ptr %24, align 8, !tbaa !15
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.XEmacPs_BdRing, ptr %26, i32 0, i32 16
  %28 = load i32, ptr %27, align 8, !tbaa !10
  %29 = icmp ne i32 %25, %28
  br i1 %29, label %30, label %31

30:                                               ; preds = %22
  store i64 526, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %78

31:                                               ; preds = %22
  %32 = load i8, ptr %7, align 1, !tbaa !9
  %33 = zext i8 %32 to i32
  %34 = icmp ne i32 %33, 1
  br i1 %34, label %35, label %40

35:                                               ; preds = %31
  %36 = load i8, ptr %7, align 1, !tbaa !9
  %37 = zext i8 %36 to i32
  %38 = icmp ne i32 %37, 2
  br i1 %38, label %39, label %40

39:                                               ; preds = %35
  store i64 15, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %78

40:                                               ; preds = %35, %31
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.XEmacPs_BdRing, ptr %41, i32 0, i32 1
  %43 = load i64, ptr %42, align 8, !tbaa !16
  store i64 %43, ptr %9, align 8, !tbaa !17
  store i32 0, ptr %8, align 4, !tbaa !18
  br label %44

44:                                               ; preds = %60, %40
  %45 = load i32, ptr %8, align 4, !tbaa !18
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.XEmacPs_BdRing, ptr %46, i32 0, i32 16
  %48 = load i32, ptr %47, align 8, !tbaa !10
  %49 = icmp ult i32 %45, %48
  br i1 %49, label %50, label %63

50:                                               ; preds = %44
  %51 = load i64, ptr %9, align 8, !tbaa !17
  %52 = inttoptr i64 %51 to ptr
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %52, ptr align 4 %53, i64 8, i1 false)
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.XEmacPs_BdRing, ptr %54, i32 0, i32 5
  %56 = load i32, ptr %55, align 8, !tbaa !19
  %57 = zext i32 %56 to i64
  %58 = load i64, ptr %9, align 8, !tbaa !17
  %59 = add i64 %58, %57
  store i64 %59, ptr %9, align 8, !tbaa !17
  br label %60

60:                                               ; preds = %50
  %61 = load i32, ptr %8, align 4, !tbaa !18
  %62 = add i32 %61, 1
  store i32 %62, ptr %8, align 4, !tbaa !18
  br label %44, !llvm.loop !20

63:                                               ; preds = %44
  %64 = load ptr, ptr %5, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.XEmacPs_BdRing, ptr %64, i32 0, i32 5
  %66 = load i32, ptr %65, align 8, !tbaa !19
  %67 = zext i32 %66 to i64
  %68 = load i64, ptr %9, align 8, !tbaa !17
  %69 = sub i64 %68, %67
  store i64 %69, ptr %9, align 8, !tbaa !17
  %70 = load i8, ptr %7, align 1, !tbaa !9
  %71 = zext i8 %70 to i32
  %72 = icmp eq i32 %71, 2
  br i1 %72, label %73, label %75

73:                                               ; preds = %63
  %74 = load i64, ptr %9, align 8, !tbaa !17
  call void @XEmacPs_BdSetRxWrap(i64 noundef %74)
  br label %77

75:                                               ; preds = %63
  %76 = load i64, ptr %9, align 8, !tbaa !17
  call void @XEmacPs_BdSetTxWrap(i64 noundef %76)
  br label %77

77:                                               ; preds = %75, %73
  store i64 0, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %78

78:                                               ; preds = %77, %39, %30, %21, %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %79 = load i64, ptr %4, align 8
  ret i64 %79
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
declare hidden void @XEmacPs_BdSetRxWrap(i64 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @XEmacPs_BdSetTxWrap(i64 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!7, !7, i64 0}
!10 = !{!11, !13, i64 104}
!11 = !{!"", !12, i64 0, !12, i64 8, !12, i64 16, !13, i64 24, !13, i64 28, !13, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !13, i64 88, !13, i64 92, !13, i64 96, !13, i64 100, !13, i64 104}
!12 = !{!"long", !7, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!11, !13, i64 28}
!15 = !{!11, !13, i64 96}
!16 = !{!11, !12, i64 8}
!17 = !{!12, !12, i64 0}
!18 = !{!13, !13, i64 0}
!19 = !{!11, !13, i64 32}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


; 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 range(i64 0, 527) i64 @XEmacPs_BdRingClone(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i8 noundef zeroext %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 104
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %6 = icmp eq i32 %5, 0
  br i1 %6, label %31, label %7

7:                                                ; preds = %3
  %8 = getelementptr inbounds i8, ptr %0, i64 28
  %9 = load i32, ptr %8, align 4, !tbaa !12
  %10 = icmp eq i32 %9, 514
  br i1 %10, label %31, label %11

11:                                               ; preds = %7
  %12 = getelementptr inbounds i8, ptr %0, i64 96
  %13 = load i32, ptr %12, align 8, !tbaa !13
  %.not = icmp eq i32 %13, %5
  br i1 %.not, label %14, label %31

14:                                               ; preds = %11
  %15 = add i8 %2, -3
  %or.cond = icmp ult i8 %15, -2
  br i1 %or.cond, label %31, label %.lr.ph

.lr.ph:                                           ; preds = %14
  %16 = getelementptr inbounds i8, ptr %0, i64 8
  %17 = load i64, ptr %16, align 8, !tbaa !14
  %18 = getelementptr inbounds i8, ptr %0, i64 32
  br label %19

19:                                               ; preds = %.lr.ph, %19
  %.025 = phi i64 [ %17, %.lr.ph ], [ %24, %19 ]
  %.02124 = phi i32 [ 0, %.lr.ph ], [ %25, %19 ]
  %20 = inttoptr i64 %.025 to ptr
  %21 = load i64, ptr %1, align 4
  store i64 %21, ptr %20, align 1
  %22 = load i32, ptr %18, align 8, !tbaa !15
  %23 = zext i32 %22 to i64
  %24 = add i64 %.025, %23
  %25 = add nuw i32 %.02124, 1
  %26 = load i32, ptr %4, align 8, !tbaa !5
  %27 = icmp ult i32 %25, %26
  br i1 %27, label %19, label %._crit_edge, !llvm.loop !16

._crit_edge:                                      ; preds = %19
  %28 = icmp eq i8 %2, 2
  br i1 %28, label %29, label %30

29:                                               ; preds = %._crit_edge
  tail call void @XEmacPs_BdSetRxWrap(i64 noundef %.025)
  br label %31

30:                                               ; preds = %._crit_edge
  tail call void @XEmacPs_BdSetTxWrap(i64 noundef %.025)
  br label %31

31:                                               ; preds = %29, %30, %14, %11, %7, %3
  %.022 = phi i64 [ 523, %3 ], [ 5, %7 ], [ 526, %11 ], [ 15, %14 ], [ 0, %30 ], [ 0, %29 ]
  ret i64 %.022
}

; Function Attrs: nounwind uwtable
declare hidden void @XEmacPs_BdSetRxWrap(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @XEmacPs_BdSetTxWrap(i64 noundef) local_unnamed_addr #0

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" }

!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 104}
!6 = !{!"", !7, i64 0, !7, i64 8, !7, i64 16, !10, i64 24, !10, i64 28, !10, i64 32, !11, i64 40, !11, i64 48, !11, i64 56, !11, i64 64, !11, i64 72, !11, i64 80, !10, i64 88, !10, i64 92, !10, i64 96, !10, i64 100, !10, i64 104}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !10, i64 28}
!13 = !{!6, !10, i64 96}
!14 = !{!6, !7, i64 8}
!15 = !{!6, !10, i64 32}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass