; LLVM IR from Stephane-D/SGDK :: src (dma)

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

; 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 zeroext i8 @DMA_queueDma(i8 noundef zeroext %0, ptr noundef %1, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4) #1 {
  %6 = alloca i8, align 1
  %7 = alloca i8, align 1
  %8 = alloca ptr, align 8
  %9 = alloca i16, align 2
  %10 = alloca i16, align 2
  %11 = alloca i16, align 2
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  %14 = alloca i64, align 8
  %15 = alloca i16, align 2
  %16 = alloca i32, align 4
  store i8 %0, ptr %7, align 1, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !8
  store i16 %2, ptr %9, align 2, !tbaa !10
  store i16 %3, ptr %10, align 2, !tbaa !10
  store i16 %4, ptr %11, align 2, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %17 = load ptr, ptr %8, align 8, !tbaa !8
  %18 = ptrtoint ptr %17 to i64
  store i64 %18, ptr %12, align 8, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %19 = load i64, ptr %12, align 8, !tbaa !12
  %20 = and i64 %19, 131071
  %21 = sub i64 131072, %20
  store i64 %21, ptr %13, align 8, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #3
  %22 = load i64, ptr %13, align 8, !tbaa !12
  %23 = lshr i64 %22, 1
  store i64 %23, ptr %14, align 8, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 2, ptr %15) #3
  %24 = load i16, ptr %10, align 2, !tbaa !10
  %25 = zext i16 %24 to i64
  %26 = load i64, ptr %14, align 8, !tbaa !12
  %27 = icmp ugt i64 %25, %26
  br i1 %27, label %28, label %51

28:                                               ; preds = %5
  %29 = load i8, ptr %7, align 1, !tbaa !5
  %30 = load i64, ptr %12, align 8, !tbaa !12
  %31 = load i64, ptr %13, align 8, !tbaa !12
  %32 = add i64 %30, %31
  %33 = inttoptr i64 %32 to ptr
  %34 = load i16, ptr %9, align 2, !tbaa !10
  %35 = zext i16 %34 to i64
  %36 = load i64, ptr %13, align 8, !tbaa !12
  %37 = add i64 %35, %36
  %38 = trunc i64 %37 to i16
  %39 = load i16, ptr %10, align 2, !tbaa !10
  %40 = zext i16 %39 to i64
  %41 = load i64, ptr %14, align 8, !tbaa !12
  %42 = sub i64 %40, %41
  %43 = trunc i64 %42 to i16
  %44 = load i16, ptr %11, align 2, !tbaa !10
  %45 = call zeroext i8 @DMA_queueDmaFast(i8 noundef zeroext %29, ptr noundef %33, i16 noundef zeroext %38, i16 noundef zeroext %43, i16 noundef zeroext %44)
  %46 = icmp ne i8 %45, 0
  br i1 %46, label %48, label %47

47:                                               ; preds = %28
  store i8 0, ptr %6, align 1
  store i32 1, ptr %16, align 4
  br label %60

48:                                               ; preds = %28
  %49 = load i64, ptr %14, align 8, !tbaa !12
  %50 = trunc i64 %49 to i16
  store i16 %50, ptr %15, align 2, !tbaa !10
  br label %53

51:                                               ; preds = %5
  %52 = load i16, ptr %10, align 2, !tbaa !10
  store i16 %52, ptr %15, align 2, !tbaa !10
  br label %53

53:                                               ; preds = %51, %48
  %54 = load i8, ptr %7, align 1, !tbaa !5
  %55 = load ptr, ptr %8, align 8, !tbaa !8
  %56 = load i16, ptr %9, align 2, !tbaa !10
  %57 = load i16, ptr %15, align 2, !tbaa !10
  %58 = load i16, ptr %11, align 2, !tbaa !10
  %59 = call zeroext i8 @DMA_queueDmaFast(i8 noundef zeroext %54, ptr noundef %55, i16 noundef zeroext %56, i16 noundef zeroext %57, i16 noundef zeroext %58)
  store i8 %59, ptr %6, align 1
  store i32 1, ptr %16, align 4
  br label %60

60:                                               ; preds = %53, %47
  call void @llvm.lifetime.end.p0(i64 2, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  %61 = load i8, ptr %6, align 1
  ret i8 %61
}

; Function Attrs: noinline nounwind uwtable
declare dso_local zeroext i8 @DMA_queueDmaFast(i8 noundef zeroext, ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext) #2

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 = { noinline 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 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"short", !6, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"long", !6, 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"

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @DMA_queueDma(i8 noundef zeroext %0, ptr noundef %1, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4) local_unnamed_addr #0 {
  %6 = ptrtoint ptr %1 to i64
  %7 = and i64 %6, 131071
  %8 = sub nuw nsw i64 131072, %7
  %9 = lshr i64 %8, 1
  %10 = zext i16 %3 to i64
  %11 = icmp ult i64 %9, %10
  br i1 %11, label %12, label %20

12:                                               ; preds = %5
  %13 = add i64 %8, %6
  %14 = inttoptr i64 %13 to ptr
  %15 = trunc i64 %8 to i16
  %16 = add i16 %15, %2
  %17 = trunc i64 %9 to i16
  %18 = sub i16 %3, %17
  %19 = tail call zeroext i8 @DMA_queueDmaFast(i8 noundef zeroext %0, ptr noundef %14, i16 noundef zeroext %16, i16 noundef zeroext %18, i16 noundef zeroext %4)
  %.not = icmp eq i8 %19, 0
  br i1 %.not, label %22, label %20

20:                                               ; preds = %12, %5
  %.0 = phi i16 [ %3, %5 ], [ %17, %12 ]
  %21 = tail call zeroext i8 @DMA_queueDmaFast(i8 noundef zeroext %0, ptr noundef %1, i16 noundef zeroext %2, i16 noundef zeroext %.0, i16 noundef zeroext %4)
  br label %22

22:                                               ; preds = %12, %20
  %.020 = phi i8 [ %21, %20 ], [ 0, %12 ]
  ret i8 %.020
}

; Function Attrs: noinline nounwind uwtable
declare dso_local zeroext i8 @DMA_queueDmaFast(i8 noundef zeroext, ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext) 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 = { noinline 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)"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass,TailCallElimPass