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

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

14:                                               ; preds = %3
  store i64 1, ptr %7, align 8, !tbaa !14
  br label %66

15:                                               ; preds = %3
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.XEmacPs_BdRing, ptr %16, i32 0, i32 6
  %18 = load ptr, ptr %17, align 8, !tbaa !15
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %18, ptr %19, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.XEmacPs_BdRing, ptr %20, i32 0, i32 6
  %22 = load ptr, ptr %21, align 8, !tbaa !15
  %23 = ptrtoint ptr %22 to i64
  store i64 %23, ptr %8, align 8, !tbaa !14
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.XEmacPs_BdRing, ptr %24, i32 0, i32 5
  %26 = load i32, ptr %25, align 8, !tbaa !16
  %27 = load i32, ptr %5, align 4, !tbaa !9
  %28 = mul i32 %26, %27
  %29 = zext i32 %28 to i64
  %30 = load i64, ptr %8, align 8, !tbaa !14
  %31 = add i64 %30, %29
  store i64 %31, ptr %8, align 8, !tbaa !14
  %32 = load i64, ptr %8, align 8, !tbaa !14
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.XEmacPs_BdRing, ptr %33, i32 0, i32 2
  %35 = load i64, ptr %34, align 8, !tbaa !17
  %36 = icmp ugt i64 %32, %35
  br i1 %36, label %44, label %37

37:                                               ; preds = %15
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.XEmacPs_BdRing, ptr %38, i32 0, i32 6
  %40 = load ptr, ptr %39, align 8, !tbaa !15
  %41 = ptrtoint ptr %40 to i64
  %42 = load i64, ptr %8, align 8, !tbaa !14
  %43 = icmp ugt i64 %41, %42
  br i1 %43, label %44, label %51

44:                                               ; preds = %37, %15
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.XEmacPs_BdRing, ptr %45, i32 0, i32 3
  %47 = load i32, ptr %46, align 8, !tbaa !18
  %48 = zext i32 %47 to i64
  %49 = load i64, ptr %8, align 8, !tbaa !14
  %50 = sub i64 %49, %48
  store i64 %50, ptr %8, align 8, !tbaa !14
  br label %51

51:                                               ; preds = %44, %37
  %52 = load i64, ptr %8, align 8, !tbaa !14
  %53 = inttoptr i64 %52 to ptr
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.XEmacPs_BdRing, ptr %54, i32 0, i32 6
  store ptr %53, ptr %55, align 8, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  %56 = load i32, ptr %5, align 4, !tbaa !9
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.XEmacPs_BdRing, ptr %57, i32 0, i32 14
  %59 = load i32, ptr %58, align 8, !tbaa !11
  %60 = sub i32 %59, %56
  store i32 %60, ptr %58, align 8, !tbaa !11
  %61 = load i32, ptr %5, align 4, !tbaa !9
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.XEmacPs_BdRing, ptr %62, i32 0, i32 13
  %64 = load i32, ptr %63, align 4, !tbaa !19
  %65 = add i32 %64, %61
  store i32 %65, ptr %63, align 4, !tbaa !19
  store i64 0, ptr %7, align 8, !tbaa !14
  br label %66

66:                                               ; preds = %51, %14
  %67 = load i64, ptr %7, align 8, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  ret i64 %67
}

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 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 96}
!12 = !{!"", !13, i64 0, !13, i64 8, !13, i64 16, !10, i64 24, !10, i64 28, !10, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !10, i64 88, !10, i64 92, !10, i64 96, !10, i64 100, !10, i64 104}
!13 = !{!"long", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!12, !6, i64 40}
!16 = !{!12, !10, i64 32}
!17 = !{!12, !13, i64 16}
!18 = !{!12, !10, i64 24}
!19 = !{!12, !10, i64 92}


; 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(argmem: readwrite) uwtable
define dso_local range(i64 0, 2) i64 @XEmacPs_BdRingAlloc(ptr nocapture noundef %0, i32 noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 96
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %6 = icmp ult i32 %5, %1
  br i1 %6, label %31, label %7

7:                                                ; preds = %3
  %8 = getelementptr inbounds i8, ptr %0, i64 40
  %9 = load ptr, ptr %8, align 8, !tbaa !12
  store ptr %9, ptr %2, align 8, !tbaa !13
  %10 = ptrtoint ptr %9 to i64
  %11 = getelementptr inbounds i8, ptr %0, i64 32
  %12 = load i32, ptr %11, align 8, !tbaa !14
  %13 = mul i32 %12, %1
  %14 = zext i32 %13 to i64
  %15 = add i64 %14, %10
  %16 = getelementptr inbounds i8, ptr %0, i64 16
  %17 = load i64, ptr %16, align 8, !tbaa !15
  %18 = icmp ugt i64 %15, %17
  %19 = icmp ult i64 %15, %10
  %or.cond = or i1 %18, %19
  br i1 %or.cond, label %20, label %25

20:                                               ; preds = %7
  %21 = getelementptr inbounds i8, ptr %0, i64 24
  %22 = load i32, ptr %21, align 8, !tbaa !16
  %23 = zext i32 %22 to i64
  %24 = sub i64 %15, %23
  br label %25

25:                                               ; preds = %7, %20
  %.0 = phi i64 [ %24, %20 ], [ %15, %7 ]
  %26 = inttoptr i64 %.0 to ptr
  store ptr %26, ptr %8, align 8, !tbaa !12
  %27 = sub i32 %5, %1
  store i32 %27, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds i8, ptr %0, i64 92
  %29 = load i32, ptr %28, align 4, !tbaa !17
  %30 = add i32 %29, %1
  store i32 %30, ptr %28, align 4, !tbaa !17
  br label %31

31:                                               ; preds = %3, %25
  %.020 = phi i64 [ 0, %25 ], [ 1, %3 ]
  ret i64 %.020
}

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, !10, i64 96}
!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, !11, i64 40}
!13 = !{!11, !11, i64 0}
!14 = !{!6, !10, i64 32}
!15 = !{!6, !7, i64 16}
!16 = !{!6, !10, i64 24}
!17 = !{!6, !10, i64 92}


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