; 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: noinline nounwind uwtable
define dso_local zeroext i8 @VDP_waitVInt() #1 {
  %1 = alloca i8, align 1
  %2 = alloca i64, align 8
  %3 = alloca i16, align 2
  %4 = alloca i16, align 2
  %5 = alloca i8, align 1
  %6 = call zeroext i8 @SYS_isInVInt()
  %7 = icmp ne i8 %6, 0
  br i1 %7, label %8, label %9

8:                                                ; preds = %0
  store i8 0, ptr %1, align 1
  br label %36

9:                                                ; preds = %0
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  %10 = load volatile i64, ptr @vtimer, align 8, !tbaa !5
  store i64 %10, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %3) #4
  %11 = load volatile i16, ptr inttoptr (i64 12582920 to ptr), align 2, !tbaa !9
  %12 = zext i16 %11 to i32
  %13 = ashr i32 %12, 8
  %14 = trunc i32 %13 to i16
  store i16 %14, ptr %3, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #4
  %15 = load volatile i16, ptr inttoptr (i64 12582916 to ptr), align 2, !tbaa !9
  %16 = zext i16 %15 to i32
  %17 = and i32 %16, 8
  %18 = trunc i32 %17 to i16
  store i16 %18, ptr %4, align 2, !tbaa !9
  %19 = load i16, ptr %3, align 2, !tbaa !9
  store i16 %19, ptr @lastVCnt, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #4
  %20 = load i16, ptr %4, align 2, !tbaa !9
  %21 = load i16, ptr %3, align 2, !tbaa !9
  %22 = load i64, ptr %2, align 8, !tbaa !5
  %23 = call zeroext i8 @computeFrameCPULoad(i16 noundef zeroext %20, i16 noundef zeroext %21, i64 noundef %22)
  store i8 %23, ptr %5, align 1, !tbaa !11
  %24 = load i64, ptr @task_pc, align 8, !tbaa !5
  %25 = icmp ne i64 %24, 0
  br i1 %25, label %26, label %27

26:                                               ; preds = %9
  call void @TSK_userYield()
  br label %34

27:                                               ; preds = %9
  br label %28

28:                                               ; preds = %32, %27
  %29 = load volatile i64, ptr @vtimer, align 8, !tbaa !5
  %30 = load i64, ptr %2, align 8, !tbaa !5
  %31 = icmp eq i64 %29, %30
  br i1 %31, label %32, label %33

32:                                               ; preds = %28
  br label %28, !llvm.loop !12

33:                                               ; preds = %28
  br label %34

34:                                               ; preds = %33, %26
  %35 = load i8, ptr %5, align 1, !tbaa !11
  store i8 %35, ptr %1, align 1
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  br label %36

36:                                               ; preds = %34, %8
  %37 = load i8, ptr %1, align 1
  ret i8 %37
}

declare zeroext i8 @SYS_isInVInt() #2

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

declare void @TSK_userYield() #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = !{!"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 = !{!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"

@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: noinline nounwind uwtable
define dso_local zeroext i8 @VDP_waitVInt() local_unnamed_addr #0 {
  %1 = tail call zeroext i8 @SYS_isInVInt() #3
  %.not = icmp eq i8 %1, 0
  br i1 %.not, label %2, label %.loopexit

2:                                                ; preds = %0
  %3 = load volatile i64, ptr @vtimer, align 8, !tbaa !5
  %4 = load volatile i16, ptr inttoptr (i64 12582920 to ptr), align 8, !tbaa !9
  %5 = lshr i16 %4, 8
  %6 = load volatile i16, ptr inttoptr (i64 12582916 to ptr), align 4, !tbaa !9
  %7 = and i16 %6, 8
  store i16 %5, ptr @lastVCnt, align 2, !tbaa !9
  %8 = tail call zeroext i8 @computeFrameCPULoad(i16 noundef zeroext %7, i16 noundef zeroext %5, i64 noundef %3)
  %9 = load i64, ptr @task_pc, align 8, !tbaa !5
  %.not7 = icmp eq i64 %9, 0
  br i1 %.not7, label %.preheader, label %10

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

.preheader:                                       ; preds = %2, %.preheader
  %11 = load volatile i64, ptr @vtimer, align 8, !tbaa !5
  %12 = icmp eq i64 %11, %3
  br i1 %12, label %.preheader, label %.loopexit, !llvm.loop !11

.loopexit:                                        ; preds = %.preheader, %10, %0
  %.0 = phi i8 [ 0, %0 ], [ %8, %10 ], [ %8, %.preheader ]
  ret i8 %.0
}

declare zeroext i8 @SYS_isInVInt() local_unnamed_addr #1

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

declare void @TSK_userYield() local_unnamed_addr #1

attributes #0 = { 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 #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 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"}


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