; LLVM IR from stillwater-sc/universal :: c_api/shim/test/posit/exact_test.ll (Z12add_compressP9workbuf_u)

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

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z3cmpPKvS0_(ptr noundef, ptr noundef) #0

; Function Attrs: inlinehint mustprogress uwtable
declare hidden noundef i32 @_ZL10posit8_cmp8posit8_uS_(i8, i8) #1

; 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) #2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z12add_compressP9workbuf_u(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i8, align 1
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca %struct.posit8x2_s, align 1
  %9 = alloca %union.posit8_u, align 1
  %10 = alloca %union.posit8_u, align 1
  %11 = alloca %union.posit8_u, align 1
  %12 = alloca %union.posit8_u, align 1
  %13 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  store i32 0, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  store i32 1023, ptr %4, align 4, !tbaa !9
  br label %14

14:                                               ; preds = %87, %1
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #5
  store i8 1, ptr %5, align 1, !tbaa !11
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = load i32, ptr %4, align 4, !tbaa !9
  %17 = add nsw i32 %16, 1
  %18 = sext i32 %17 to i64
  call void @qsort(ptr noundef %15, i64 noundef %18, i64 noundef 1, ptr noundef @_Z3cmpPKvS0_)
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  %19 = load i32, ptr %4, align 4, !tbaa !9
  store i32 %19, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %74, %14
  %21 = load i32, ptr %7, align 4, !tbaa !9
  %22 = load i32, ptr %4, align 4, !tbaa !9
  %23 = icmp sle i32 %21, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  br label %77

25:                                               ; preds = %20
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  %27 = load i32, ptr %7, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [1024 x i8], ptr %26, i64 0, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !13
  %31 = icmp ne i8 %30, 0
  br i1 %31, label %33, label %32

32:                                               ; preds = %25
  br label %74

33:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #5
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = load i32, ptr %7, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [1024 x %union.posit8_u], ptr %34, i64 0, i64 %36
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %9, ptr align 1 %37, i64 1, i1 false), !tbaa.struct !14
  %38 = load ptr, ptr %2, align 8, !tbaa !5
  %39 = load i32, ptr %7, align 4, !tbaa !9
  %40 = add nsw i32 %39, 1
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [1024 x %union.posit8_u], ptr %38, i64 0, i64 %41
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %10, ptr align 1 %42, i64 1, i1 false), !tbaa.struct !14
  %43 = getelementptr inbounds %union.posit8_u, ptr %9, i32 0, i32 0
  %44 = load i8, ptr %43, align 1
  %45 = getelementptr inbounds %union.posit8_u, ptr %10, i32 0, i32 0
  %46 = load i8, ptr %45, align 1
  %47 = call i16 @_ZL27posit8_add_exact_bruteforce8posit8_uS_b(i8 %44, i8 %46, i1 noundef zeroext false)
  store i16 %47, ptr %8, align 1
  %48 = load ptr, ptr %2, align 8, !tbaa !5
  %49 = load i32, ptr %7, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [1024 x %union.posit8_u], ptr %48, i64 0, i64 %50
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %11, ptr align 1 %51, i64 1, i1 false), !tbaa.struct !14
  %52 = getelementptr inbounds %struct.posit8x2_s, ptr %8, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %12, ptr align 1 %52, i64 1, i1 false), !tbaa.struct !14
  %53 = getelementptr inbounds %union.posit8_u, ptr %11, i32 0, i32 0
  %54 = load i8, ptr %53, align 1
  %55 = getelementptr inbounds %union.posit8_u, ptr %12, i32 0, i32 0
  %56 = load i8, ptr %55, align 1
  %57 = call noundef i32 @_ZL10posit8_cmp8posit8_uS_(i8 %54, i8 %56)
  %58 = icmp ne i32 %57, 0
  br i1 %58, label %59, label %60

59:                                               ; preds = %33
  store i8 0, ptr %5, align 1, !tbaa !11
  br label %60

60:                                               ; preds = %59, %33
  %61 = getelementptr inbounds %struct.posit8x2_s, ptr %8, i32 0, i32 0
  %62 = load ptr, ptr %2, align 8, !tbaa !5
  %63 = load i32, ptr %7, align 4, !tbaa !9
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [1024 x %union.posit8_u], ptr %62, i64 0, i64 %64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %65, ptr align 1 %61, i64 1, i1 false), !tbaa.struct !14
  %66 = getelementptr inbounds %struct.posit8x2_s, ptr %8, i32 0, i32 1
  %67 = load ptr, ptr %2, align 8, !tbaa !5
  %68 = load i32, ptr %7, align 4, !tbaa !9
  %69 = add nsw i32 %68, 1
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [1024 x %union.posit8_u], ptr %67, i64 0, i64 %70
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %71, ptr align 1 %66, i64 1, i1 false), !tbaa.struct !14
  %72 = load i32, ptr %7, align 4, !tbaa !9
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #5
  br label %74

74:                                               ; preds = %60, %32
  %75 = load i32, ptr %7, align 4, !tbaa !9
  %76 = add nsw i32 %75, 2
  store i32 %76, ptr %7, align 4, !tbaa !9
  br label %20, !llvm.loop !15

77:                                               ; preds = %24
  %78 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %78, ptr %4, align 4, !tbaa !9
  %79 = load i32, ptr %3, align 4, !tbaa !9
  %80 = add nsw i32 %79, 1
  store i32 %80, ptr %3, align 4, !tbaa !9
  %81 = load i8, ptr %5, align 1, !tbaa !11, !range !17, !noundef !18
  %82 = trunc i8 %81 to i1
  br i1 %82, label %83, label %84

83:                                               ; preds = %77
  store i32 2, ptr %13, align 4
  br label %85

84:                                               ; preds = %77
  store i32 0, ptr %13, align 4
  br label %85

85:                                               ; preds = %84, %83
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #5
  %86 = load i32, ptr %13, align 4
  switch i32 %86, label %90 [
    i32 0, label %87
    i32 2, label %88
  ]

87:                                               ; preds = %85
  br label %14, !llvm.loop !19

88:                                               ; preds = %85
  %89 = load i32, ptr %3, align 4, !tbaa !9
  store i32 1, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  ret i32 %89

90:                                               ; preds = %85
  unreachable
}

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

declare void @qsort(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #4

; Function Attrs: mustprogress uwtable
declare hidden i16 @_ZL27posit8_add_exact_bruteforce8posit8_uS_b(i8, i8, i1 noundef zeroext) #0

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

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

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{i64 0, i64 1, !13}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{i8 0, i8 2}
!18 = !{}
!19 = distinct !{!19, !16}


; 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] }

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z3cmpPKvS0_(ptr noundef, ptr noundef) #0

; Function Attrs: inlinehint mustprogress uwtable
declare hidden noundef i32 @_ZL10posit8_cmp8posit8_uS_(i8, i8) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local noundef range(i32 -2147483647, -2147483648) i32 @_Z12add_compressP9workbuf_u(ptr noundef %0) local_unnamed_addr #0 {
  br label %2

2:                                                ; preds = %._crit_edge, %1
  %.028 = phi i32 [ 1023, %1 ], [ %.131, %._crit_edge ]
  %.027 = phi i32 [ 0, %1 ], [ %6, %._crit_edge ]
  %3 = add nsw i32 %.028, 1
  %4 = sext i32 %3 to i64
  tail call void @qsort(ptr noundef %0, i64 noundef %4, i64 noundef 1, ptr noundef nonnull @_Z3cmpPKvS0_)
  %.not35 = icmp slt i32 %.028, 0
  br i1 %.not35, label %._crit_edge.thread, label %.lr.ph

._crit_edge.thread:                               ; preds = %2
  %5 = add nuw nsw i32 %.027, 1
  br label %.loopexit

._crit_edge:                                      ; preds = %15
  %6 = add nuw nsw i32 %.027, 1
  br i1 %.2, label %.loopexit, label %2

.lr.ph:                                           ; preds = %2, %15
  %indvars.iv = phi i64 [ %indvars.iv.next, %15 ], [ 0, %2 ]
  %.02938 = phi i1 [ %.2, %15 ], [ true, %2 ]
  %.03037 = phi i32 [ %.131, %15 ], [ %.028, %2 ]
  %7 = getelementptr inbounds [1024 x i8], ptr %0, i64 0, i64 %indvars.iv
  %8 = load i8, ptr %7, align 1, !tbaa !5
  %.not33 = icmp eq i8 %8, 0
  br i1 %.not33, label %15, label %9

9:                                                ; preds = %.lr.ph
  %10 = or disjoint i64 %indvars.iv, 1
  %11 = getelementptr inbounds [1024 x %union.posit8_u], ptr %0, i64 0, i64 %10
  %.sroa.02.0.copyload = load i8, ptr %11, align 1, !tbaa !5
  %12 = tail call i16 @_ZL27posit8_add_exact_bruteforce8posit8_uS_b(i8 %8, i8 %.sroa.02.0.copyload, i1 noundef zeroext false)
  %.sroa.04.0.extract.trunc = trunc i16 %12 to i8
  %.sroa.5.0.extract.shift = lshr i16 %12, 8
  %.sroa.5.0.extract.trunc = trunc nuw i16 %.sroa.5.0.extract.shift to i8
  %.sroa.01.0.copyload = load i8, ptr %7, align 1, !tbaa !5
  %13 = tail call noundef i32 @_ZL10posit8_cmp8posit8_uS_(i8 %.sroa.01.0.copyload, i8 %.sroa.04.0.extract.trunc)
  %.not34 = icmp eq i32 %13, 0
  %spec.select = select i1 %.not34, i1 %.02938, i1 false
  store i8 %.sroa.04.0.extract.trunc, ptr %7, align 1, !tbaa !5
  store i8 %.sroa.5.0.extract.trunc, ptr %11, align 1, !tbaa !5
  %14 = trunc nuw nsw i64 %10 to i32
  br label %15

15:                                               ; preds = %.lr.ph, %9
  %.131 = phi i32 [ %14, %9 ], [ %.03037, %.lr.ph ]
  %.2 = phi i1 [ %spec.select, %9 ], [ %.02938, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 2
  %16 = trunc nuw i64 %indvars.iv.next to i32
  %.not = icmp slt i32 %.028, %16
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !8

.loopexit:                                        ; preds = %._crit_edge, %._crit_edge.thread
  %17 = phi i32 [ %5, %._crit_edge.thread ], [ %6, %._crit_edge ]
  ret i32 %17
}

; Function Attrs: nofree
declare void @qsort(ptr noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
declare hidden i16 @_ZL27posit8_add_exact_bruteforce8posit8_uS_b(i8, i8, i1 noundef zeroext) local_unnamed_addr #0

attributes #0 = { mustprogress 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 = { inlinehint mustprogress 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 = { nofree "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)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: AggressiveInstCombinePass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass