; 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

; 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_readJoypad(i16 noundef zeroext %0) #1 {
  %2 = alloca i16, align 2
  %3 = alloca i16, align 2
  %4 = alloca i16, align 2
  %5 = alloca i16, align 2
  store i16 %0, ptr %3, align 2, !tbaa !5
  %6 = load i16, ptr %3, align 2, !tbaa !5
  %7 = zext i16 %6 to i32
  %8 = icmp eq i32 %7, 65535
  br i1 %8, label %9, label %27

9:                                                ; preds = %1
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #2
  store i16 0, ptr %5, align 2, !tbaa !5
  store i16 8, ptr %4, align 2, !tbaa !5
  br label %10

10:                                               ; preds = %14, %9
  %11 = load i16, ptr %4, align 2, !tbaa !5
  %12 = add i16 %11, -1
  store i16 %12, ptr %4, align 2, !tbaa !5
  %13 = icmp ne i16 %11, 0
  br i1 %13, label %14, label %25

14:                                               ; preds = %10
  %15 = load i16, ptr %4, align 2, !tbaa !5
  %16 = zext i16 %15 to i64
  %17 = getelementptr inbounds [8 x i16], ptr @joyState, i64 0, i64 %16
  %18 = load volatile i16, ptr %17, align 2, !tbaa !5
  %19 = zext i16 %18 to i32
  %20 = and i32 %19, 4095
  %21 = load i16, ptr %5, align 2, !tbaa !5
  %22 = zext i16 %21 to i32
  %23 = or i32 %22, %20
  %24 = trunc i32 %23 to i16
  store i16 %24, ptr %5, align 2, !tbaa !5
  br label %10, !llvm.loop !9

25:                                               ; preds = %10
  %26 = load i16, ptr %5, align 2, !tbaa !5
  store i16 %26, ptr %2, align 2
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #2
  br label %38

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

31:                                               ; preds = %27
  %32 = load i16, ptr %3, align 2, !tbaa !5
  %33 = zext i16 %32 to i64
  %34 = getelementptr inbounds [8 x i16], ptr @joyState, i64 0, i64 %33
  %35 = load volatile i16, ptr %34, align 2, !tbaa !5
  store i16 %35, ptr %2, align 2
  br label %38

36:                                               ; preds = %27
  br label %37

37:                                               ; preds = %36
  store i16 0, ptr %2, align 2
  br label %38

38:                                               ; preds = %37, %31, %25
  %39 = load i16, ptr %2, align 2
  ret i16 %39
}

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


; 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

; Function Attrs: mustprogress nofree norecurse nounwind willreturn memory(readwrite, argmem: none) uwtable
define dso_local zeroext i16 @JOY_readJoypad(i16 noundef zeroext %0) local_unnamed_addr #0 {
  %2 = icmp eq i16 %0, -1
  br i1 %2, label %.preheader.preheader, label %19

.preheader.preheader:                             ; preds = %1
  %3 = load volatile i16, ptr getelementptr inbounds (i8, ptr @joyState, i64 14), align 2, !tbaa !5
  %4 = load volatile i16, ptr getelementptr inbounds (i8, ptr @joyState, i64 12), align 4, !tbaa !5
  %5 = or i16 %4, %3
  %6 = load volatile i16, ptr getelementptr inbounds (i8, ptr @joyState, i64 10), align 2, !tbaa !5
  %7 = or i16 %6, %5
  %8 = load volatile i16, ptr getelementptr inbounds (i8, ptr @joyState, i64 8), align 8, !tbaa !5
  %9 = or i16 %8, %7
  %10 = load volatile i16, ptr getelementptr inbounds (i8, ptr @joyState, i64 6), align 2, !tbaa !5
  %11 = or i16 %10, %9
  %12 = load volatile i16, ptr getelementptr inbounds (i8, ptr @joyState, i64 4), align 4, !tbaa !5
  %13 = or i16 %12, %11
  %14 = load volatile i16, ptr getelementptr inbounds (i8, ptr @joyState, i64 2), align 2, !tbaa !5
  %15 = or i16 %14, %13
  %16 = load volatile i16, ptr @joyState, align 16, !tbaa !5
  %17 = or i16 %16, %15
  %18 = and i16 %17, 4095
  br label %.loopexit

19:                                               ; preds = %1
  %20 = icmp ult i16 %0, 8
  br i1 %20, label %21, label %.loopexit

21:                                               ; preds = %19
  %22 = zext nneg i16 %0 to i64
  %23 = getelementptr inbounds [8 x i16], ptr @joyState, i64 0, i64 %22
  %24 = load volatile i16, ptr %23, align 2, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %.preheader.preheader, %19, %21
  %.09 = phi i16 [ %24, %21 ], [ 0, %19 ], [ %18, %.preheader.preheader ]
  ret i16 %.09
}

attributes #0 = { mustprogress nofree norecurse nounwind willreturn memory(readwrite, argmem: none) 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" }

!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"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass