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

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

@joyState = external hidden global [8 x i16], align 16

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

declare zeroext i8 @SYS_doVBlankProcess() #1

; 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
define dso_local zeroext i16 @JOY_waitPressTime(i16 noundef zeroext %0, i16 noundef zeroext %1, i16 noundef zeroext %2) #2 {
  %4 = alloca i16, align 2
  %5 = alloca i16, align 2
  %6 = alloca i16, align 2
  %7 = alloca i16, align 2
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i16, align 2
  %11 = alloca i16, align 2
  %12 = alloca i32, align 4
  store i16 %0, ptr %5, align 2, !tbaa !5
  store i16 %1, ptr %6, align 2, !tbaa !5
  store i16 %2, ptr %7, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %13 = call i64 @getTime(i16 noundef zeroext 1)
  %14 = shl i64 %13, 2
  store i64 %14, ptr %9, align 8, !tbaa !9
  %15 = load i16, ptr %7, align 2, !tbaa !5
  %16 = icmp ne i16 %15, 0
  br i1 %16, label %17, label %22

17:                                               ; preds = %3
  %18 = load i64, ptr %9, align 8, !tbaa !9
  %19 = load i16, ptr %7, align 2, !tbaa !5
  %20 = zext i16 %19 to i64
  %21 = add i64 %18, %20
  store i64 %21, ptr %8, align 8, !tbaa !9
  br label %23

22:                                               ; preds = %3
  store i64 4294967295, ptr %8, align 8, !tbaa !9
  br label %23

23:                                               ; preds = %22, %17
  br label %24

24:                                               ; preds = %84, %23
  %25 = load i64, ptr %9, align 8, !tbaa !9
  %26 = load i64, ptr %8, align 8, !tbaa !9
  %27 = icmp ult i64 %25, %26
  br i1 %27, label %28, label %85

28:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #4
  %29 = load i64, ptr %8, align 8, !tbaa !9
  %30 = load i64, ptr %9, align 8, !tbaa !9
  %31 = sub i64 %29, %30
  %32 = icmp uge i64 %31, 20
  br i1 %32, label %33, label %35

33:                                               ; preds = %28
  %34 = call zeroext i8 @SYS_doVBlankProcess()
  br label %36

35:                                               ; preds = %28
  call void @JOY_update()
  br label %36

36:                                               ; preds = %35, %33
  %37 = load i16, ptr %5, align 2, !tbaa !5
  %38 = zext i16 %37 to i32
  %39 = icmp eq i32 %38, 65535
  br i1 %39, label %40, label %64

40:                                               ; preds = %36
  call void @llvm.lifetime.start.p0(i64 2, ptr %11) #4
  store i16 8, ptr %11, align 2, !tbaa !5
  br label %41

41:                                               ; preds = %59, %40
  %42 = load i16, ptr %11, align 2, !tbaa !5
  %43 = add i16 %42, -1
  store i16 %43, ptr %11, align 2, !tbaa !5
  %44 = icmp ne i16 %42, 0
  br i1 %44, label %45, label %60

45:                                               ; preds = %41
  %46 = load i16, ptr %11, align 2, !tbaa !5
  %47 = zext i16 %46 to i64
  %48 = getelementptr inbounds [8 x i16], ptr @joyState, i64 0, i64 %47
  %49 = load volatile i16, ptr %48, align 2, !tbaa !5
  %50 = zext i16 %49 to i32
  %51 = load i16, ptr %6, align 2, !tbaa !5
  %52 = zext i16 %51 to i32
  %53 = and i32 %50, %52
  %54 = trunc i32 %53 to i16
  store i16 %54, ptr %10, align 2, !tbaa !5
  %55 = load i16, ptr %10, align 2, !tbaa !5
  %56 = icmp ne i16 %55, 0
  br i1 %56, label %57, label %59

57:                                               ; preds = %45
  %58 = load i16, ptr %10, align 2, !tbaa !5
  store i16 %58, ptr %4, align 2
  store i32 1, ptr %12, align 4
  br label %61

59:                                               ; preds = %45
  br label %41, !llvm.loop !11

60:                                               ; preds = %41
  store i32 0, ptr %12, align 4
  br label %61

61:                                               ; preds = %60, %57
  call void @llvm.lifetime.end.p0(i64 2, ptr %11) #4
  %62 = load i32, ptr %12, align 4
  switch i32 %62, label %82 [
    i32 0, label %63
  ]

63:                                               ; preds = %61
  br label %79

64:                                               ; preds = %36
  %65 = load i16, ptr %5, align 2, !tbaa !5
  %66 = zext i16 %65 to i64
  %67 = getelementptr inbounds [8 x i16], ptr @joyState, i64 0, i64 %66
  %68 = load volatile i16, ptr %67, align 2, !tbaa !5
  %69 = zext i16 %68 to i32
  %70 = load i16, ptr %6, align 2, !tbaa !5
  %71 = zext i16 %70 to i32
  %72 = and i32 %69, %71
  %73 = trunc i32 %72 to i16
  store i16 %73, ptr %10, align 2, !tbaa !5
  %74 = load i16, ptr %10, align 2, !tbaa !5
  %75 = icmp ne i16 %74, 0
  br i1 %75, label %76, label %78

76:                                               ; preds = %64
  %77 = load i16, ptr %10, align 2, !tbaa !5
  store i16 %77, ptr %4, align 2
  store i32 1, ptr %12, align 4
  br label %82

78:                                               ; preds = %64
  br label %79

79:                                               ; preds = %78, %63
  %80 = call i64 @getTime(i16 noundef zeroext 1)
  %81 = shl i64 %80, 2
  store i64 %81, ptr %9, align 8, !tbaa !9
  store i32 0, ptr %12, align 4
  br label %82

82:                                               ; preds = %79, %76, %61
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #4
  %83 = load i32, ptr %12, align 4
  switch i32 %83, label %86 [
    i32 0, label %84
  ]

84:                                               ; preds = %82
  br label %24, !llvm.loop !13

85:                                               ; preds = %24
  store i16 0, ptr %4, align 2
  store i32 1, ptr %12, align 4
  br label %86

86:                                               ; preds = %85, %82
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %87 = load i16, ptr %4, align 2
  ret i16 %87
}

declare i64 @getTime(i16 noundef zeroext) #1

; Function Attrs: noinline nounwind uwtable
declare dso_local void @JOY_update() #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

@joyState = external hidden global [8 x i16], align 16

declare zeroext i8 @SYS_doVBlankProcess() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local zeroext i16 @JOY_waitPressTime(i16 noundef zeroext %0, i16 noundef zeroext %1, i16 noundef zeroext %2) local_unnamed_addr #1 {
  %4 = tail call i64 @getTime(i16 noundef zeroext 1) #3
  %5 = shl i64 %4, 2
  %.not = icmp eq i16 %2, 0
  %6 = zext i16 %2 to i64
  %7 = add i64 %5, %6
  %.024 = select i1 %.not, i64 4294967295, i64 %7
  %8 = icmp ult i64 %5, %.024
  br i1 %8, label %.lr.ph, label %.thread34

.lr.ph:                                           ; preds = %3
  %9 = icmp eq i16 %0, -1
  %10 = zext i16 %0 to i64
  %11 = getelementptr inbounds [8 x i16], ptr @joyState, i64 0, i64 %10
  br i1 %9, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph, %.thread.loopexit.us
  %.02239.us = phi i64 [ %41, %.thread.loopexit.us ], [ %5, %.lr.ph ]
  %12 = sub i64 %.024, %.02239.us
  %13 = icmp ugt i64 %12, 19
  br i1 %13, label %15, label %14

14:                                               ; preds = %.lr.ph.split.us
  tail call void @JOY_update()
  br label %.preheader.us

15:                                               ; preds = %.lr.ph.split.us
  %16 = tail call zeroext i8 @SYS_doVBlankProcess() #3
  br label %.preheader.us

.preheader.us:                                    ; preds = %15, %14
  %17 = load volatile i16, ptr getelementptr inbounds (i8, ptr @joyState, i64 14), align 2, !tbaa !5
  %18 = and i16 %17, %1
  %.not32.us = icmp eq i16 %18, 0
  br i1 %.not32.us, label %19, label %.thread34, !llvm.loop !9

19:                                               ; preds = %.preheader.us
  %20 = load volatile i16, ptr getelementptr inbounds (i8, ptr @joyState, i64 12), align 4, !tbaa !5
  %21 = and i16 %20, %1
  %.not32.us.1 = icmp eq i16 %21, 0
  br i1 %.not32.us.1, label %22, label %.thread34, !llvm.loop !9

22:                                               ; preds = %19
  %23 = load volatile i16, ptr getelementptr inbounds (i8, ptr @joyState, i64 10), align 2, !tbaa !5
  %24 = and i16 %23, %1
  %.not32.us.2 = icmp eq i16 %24, 0
  br i1 %.not32.us.2, label %25, label %.thread34, !llvm.loop !9

25:                                               ; preds = %22
  %26 = load volatile i16, ptr getelementptr inbounds (i8, ptr @joyState, i64 8), align 8, !tbaa !5
  %27 = and i16 %26, %1
  %.not32.us.3 = icmp eq i16 %27, 0
  br i1 %.not32.us.3, label %28, label %.thread34, !llvm.loop !9

28:                                               ; preds = %25
  %29 = load volatile i16, ptr getelementptr inbounds (i8, ptr @joyState, i64 6), align 2, !tbaa !5
  %30 = and i16 %29, %1
  %.not32.us.4 = icmp eq i16 %30, 0
  br i1 %.not32.us.4, label %31, label %.thread34, !llvm.loop !9

31:                                               ; preds = %28
  %32 = load volatile i16, ptr getelementptr inbounds (i8, ptr @joyState, i64 4), align 4, !tbaa !5
  %33 = and i16 %32, %1
  %.not32.us.5 = icmp eq i16 %33, 0
  br i1 %.not32.us.5, label %34, label %.thread34, !llvm.loop !9

34:                                               ; preds = %31
  %35 = load volatile i16, ptr getelementptr inbounds (i8, ptr @joyState, i64 2), align 2, !tbaa !5
  %36 = and i16 %35, %1
  %.not32.us.6 = icmp eq i16 %36, 0
  br i1 %.not32.us.6, label %37, label %.thread34, !llvm.loop !9

37:                                               ; preds = %34
  %38 = load volatile i16, ptr @joyState, align 16, !tbaa !5
  %39 = and i16 %38, %1
  %.not32.us.7 = icmp eq i16 %39, 0
  br i1 %.not32.us.7, label %.thread.loopexit.us, label %.thread34, !llvm.loop !9

.thread.loopexit.us:                              ; preds = %37
  %40 = tail call i64 @getTime(i16 noundef zeroext 1) #3
  %41 = shl i64 %40, 2
  %42 = icmp ult i64 %41, %.024
  br i1 %42, label %.lr.ph.split.us, label %.thread34, !llvm.loop !11

.lr.ph.split:                                     ; preds = %.lr.ph, %.thread
  %.02239 = phi i64 [ %52, %.thread ], [ %5, %.lr.ph ]
  %43 = sub i64 %.024, %.02239
  %44 = icmp ugt i64 %43, 19
  br i1 %44, label %45, label %47

45:                                               ; preds = %.lr.ph.split
  %46 = tail call zeroext i8 @SYS_doVBlankProcess() #3
  br label %48

47:                                               ; preds = %.lr.ph.split
  tail call void @JOY_update()
  br label %48

48:                                               ; preds = %47, %45
  %49 = load volatile i16, ptr %11, align 2, !tbaa !5
  %50 = and i16 %49, %1
  %.not30 = icmp eq i16 %50, 0
  br i1 %.not30, label %.thread, label %.thread34

.thread:                                          ; preds = %48
  %51 = tail call i64 @getTime(i16 noundef zeroext 1) #3
  %52 = shl i64 %51, 2
  %53 = icmp ult i64 %52, %.024
  br i1 %53, label %.lr.ph.split, label %.thread34, !llvm.loop !11

.thread34:                                        ; preds = %.thread, %48, %.thread.loopexit.us, %.preheader.us, %19, %22, %25, %28, %31, %34, %37, %3
  %.4 = phi i16 [ 0, %3 ], [ 0, %.thread.loopexit.us ], [ %18, %.preheader.us ], [ %21, %19 ], [ %24, %22 ], [ %27, %25 ], [ %30, %28 ], [ %33, %31 ], [ %36, %34 ], [ %39, %37 ], [ 0, %.thread ], [ %50, %48 ]
  ret i16 %.4
}

declare i64 @getTime(i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: noinline nounwind uwtable
declare dso_local void @JOY_update() local_unnamed_addr #2

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


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass