; 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

declare void @SYS_disableInts() #2

declare void @SYS_enableInts() #2

; Function Attrs: noinline nounwind uwtable
define dso_local void @Z80_upload(i16 noundef zeroext %0, ptr noundef %1, i16 noundef zeroext %2) #3 {
  %4 = alloca i16, align 2
  %5 = alloca ptr, align 8
  %6 = alloca i16, align 2
  %7 = alloca i8, align 1
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i16, align 2
  store i16 %0, ptr %4, align 2, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  store i16 %2, ptr %6, align 2, !tbaa !5
  call void @SYS_disableInts()
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #4
  %11 = call zeroext i8 @Z80_getAndRequestBus(i8 noundef zeroext 1)
  store i8 %11, ptr %7, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %12 = load ptr, ptr %5, align 8, !tbaa !9
  store ptr %12, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %13 = load i16, ptr %4, align 2, !tbaa !5
  %14 = zext i16 %13 to i32
  %15 = add nsw i32 10485760, %14
  %16 = sext i32 %15 to i64
  %17 = inttoptr i64 %16 to ptr
  store ptr %17, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #4
  %18 = load i16, ptr %6, align 2, !tbaa !5
  store i16 %18, ptr %10, align 2, !tbaa !5
  br label %19

19:                                               ; preds = %23, %3
  %20 = load i16, ptr %10, align 2, !tbaa !5
  %21 = add i16 %20, -1
  store i16 %21, ptr %10, align 2, !tbaa !5
  %22 = icmp ne i16 %20, 0
  br i1 %22, label %23, label %29

23:                                               ; preds = %19
  %24 = load ptr, ptr %8, align 8, !tbaa !9
  %25 = getelementptr inbounds i8, ptr %24, i32 1
  store ptr %25, ptr %8, align 8, !tbaa !9
  %26 = load i8, ptr %24, align 1, !tbaa !11
  %27 = load ptr, ptr %9, align 8, !tbaa !9
  %28 = getelementptr inbounds i8, ptr %27, i32 1
  store ptr %28, ptr %9, align 8, !tbaa !9
  store volatile i8 %26, ptr %27, align 1, !tbaa !11
  br label %19, !llvm.loop !12

29:                                               ; preds = %19
  %30 = load i8, ptr %7, align 1, !tbaa !11
  %31 = icmp ne i8 %30, 0
  br i1 %31, label %33, label %32

32:                                               ; preds = %29
  call void @Z80_releaseBus()
  br label %33

33:                                               ; preds = %32, %29
  call void @SYS_enableInts()
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #4
  ret void
}

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 = { "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 = { 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 #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}
!11 = !{!7, !7, 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

declare void @SYS_disableInts() local_unnamed_addr #1

declare void @SYS_enableInts() local_unnamed_addr #1

; Function Attrs: noinline nounwind uwtable
define dso_local void @Z80_upload(i16 noundef zeroext %0, ptr nocapture noundef readonly %1, i16 noundef zeroext %2) local_unnamed_addr #2 {
  tail call void @SYS_disableInts() #3
  %4 = tail call zeroext i8 @Z80_getAndRequestBus(i8 noundef zeroext 1)
  %.not9 = icmp eq i16 %2, 0
  br i1 %.not9, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %5 = zext i16 %0 to i64
  %6 = or disjoint i64 %5, 10485760
  %7 = inttoptr i64 %6 to ptr
  %xtraiter = and i16 %2, 7
  %lcmp.mod.not = icmp eq i16 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader, %.lr.ph.prol
  %.012.prol = phi i16 [ %8, %.lr.ph.prol ], [ %2, %.lr.ph.preheader ]
  %.0611.prol = phi ptr [ %11, %.lr.ph.prol ], [ %7, %.lr.ph.preheader ]
  %.0710.prol = phi ptr [ %9, %.lr.ph.prol ], [ %1, %.lr.ph.preheader ]
  %prol.iter = phi i16 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader ]
  %8 = add i16 %.012.prol, -1
  %9 = getelementptr inbounds i8, ptr %.0710.prol, i64 1
  %10 = load i8, ptr %.0710.prol, align 1, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %.0611.prol, i64 1
  store volatile i8 %10, ptr %.0611.prol, align 1, !tbaa !5
  %prol.iter.next = add i16 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i16 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol, !llvm.loop !8

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.012.unr = phi i16 [ %2, %.lr.ph.preheader ], [ %8, %.lr.ph.prol ]
  %.0611.unr = phi ptr [ %7, %.lr.ph.preheader ], [ %11, %.lr.ph.prol ]
  %.0710.unr = phi ptr [ %1, %.lr.ph.preheader ], [ %9, %.lr.ph.prol ]
  %12 = icmp ult i16 %2, 8
  br i1 %12, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.012 = phi i16 [ %34, %.lr.ph ], [ %.012.unr, %.lr.ph.prol.loopexit ]
  %.0611 = phi ptr [ %37, %.lr.ph ], [ %.0611.unr, %.lr.ph.prol.loopexit ]
  %.0710 = phi ptr [ %35, %.lr.ph ], [ %.0710.unr, %.lr.ph.prol.loopexit ]
  %13 = getelementptr inbounds i8, ptr %.0710, i64 1
  %14 = load i8, ptr %.0710, align 1, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %.0611, i64 1
  store volatile i8 %14, ptr %.0611, align 1, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %.0710, i64 2
  %17 = load i8, ptr %13, align 1, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %.0611, i64 2
  store volatile i8 %17, ptr %15, align 1, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %.0710, i64 3
  %20 = load i8, ptr %16, align 1, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %.0611, i64 3
  store volatile i8 %20, ptr %18, align 1, !tbaa !5
  %22 = getelementptr inbounds i8, ptr %.0710, i64 4
  %23 = load i8, ptr %19, align 1, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %.0611, i64 4
  store volatile i8 %23, ptr %21, align 1, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %.0710, i64 5
  %26 = load i8, ptr %22, align 1, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %.0611, i64 5
  store volatile i8 %26, ptr %24, align 1, !tbaa !5
  %28 = getelementptr inbounds i8, ptr %.0710, i64 6
  %29 = load i8, ptr %25, align 1, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %.0611, i64 6
  store volatile i8 %29, ptr %27, align 1, !tbaa !5
  %31 = getelementptr inbounds i8, ptr %.0710, i64 7
  %32 = load i8, ptr %28, align 1, !tbaa !5
  %33 = getelementptr inbounds i8, ptr %.0611, i64 7
  store volatile i8 %32, ptr %30, align 1, !tbaa !5
  %34 = add i16 %.012, -8
  %35 = getelementptr inbounds i8, ptr %.0710, i64 8
  %36 = load i8, ptr %31, align 1, !tbaa !5
  %37 = getelementptr inbounds i8, ptr %.0611, i64 8
  store volatile i8 %36, ptr %33, align 1, !tbaa !5
  %.not.7 = icmp eq i16 %34, 0
  br i1 %.not.7, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %3
  %.not8 = icmp eq i8 %4, 0
  br i1 %.not8, label %38, label %39

38:                                               ; preds = %._crit_edge
  tail call void @Z80_releaseBus()
  br label %39

39:                                               ; preds = %38, %._crit_edge
  tail call void @SYS_enableInts() #3
  ret void
}

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 = { 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 = { 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.unroll.disable"}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass