; LLVM IR from Stephane-D/SGDK :: sample/fx/h-int/wobble/src (helpers)

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

%struct.MenuItem = type { i16, i16, ptr, %union.anon, i32 }
%union.anon = type { %struct.MenuItemS16 }
%struct.MenuItemS16 = type { ptr, i16, i16, i16 }

@menuItems = external hidden global ptr, align 8
@menuItemsNum = external hidden global i16, align 2
@menuLineBuffer = external hidden global [40 x i8], 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 void @resetMenu() #1 {
  %1 = alloca i16, align 2
  %2 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 2, ptr %1) #4
  store i16 0, ptr %1, align 2, !tbaa !5
  br label %3

3:                                                ; preds = %32, %0
  %4 = load i16, ptr %1, align 2, !tbaa !5
  %5 = zext i16 %4 to i32
  %6 = load i16, ptr @menuItemsNum, align 2, !tbaa !5
  %7 = zext i16 %6 to i32
  %8 = icmp slt i32 %5, %7
  br i1 %8, label %10, label %9

9:                                                ; preds = %3
  call void @llvm.lifetime.end.p0(i64 2, ptr %1) #4
  br label %35

10:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  %11 = load ptr, ptr @menuItems, align 8, !tbaa !9
  %12 = load i16, ptr %1, align 2, !tbaa !5
  %13 = zext i16 %12 to i32
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds %struct.MenuItem, ptr %11, i64 %14
  store ptr %15, ptr %2, align 8, !tbaa !9
  %16 = load ptr, ptr %2, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.MenuItem, ptr %16, i32 0, i32 2
  %18 = load ptr, ptr %17, align 8, !tbaa !11
  %19 = load ptr, ptr %2, align 8, !tbaa !9
  %20 = getelementptr inbounds %struct.MenuItem, ptr %19, i32 0, i32 3
  %21 = getelementptr inbounds %struct.MenuItemS16, ptr %20, i32 0, i32 0
  %22 = load ptr, ptr %21, align 8, !tbaa !14
  %23 = load i16, ptr %22, align 2, !tbaa !5
  %24 = sext i16 %23 to i32
  %25 = call i32 (ptr, ptr, ...) @sprintf(ptr noundef @menuLineBuffer, ptr noundef %18, i32 noundef %24) #4
  %26 = load ptr, ptr %2, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.MenuItem, ptr %26, i32 0, i32 0
  %28 = load i16, ptr %27, align 8, !tbaa !15
  %29 = load ptr, ptr %2, align 8, !tbaa !9
  %30 = getelementptr inbounds %struct.MenuItem, ptr %29, i32 0, i32 1
  %31 = load i16, ptr %30, align 2, !tbaa !16
  call void @VDP_drawText(ptr noundef @menuLineBuffer, i16 noundef zeroext %28, i16 noundef zeroext %31)
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  br label %32

32:                                               ; preds = %10
  %33 = load i16, ptr %1, align 2, !tbaa !5
  %34 = add i16 %33, 1
  store i16 %34, ptr %1, align 2, !tbaa !5
  br label %3, !llvm.loop !17

35:                                               ; preds = %9
  ret void
}

; Function Attrs: nounwind
declare i32 @sprintf(ptr noundef, ptr noundef, ...) #2

declare void @VDP_drawText(ptr noundef, i16 noundef zeroext, i16 noundef zeroext) #3

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 "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 = { "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 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"MenuItem", !6, i64 0, !6, i64 2, !10, i64 8, !7, i64 16, !13, i64 32}
!13 = !{!"int", !7, i64 0}
!14 = !{!7, !7, i64 0}
!15 = !{!12, !6, i64 0}
!16 = !{!12, !6, i64 2}
!17 = distinct !{!17, !18}
!18 = !{!"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"

%struct.MenuItem = type { i16, i16, ptr, %union.anon, i32 }
%union.anon = type { %struct.MenuItemS16 }
%struct.MenuItemS16 = type { ptr, i16, i16, i16 }

@menuItems = external hidden local_unnamed_addr global ptr, align 8
@menuItemsNum = external hidden local_unnamed_addr global i16, align 2
@menuLineBuffer = external hidden global [40 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local void @resetMenu() local_unnamed_addr #0 {
  %1 = load i16, ptr @menuItemsNum, align 2, !tbaa !5
  %.not = icmp eq i16 %1, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %0
  ret void

.lr.ph:                                           ; preds = %0, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  %2 = load ptr, ptr @menuItems, align 8, !tbaa !9
  %3 = getelementptr inbounds %struct.MenuItem, ptr %2, i64 %indvars.iv
  %4 = getelementptr inbounds i8, ptr %3, i64 8
  %5 = load ptr, ptr %4, align 8, !tbaa !11
  %6 = getelementptr inbounds i8, ptr %3, i64 16
  %7 = load ptr, ptr %6, align 8, !tbaa !14
  %8 = load i16, ptr %7, align 2, !tbaa !5
  %9 = sext i16 %8 to i32
  %10 = tail call i32 (ptr, ptr, ...) @sprintf(ptr noundef nonnull dereferenceable(1) @menuLineBuffer, ptr noundef nonnull dereferenceable(1) %5, i32 noundef %9) #3
  %11 = load i16, ptr %3, align 8, !tbaa !15
  %12 = getelementptr inbounds i8, ptr %3, i64 2
  %13 = load i16, ptr %12, align 2, !tbaa !16
  tail call void @VDP_drawText(ptr noundef nonnull @menuLineBuffer, i16 noundef zeroext %11, i16 noundef zeroext %13) #3
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %14 = load i16, ptr @menuItemsNum, align 2, !tbaa !5
  %15 = zext i16 %14 to i64
  %16 = icmp ult i64 %indvars.iv.next, %15
  br i1 %16, label %.lr.ph, label %._crit_edge, !llvm.loop !17
}

; Function Attrs: nofree nounwind
declare noundef i32 @sprintf(ptr noalias nocapture noundef writeonly, ptr nocapture noundef readonly, ...) local_unnamed_addr #1

declare void @VDP_drawText(ptr noundef, i16 noundef zeroext, i16 noundef zeroext) local_unnamed_addr #2

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

!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 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"MenuItem", !6, i64 0, !6, i64 2, !10, i64 8, !7, i64 16, !13, i64 32}
!13 = !{!"int", !7, i64 0}
!14 = !{!7, !7, i64 0}
!15 = !{!12, !6, i64 0}
!16 = !{!12, !6, i64 2}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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