; LLVM IR from stillwater-sc/universal :: c_api/pure_c/posit (posit8)

; 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.posit8_u = type { [1 x i8] }

; Function Attrs: nounwind uwtable
declare dso_local float @posit8_fraction_value(i8 noundef zeroext) #0

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

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

; Function Attrs: nounwind uwtable
define dso_local float @posit8_tof(i8 %0) #0 {
  %2 = alloca float, align 4
  %3 = alloca %union.posit8_u, align 1
  %4 = alloca i8, align 1
  %5 = alloca i8, align 1
  %6 = alloca i8, align 1
  %7 = alloca float, align 4
  %8 = alloca float, align 4
  %9 = alloca float, align 4
  %10 = alloca float, align 4
  %11 = getelementptr inbounds %union.posit8_u, ptr %3, i32 0, i32 0
  store i8 %0, ptr %11, align 1
  %12 = load i8, ptr %3, align 1, !tbaa !5
  %13 = zext i8 %12 to i32
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %15, label %16

15:                                               ; preds = %1
  store float 0.000000e+00, ptr %2, align 4
  br label %70

16:                                               ; preds = %1
  %17 = load i8, ptr %3, align 1, !tbaa !5
  %18 = zext i8 %17 to i32
  %19 = icmp eq i32 %18, 128
  br i1 %19, label %20, label %21

20:                                               ; preds = %16
  store float 0x7FF8000000000000, ptr %2, align 4
  br label %70

21:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #3
  %22 = load i8, ptr %3, align 1, !tbaa !5
  %23 = zext i8 %22 to i32
  %24 = and i32 %23, 128
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %26, label %30

26:                                               ; preds = %21
  %27 = load i8, ptr %3, align 1, !tbaa !5
  %28 = zext i8 %27 to i32
  %29 = sub nsw i32 0, %28
  br label %33

30:                                               ; preds = %21
  %31 = load i8, ptr %3, align 1, !tbaa !5
  %32 = zext i8 %31 to i32
  br label %33

33:                                               ; preds = %30, %26
  %34 = phi i32 [ %29, %26 ], [ %32, %30 ]
  %35 = trunc i32 %34 to i8
  store i8 %35, ptr %4, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #3
  store i8 0, ptr %5, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #3
  %36 = load i8, ptr %4, align 1, !tbaa !5
  %37 = call signext i8 @posit8_decode_regime(i8 noundef zeroext %36, ptr noundef %5)
  store i8 %37, ptr %6, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %38 = getelementptr inbounds %union.posit8_u, ptr %3, i32 0, i32 0
  %39 = load i8, ptr %38, align 1
  %40 = call i32 @posit8_sign_value(i8 %39)
  %41 = sitofp i32 %40 to float
  store float %41, ptr %7, align 4, !tbaa !8
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %42 = load i8, ptr %6, align 1, !tbaa !5
  %43 = sext i8 %42 to i32
  %44 = icmp sgt i32 %43, 0
  br i1 %44, label %45, label %50

45:                                               ; preds = %33
  %46 = load i8, ptr %6, align 1, !tbaa !5
  %47 = sext i8 %46 to i32
  %48 = shl i32 1, %47
  %49 = uitofp i32 %48 to float
  br label %57

50:                                               ; preds = %33
  %51 = load i8, ptr %6, align 1, !tbaa !5
  %52 = sext i8 %51 to i32
  %53 = sub nsw i32 0, %52
  %54 = shl i32 1, %53
  %55 = uitofp i32 %54 to float
  %56 = fdiv float 1.000000e+00, %55
  br label %57

57:                                               ; preds = %50, %45
  %58 = phi float [ %49, %45 ], [ %56, %50 ]
  store float %58, ptr %8, align 4, !tbaa !8
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store float 1.000000e+00, ptr %9, align 4, !tbaa !8
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store float 1.000000e+00, ptr %10, align 4, !tbaa !8
  %59 = load i8, ptr %5, align 1, !tbaa !5
  %60 = call float @posit8_fraction_value(i8 noundef zeroext %59)
  %61 = load float, ptr %10, align 4, !tbaa !8
  %62 = fadd float %61, %60
  store float %62, ptr %10, align 4, !tbaa !8
  %63 = load float, ptr %7, align 4, !tbaa !8
  %64 = load float, ptr %8, align 4, !tbaa !8
  %65 = fmul float %63, %64
  %66 = load float, ptr %9, align 4, !tbaa !8
  %67 = fmul float %65, %66
  %68 = load float, ptr %10, align 4, !tbaa !8
  %69 = fmul float %67, %68
  store float %69, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #3
  br label %70

70:                                               ; preds = %57, %20, %15
  %71 = load float, ptr %2, align 4
  ret float %71
}

; Function Attrs: inlinehint nounwind uwtable
declare signext i8 @posit8_decode_regime(i8 noundef zeroext, ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare i32 @posit8_sign_value(i8) #2

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"float", !6, 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 float @posit8_fraction_value(i8 noundef zeroext) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
define dso_local float @posit8_tof(i8 %0) local_unnamed_addr #0 {
  %2 = alloca i8, align 1
  switch i8 %0, label %4 [
    i8 0, label %26
    i8 -128, label %3
  ]

3:                                                ; preds = %1
  br label %26

4:                                                ; preds = %1
  %5 = tail call i8 @llvm.abs.i8(i8 %0, i1 true)
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %2) #4
  store i8 0, ptr %2, align 1, !tbaa !5
  %6 = call signext i8 @posit8_decode_regime(i8 noundef zeroext %5, ptr noundef nonnull %2)
  %7 = call i32 @posit8_sign_value(i8 %0)
  %8 = sitofp i32 %7 to float
  %9 = sext i8 %6 to i32
  %10 = icmp sgt i8 %6, 0
  br i1 %10, label %11, label %14

11:                                               ; preds = %4
  %12 = shl nuw i32 1, %9
  %13 = uitofp i32 %12 to float
  br label %19

14:                                               ; preds = %4
  %15 = sub nsw i32 0, %9
  %16 = shl nuw i32 1, %15
  %17 = uitofp i32 %16 to float
  %18 = fdiv float 1.000000e+00, %17
  br label %19

19:                                               ; preds = %14, %11
  %20 = phi float [ %13, %11 ], [ %18, %14 ]
  %21 = load i8, ptr %2, align 1, !tbaa !5
  %22 = call float @posit8_fraction_value(i8 noundef zeroext %21)
  %23 = fadd float %22, 1.000000e+00
  %24 = fmul float %20, %8
  %25 = fmul float %24, %23
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %2) #4
  br label %26

26:                                               ; preds = %1, %19, %3
  %.0 = phi float [ 0x7FF8000000000000, %3 ], [ %25, %19 ], [ 0.000000e+00, %1 ]
  ret float %.0
}

; Function Attrs: inlinehint nounwind uwtable
declare signext i8 @posit8_decode_regime(i8 noundef zeroext, ptr noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare i32 @posit8_sign_value(i8) local_unnamed_addr #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i8 @llvm.abs.i8(i8, i1 immarg) #3

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" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 nosync nounwind speculatable willreturn memory(none) }
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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}


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