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

; 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
declare dso_local i64 @u16ToBCD(i16 noundef zeroext) #1

; Function Attrs: nounwind uwtable
define dso_local i64 @u32ToBCD(i64 noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i16, align 2
  %5 = alloca i16, align 2
  store i64 %0, ptr %3, align 8, !tbaa !5
  %6 = load i64, ptr %3, align 8, !tbaa !5
  %7 = icmp ugt i64 %6, 99999999
  br i1 %7, label %8, label %9

8:                                                ; preds = %1
  store i64 2576980377, ptr %2, align 8
  br label %29

9:                                                ; preds = %1
  %10 = load i64, ptr %3, align 8, !tbaa !5
  %11 = icmp uge i64 %10, 65536
  br i1 %11, label %12, label %25

12:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #2
  %13 = load i64, ptr %3, align 8, !tbaa !5
  %14 = urem i64 %13, 10000
  %15 = trunc i64 %14 to i16
  store i16 %15, ptr %4, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #2
  %16 = load i64, ptr %3, align 8, !tbaa !5
  %17 = udiv i64 %16, 10000
  %18 = trunc i64 %17 to i16
  store i16 %18, ptr %5, align 2, !tbaa !9
  %19 = load i16, ptr %5, align 2, !tbaa !9
  %20 = call i64 @u16ToBCD(i16 noundef zeroext %19)
  %21 = shl i64 %20, 16
  %22 = load i16, ptr %4, align 2, !tbaa !9
  %23 = call i64 @u16ToBCD(i16 noundef zeroext %22)
  %24 = or i64 %21, %23
  store i64 %24, ptr %2, align 8
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #2
  br label %29

25:                                               ; preds = %9
  %26 = load i64, ptr %3, align 8, !tbaa !5
  %27 = trunc i64 %26 to i16
  %28 = call i64 @u16ToBCD(i16 noundef zeroext %27)
  store i64 %28, ptr %2, align 8
  br label %29

29:                                               ; preds = %25, %12, %8
  %30 = load i64, ptr %2, align 8
  ret i64 %30
}

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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !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"

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

; Function Attrs: nounwind uwtable
define dso_local i64 @u32ToBCD(i64 noundef %0) local_unnamed_addr #0 {
  %2 = icmp ugt i64 %0, 99999999
  br i1 %2, label %17, label %3

3:                                                ; preds = %1
  %4 = icmp ugt i64 %0, 65535
  br i1 %4, label %5, label %14

5:                                                ; preds = %3
  %.lhs.trunc = trunc nuw i64 %0 to i32
  %6 = urem i32 %.lhs.trunc, 10000
  %7 = trunc nuw nsw i32 %6 to i16
  %8 = udiv i32 %.lhs.trunc, 10000
  %9 = trunc nuw i32 %8 to i16
  %10 = tail call i64 @u16ToBCD(i16 noundef zeroext %9)
  %11 = shl i64 %10, 16
  %12 = tail call i64 @u16ToBCD(i16 noundef zeroext %7)
  %13 = or i64 %11, %12
  br label %17

14:                                               ; preds = %3
  %15 = trunc nuw i64 %0 to i16
  %16 = tail call i64 @u16ToBCD(i16 noundef zeroext %15)
  br label %17

17:                                               ; preds = %1, %14, %5
  %.0 = phi i64 [ %13, %5 ], [ %16, %14 ], [ 2576980377, %1 ]
  ret i64 %.0
}

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

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


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