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

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

@cnv_bcd_tab = external constant [100 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: alwaysinline nounwind uwtable
declare dso_local i64 @divmodu(i64 noundef, i16 noundef zeroext) #1

; Function Attrs: nounwind uwtable
define dso_local i64 @u16ToBCD(i16 noundef zeroext %0) #2 {
  %2 = alloca i64, align 8
  %3 = alloca i16, align 2
  %4 = alloca i16, align 2
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i16, align 2
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca i16, align 2
  store i16 %0, ptr %3, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #3
  %11 = load i16, ptr %3, align 2, !tbaa !5
  store i16 %11, ptr %4, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %12 = load i16, ptr %4, align 2, !tbaa !5
  %13 = zext i16 %12 to i32
  %14 = icmp sge i32 %13, 100
  br i1 %14, label %15, label %29

15:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %16 = load i16, ptr %4, align 2, !tbaa !5
  %17 = zext i16 %16 to i64
  %18 = call i64 @divmodu(i64 noundef %17, i16 noundef zeroext 100)
  store i64 %18, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %7) #3
  %19 = load i64, ptr %6, align 8, !tbaa !9
  %20 = lshr i64 %19, 16
  %21 = trunc i64 %20 to i16
  store i16 %21, ptr %7, align 2, !tbaa !5
  %22 = load i64, ptr %6, align 8, !tbaa !9
  %23 = trunc i64 %22 to i16
  store i16 %23, ptr %4, align 2, !tbaa !5
  %24 = load i16, ptr %7, align 2, !tbaa !5
  %25 = zext i16 %24 to i64
  %26 = getelementptr inbounds [100 x i8], ptr @cnv_bcd_tab, i64 0, i64 %25
  %27 = load i8, ptr %26, align 1, !tbaa !11
  %28 = zext i8 %27 to i64
  store i64 %28, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 2, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %35

29:                                               ; preds = %1
  %30 = load i16, ptr %4, align 2, !tbaa !5
  %31 = zext i16 %30 to i64
  %32 = getelementptr inbounds [100 x i8], ptr @cnv_bcd_tab, i64 0, i64 %31
  %33 = load i8, ptr %32, align 1, !tbaa !11
  %34 = zext i8 %33 to i64
  store i64 %34, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %77

35:                                               ; preds = %15
  %36 = load i16, ptr %4, align 2, !tbaa !5
  %37 = zext i16 %36 to i32
  %38 = icmp sge i32 %37, 100
  br i1 %38, label %39, label %57

39:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %40 = load i16, ptr %4, align 2, !tbaa !5
  %41 = zext i16 %40 to i64
  %42 = call i64 @divmodu(i64 noundef %41, i16 noundef zeroext 100)
  store i64 %42, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #3
  %43 = load i64, ptr %9, align 8, !tbaa !9
  %44 = lshr i64 %43, 16
  %45 = trunc i64 %44 to i16
  store i16 %45, ptr %10, align 2, !tbaa !5
  %46 = load i64, ptr %9, align 8, !tbaa !9
  %47 = trunc i64 %46 to i16
  store i16 %47, ptr %4, align 2, !tbaa !5
  %48 = load i16, ptr %10, align 2, !tbaa !5
  %49 = zext i16 %48 to i64
  %50 = getelementptr inbounds [100 x i8], ptr @cnv_bcd_tab, i64 0, i64 %49
  %51 = load i8, ptr %50, align 1, !tbaa !11
  %52 = zext i8 %51 to i32
  %53 = shl i32 %52, 8
  %54 = sext i32 %53 to i64
  %55 = load i64, ptr %5, align 8, !tbaa !9
  %56 = or i64 %55, %54
  store i64 %56, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %67

57:                                               ; preds = %35
  %58 = load i64, ptr %5, align 8, !tbaa !9
  %59 = load i16, ptr %4, align 2, !tbaa !5
  %60 = zext i16 %59 to i64
  %61 = getelementptr inbounds [100 x i8], ptr @cnv_bcd_tab, i64 0, i64 %60
  %62 = load i8, ptr %61, align 1, !tbaa !11
  %63 = zext i8 %62 to i32
  %64 = shl i32 %63, 8
  %65 = sext i32 %64 to i64
  %66 = or i64 %58, %65
  store i64 %66, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %77

67:                                               ; preds = %39
  %68 = load i64, ptr %5, align 8, !tbaa !9
  %69 = load i16, ptr %4, align 2, !tbaa !5
  %70 = zext i16 %69 to i64
  %71 = getelementptr inbounds [100 x i8], ptr @cnv_bcd_tab, i64 0, i64 %70
  %72 = load i8, ptr %71, align 1, !tbaa !11
  %73 = zext i8 %72 to i32
  %74 = shl i32 %73, 16
  %75 = sext i32 %74 to i64
  %76 = or i64 %68, %75
  store i64 %76, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %77

77:                                               ; preds = %67, %57, %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #3
  %78 = load i64, ptr %2, align 8
  ret i64 %78
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { alwaysinline 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 = !{!"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 = !{!7, !7, 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"

@cnv_bcd_tab = external local_unnamed_addr constant [100 x i8], align 16

; Function Attrs: alwaysinline nounwind uwtable
declare dso_local i64 @divmodu(i64 noundef, i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i64 0, 16777216) i64 @u16ToBCD(i16 noundef zeroext %0) local_unnamed_addr #1 {
  %2 = icmp ugt i16 %0, 99
  %3 = zext i16 %0 to i64
  br i1 %2, label %4, label %14

4:                                                ; preds = %1
  %5 = tail call i64 @divmodu(i64 noundef %3, i16 noundef zeroext 100)
  %6 = lshr i64 %5, 16
  %7 = and i64 %6, 65535
  %8 = getelementptr inbounds [100 x i8], ptr @cnv_bcd_tab, i64 0, i64 %7
  %9 = load i8, ptr %8, align 1, !tbaa !5
  %10 = zext i8 %9 to i64
  %11 = and i64 %5, 65532
  %12 = icmp ugt i64 %11, 99
  %13 = and i64 %5, 65535
  br i1 %12, label %18, label %33

14:                                               ; preds = %1
  %15 = getelementptr inbounds [100 x i8], ptr @cnv_bcd_tab, i64 0, i64 %3
  %16 = load i8, ptr %15, align 1, !tbaa !5
  %17 = zext i8 %16 to i64
  br label %39

18:                                               ; preds = %4
  %19 = tail call i64 @divmodu(i64 noundef %13, i16 noundef zeroext 100)
  %20 = lshr i64 %19, 16
  %21 = and i64 %20, 65535
  %22 = getelementptr inbounds [100 x i8], ptr @cnv_bcd_tab, i64 0, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !5
  %24 = zext i8 %23 to i64
  %25 = shl nuw nsw i64 %24, 8
  %26 = and i64 %19, 65535
  %27 = getelementptr inbounds [100 x i8], ptr @cnv_bcd_tab, i64 0, i64 %26
  %28 = load i8, ptr %27, align 1, !tbaa !5
  %29 = zext i8 %28 to i64
  %30 = shl nuw nsw i64 %29, 16
  %31 = or disjoint i64 %25, %30
  %32 = or disjoint i64 %31, %10
  br label %39

33:                                               ; preds = %4
  %34 = getelementptr inbounds [100 x i8], ptr @cnv_bcd_tab, i64 0, i64 %13
  %35 = load i8, ptr %34, align 1, !tbaa !5
  %36 = zext i8 %35 to i64
  %37 = shl nuw nsw i64 %36, 8
  %38 = or disjoint i64 %37, %10
  br label %39

39:                                               ; preds = %18, %33, %14
  %.0 = phi i64 [ %32, %18 ], [ %38, %33 ], [ %17, %14 ]
  ret i64 %.0
}

attributes #0 = { alwaysinline 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 = { 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" }

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


; Effective O3 passes: GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass