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

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

%union.libxsmm_float16_ushort = type { i16 }

; 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

declare zeroext i16 @libxsmm_convert_f32_to_f16(float noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @libxsmm_lsfr_i32(ptr noundef, ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @libxsmm_stochastic_convert_fp32_bf8(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3, i32 noundef %4) #2 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca %union.libxsmm_float16_ushort, align 2
  %15 = alloca i8, align 1
  %16 = alloca i16, align 2
  %17 = alloca i32, align 4
  %18 = alloca i16, align 2
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  store i32 %4, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  store i32 0, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  store i32 0, ptr %12, align 4, !tbaa !9
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %19

19:                                               ; preds = %118, %5
  %20 = load i32, ptr %11, align 4, !tbaa !9
  %21 = load i32, ptr %8, align 4, !tbaa !9
  %22 = icmp ult i32 %20, %21
  br i1 %22, label %23, label %121

23:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  %24 = load i32, ptr %11, align 4, !tbaa !9
  %25 = add i32 %24, 16
  %26 = load i32, ptr %8, align 4, !tbaa !9
  %27 = icmp ult i32 %25, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %23
  br label %33

29:                                               ; preds = %23
  %30 = load i32, ptr %8, align 4, !tbaa !9
  %31 = load i32, ptr %11, align 4, !tbaa !9
  %32 = sub i32 %30, %31
  br label %33

33:                                               ; preds = %29, %28
  %34 = phi i32 [ 16, %28 ], [ %32, %29 ]
  store i32 %34, ptr %13, align 4, !tbaa !9
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %35

35:                                               ; preds = %114, %33
  %36 = load i32, ptr %12, align 4, !tbaa !9
  %37 = load i32, ptr %13, align 4, !tbaa !9
  %38 = icmp ult i32 %36, %37
  br i1 %38, label %39, label %117

39:                                               ; preds = %35
  call void @llvm.lifetime.start.p0(i64 2, ptr %14) #4
  call void @llvm.memset.p0.i64(ptr align 2 %14, i8 0, i64 2, i1 false)
  call void @llvm.lifetime.start.p0(i64 1, ptr %15) #4
  call void @llvm.lifetime.start.p0(i64 2, ptr %16) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #4
  call void @llvm.lifetime.start.p0(i64 2, ptr %18) #4
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = load i32, ptr %11, align 4, !tbaa !9
  %42 = load i32, ptr %12, align 4, !tbaa !9
  %43 = add i32 %41, %42
  %44 = zext i32 %43 to i64
  %45 = getelementptr inbounds float, ptr %40, i64 %44
  %46 = load float, ptr %45, align 4, !tbaa !11
  %47 = call zeroext i16 @libxsmm_convert_f32_to_f16(float noundef %46)
  store i16 %47, ptr %14, align 2, !tbaa !13
  %48 = load ptr, ptr %9, align 8, !tbaa !5
  %49 = load i32, ptr %10, align 4, !tbaa !9
  %50 = load i32, ptr %12, align 4, !tbaa !9
  %51 = add i32 %49, %50
  %52 = urem i32 %51, 16
  call void @libxsmm_lsfr_i32(ptr noundef %48, ptr noundef %17, i32 noundef %52)
  %53 = load i32, ptr %17, align 4, !tbaa !9
  %54 = lshr i32 %53, 24
  %55 = and i32 %54, 255
  %56 = trunc i32 %55 to i16
  store i16 %56, ptr %18, align 2, !tbaa !14
  %57 = load i16, ptr %14, align 2, !tbaa !13
  %58 = zext i16 %57 to i32
  %59 = ashr i32 %58, 8
  %60 = and i32 %59, 1
  %61 = trunc i32 %60 to i16
  store i16 %61, ptr %16, align 2, !tbaa !14
  %62 = load i16, ptr %14, align 2, !tbaa !13
  %63 = zext i16 %62 to i32
  %64 = and i32 %63, 31744
  %65 = icmp eq i32 %64, 31744
  br i1 %65, label %66, label %80

66:                                               ; preds = %39
  %67 = load i16, ptr %14, align 2, !tbaa !13
  %68 = zext i16 %67 to i32
  %69 = and i32 %68, 1023
  %70 = icmp eq i32 %69, 0
  br i1 %70, label %71, label %74

71:                                               ; preds = %66
  %72 = load i16, ptr %14, align 2, !tbaa !13
  %73 = zext i16 %72 to i32
  br label %78

74:                                               ; preds = %66
  %75 = load i16, ptr %14, align 2, !tbaa !13
  %76 = zext i16 %75 to i32
  %77 = or i32 %76, 512
  br label %78

78:                                               ; preds = %74, %71
  %79 = phi i32 [ %73, %71 ], [ %77, %74 ]
  br label %100

80:                                               ; preds = %39
  %81 = load i16, ptr %14, align 2, !tbaa !13
  %82 = zext i16 %81 to i32
  %83 = and i32 %82, 31744
  %84 = icmp eq i32 %83, 0
  br i1 %84, label %85, label %92

85:                                               ; preds = %80
  %86 = load i16, ptr %14, align 2, !tbaa !13
  %87 = zext i16 %86 to i32
  %88 = add nsw i32 %87, 127
  %89 = load i16, ptr %16, align 2, !tbaa !14
  %90 = zext i16 %89 to i32
  %91 = add nsw i32 %88, %90
  br label %98

92:                                               ; preds = %80
  %93 = load i16, ptr %14, align 2, !tbaa !13
  %94 = zext i16 %93 to i32
  %95 = load i16, ptr %18, align 2, !tbaa !14
  %96 = zext i16 %95 to i32
  %97 = add nsw i32 %94, %96
  br label %98

98:                                               ; preds = %92, %85
  %99 = phi i32 [ %91, %85 ], [ %97, %92 ]
  br label %100

100:                                              ; preds = %98, %78
  %101 = phi i32 [ %79, %78 ], [ %99, %98 ]
  %102 = trunc i32 %101 to i16
  store i16 %102, ptr %14, align 2, !tbaa !13
  %103 = load i16, ptr %14, align 2, !tbaa !13
  %104 = zext i16 %103 to i32
  %105 = ashr i32 %104, 8
  %106 = trunc i32 %105 to i8
  store i8 %106, ptr %15, align 1, !tbaa !13
  %107 = load i8, ptr %15, align 1, !tbaa !13
  %108 = load ptr, ptr %7, align 8, !tbaa !5
  %109 = load i32, ptr %11, align 4, !tbaa !9
  %110 = load i32, ptr %12, align 4, !tbaa !9
  %111 = add i32 %109, %110
  %112 = zext i32 %111 to i64
  %113 = getelementptr inbounds i8, ptr %108, i64 %112
  store i8 %107, ptr %113, align 1, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 2, ptr %18) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %14) #4
  br label %114

114:                                              ; preds = %100
  %115 = load i32, ptr %12, align 4, !tbaa !9
  %116 = add i32 %115, 1
  store i32 %116, ptr %12, align 4, !tbaa !9
  br label %35, !llvm.loop !16

117:                                              ; preds = %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  br label %118

118:                                              ; preds = %117
  %119 = load i32, ptr %11, align 4, !tbaa !9
  %120 = add i32 %119, 16
  store i32 %120, ptr %11, align 4, !tbaa !9
  br label %19, !llvm.loop !18

121:                                              ; preds = %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"any pointer", !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 = !{!"float", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"short", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}


; 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: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

declare zeroext i16 @libxsmm_convert_f32_to_f16(float noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @libxsmm_lsfr_i32(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local void @libxsmm_stochastic_convert_fp32_bf8(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1, i32 noundef %2, ptr noundef %3, i32 noundef %4) local_unnamed_addr #2 {
  %6 = alloca i32, align 4
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph34

.loopexit:                                        ; preds = %37, %.lr.ph34
  br i1 %8, label %.lr.ph34, label %._crit_edge, !llvm.loop !5

.lr.ph34:                                         ; preds = %5, %.loopexit
  %.033 = phi i32 [ %7, %.loopexit ], [ 0, %5 ]
  %7 = add i32 %.033, 16
  %8 = icmp ult i32 %7, %2
  %9 = sub i32 %2, %.033
  %10 = select i1 %8, i32 16, i32 %9
  %.not35 = icmp eq i32 %10, 0
  br i1 %.not35, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.lr.ph34
  %11 = zext i32 %10 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %37
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %37 ]
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %6) #3
  %12 = trunc nuw i64 %indvars.iv to i32
  %13 = add i32 %.033, %12
  %14 = zext i32 %13 to i64
  %15 = getelementptr inbounds float, ptr %0, i64 %14
  %16 = load float, ptr %15, align 4, !tbaa !7
  %17 = call zeroext i16 @libxsmm_convert_f32_to_f16(float noundef %16) #3
  %18 = add i32 %12, %4
  %19 = and i32 %18, 15
  call void @libxsmm_lsfr_i32(ptr noundef %3, ptr noundef nonnull %6, i32 noundef %19)
  %20 = zext i16 %17 to i32
  %21 = and i32 %20, 31744
  switch i32 %21, label %33 [
    i32 31744, label %22
    i32 0, label %27
  ]

22:                                               ; preds = %.lr.ph
  %23 = and i32 %20, 1023
  %24 = icmp eq i32 %23, 0
  %25 = or i32 %20, 512
  %26 = select i1 %24, i32 %20, i32 %25
  br label %37

27:                                               ; preds = %.lr.ph
  %28 = lshr i16 %17, 8
  %29 = and i16 %28, 1
  %30 = add nuw nsw i32 %20, 127
  %31 = zext nneg i16 %29 to i32
  %32 = add nuw nsw i32 %30, %31
  br label %37

33:                                               ; preds = %.lr.ph
  %34 = load i32, ptr %6, align 4, !tbaa !11
  %35 = lshr i32 %34, 24
  %36 = add nuw nsw i32 %35, %20
  br label %37

37:                                               ; preds = %27, %33, %22
  %38 = phi i32 [ %26, %22 ], [ %32, %27 ], [ %36, %33 ]
  %39 = lshr i32 %38, 8
  %40 = trunc i32 %39 to i8
  %41 = getelementptr inbounds i8, ptr %1, i64 %14
  store i8 %40, ptr %41, align 1, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %6) #3
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %42 = icmp ult i64 %indvars.iv.next, %11
  br i1 %42, label %.lr.ph, label %.loopexit, !llvm.loop !14

._crit_edge:                                      ; preds = %.loopexit, %5
  ret void
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !8, i64 0}
!8 = !{!"float", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !9, i64 0}
!13 = !{!9, !9, i64 0}
!14 = distinct !{!14, !6}


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