; LLVM IR from Stephane-D/SGDK :: tools/xgmtool/src (ym2612)

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

@duals = external hidden constant [7 x [2 x i32]], 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 ptr @YM2612_getDualReg(i32 noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %32, %1
  %8 = load i32, ptr %4, align 4, !tbaa !5
  %9 = icmp slt i32 %8, 7
  br i1 %9, label %10, label %35

10:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [7 x [2 x i32]], ptr @duals, i64 0, i64 %12
  %14 = getelementptr inbounds [2 x i32], ptr %13, i64 0, i64 0
  store ptr %14, ptr %5, align 8, !tbaa !9
  %15 = load ptr, ptr %5, align 8, !tbaa !9
  %16 = getelementptr inbounds i32, ptr %15, i64 0
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = load i32, ptr %3, align 4, !tbaa !5
  %19 = icmp eq i32 %17, %18
  br i1 %19, label %26, label %20

20:                                               ; preds = %10
  %21 = load ptr, ptr %5, align 8, !tbaa !9
  %22 = getelementptr inbounds i32, ptr %21, i64 1
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = icmp eq i32 %23, %24
  br i1 %25, label %26, label %28

26:                                               ; preds = %20, %10
  %27 = load ptr, ptr %5, align 8, !tbaa !9
  store ptr %27, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %29

28:                                               ; preds = %20
  store i32 0, ptr %6, align 4
  br label %29

29:                                               ; preds = %28, %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  %30 = load i32, ptr %6, align 4
  switch i32 %30, label %36 [
    i32 0, label %31
  ]

31:                                               ; preds = %29
  br label %32

32:                                               ; preds = %31
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr %4, align 4, !tbaa !5
  br label %7, !llvm.loop !11

35:                                               ; preds = %7
  store ptr null, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %36

36:                                               ; preds = %35, %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %37 = load ptr, ptr %2, align 8
  ret ptr %37
}

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 = !{!"int", !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 = distinct !{!11, !12}
!12 = !{!"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"

@duals = external hidden constant [7 x [2 x i32]], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local ptr @YM2612_getDualReg(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @duals, align 16, !tbaa !5
  %3 = icmp eq i32 %2, %0
  %4 = load i32, ptr getelementptr inbounds (i8, ptr @duals, i64 4), align 4
  %.not = icmp eq i32 %4, %0
  %or.cond23 = select i1 %3, i1 true, i1 %.not
  br i1 %or.cond23, label %.thread, label %5

5:                                                ; preds = %1
  %6 = load i32, ptr getelementptr inbounds (i8, ptr @duals, i64 8), align 8, !tbaa !5
  %7 = icmp eq i32 %6, %0
  %8 = load i32, ptr getelementptr inbounds (i8, ptr @duals, i64 12), align 4
  %.not.1 = icmp eq i32 %8, %0
  %or.cond = select i1 %7, i1 true, i1 %.not.1
  br i1 %or.cond, label %.thread, label %9

9:                                                ; preds = %5
  %10 = load i32, ptr getelementptr inbounds (i8, ptr @duals, i64 16), align 16, !tbaa !5
  %11 = icmp eq i32 %10, %0
  %12 = load i32, ptr getelementptr inbounds (i8, ptr @duals, i64 20), align 4
  %.not.2 = icmp eq i32 %12, %0
  %or.cond18 = select i1 %11, i1 true, i1 %.not.2
  br i1 %or.cond18, label %.thread, label %13

13:                                               ; preds = %9
  %14 = load i32, ptr getelementptr inbounds (i8, ptr @duals, i64 24), align 8, !tbaa !5
  %15 = icmp eq i32 %14, %0
  %16 = load i32, ptr getelementptr inbounds (i8, ptr @duals, i64 28), align 4
  %.not.3 = icmp eq i32 %16, %0
  %or.cond19 = select i1 %15, i1 true, i1 %.not.3
  br i1 %or.cond19, label %.thread, label %17

17:                                               ; preds = %13
  %18 = load i32, ptr getelementptr inbounds (i8, ptr @duals, i64 32), align 16, !tbaa !5
  %19 = icmp eq i32 %18, %0
  %20 = load i32, ptr getelementptr inbounds (i8, ptr @duals, i64 36), align 4
  %.not.4 = icmp eq i32 %20, %0
  %or.cond20 = select i1 %19, i1 true, i1 %.not.4
  br i1 %or.cond20, label %.thread, label %21

21:                                               ; preds = %17
  %22 = load i32, ptr getelementptr inbounds (i8, ptr @duals, i64 40), align 8, !tbaa !5
  %23 = icmp eq i32 %22, %0
  %24 = load i32, ptr getelementptr inbounds (i8, ptr @duals, i64 44), align 4
  %.not.5 = icmp eq i32 %24, %0
  %or.cond21 = select i1 %23, i1 true, i1 %.not.5
  br i1 %or.cond21, label %.thread, label %25

25:                                               ; preds = %21
  %26 = load i32, ptr getelementptr inbounds (i8, ptr @duals, i64 48), align 16, !tbaa !5
  %27 = icmp eq i32 %26, %0
  %28 = load i32, ptr getelementptr inbounds (i8, ptr @duals, i64 52), align 4
  %.not.6 = icmp eq i32 %28, %0
  %or.cond22 = select i1 %27, i1 true, i1 %.not.6
  %spec.select = select i1 %or.cond22, ptr getelementptr inbounds (i8, ptr @duals, i64 48), ptr null
  br label %.thread

.thread:                                          ; preds = %25, %21, %17, %13, %9, %5, %1
  %.2 = phi ptr [ @duals, %1 ], [ getelementptr inbounds (i8, ptr @duals, i64 8), %5 ], [ getelementptr inbounds (i8, ptr @duals, i64 16), %9 ], [ getelementptr inbounds (i8, ptr @duals, i64 24), %13 ], [ getelementptr inbounds (i8, ptr @duals, i64 32), %17 ], [ getelementptr inbounds (i8, ptr @duals, i64 40), %21 ], [ %spec.select, %25 ]
  ret ptr %.2
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


; Effective O3 passes: ADCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass