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

@flag = external hidden global i16, align 2
@queueIndex = external hidden global i16, align 2
@queueIndexLimit = external hidden global i16, align 2

; Function Attrs: nounwind uwtable
declare dso_local void @DMA_clearQueue() #0

; Function Attrs: nounwind uwtable
define dso_local void @DMA_flushQueue() #0 {
  %1 = alloca i16, align 2
  %2 = alloca i8, align 1
  %3 = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 2, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %2) #3
  %4 = load i16, ptr @queueIndex, align 2, !tbaa !5
  store i16 %4, ptr %1, align 2, !tbaa !5
  %5 = load i16, ptr @queueIndexLimit, align 2, !tbaa !5
  %6 = icmp ne i16 %5, 0
  br i1 %6, label %7, label %15

7:                                                ; preds = %0
  %8 = load i16, ptr @flag, align 2, !tbaa !5
  %9 = zext i16 %8 to i32
  %10 = and i32 %9, 2
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %12, label %14

12:                                               ; preds = %7
  %13 = load i16, ptr @queueIndexLimit, align 2, !tbaa !5
  store i16 %13, ptr %1, align 2, !tbaa !5
  br label %14

14:                                               ; preds = %12, %7
  br label %15

15:                                               ; preds = %14, %0
  %16 = call zeroext i8 @VDP_getAutoInc()
  store i8 %16, ptr %2, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #3
  %17 = call zeroext i8 @Z80_isBusTaken()
  store i8 %17, ptr %3, align 1, !tbaa !9
  %18 = load i8, ptr %3, align 1, !tbaa !9
  %19 = icmp ne i8 %18, 0
  br i1 %19, label %21, label %20

20:                                               ; preds = %15
  call void @Z80_requestBus(i8 noundef zeroext 0)
  br label %21

21:                                               ; preds = %20, %15
  %22 = load i16, ptr %1, align 2, !tbaa !5
  call void @flushQueue(i16 noundef zeroext %22)
  %23 = load i8, ptr %3, align 1, !tbaa !9
  %24 = icmp ne i8 %23, 0
  br i1 %24, label %26, label %25

25:                                               ; preds = %21
  call void @Z80_releaseBus()
  br label %26

26:                                               ; preds = %25, %21
  call void @DMA_clearQueue()
  %27 = load i8, ptr %2, align 1, !tbaa !9
  call void @VDP_setAutoInc(i8 noundef zeroext %27)
  call void @llvm.lifetime.end.p0(i64 1, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %1) #3
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

declare zeroext i8 @VDP_getAutoInc() #2

declare zeroext i8 @Z80_isBusTaken() #2

declare void @Z80_requestBus(i8 noundef zeroext) #2

declare void @flushQueue(i16 noundef zeroext) #2

declare void @Z80_releaseBus() #2

declare void @VDP_setAutoInc(i8 noundef zeroext) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!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"

@flag = external hidden local_unnamed_addr global i16, align 2
@queueIndex = external hidden local_unnamed_addr global i16, align 2
@queueIndexLimit = external hidden local_unnamed_addr global i16, align 2

; Function Attrs: nounwind uwtable
declare dso_local void @DMA_clearQueue() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @DMA_flushQueue() local_unnamed_addr #0 {
  %1 = load i16, ptr @queueIndex, align 2, !tbaa !5
  %2 = load i16, ptr @queueIndexLimit, align 2, !tbaa !5
  %.not = icmp eq i16 %2, 0
  %3 = load i16, ptr @flag, align 2
  %4 = and i16 %3, 2
  %.not5 = icmp eq i16 %4, 0
  %5 = select i1 %.not, i1 true, i1 %.not5
  %.0 = select i1 %5, i16 %1, i16 %2
  %6 = tail call zeroext i8 @VDP_getAutoInc() #2
  %7 = tail call zeroext i8 @Z80_isBusTaken() #2
  %.not6 = icmp eq i8 %7, 0
  br i1 %.not6, label %8, label %.critedge

8:                                                ; preds = %0
  tail call void @Z80_requestBus(i8 noundef zeroext 0) #2
  tail call void @flushQueue(i16 noundef zeroext %.0) #2
  tail call void @Z80_releaseBus() #2
  br label %9

.critedge:                                        ; preds = %0
  tail call void @flushQueue(i16 noundef zeroext %.0) #2
  br label %9

9:                                                ; preds = %.critedge, %8
  tail call void @DMA_clearQueue()
  tail call void @VDP_setAutoInc(i8 noundef zeroext %6) #2
  ret void
}

declare zeroext i8 @VDP_getAutoInc() local_unnamed_addr #1

declare zeroext i8 @Z80_isBusTaken() local_unnamed_addr #1

declare void @Z80_requestBus(i8 noundef zeroext) local_unnamed_addr #1

declare void @flushQueue(i16 noundef zeroext) local_unnamed_addr #1

declare void @Z80_releaseBus() local_unnamed_addr #1

declare void @VDP_setAutoInc(i8 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 = { "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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


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