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

@dmaQueues = external dso_local global ptr, align 8
@queueSize = external hidden global i16, align 2

declare void @MEM_free(ptr noundef) #0

declare ptr @MEM_alloc(i16 noundef zeroext) #0

; Function Attrs: nounwind uwtable
define dso_local void @DMA_setMaxQueueSize(i16 noundef zeroext %0) #1 {
  %2 = alloca i16, align 2
  store i16 %0, ptr %2, align 2, !tbaa !5
  %3 = load i16, ptr %2, align 2, !tbaa !5
  %4 = zext i16 %3 to i32
  %5 = icmp sgt i32 32, %4
  br i1 %5, label %6, label %7

6:                                                ; preds = %1
  br label %10

7:                                                ; preds = %1
  %8 = load i16, ptr %2, align 2, !tbaa !5
  %9 = zext i16 %8 to i32
  br label %10

10:                                               ; preds = %7, %6
  %11 = phi i32 [ 32, %6 ], [ %9, %7 ]
  %12 = trunc i32 %11 to i16
  store i16 %12, ptr @queueSize, align 2, !tbaa !5
  %13 = load ptr, ptr @dmaQueues, align 8, !tbaa !9
  %14 = icmp ne ptr %13, null
  br i1 %14, label %15, label %17

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

17:                                               ; preds = %15, %10
  %18 = load i16, ptr @queueSize, align 2, !tbaa !5
  %19 = zext i16 %18 to i64
  %20 = mul i64 %19, 32
  %21 = trunc i64 %20 to i16
  %22 = call ptr @MEM_alloc(i16 noundef zeroext %21)
  store ptr %22, ptr @dmaQueues, align 8, !tbaa !9
  call void @DMA_clearQueue()
  ret void
}

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

attributes #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 = { 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"

@dmaQueues = external dso_local local_unnamed_addr global ptr, align 8
@queueSize = external hidden local_unnamed_addr global i16, align 2

declare void @MEM_free(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local void @DMA_setMaxQueueSize(i16 noundef zeroext %0) local_unnamed_addr #1 {
  %2 = tail call i16 @llvm.umax.i16(i16 %0, i16 32)
  store i16 %2, ptr @queueSize, align 2, !tbaa !5
  %3 = load ptr, ptr @dmaQueues, align 8, !tbaa !9
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %5, label %4

4:                                                ; preds = %1
  tail call void @MEM_free(ptr noundef nonnull %3) #3
  %.pre = load i16, ptr @queueSize, align 2, !tbaa !5
  br label %5

5:                                                ; preds = %4, %1
  %6 = phi i16 [ %.pre, %4 ], [ %2, %1 ]
  %7 = shl i16 %6, 5
  %8 = tail call ptr @MEM_alloc(i16 noundef zeroext %7) #3
  store ptr %8, ptr @dmaQueues, align 8, !tbaa !9
  tail call void @DMA_clearQueue()
  ret void
}

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

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

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


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