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

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

@lastVCnt = external dso_local global i16, align 2
@vtimer = external global i64, align 8
@task_pc = external global i64, align 8

; 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 @VDP_isEnable() #1

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @computeFrameCPULoad(i16 noundef zeroext, i16 noundef zeroext, i64 noundef) #1

declare void @TSK_userYield() #2

; Function Attrs: noinline nounwind uwtable
define dso_local zeroext i8 @VDP_waitVBlank(i8 noundef zeroext %0) #3 {
  %2 = alloca i8, align 1
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i16, align 2
  %6 = alloca i16, align 2
  %7 = alloca i8, align 1
  %8 = alloca i8, align 1
  %9 = alloca i8, align 1
  store i8 %0, ptr %2, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  store ptr inttoptr (i64 12582916 to ptr), ptr %3, align 8, !tbaa !8
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %10 = load volatile i64, ptr @vtimer, align 8, !tbaa !10
  store i64 %10, ptr %4, align 8, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #4
  %11 = load volatile i16, ptr inttoptr (i64 12582920 to ptr), align 2, !tbaa !12
  %12 = zext i16 %11 to i32
  %13 = ashr i32 %12, 8
  %14 = trunc i32 %13 to i16
  store i16 %14, ptr %5, align 2, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 2, ptr %6) #4
  %15 = load ptr, ptr %3, align 8, !tbaa !8
  %16 = load volatile i16, ptr %15, align 2, !tbaa !12
  %17 = zext i16 %16 to i32
  %18 = and i32 %17, 8
  %19 = trunc i32 %18 to i16
  store i16 %19, ptr %6, align 2, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #4
  %20 = load i64, ptr @task_pc, align 8, !tbaa !10
  %21 = icmp ne i64 %20, 0
  %22 = zext i1 %21 to i32
  %23 = trunc i32 %22 to i8
  store i8 %23, ptr %7, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #4
  %24 = call zeroext i8 @VDP_isEnable()
  store i8 %24, ptr %8, align 1, !tbaa !5
  %25 = load i16, ptr %5, align 2, !tbaa !12
  store i16 %25, ptr @lastVCnt, align 2, !tbaa !12
  %26 = load i8, ptr %2, align 1, !tbaa !5
  %27 = zext i8 %26 to i32
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %29, label %49

29:                                               ; preds = %1
  %30 = load i16, ptr %6, align 2, !tbaa !12
  %31 = zext i16 %30 to i32
  %32 = icmp ne i32 %31, 0
  br i1 %32, label %33, label %49

33:                                               ; preds = %29
  %34 = load i8, ptr %8, align 1, !tbaa !5
  %35 = zext i8 %34 to i32
  %36 = icmp ne i32 %35, 0
  br i1 %36, label %37, label %49

37:                                               ; preds = %33
  %38 = load i8, ptr %7, align 1, !tbaa !5
  %39 = icmp ne i8 %38, 0
  br i1 %39, label %49, label %40

40:                                               ; preds = %37
  br label %41

41:                                               ; preds = %47, %40
  %42 = load ptr, ptr %3, align 8, !tbaa !8
  %43 = load volatile i16, ptr %42, align 2, !tbaa !12
  %44 = zext i16 %43 to i32
  %45 = and i32 %44, 8
  %46 = icmp ne i32 %45, 0
  br i1 %46, label %47, label %48

47:                                               ; preds = %41
  br label %41, !llvm.loop !14

48:                                               ; preds = %41
  br label %49

49:                                               ; preds = %48, %37, %33, %29, %1
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #4
  %50 = load i16, ptr %6, align 2, !tbaa !12
  %51 = load i16, ptr %5, align 2, !tbaa !12
  %52 = load i64, ptr %4, align 8, !tbaa !10
  %53 = call zeroext i8 @computeFrameCPULoad(i16 noundef zeroext %50, i16 noundef zeroext %51, i64 noundef %52)
  store i8 %53, ptr %9, align 1, !tbaa !5
  %54 = load i8, ptr %7, align 1, !tbaa !5
  %55 = icmp ne i8 %54, 0
  br i1 %55, label %56, label %57

56:                                               ; preds = %49
  call void @TSK_userYield()
  br label %67

57:                                               ; preds = %49
  br label %58

58:                                               ; preds = %65, %57
  %59 = load ptr, ptr %3, align 8, !tbaa !8
  %60 = load volatile i16, ptr %59, align 2, !tbaa !12
  %61 = zext i16 %60 to i32
  %62 = and i32 %61, 8
  %63 = icmp ne i32 %62, 0
  %64 = xor i1 %63, true
  br i1 %64, label %65, label %66

65:                                               ; preds = %58
  br label %58, !llvm.loop !16

66:                                               ; preds = %58
  br label %67

67:                                               ; preds = %66, %56
  %68 = load i8, ptr %9, align 1, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret i8 %68
}

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 = !{!"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 = !{!"long", !6, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"short", !6, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


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

@lastVCnt = external dso_local local_unnamed_addr global i16, align 2
@vtimer = external global i64, align 8
@task_pc = external local_unnamed_addr global i64, align 8

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @VDP_isEnable() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @computeFrameCPULoad(i16 noundef zeroext, i16 noundef zeroext, i64 noundef) local_unnamed_addr #0

declare void @TSK_userYield() local_unnamed_addr #1

; Function Attrs: noinline nounwind uwtable
define dso_local zeroext i8 @VDP_waitVBlank(i8 noundef zeroext %0) local_unnamed_addr #2 {
  %2 = load volatile i64, ptr @vtimer, align 8, !tbaa !5
  %3 = load volatile i16, ptr inttoptr (i64 12582920 to ptr), align 8, !tbaa !9
  %4 = lshr i16 %3, 8
  %5 = load volatile i16, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !9
  %6 = and i16 %5, 8
  %7 = load i64, ptr @task_pc, align 8, !tbaa !5
  %8 = icmp ne i64 %7, 0
  %9 = tail call zeroext i8 @VDP_isEnable()
  store i16 %4, ptr @lastVCnt, align 2, !tbaa !9
  %10 = icmp eq i8 %0, 0
  %11 = icmp eq i16 %6, 0
  %or.cond.not22 = select i1 %10, i1 true, i1 %11
  %12 = icmp eq i8 %9, 0
  %or.cond5.not19 = select i1 %or.cond.not22, i1 true, i1 %12
  %or.cond7 = select i1 %or.cond5.not19, i1 true, i1 %8
  br i1 %or.cond7, label %.loopexit25, label %.preheader24

.preheader24:                                     ; preds = %1, %.preheader24
  %13 = load volatile i16, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !9
  %14 = and i16 %13, 8
  %.not = icmp eq i16 %14, 0
  br i1 %.not, label %.loopexit25, label %.preheader24, !llvm.loop !11

.loopexit25:                                      ; preds = %.preheader24, %1
  %15 = tail call zeroext i8 @computeFrameCPULoad(i16 noundef zeroext %6, i16 noundef zeroext %4, i64 noundef %2)
  br i1 %8, label %16, label %.preheader

16:                                               ; preds = %.loopexit25
  tail call void @TSK_userYield() #3
  br label %.loopexit

.preheader:                                       ; preds = %.loopexit25, %.preheader
  %17 = load volatile i16, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !9
  %18 = and i16 %17, 8
  %.not23 = icmp eq i16 %18, 0
  br i1 %.not23, label %.preheader, label %.loopexit, !llvm.loop !13

.loopexit:                                        ; preds = %.preheader, %16
  ret i8 %15
}

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


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