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

@maxTransferPerFrame = external hidden global i16, align 2
@flag = external hidden global i16, align 2
@VBlankProcess = external global i16, align 2
@dmaQueues = external dso_local global ptr, align 8
@dmaDataBuffer = external dso_local global ptr, align 8
@queueSize = external hidden global i16, align 2

; Function Attrs: noinline nounwind uwtable
define dso_local void @DMA_initEx(i16 noundef zeroext %0, i16 noundef zeroext %1, i16 noundef zeroext %2) #0 {
  %4 = alloca i16, align 2
  %5 = alloca i16, align 2
  %6 = alloca i16, align 2
  store i16 %0, ptr %4, align 2, !tbaa !5
  store i16 %1, ptr %5, align 2, !tbaa !5
  store i16 %2, ptr %6, align 2, !tbaa !5
  %7 = load i16, ptr %5, align 2, !tbaa !5
  store i16 %7, ptr @maxTransferPerFrame, align 2, !tbaa !5
  store i16 1, ptr @flag, align 2, !tbaa !5
  %8 = load volatile i16, ptr @VBlankProcess, align 2, !tbaa !5
  %9 = zext i16 %8 to i32
  %10 = or i32 %9, 4
  %11 = trunc i32 %10 to i16
  store volatile i16 %11, ptr @VBlankProcess, align 2, !tbaa !5
  %12 = load ptr, ptr @dmaQueues, align 8, !tbaa !9
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %16

14:                                               ; preds = %3
  %15 = load ptr, ptr @dmaQueues, align 8, !tbaa !9
  call void @MEM_free(ptr noundef %15)
  store ptr null, ptr @dmaQueues, align 8, !tbaa !9
  br label %16

16:                                               ; preds = %14, %3
  %17 = load ptr, ptr @dmaDataBuffer, align 8, !tbaa !9
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %21

19:                                               ; preds = %16
  %20 = load ptr, ptr @dmaDataBuffer, align 8, !tbaa !9
  call void @MEM_free(ptr noundef %20)
  store ptr null, ptr @dmaDataBuffer, align 8, !tbaa !9
  br label %21

21:                                               ; preds = %19, %16
  call void @MEM_pack()
  %22 = load i16, ptr %4, align 2, !tbaa !5
  %23 = icmp ne i16 %22, 0
  br i1 %23, label %24, label %35

24:                                               ; preds = %21
  %25 = load i16, ptr %4, align 2, !tbaa !5
  %26 = zext i16 %25 to i32
  %27 = icmp sgt i32 32, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %24
  br label %32

29:                                               ; preds = %24
  %30 = load i16, ptr %4, align 2, !tbaa !5
  %31 = zext i16 %30 to i32
  br label %32

32:                                               ; preds = %29, %28
  %33 = phi i32 [ 32, %28 ], [ %31, %29 ]
  %34 = trunc i32 %33 to i16
  store i16 %34, ptr @queueSize, align 2, !tbaa !5
  br label %36

35:                                               ; preds = %21
  store i16 80, ptr @queueSize, align 2, !tbaa !5
  br label %36

36:                                               ; preds = %35, %32
  %37 = load i16, ptr @queueSize, align 2, !tbaa !5
  %38 = zext i16 %37 to i64
  %39 = mul i64 %38, 32
  %40 = trunc i64 %39 to i16
  %41 = call ptr @MEM_alloc(i16 noundef zeroext %40)
  store ptr %41, ptr @dmaQueues, align 8, !tbaa !9
  %42 = load i16, ptr %6, align 2, !tbaa !5
  %43 = icmp ne i16 %42, 0
  br i1 %43, label %44, label %46

44:                                               ; preds = %36
  %45 = load i16, ptr %6, align 2, !tbaa !5
  call void @DMA_setBufferSize(i16 noundef zeroext %45)
  br label %47

46:                                               ; preds = %36
  call void @DMA_setBufferSizeToDefault()
  br label %47

47:                                               ; preds = %46, %44
  ret void
}

declare void @MEM_free(ptr noundef) #1

declare void @MEM_pack() #1

declare ptr @MEM_alloc(i16 noundef zeroext) #1

; Function Attrs: nounwind uwtable
declare dso_local void @DMA_setBufferSize(i16 noundef zeroext) #2

; Function Attrs: nounwind uwtable
declare dso_local void @DMA_setBufferSizeToDefault() #2

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

@maxTransferPerFrame = external hidden local_unnamed_addr global i16, align 2
@flag = external hidden local_unnamed_addr global i16, align 2
@VBlankProcess = external global i16, align 2
@dmaQueues = external dso_local local_unnamed_addr global ptr, align 8
@dmaDataBuffer = external dso_local local_unnamed_addr global ptr, align 8
@queueSize = external hidden local_unnamed_addr global i16, align 2

; Function Attrs: noinline nounwind uwtable
define dso_local void @DMA_initEx(i16 noundef zeroext %0, i16 noundef zeroext %1, i16 noundef zeroext %2) local_unnamed_addr #0 {
  store i16 %1, ptr @maxTransferPerFrame, align 2, !tbaa !5
  store i16 1, ptr @flag, align 2, !tbaa !5
  %4 = load volatile i16, ptr @VBlankProcess, align 2, !tbaa !5
  %5 = or i16 %4, 4
  store volatile i16 %5, ptr @VBlankProcess, align 2, !tbaa !5
  %6 = load ptr, ptr @dmaQueues, align 8, !tbaa !9
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %8, label %7

7:                                                ; preds = %3
  tail call void @MEM_free(ptr noundef nonnull %6) #4
  store ptr null, ptr @dmaQueues, align 8, !tbaa !9
  br label %8

8:                                                ; preds = %7, %3
  %9 = load ptr, ptr @dmaDataBuffer, align 8, !tbaa !9
  %.not9 = icmp eq ptr %9, null
  br i1 %.not9, label %11, label %10

10:                                               ; preds = %8
  tail call void @MEM_free(ptr noundef nonnull %9) #4
  store ptr null, ptr @dmaDataBuffer, align 8, !tbaa !9
  br label %11

11:                                               ; preds = %10, %8
  tail call void @MEM_pack() #4
  %.not10 = icmp eq i16 %0, 0
  %12 = tail call i16 @llvm.umax.i16(i16 %0, i16 32)
  %storemerge = select i1 %.not10, i16 80, i16 %12
  store i16 %storemerge, ptr @queueSize, align 2, !tbaa !5
  %13 = shl i16 %storemerge, 5
  %14 = tail call ptr @MEM_alloc(i16 noundef zeroext %13) #4
  store ptr %14, ptr @dmaQueues, align 8, !tbaa !9
  %.not11 = icmp eq i16 %2, 0
  br i1 %.not11, label %16, label %15

15:                                               ; preds = %11
  tail call void @DMA_setBufferSize(i16 noundef zeroext %2)
  br label %17

16:                                               ; preds = %11
  tail call void @DMA_setBufferSizeToDefault()
  br label %17

17:                                               ; preds = %16, %15
  ret void
}

declare void @MEM_free(ptr noundef) local_unnamed_addr #1

declare void @MEM_pack() local_unnamed_addr #1

declare ptr @MEM_alloc(i16 noundef zeroext) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @DMA_setBufferSize(i16 noundef zeroext) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local void @DMA_setBufferSizeToDefault() local_unnamed_addr #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.umax.i16(i16, i16) #3

attributes #0 = { 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 #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 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}


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