; 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] }
%struct.div_t = type { i32, i32 }

@__const.posit8_divp8.p = external hidden unnamed_addr constant %union.posit8_u, align 1

; 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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

; Function Attrs: inlinehint nounwind uwtable
declare zeroext i1 @posit8_isnar(i8) #2

; Function Attrs: inlinehint nounwind uwtable
declare zeroext i1 @posit8_iszero(i8) #2

; Function Attrs: nounwind uwtable
define dso_local i8 @posit8_divp8(i8 %0, i8 %1) #3 {
  %3 = alloca %union.posit8_u, align 1
  %4 = alloca %union.posit8_u, align 1
  %5 = alloca %union.posit8_u, align 1
  %6 = alloca i8, align 1
  %7 = alloca i8, align 1
  %8 = alloca i8, align 1
  %9 = alloca i16, align 2
  %10 = alloca i8, align 1
  %11 = alloca i16, align 2
  %12 = alloca %struct.div_t, align 4
  %13 = alloca i16, align 2
  %14 = alloca i16, align 2
  %15 = alloca i8, align 1
  %16 = alloca i8, align 1
  %17 = alloca i8, align 1
  %18 = getelementptr inbounds %union.posit8_u, ptr %4, i32 0, i32 0
  store i8 %0, ptr %18, align 1
  %19 = getelementptr inbounds %union.posit8_u, ptr %5, i32 0, i32 0
  store i8 %1, ptr %19, align 1
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %3, ptr align 1 @__const.posit8_divp8.p, i64 1, i1 false)
  %20 = getelementptr inbounds %union.posit8_u, ptr %4, i32 0, i32 0
  %21 = load i8, ptr %20, align 1
  %22 = call zeroext i1 @posit8_isnar(i8 %21)
  br i1 %22, label %31, label %23

23:                                               ; preds = %2
  %24 = getelementptr inbounds %union.posit8_u, ptr %5, i32 0, i32 0
  %25 = load i8, ptr %24, align 1
  %26 = call zeroext i1 @posit8_isnar(i8 %25)
  br i1 %26, label %31, label %27

27:                                               ; preds = %23
  %28 = getelementptr inbounds %union.posit8_u, ptr %5, i32 0, i32 0
  %29 = load i8, ptr %28, align 1
  %30 = call zeroext i1 @posit8_iszero(i8 %29)
  br i1 %30, label %31, label %32

31:                                               ; preds = %27, %23, %2
  br label %147

32:                                               ; preds = %27
  %33 = getelementptr inbounds %union.posit8_u, ptr %4, i32 0, i32 0
  %34 = load i8, ptr %33, align 1
  %35 = call zeroext i1 @posit8_iszero(i8 %34)
  br i1 %35, label %36, label %37

36:                                               ; preds = %32
  store i8 0, ptr %3, align 1, !tbaa !5
  br label %147

37:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #5
  %38 = load i8, ptr %4, align 1, !tbaa !5
  %39 = zext i8 %38 to i32
  %40 = and i32 %39, 128
  %41 = icmp ne i32 %40, 0
  %42 = zext i1 %41 to i32
  %43 = load i8, ptr %5, align 1, !tbaa !5
  %44 = zext i8 %43 to i32
  %45 = and i32 %44, 128
  %46 = icmp ne i32 %45, 0
  %47 = zext i1 %46 to i32
  %48 = xor i32 %42, %47
  %49 = icmp ne i32 %48, 0
  %50 = zext i1 %49 to i8
  store i8 %50, ptr %6, align 1, !tbaa !8
  %51 = load i8, ptr %4, align 1, !tbaa !5
  %52 = zext i8 %51 to i32
  %53 = and i32 %52, 128
  %54 = icmp ne i32 %53, 0
  br i1 %54, label %55, label %59

55:                                               ; preds = %37
  %56 = load i8, ptr %4, align 1, !tbaa !5
  %57 = zext i8 %56 to i32
  %58 = sub nsw i32 0, %57
  br label %62

59:                                               ; preds = %37
  %60 = load i8, ptr %4, align 1, !tbaa !5
  %61 = zext i8 %60 to i32
  br label %62

62:                                               ; preds = %59, %55
  %63 = phi i32 [ %58, %55 ], [ %61, %59 ]
  %64 = trunc i32 %63 to i8
  store i8 %64, ptr %4, align 1, !tbaa !5
  %65 = load i8, ptr %5, align 1, !tbaa !5
  %66 = zext i8 %65 to i32
  %67 = and i32 %66, 128
  %68 = icmp ne i32 %67, 0
  br i1 %68, label %69, label %73

69:                                               ; preds = %62
  %70 = load i8, ptr %5, align 1, !tbaa !5
  %71 = zext i8 %70 to i32
  %72 = sub nsw i32 0, %71
  br label %76

73:                                               ; preds = %62
  %74 = load i8, ptr %5, align 1, !tbaa !5
  %75 = zext i8 %74 to i32
  br label %76

76:                                               ; preds = %73, %69
  %77 = phi i32 [ %72, %69 ], [ %75, %73 ]
  %78 = trunc i32 %77 to i8
  store i8 %78, ptr %5, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #5
  store i8 0, ptr %7, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #5
  %79 = load i8, ptr %4, align 1, !tbaa !5
  %80 = call signext i8 @posit8_decode_regime(i8 noundef zeroext %79, ptr noundef %7)
  store i8 %80, ptr %8, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #5
  %81 = load i8, ptr %7, align 1, !tbaa !5
  %82 = zext i8 %81 to i32
  %83 = or i32 128, %82
  %84 = shl i32 %83, 7
  %85 = trunc i32 %84 to i16
  store i16 %85, ptr %9, align 2, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #5
  %86 = load i8, ptr %5, align 1, !tbaa !5
  %87 = call signext i8 @posit8_decode_regime(i8 noundef zeroext %86, ptr noundef %7)
  store i8 %87, ptr %10, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %11) #5
  %88 = load i8, ptr %7, align 1, !tbaa !5
  %89 = zext i8 %88 to i32
  %90 = or i32 128, %89
  %91 = trunc i32 %90 to i16
  store i16 %91, ptr %11, align 2, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  %92 = load i16, ptr %9, align 2, !tbaa !10
  %93 = zext i16 %92 to i32
  %94 = load i16, ptr %11, align 2, !tbaa !10
  %95 = zext i16 %94 to i32
  %96 = call i64 @div(i32 noundef %93, i32 noundef %95) #6
  store i64 %96, ptr %12, align 4
  call void @llvm.lifetime.start.p0(i64 2, ptr %13) #5
  %97 = getelementptr inbounds %struct.div_t, ptr %12, i32 0, i32 0
  %98 = load i32, ptr %97, align 4, !tbaa !12
  %99 = trunc i32 %98 to i16
  store i16 %99, ptr %13, align 2, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 2, ptr %14) #5
  %100 = getelementptr inbounds %struct.div_t, ptr %12, i32 0, i32 1
  %101 = load i32, ptr %100, align 4, !tbaa !15
  %102 = trunc i32 %101 to i16
  store i16 %102, ptr %14, align 2, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 1, ptr %15) #5
  %103 = load i8, ptr %8, align 1, !tbaa !5
  %104 = sext i8 %103 to i32
  %105 = load i8, ptr %10, align 1, !tbaa !5
  %106 = sext i8 %105 to i32
  %107 = sub nsw i32 %104, %106
  %108 = trunc i32 %107 to i8
  store i8 %108, ptr %15, align 1, !tbaa !5
  %109 = load i16, ptr %13, align 2, !tbaa !10
  %110 = zext i16 %109 to i32
  %111 = icmp ne i32 %110, 0
  br i1 %111, label %112, label %128

112:                                              ; preds = %76
  call void @llvm.lifetime.start.p0(i64 1, ptr %16) #5
  %113 = load i16, ptr %13, align 2, !tbaa !10
  %114 = zext i16 %113 to i32
  %115 = ashr i32 %114, 7
  %116 = icmp ne i32 %115, 0
  %117 = zext i1 %116 to i8
  store i8 %117, ptr %16, align 1, !tbaa !8
  %118 = load i8, ptr %16, align 1, !tbaa !8, !range !16, !noundef !17
  %119 = trunc i8 %118 to i1
  br i1 %119, label %127, label %120

120:                                              ; preds = %112
  %121 = load i8, ptr %15, align 1, !tbaa !5
  %122 = add i8 %121, -1
  store i8 %122, ptr %15, align 1, !tbaa !5
  %123 = load i16, ptr %13, align 2, !tbaa !10
  %124 = zext i16 %123 to i32
  %125 = shl i32 %124, 1
  %126 = trunc i32 %125 to i16
  store i16 %126, ptr %13, align 2, !tbaa !10
  br label %127

127:                                              ; preds = %120, %112
  call void @llvm.lifetime.end.p0(i64 1, ptr %16) #5
  br label %128

128:                                              ; preds = %127, %76
  call void @llvm.lifetime.start.p0(i64 1, ptr %17) #5
  %129 = load i8, ptr %15, align 1, !tbaa !5
  %130 = load i16, ptr %13, align 2, !tbaa !10
  %131 = load i16, ptr %14, align 2, !tbaa !10
  %132 = zext i16 %131 to i32
  %133 = icmp ne i32 %132, 0
  %134 = call zeroext i8 @posit8_roundDiv(i8 noundef signext %129, i16 noundef zeroext %130, i1 noundef zeroext %133)
  store i8 %134, ptr %17, align 1, !tbaa !5
  %135 = load i8, ptr %6, align 1, !tbaa !8, !range !16, !noundef !17
  %136 = trunc i8 %135 to i1
  br i1 %136, label %137, label %141

137:                                              ; preds = %128
  %138 = load i8, ptr %17, align 1, !tbaa !5
  %139 = zext i8 %138 to i32
  %140 = sub nsw i32 0, %139
  br label %144

141:                                              ; preds = %128
  %142 = load i8, ptr %17, align 1, !tbaa !5
  %143 = zext i8 %142 to i32
  br label %144

144:                                              ; preds = %141, %137
  %145 = phi i32 [ %140, %137 ], [ %143, %141 ]
  %146 = trunc i32 %145 to i8
  store i8 %146, ptr %3, align 1, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 1, ptr %17) #5
  call void @llvm.lifetime.end.p0(i64 1, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 2, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 2, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 2, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #5
  br label %147

147:                                              ; preds = %144, %36, %31
  %148 = getelementptr inbounds %union.posit8_u, ptr %3, i32 0, i32 0
  %149 = load i8, ptr %148, align 1
  ret i8 %149
}

; Function Attrs: nounwind willreturn memory(none)
declare i64 @div(i32 noundef, i32 noundef) #4

; Function Attrs: inlinehint nounwind uwtable
declare zeroext i8 @posit8_roundDiv(i8 noundef signext, i16 noundef zeroext, i1 noundef zeroext) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree 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 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 #4 = { nounwind willreturn memory(none) "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 #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(none) }

!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 = !{!"_Bool", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"short", !6, i64 0}
!12 = !{!13, !14, i64 0}
!13 = !{!"", !14, i64 0, !14, i64 4}
!14 = !{!"int", !6, i64 0}
!15 = !{!13, !14, i64 4}
!16 = !{i8 0, i8 2}
!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"

%union.posit8_u = type { [1 x i8] }

@__const.posit8_divp8.p = external hidden unnamed_addr constant %union.posit8_u, align 1

; 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

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

; Function Attrs: inlinehint nounwind uwtable
declare zeroext i1 @posit8_isnar(i8) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare zeroext i1 @posit8_iszero(i8) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i8 @posit8_divp8(i8 %0, i8 %1) local_unnamed_addr #2 {
  %3 = alloca i8, align 1
  %.sroa.029.0.copyload = load i8, ptr @__const.posit8_divp8.p, align 1
  %4 = tail call zeroext i1 @posit8_isnar(i8 %0)
  br i1 %4, label %37, label %5

5:                                                ; preds = %2
  %6 = tail call zeroext i1 @posit8_isnar(i8 %1)
  br i1 %6, label %37, label %7

7:                                                ; preds = %5
  %8 = tail call zeroext i1 @posit8_iszero(i8 %1)
  br i1 %8, label %37, label %9

9:                                                ; preds = %7
  %10 = tail call zeroext i1 @posit8_iszero(i8 %0)
  br i1 %10, label %37, label %11

11:                                               ; preds = %9
  %.lobit34 = xor i8 %1, %0
  %12 = tail call i8 @llvm.abs.i8(i8 %0, i1 false)
  %13 = tail call i8 @llvm.abs.i8(i8 %1, i1 false)
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %3) #5
  store i8 0, ptr %3, align 1, !tbaa !5
  %14 = call signext i8 @posit8_decode_regime(i8 noundef zeroext %12, ptr noundef nonnull %3)
  %15 = load i8, ptr %3, align 1, !tbaa !5
  %16 = or i8 %15, -128
  %17 = zext i8 %16 to i32
  %18 = shl nuw nsw i32 %17, 7
  %19 = call signext i8 @posit8_decode_regime(i8 noundef zeroext %13, ptr noundef nonnull %3)
  %20 = load i8, ptr %3, align 1, !tbaa !5
  %21 = or i8 %20, -128
  %22 = zext i8 %21 to i32
  %23 = call i64 @div(i32 noundef %18, i32 noundef %22) #6
  %24 = trunc i64 %23 to i16
  %25 = sub i8 %14, %19
  %26 = trunc i64 %23 to i32
  %27 = and i32 %26, 65535
  %.not35 = icmp eq i32 %27, 0
  br i1 %.not35, label %31, label %28

28:                                               ; preds = %11
  %.not36 = icmp ult i32 %27, 128
  %29 = zext i1 %.not36 to i16
  %.030 = shl i16 %24, %29
  %30 = sext i1 %.not36 to i8
  %.0 = add i8 %25, %30
  br label %31

31:                                               ; preds = %28, %11
  %.131 = phi i16 [ %.030, %28 ], [ %24, %11 ]
  %.1 = phi i8 [ %.0, %28 ], [ %25, %11 ]
  %32 = and i64 %23, 281470681743360
  %33 = icmp ne i64 %32, 0
  %34 = call zeroext i8 @posit8_roundDiv(i8 noundef signext %.1, i16 noundef zeroext %.131, i1 noundef zeroext %33)
  %35 = sub i8 0, %34
  %.not37 = icmp slt i8 %.lobit34, 0
  %36 = select i1 %.not37, i8 %35, i8 %34
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %3) #5
  br label %37

37:                                               ; preds = %9, %2, %5, %7, %31
  %.sroa.029.0 = phi i8 [ %.sroa.029.0.copyload, %2 ], [ %.sroa.029.0.copyload, %5 ], [ %.sroa.029.0.copyload, %7 ], [ %36, %31 ], [ 0, %9 ]
  ret i8 %.sroa.029.0
}

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare i64 @div(i32 noundef, i32 noundef) local_unnamed_addr #3

; Function Attrs: inlinehint nounwind uwtable
declare zeroext i8 @posit8_roundDiv(i8 noundef signext, i16 noundef zeroext, i1 noundef zeroext) local_unnamed_addr #1

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(none) }

!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: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass