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

26:                                               ; preds = %5
  %27 = load i8, ptr %6, align 1, !tbaa !5
  %28 = load i64, ptr %11, align 8, !tbaa !12
  %29 = load i64, ptr %12, align 8, !tbaa !12
  %30 = add i64 %28, %29
  %31 = inttoptr i64 %30 to ptr
  %32 = load i16, ptr %8, align 2, !tbaa !10
  %33 = zext i16 %32 to i64
  %34 = load i64, ptr %12, align 8, !tbaa !12
  %35 = add i64 %33, %34
  %36 = trunc i64 %35 to i16
  %37 = load i16, ptr %9, align 2, !tbaa !10
  %38 = zext i16 %37 to i64
  %39 = load i64, ptr %13, align 8, !tbaa !12
  %40 = sub i64 %38, %39
  %41 = trunc i64 %40 to i16
  %42 = load i16, ptr %10, align 2, !tbaa !10
  call void @DMA_doDmaFast(i8 noundef zeroext %27, ptr noundef %31, i16 noundef zeroext %36, i16 noundef zeroext %41, i16 noundef signext %42)
  %43 = load i64, ptr %13, align 8, !tbaa !12
  store i64 %43, ptr %14, align 8, !tbaa !12
  br label %47

44:                                               ; preds = %5
  %45 = load i16, ptr %9, align 2, !tbaa !10
  %46 = zext i16 %45 to i64
  store i64 %46, ptr %14, align 8, !tbaa !12
  br label %47

47:                                               ; preds = %44, %26
  %48 = load i8, ptr %6, align 1, !tbaa !5
  %49 = load ptr, ptr %7, align 8, !tbaa !8
  %50 = load i16, ptr %8, align 2, !tbaa !10
  %51 = load i64, ptr %14, align 8, !tbaa !12
  %52 = trunc i64 %51 to i16
  %53 = load i16, ptr %10, align 2, !tbaa !10
  call void @DMA_doDmaFast(i8 noundef zeroext %48, ptr noundef %49, i16 noundef zeroext %50, i16 noundef zeroext %52, i16 noundef signext %53)
  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
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  ret void
}

; Function Attrs: noinline nounwind uwtable
declare dso_local void @DMA_doDmaFast(i8 noundef zeroext, ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef signext) #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 void @DMA_doDma(i8 noundef zeroext %0, ptr noundef %1, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef signext %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 %19

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
  tail call void @DMA_doDmaFast(i8 noundef zeroext %0, ptr noundef %14, i16 noundef zeroext %16, i16 noundef zeroext %18, i16 noundef signext %4)
  br label %19

19:                                               ; preds = %5, %12
  %.0 = phi i16 [ %17, %12 ], [ %3, %5 ]
  tail call void @DMA_doDmaFast(i8 noundef zeroext %0, ptr noundef %1, i16 noundef zeroext %2, i16 noundef zeroext %.0, i16 noundef signext %4)
  ret void
}

; Function Attrs: noinline nounwind uwtable
declare dso_local void @DMA_doDmaFast(i8 noundef zeroext, ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef signext) 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,SROAPass,SimplifyCFGPass,TailCallElimPass