; LLVM IR from Stephane-D/SGDK :: sample/demo/bad-apple/src (pcm)

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

@z80_drv = external constant [3584 x i8], align 16
@smp_null_dpcm = external constant [128 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local void @loadDriver() #0 {
  %1 = alloca ptr, align 8
  %2 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  call void @Z80_upload(i16 noundef zeroext 0, ptr noundef @z80_drv, i16 noundef zeroext 3584)
  call void @Z80_requestBus(i8 noundef zeroext 1)
  store ptr inttoptr (i64 10486052 to ptr), ptr %1, align 8, !tbaa !5
  store i64 ptrtoint (ptr @smp_null_dpcm to i64), ptr %2, align 8, !tbaa !9
  %3 = load i64, ptr %2, align 8, !tbaa !9
  %4 = lshr i64 %3, 7
  %5 = trunc i64 %4 to i8
  %6 = load ptr, ptr %1, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %6, i64 0
  store volatile i8 %5, ptr %7, align 1, !tbaa !11
  %8 = load i64, ptr %2, align 8, !tbaa !9
  %9 = lshr i64 %8, 15
  %10 = trunc i64 %9 to i8
  %11 = load ptr, ptr %1, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %11, i64 1
  store volatile i8 %10, ptr %12, align 1, !tbaa !11
  %13 = load ptr, ptr %1, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %13, i64 2
  store volatile i8 1, ptr %14, align 1, !tbaa !11
  %15 = load ptr, ptr %1, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %15, i64 3
  store volatile i8 0, ptr %16, align 1, !tbaa !11
  call void @Z80_releaseBus()
  br label %17

17:                                               ; preds = %20, %0
  %18 = call zeroext i8 @Z80_isBusTaken()
  %19 = icmp ne i8 %18, 0
  br i1 %19, label %20, label %21

20:                                               ; preds = %17
  br label %17, !llvm.loop !12

21:                                               ; preds = %17
  br label %22

22:                                               ; preds = %31, %21
  %23 = call zeroext i8 @Z80_isDriverReady()
  %24 = icmp ne i8 %23, 0
  %25 = xor i1 %24, true
  br i1 %25, label %26, label %32

26:                                               ; preds = %22
  br label %27

27:                                               ; preds = %30, %26
  %28 = call zeroext i8 @Z80_isBusTaken()
  %29 = icmp ne i8 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %27
  br label %27, !llvm.loop !14

31:                                               ; preds = %27
  br label %22, !llvm.loop !15

32:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

declare void @Z80_upload(i16 noundef zeroext, ptr noundef, i16 noundef zeroext) #2

declare void @Z80_requestBus(i8 noundef zeroext) #2

declare void @Z80_releaseBus() #2

declare zeroext i8 @Z80_isBusTaken() #2

declare zeroext i8 @Z80_isDriverReady() #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}
!15 = distinct !{!15, !13}


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

@z80_drv = external constant [3584 x i8], align 16
@smp_null_dpcm = external constant [128 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local void @loadDriver() local_unnamed_addr #0 {
  tail call void @Z80_upload(i16 noundef zeroext 0, ptr noundef nonnull @z80_drv, i16 noundef zeroext 3584) #2
  tail call void @Z80_requestBus(i8 noundef zeroext 1) #2
  %1 = lshr i64 ptrtoint (ptr @smp_null_dpcm to i64), 7
  %2 = trunc i64 %1 to i8
  store volatile i8 %2, ptr inttoptr (i64 10486052 to ptr), align 4, !tbaa !5
  %3 = lshr i64 ptrtoint (ptr @smp_null_dpcm to i64), 15
  %4 = trunc i64 %3 to i8
  store volatile i8 %4, ptr inttoptr (i64 10486053 to ptr), align 1, !tbaa !5
  store volatile i8 1, ptr inttoptr (i64 10486054 to ptr), align 2, !tbaa !5
  store volatile i8 0, ptr inttoptr (i64 10486055 to ptr), align 1, !tbaa !5
  tail call void @Z80_releaseBus() #2
  br label %5

5:                                                ; preds = %5, %0
  %6 = tail call zeroext i8 @Z80_isBusTaken() #2
  %.not = icmp eq i8 %6, 0
  br i1 %.not, label %.preheader7, label %5, !llvm.loop !8

.preheader7:                                      ; preds = %5
  %7 = tail call zeroext i8 @Z80_isDriverReady() #2
  %.not58 = icmp eq i8 %7, 0
  br i1 %.not58, label %.preheader, label %._crit_edge

.preheader:                                       ; preds = %.preheader7, %.preheader.backedge
  %8 = tail call zeroext i8 @Z80_isBusTaken() #2
  %.not6 = icmp eq i8 %8, 0
  br i1 %.not6, label %9, label %.preheader.backedge

.preheader.backedge:                              ; preds = %.preheader, %9
  br label %.preheader, !llvm.loop !10

9:                                                ; preds = %.preheader
  %10 = tail call zeroext i8 @Z80_isDriverReady() #2
  %.not5 = icmp eq i8 %10, 0
  br i1 %.not5, label %.preheader.backedge, label %._crit_edge

._crit_edge:                                      ; preds = %9, %.preheader7
  ret void
}

declare void @Z80_upload(i16 noundef zeroext, ptr noundef, i16 noundef zeroext) local_unnamed_addr #1

declare void @Z80_requestBus(i8 noundef zeroext) local_unnamed_addr #1

declare void @Z80_releaseBus() local_unnamed_addr #1

declare zeroext i8 @Z80_isBusTaken() local_unnamed_addr #1

declare zeroext i8 @Z80_isDriverReady() local_unnamed_addr #1

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 = { 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"}
!10 = distinct !{!10, !9}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass