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

@dmaDataBuffer = external dso_local global ptr, align 8
@dataBufferSize = 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_setBufferSize(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 2048, %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 [ 2048, %6 ], [ %9, %7 ]
  %12 = sdiv i32 %11, 2
  %13 = trunc i32 %12 to i16
  store i16 %13, ptr @dataBufferSize, align 2, !tbaa !5
  %14 = load ptr, ptr @dmaDataBuffer, align 8, !tbaa !9
  %15 = icmp ne ptr %14, null
  br i1 %15, label %16, label %18

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

18:                                               ; preds = %16, %10
  %19 = load i16, ptr @dataBufferSize, align 2, !tbaa !5
  %20 = icmp ne i16 %19, 0
  br i1 %20, label %21, label %27

21:                                               ; preds = %18
  %22 = load i16, ptr @dataBufferSize, align 2, !tbaa !5
  %23 = zext i16 %22 to i64
  %24 = mul i64 %23, 2
  %25 = trunc i64 %24 to i16
  %26 = call ptr @MEM_alloc(i16 noundef zeroext %25)
  store ptr %26, ptr @dmaDataBuffer, align 8, !tbaa !9
  br label %28

27:                                               ; preds = %18
  store ptr null, ptr @dmaDataBuffer, align 8, !tbaa !9
  br label %28

28:                                               ; preds = %27, %21
  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"

@dmaDataBuffer = external dso_local local_unnamed_addr global ptr, align 8
@dataBufferSize = 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_setBufferSize(i16 noundef zeroext %0) local_unnamed_addr #1 {
  %2 = tail call i16 @llvm.umax.i16(i16 %0, i16 2048)
  %3 = lshr i16 %2, 1
  store i16 %3, ptr @dataBufferSize, align 2, !tbaa !5
  %4 = load ptr, ptr @dmaDataBuffer, align 8, !tbaa !9
  %.not = icmp eq ptr %4, null
  br i1 %.not, label %.thread, label %5

5:                                                ; preds = %1
  tail call void @MEM_free(ptr noundef nonnull %4) #3
  %.pr = load i16, ptr @dataBufferSize, align 2, !tbaa !5
  %.not3 = icmp eq i16 %.pr, 0
  br i1 %.not3, label %9, label %.thread

.thread:                                          ; preds = %1, %5
  %6 = phi i16 [ %.pr, %5 ], [ %3, %1 ]
  %7 = shl i16 %6, 1
  %8 = tail call ptr @MEM_alloc(i16 noundef zeroext %7) #3
  br label %9

9:                                                ; preds = %5, %.thread
  %storemerge = phi ptr [ %8, %.thread ], [ null, %5 ]
  store ptr %storemerge, ptr @dmaDataBuffer, 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,GlobalOptPass,IPSCCPPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass,TailCallElimPass