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

@joyEventCB = external hidden global ptr, align 8
@portSupport = external hidden global [2 x i8], align 1
@joyType = external hidden global [8 x i8], align 1
@joyState = external hidden global [8 x i16], align 16
@randomSeedSet = external global i8, align 1

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

declare void @SYS_disableInts() #1

declare void @SYS_enableInts() #1

; 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 void @JOY_update() #2 {
  %1 = alloca i16, align 2
  %2 = alloca i16, align 2
  %3 = alloca i16, align 2
  %4 = alloca i16, align 2
  %5 = alloca i16, align 2
  call void @llvm.lifetime.start.p0(i64 2, ptr %1) #4
  call void @llvm.lifetime.start.p0(i64 2, ptr %2) #4
  call void @llvm.lifetime.start.p0(i64 2, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #4
  call void @SYS_disableInts()
  %6 = load i8, ptr @portSupport, align 1, !tbaa !5
  %7 = zext i8 %6 to i16
  store i16 %7, ptr %1, align 2, !tbaa !8
  %8 = load i16, ptr %1, align 2, !tbaa !8
  %9 = zext i16 %8 to i32
  switch i32 %9, label %10 [
    i32 0, label %22
    i32 1, label %11
    i32 2, label %13
    i32 3, label %15
    i32 4, label %17
    i32 5, label %19
    i32 6, label %20
    i32 7, label %21
    i32 8, label %21
    i32 9, label %21
    i32 10, label %21
  ]

10:                                               ; preds = %0
  br label %22

11:                                               ; preds = %0
  %12 = call zeroext i16 @read3Btn(i16 noundef zeroext 0)
  store i16 %12, ptr %2, align 2, !tbaa !8
  br label %22

13:                                               ; preds = %0
  %14 = call zeroext i16 @read6Btn(i16 noundef zeroext 0)
  store i16 %14, ptr %2, align 2, !tbaa !8
  br label %22

15:                                               ; preds = %0
  %16 = call zeroext i16 @readMouse(i16 noundef zeroext 0)
  store i16 %16, ptr %2, align 2, !tbaa !8
  br label %22

17:                                               ; preds = %0
  %18 = call zeroext i16 @readTrackball(i16 noundef zeroext 0)
  store i16 %18, ptr %2, align 2, !tbaa !8
  br label %22

19:                                               ; preds = %0
  call void @readTeamPlayer(i16 noundef zeroext 0)
  br label %22

20:                                               ; preds = %0
  call void @readEa4WayPlay()
  br label %22

21:                                               ; preds = %0, %0, %0, %0
  call void @readLightgun(i16 noundef zeroext 0)
  br label %22

22:                                               ; preds = %21, %20, %19, %17, %15, %13, %11, %10, %0
  %23 = load i16, ptr %1, align 2, !tbaa !8
  %24 = zext i16 %23 to i32
  %25 = icmp sgt i32 %24, 0
  br i1 %25, label %26, label %57

26:                                               ; preds = %22
  %27 = load i16, ptr %1, align 2, !tbaa !8
  %28 = zext i16 %27 to i32
  %29 = icmp slt i32 %28, 5
  br i1 %29, label %30, label %57

30:                                               ; preds = %26
  %31 = load i16, ptr %2, align 2, !tbaa !8
  %32 = zext i16 %31 to i32
  %33 = and i32 %32, 4095
  %34 = trunc i32 %33 to i16
  store i16 %34, ptr %3, align 2, !tbaa !8
  %35 = load volatile i16, ptr @joyState, align 16, !tbaa !8
  %36 = zext i16 %35 to i32
  %37 = load i16, ptr %3, align 2, !tbaa !8
  %38 = zext i16 %37 to i32
  %39 = xor i32 %36, %38
  %40 = trunc i32 %39 to i16
  store i16 %40, ptr %4, align 2, !tbaa !8
  %41 = load i16, ptr %2, align 2, !tbaa !8
  %42 = zext i16 %41 to i32
  %43 = ashr i32 %42, 12
  %44 = trunc i32 %43 to i8
  store volatile i8 %44, ptr @joyType, align 1, !tbaa !5
  %45 = load i16, ptr %3, align 2, !tbaa !8
  store volatile i16 %45, ptr @joyState, align 16, !tbaa !8
  %46 = load ptr, ptr @joyEventCB, align 8, !tbaa !10
  %47 = icmp ne ptr %46, null
  br i1 %47, label %48, label %56

48:                                               ; preds = %30
  %49 = load i16, ptr %4, align 2, !tbaa !8
  %50 = zext i16 %49 to i32
  %51 = icmp ne i32 %50, 0
  br i1 %51, label %52, label %56

52:                                               ; preds = %48
  %53 = load ptr, ptr @joyEventCB, align 8, !tbaa !10
  %54 = load i16, ptr %4, align 2, !tbaa !8
  %55 = load i16, ptr %3, align 2, !tbaa !8
  call void %53(i16 noundef zeroext 0, i16 noundef zeroext %54, i16 noundef zeroext %55)
  br label %56

56:                                               ; preds = %52, %48, %30
  br label %57

57:                                               ; preds = %56, %26, %22
  %58 = load i8, ptr getelementptr inbounds ([2 x i8], ptr @portSupport, i64 0, i64 1), align 1, !tbaa !5
  %59 = zext i8 %58 to i16
  store i16 %59, ptr %1, align 2, !tbaa !8
  %60 = load i16, ptr %1, align 2, !tbaa !8
  %61 = zext i16 %60 to i32
  switch i32 %61, label %62 [
    i32 0, label %73
    i32 1, label %63
    i32 2, label %65
    i32 3, label %67
    i32 4, label %69
    i32 5, label %71
    i32 7, label %72
    i32 8, label %72
    i32 9, label %72
    i32 10, label %72
  ]

62:                                               ; preds = %57
  br label %73

63:                                               ; preds = %57
  %64 = call zeroext i16 @read3Btn(i16 noundef zeroext 1)
  store i16 %64, ptr %2, align 2, !tbaa !8
  br label %73

65:                                               ; preds = %57
  %66 = call zeroext i16 @read6Btn(i16 noundef zeroext 1)
  store i16 %66, ptr %2, align 2, !tbaa !8
  br label %73

67:                                               ; preds = %57
  %68 = call zeroext i16 @readMouse(i16 noundef zeroext 1)
  store i16 %68, ptr %2, align 2, !tbaa !8
  br label %73

69:                                               ; preds = %57
  %70 = call zeroext i16 @readTrackball(i16 noundef zeroext 1)
  store i16 %70, ptr %2, align 2, !tbaa !8
  br label %73

71:                                               ; preds = %57
  call void @readTeamPlayer(i16 noundef zeroext 1)
  br label %73

72:                                               ; preds = %57, %57, %57, %57
  call void @readLightgun(i16 noundef zeroext 1)
  br label %73

73:                                               ; preds = %72, %71, %69, %67, %65, %63, %62, %57
  %74 = load i16, ptr %1, align 2, !tbaa !8
  %75 = zext i16 %74 to i32
  %76 = icmp sgt i32 %75, 0
  br i1 %76, label %77, label %108

77:                                               ; preds = %73
  %78 = load i16, ptr %1, align 2, !tbaa !8
  %79 = zext i16 %78 to i32
  %80 = icmp slt i32 %79, 5
  br i1 %80, label %81, label %108

81:                                               ; preds = %77
  %82 = load i16, ptr %2, align 2, !tbaa !8
  %83 = zext i16 %82 to i32
  %84 = and i32 %83, 4095
  %85 = trunc i32 %84 to i16
  store i16 %85, ptr %3, align 2, !tbaa !8
  %86 = load volatile i16, ptr getelementptr inbounds ([8 x i16], ptr @joyState, i64 0, i64 1), align 2, !tbaa !8
  %87 = zext i16 %86 to i32
  %88 = load i16, ptr %3, align 2, !tbaa !8
  %89 = zext i16 %88 to i32
  %90 = xor i32 %87, %89
  %91 = trunc i32 %90 to i16
  store i16 %91, ptr %4, align 2, !tbaa !8
  %92 = load i16, ptr %2, align 2, !tbaa !8
  %93 = zext i16 %92 to i32
  %94 = ashr i32 %93, 12
  %95 = trunc i32 %94 to i8
  store volatile i8 %95, ptr getelementptr inbounds ([8 x i8], ptr @joyType, i64 0, i64 1), align 1, !tbaa !5
  %96 = load i16, ptr %3, align 2, !tbaa !8
  store volatile i16 %96, ptr getelementptr inbounds ([8 x i16], ptr @joyState, i64 0, i64 1), align 2, !tbaa !8
  %97 = load ptr, ptr @joyEventCB, align 8, !tbaa !10
  %98 = icmp ne ptr %97, null
  br i1 %98, label %99, label %107

99:                                               ; preds = %81
  %100 = load i16, ptr %4, align 2, !tbaa !8
  %101 = zext i16 %100 to i32
  %102 = icmp ne i32 %101, 0
  br i1 %102, label %103, label %107

103:                                              ; preds = %99
  %104 = load ptr, ptr @joyEventCB, align 8, !tbaa !10
  %105 = load i16, ptr %4, align 2, !tbaa !8
  %106 = load i16, ptr %3, align 2, !tbaa !8
  call void %104(i16 noundef zeroext 1, i16 noundef zeroext %105, i16 noundef zeroext %106)
  br label %107

107:                                              ; preds = %103, %99, %81
  br label %108

108:                                              ; preds = %107, %77, %73
  call void @SYS_enableInts()
  %109 = load i8, ptr @randomSeedSet, align 1, !tbaa !5
  %110 = icmp ne i8 %109, 0
  br i1 %110, label %131, label %111

111:                                              ; preds = %108
  %112 = load volatile i16, ptr @joyState, align 16, !tbaa !8
  %113 = zext i16 %112 to i32
  %114 = icmp ne i32 %113, 0
  br i1 %114, label %119, label %115

115:                                              ; preds = %111
  %116 = load volatile i16, ptr getelementptr inbounds ([8 x i16], ptr @joyState, i64 0, i64 1), align 2, !tbaa !8
  %117 = zext i16 %116 to i32
  %118 = icmp ne i32 %117, 0
  br i1 %118, label %119, label %130

119:                                              ; preds = %115, %111
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #4
  %120 = call i64 @getTick()
  %121 = trunc i64 %120 to i16
  store i16 %121, ptr %5, align 2, !tbaa !8
  %122 = load i16, ptr %5, align 2, !tbaa !8
  %123 = zext i16 %122 to i32
  %124 = shl i32 %123, 0
  %125 = load i16, ptr %5, align 2, !tbaa !8
  %126 = zext i16 %125 to i32
  %127 = shl i32 %126, 8
  %128 = xor i32 %124, %127
  %129 = trunc i32 %128 to i16
  call void @setRandomSeed(i16 noundef zeroext %129)
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #4
  br label %130

130:                                              ; preds = %119, %115
  br label %131

131:                                              ; preds = %130, %108
  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 2, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %1) #4
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @read3Btn(i16 noundef zeroext) #3

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @read6Btn(i16 noundef zeroext) #3

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @readMouse(i16 noundef zeroext) #3

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @readTrackball(i16 noundef zeroext) #3

; Function Attrs: nounwind uwtable
declare hidden void @readTeamPlayer(i16 noundef zeroext) #3

; Function Attrs: nounwind uwtable
declare hidden void @readLightgun(i16 noundef zeroext) #3

declare i64 @getTick() #1

declare void @setRandomSeed(i16 noundef zeroext) #1

; Function Attrs: nounwind uwtable
declare hidden void @readEa4WayPlay() #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 = { 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 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 = !{!"short", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !6, i64 0}


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

@joyEventCB = external hidden local_unnamed_addr global ptr, align 8
@portSupport = external hidden local_unnamed_addr global [2 x i8], align 1
@joyType = external hidden global [8 x i8], align 1
@joyState = external hidden global [8 x i16], align 16
@randomSeedSet = external local_unnamed_addr global i8, align 1

declare void @SYS_disableInts() local_unnamed_addr #0

declare void @SYS_enableInts() local_unnamed_addr #0

; Function Attrs: noinline nounwind uwtable
define dso_local void @JOY_update() local_unnamed_addr #1 {
  tail call void @SYS_disableInts() #3
  %1 = load i8, ptr @portSupport, align 1, !tbaa !5
  switch i8 %1, label %23 [
    i8 10, label %12
    i8 1, label %2
    i8 2, label %4
    i8 3, label %6
    i8 4, label %8
    i8 5, label %10
    i8 6, label %11
    i8 7, label %12
    i8 8, label %12
    i8 9, label %12
  ]

2:                                                ; preds = %0
  %3 = tail call zeroext i16 @read3Btn(i16 noundef zeroext 0)
  br label %13

4:                                                ; preds = %0
  %5 = tail call zeroext i16 @read6Btn(i16 noundef zeroext 0)
  br label %13

6:                                                ; preds = %0
  %7 = tail call zeroext i16 @readMouse(i16 noundef zeroext 0)
  br label %13

8:                                                ; preds = %0
  %9 = tail call zeroext i16 @readTrackball(i16 noundef zeroext 0)
  br label %13

10:                                               ; preds = %0
  tail call void @readTeamPlayer(i16 noundef zeroext 0)
  br label %23

11:                                               ; preds = %0
  tail call void @readEa4WayPlay()
  br label %23

12:                                               ; preds = %0, %0, %0, %0
  tail call void @readLightgun(i16 noundef zeroext 0)
  br label %23

13:                                               ; preds = %8, %6, %4, %2
  %.0.ph = phi i16 [ %3, %2 ], [ %5, %4 ], [ %7, %6 ], [ %9, %8 ]
  %14 = and i16 %.0.ph, 4095
  %15 = load volatile i16, ptr @joyState, align 16, !tbaa !8
  %16 = lshr i16 %.0.ph, 12
  %17 = trunc nuw nsw i16 %16 to i8
  store volatile i8 %17, ptr @joyType, align 1, !tbaa !5
  store volatile i16 %14, ptr @joyState, align 16, !tbaa !8
  %18 = load ptr, ptr @joyEventCB, align 8, !tbaa !10
  %19 = icmp ne ptr %18, null
  %20 = icmp ne i16 %15, %14
  %or.cond5 = select i1 %19, i1 %20, i1 false
  br i1 %or.cond5, label %21, label %23

21:                                               ; preds = %13
  %22 = xor i16 %15, %14
  tail call void %18(i16 noundef zeroext 0, i16 noundef zeroext %22, i16 noundef zeroext %14) #3
  br label %23

23:                                               ; preds = %10, %11, %12, %0, %13, %21
  %24 = load i8, ptr getelementptr inbounds (i8, ptr @portSupport, i64 1), align 1, !tbaa !5
  switch i8 %24, label %45 [
    i8 10, label %34
    i8 1, label %25
    i8 2, label %27
    i8 3, label %29
    i8 4, label %31
    i8 5, label %33
    i8 7, label %34
    i8 8, label %34
    i8 9, label %34
  ]

25:                                               ; preds = %23
  %26 = tail call zeroext i16 @read3Btn(i16 noundef zeroext 1)
  br label %35

27:                                               ; preds = %23
  %28 = tail call zeroext i16 @read6Btn(i16 noundef zeroext 1)
  br label %35

29:                                               ; preds = %23
  %30 = tail call zeroext i16 @readMouse(i16 noundef zeroext 1)
  br label %35

31:                                               ; preds = %23
  %32 = tail call zeroext i16 @readTrackball(i16 noundef zeroext 1)
  br label %35

33:                                               ; preds = %23
  tail call void @readTeamPlayer(i16 noundef zeroext 1)
  br label %45

34:                                               ; preds = %23, %23, %23, %23
  tail call void @readLightgun(i16 noundef zeroext 1)
  br label %45

35:                                               ; preds = %31, %29, %27, %25
  %.1.ph = phi i16 [ %26, %25 ], [ %28, %27 ], [ %30, %29 ], [ %32, %31 ]
  %36 = and i16 %.1.ph, 4095
  %37 = load volatile i16, ptr getelementptr inbounds (i8, ptr @joyState, i64 2), align 2, !tbaa !8
  %38 = lshr i16 %.1.ph, 12
  %39 = trunc nuw nsw i16 %38 to i8
  store volatile i8 %39, ptr getelementptr inbounds (i8, ptr @joyType, i64 1), align 1, !tbaa !5
  store volatile i16 %36, ptr getelementptr inbounds (i8, ptr @joyState, i64 2), align 2, !tbaa !8
  %40 = load ptr, ptr @joyEventCB, align 8, !tbaa !10
  %41 = icmp ne ptr %40, null
  %42 = icmp ne i16 %37, %36
  %or.cond11 = select i1 %41, i1 %42, i1 false
  br i1 %or.cond11, label %43, label %45

43:                                               ; preds = %35
  %44 = xor i16 %37, %36
  tail call void %40(i16 noundef zeroext 1, i16 noundef zeroext %44, i16 noundef zeroext %36) #3
  br label %45

45:                                               ; preds = %33, %34, %23, %35, %43
  tail call void @SYS_enableInts() #3
  %46 = load i8, ptr @randomSeedSet, align 1, !tbaa !5
  %.not = icmp eq i8 %46, 0
  br i1 %.not, label %47, label %56

47:                                               ; preds = %45
  %48 = load volatile i16, ptr @joyState, align 16, !tbaa !8
  %.not35 = icmp eq i16 %48, 0
  br i1 %.not35, label %49, label %51

49:                                               ; preds = %47
  %50 = load volatile i16, ptr getelementptr inbounds (i8, ptr @joyState, i64 2), align 2, !tbaa !8
  %.not36 = icmp eq i16 %50, 0
  br i1 %.not36, label %56, label %51

51:                                               ; preds = %49, %47
  %52 = tail call i64 @getTick() #3
  %53 = trunc i64 %52 to i16
  %54 = shl i16 %53, 8
  %55 = xor i16 %54, %53
  tail call void @setRandomSeed(i16 noundef zeroext %55) #3
  br label %56

56:                                               ; preds = %49, %51, %45
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @read3Btn(i16 noundef zeroext) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @read6Btn(i16 noundef zeroext) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @readMouse(i16 noundef zeroext) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @readTrackball(i16 noundef zeroext) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden void @readTeamPlayer(i16 noundef zeroext) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden void @readLightgun(i16 noundef zeroext) local_unnamed_addr #2

declare i64 @getTick() local_unnamed_addr #0

declare void @setRandomSeed(i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @readEa4WayPlay() 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 = { 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 = { 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 = !{!9, !9, i64 0}
!9 = !{!"short", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !6, i64 0}


; Effective O3 passes: AggressiveInstCombinePass,CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass,TailCallElimPass