; LLVM IR from libxsmm/libxsmm :: src (libxsmm_math)

; 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
define dso_local zeroext i8 @libxsmm_convert_f32_to_bf8_stochastic(float noundef %0, i32 noundef %1) #1 {
  %3 = alloca float, align 4
  %4 = alloca i32, align 4
  %5 = alloca i16, align 2
  %6 = alloca i16, align 2
  %7 = alloca i16, align 2
  store float %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #2
  %8 = load float, ptr %3, align 4, !tbaa !5
  %9 = call zeroext i16 @libxsmm_convert_f32_to_f16(float noundef %8)
  store i16 %9, ptr %5, align 2, !tbaa !11
  %10 = load i16, ptr %5, align 2, !tbaa !11
  %11 = zext i16 %10 to i32
  %12 = and i32 %11, 31744
  %13 = icmp eq i32 %12, 31744
  br i1 %13, label %14, label %29

14:                                               ; preds = %2
  %15 = load i16, ptr %5, align 2, !tbaa !11
  %16 = zext i16 %15 to i32
  %17 = and i32 %16, 1023
  %18 = icmp eq i32 %17, 0
  br i1 %18, label %19, label %22

19:                                               ; preds = %14
  %20 = load i16, ptr %5, align 2, !tbaa !11
  %21 = zext i16 %20 to i32
  br label %26

22:                                               ; preds = %14
  %23 = load i16, ptr %5, align 2, !tbaa !11
  %24 = zext i16 %23 to i32
  %25 = or i32 %24, 512
  br label %26

26:                                               ; preds = %22, %19
  %27 = phi i32 [ %21, %19 ], [ %25, %22 ]
  %28 = trunc i32 %27 to i16
  store i16 %28, ptr %5, align 2, !tbaa !11
  br label %58

29:                                               ; preds = %2
  %30 = load i16, ptr %5, align 2, !tbaa !11
  %31 = zext i16 %30 to i32
  %32 = and i32 %31, 31744
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %34, label %44

34:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 2, ptr %6) #2
  %35 = load i32, ptr %4, align 4, !tbaa !9
  %36 = and i32 %35, 255
  %37 = trunc i32 %36 to i16
  store i16 %37, ptr %6, align 2, !tbaa !11
  %38 = load i16, ptr %5, align 2, !tbaa !11
  %39 = zext i16 %38 to i32
  %40 = load i16, ptr %6, align 2, !tbaa !11
  %41 = zext i16 %40 to i32
  %42 = add nsw i32 %39, %41
  %43 = trunc i32 %42 to i16
  store i16 %43, ptr %5, align 2, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #2
  br label %57

44:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 2, ptr %7) #2
  %45 = load i16, ptr %5, align 2, !tbaa !11
  %46 = zext i16 %45 to i32
  %47 = ashr i32 %46, 8
  %48 = and i32 %47, 1
  %49 = trunc i32 %48 to i16
  store i16 %49, ptr %7, align 2, !tbaa !11
  %50 = load i16, ptr %5, align 2, !tbaa !11
  %51 = zext i16 %50 to i32
  %52 = add nsw i32 %51, 127
  %53 = load i16, ptr %7, align 2, !tbaa !11
  %54 = zext i16 %53 to i32
  %55 = add nsw i32 %52, %54
  %56 = trunc i32 %55 to i16
  store i16 %56, ptr %5, align 2, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 2, ptr %7) #2
  br label %57

57:                                               ; preds = %44, %34
  br label %58

58:                                               ; preds = %57, %26
  %59 = load i16, ptr %5, align 2, !tbaa !11
  %60 = zext i16 %59 to i32
  %61 = ashr i32 %60, 8
  %62 = trunc i32 %61 to i8
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #2
  ret i8 %62
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i16 @libxsmm_convert_f32_to_f16(float noundef) #1

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 = !{!"float", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"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
define dso_local zeroext i8 @libxsmm_convert_f32_to_bf8_stochastic(float noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call zeroext i16 @libxsmm_convert_f32_to_f16(float noundef %0)
  %4 = zext i16 %3 to i32
  %5 = and i32 %4, 31744
  switch i32 %5, label %11 [
    i32 31744, label %6
    i32 0, label %15
  ]

6:                                                ; preds = %2
  %7 = and i32 %4, 1023
  %8 = icmp eq i32 %7, 0
  %9 = or i16 %3, 512
  %10 = select i1 %8, i16 %3, i16 %9
  br label %20

11:                                               ; preds = %2
  %12 = trunc i32 %1 to i16
  %13 = and i16 %12, 255
  %14 = add i16 %3, %13
  br label %20

15:                                               ; preds = %2
  %16 = lshr i16 %3, 8
  %17 = and i16 %16, 1
  %18 = add i16 %3, 127
  %19 = add i16 %18, %17
  br label %20

20:                                               ; preds = %11, %15, %6
  %.0 = phi i16 [ %10, %6 ], [ %14, %11 ], [ %19, %15 ]
  %21 = lshr i16 %.0, 8
  %22 = trunc nuw i16 %21 to i8
  ret i8 %22
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i16 @libxsmm_convert_f32_to_f16(float noundef) local_unnamed_addr #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: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass