; LLVM IR from Stephane-D/SGDK :: src (z80_ctrl)

; 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
declare dso_local zeroext i8 @Z80_getAndRequestBus(i8 noundef zeroext) #1

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

; Function Attrs: noinline nounwind uwtable
define dso_local void @Z80_clear() #2 {
  %1 = alloca i8, align 1
  %2 = alloca i8, align 1
  %3 = alloca ptr, align 8
  %4 = alloca i16, align 2
  call void @SYS_disableInts()
  call void @llvm.lifetime.start.p0(i64 1, ptr %1) #4
  %5 = call zeroext i8 @Z80_getAndRequestBus(i8 noundef zeroext 1)
  store i8 %5, ptr %1, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %2) #4
  %6 = call zeroext i8 @getZeroU8()
  store i8 %6, ptr %2, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  store ptr inttoptr (i64 10485760 to ptr), ptr %3, align 8, !tbaa !8
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #4
  store i16 8192, ptr %4, align 2, !tbaa !10
  br label %7

7:                                                ; preds = %11, %0
  %8 = load i16, ptr %4, align 2, !tbaa !10
  %9 = add i16 %8, -1
  store i16 %9, ptr %4, align 2, !tbaa !10
  %10 = icmp ne i16 %8, 0
  br i1 %10, label %11, label %15

11:                                               ; preds = %7
  %12 = load i8, ptr %2, align 1, !tbaa !5
  %13 = load ptr, ptr %3, align 8, !tbaa !8
  %14 = getelementptr inbounds i8, ptr %13, i32 1
  store ptr %14, ptr %3, align 8, !tbaa !8
  store volatile i8 %12, ptr %13, align 1, !tbaa !5
  br label %7, !llvm.loop !12

15:                                               ; preds = %7
  %16 = load i8, ptr %1, align 1, !tbaa !5
  %17 = icmp ne i8 %16, 0
  br i1 %17, label %19, label %18

18:                                               ; preds = %15
  call void @Z80_releaseBus()
  br label %19

19:                                               ; preds = %18, %15
  call void @SYS_enableInts()
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %1) #4
  ret void
}

declare void @SYS_disableInts() #3

declare zeroext i8 @getZeroU8() #3

declare void @SYS_enableInts() #3

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 = { "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 #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 = !{!"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 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; 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
declare dso_local zeroext i8 @Z80_getAndRequestBus(i8 noundef zeroext) local_unnamed_addr #0

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

; Function Attrs: noinline nounwind uwtable
define dso_local void @Z80_clear() local_unnamed_addr #1 {
  tail call void @SYS_disableInts() #3
  %1 = tail call zeroext i8 @Z80_getAndRequestBus(i8 noundef zeroext 1)
  %2 = tail call zeroext i8 @getZeroU8() #3
  br label %3

3:                                                ; preds = %3, %0
  %.06 = phi i16 [ 8192, %0 ], [ %11, %3 ]
  %.035 = phi ptr [ inttoptr (i64 10485760 to ptr), %0 ], [ %12, %3 ]
  %4 = getelementptr inbounds i8, ptr %.035, i64 1
  store volatile i8 %2, ptr %.035, align 1, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %.035, i64 2
  store volatile i8 %2, ptr %4, align 1, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %.035, i64 3
  store volatile i8 %2, ptr %5, align 1, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %.035, i64 4
  store volatile i8 %2, ptr %6, align 1, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %.035, i64 5
  store volatile i8 %2, ptr %7, align 1, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %.035, i64 6
  store volatile i8 %2, ptr %8, align 1, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %.035, i64 7
  store volatile i8 %2, ptr %9, align 1, !tbaa !5
  %11 = add nsw i16 %.06, -8
  %12 = getelementptr inbounds i8, ptr %.035, i64 8
  store volatile i8 %2, ptr %10, align 1, !tbaa !5
  %.not.7 = icmp eq i16 %11, 0
  br i1 %.not.7, label %13, label %3, !llvm.loop !8

13:                                               ; preds = %3
  %.not4 = icmp eq i8 %1, 0
  br i1 %.not4, label %14, label %15

14:                                               ; preds = %13
  tail call void @Z80_releaseBus()
  br label %15

15:                                               ; preds = %14, %13
  tail call void @SYS_enableInts() #3
  ret void
}

declare void @SYS_disableInts() local_unnamed_addr #2

declare zeroext i8 @getZeroU8() local_unnamed_addr #2

declare void @SYS_enableInts() local_unnamed_addr #2

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" }
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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,LoopUnrollPass,SROAPass,TailCallElimPass