; LLVM IR from sp-nitech/SPTK :: third_party/REAPER/wave/codec_riff.ll (ZNK4sptk6reaper9UlawCodec11Int16ToUlawEs)

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

@_ZN4sptk6reaper9UlawCodec8seg_uendE = external dso_local constant [8 x i16], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i8 @_ZNK4sptk6reaper9UlawCodec11Int16ToUlawEs(ptr noundef nonnull align 1 dereferenceable(1) %0, i16 noundef signext %1) #0 align 2 {
  %3 = alloca i8, align 1
  %4 = alloca ptr, align 8
  %5 = alloca i16, align 2
  %6 = alloca i16, align 2
  %7 = alloca i16, align 2
  %8 = alloca i8, align 1
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i16 %1, ptr %5, align 2, !tbaa !9
  %10 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 2, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #3
  %11 = load i16, ptr %5, align 2, !tbaa !9
  %12 = sext i16 %11 to i32
  %13 = icmp slt i32 %12, 0
  br i1 %13, label %14, label %19

14:                                               ; preds = %2
  %15 = load i16, ptr %5, align 2, !tbaa !9
  %16 = sext i16 %15 to i32
  %17 = sub nsw i32 0, %16
  %18 = trunc i32 %17 to i16
  store i16 %18, ptr %5, align 2, !tbaa !9
  store i16 127, ptr %6, align 2, !tbaa !9
  br label %20

19:                                               ; preds = %2
  store i16 255, ptr %6, align 2, !tbaa !9
  br label %20

20:                                               ; preds = %19, %14
  %21 = load i16, ptr %5, align 2, !tbaa !9
  %22 = sext i16 %21 to i32
  %23 = icmp sgt i32 %22, 8159
  br i1 %23, label %24, label %25

24:                                               ; preds = %20
  store i16 8159, ptr %5, align 2, !tbaa !9
  br label %25

25:                                               ; preds = %24, %20
  %26 = load i16, ptr %5, align 2, !tbaa !9
  %27 = sext i16 %26 to i32
  %28 = add nsw i32 %27, 33
  %29 = trunc i32 %28 to i16
  store i16 %29, ptr %5, align 2, !tbaa !9
  %30 = load i16, ptr %5, align 2, !tbaa !9
  %31 = call noundef signext i16 @_ZNK4sptk6reaper9UlawCodec13SegmentSearchEsPKsi(ptr noundef nonnull align 1 dereferenceable(1) %10, i16 noundef signext %30, ptr noundef @_ZN4sptk6reaper9UlawCodec8seg_uendE, i32 noundef 8)
  store i16 %31, ptr %7, align 2, !tbaa !9
  %32 = load i16, ptr %7, align 2, !tbaa !9
  %33 = sext i16 %32 to i32
  %34 = icmp sge i32 %33, 8
  br i1 %34, label %35, label %40

35:                                               ; preds = %25
  %36 = load i16, ptr %6, align 2, !tbaa !9
  %37 = sext i16 %36 to i32
  %38 = xor i32 127, %37
  %39 = trunc i32 %38 to i8
  store i8 %39, ptr %3, align 1
  store i32 1, ptr %9, align 4
  br label %61

40:                                               ; preds = %25
  %41 = load i16, ptr %7, align 2, !tbaa !9
  %42 = sext i16 %41 to i32
  %43 = shl i32 %42, 4
  %44 = trunc i32 %43 to i8
  %45 = zext i8 %44 to i32
  %46 = load i16, ptr %5, align 2, !tbaa !9
  %47 = sext i16 %46 to i32
  %48 = load i16, ptr %7, align 2, !tbaa !9
  %49 = sext i16 %48 to i32
  %50 = add nsw i32 %49, 1
  %51 = ashr i32 %47, %50
  %52 = and i32 %51, 15
  %53 = or i32 %45, %52
  %54 = trunc i32 %53 to i8
  store i8 %54, ptr %8, align 1, !tbaa !11
  %55 = load i8, ptr %8, align 1, !tbaa !11
  %56 = zext i8 %55 to i32
  %57 = load i16, ptr %6, align 2, !tbaa !9
  %58 = sext i16 %57 to i32
  %59 = xor i32 %56, %58
  %60 = trunc i32 %59 to i8
  store i8 %60, ptr %3, align 1
  store i32 1, ptr %9, align 4
  br label %61

61:                                               ; preds = %40, %35
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #3
  %62 = load i8, ptr %3, align 1
  ret i8 %62
}

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef signext i16 @_ZNK4sptk6reaper9UlawCodec13SegmentSearchEsPKsi(ptr noundef nonnull align 1 dereferenceable(1), i16 noundef signext, ptr noundef, i32 noundef) #2 align 2

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!7, !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"

@_ZN4sptk6reaper9UlawCodec8seg_uendE = external dso_local constant [8 x i16], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i8 @_ZNK4sptk6reaper9UlawCodec11Int16ToUlawEs(ptr noundef nonnull align 1 dereferenceable(1) %0, i16 noundef signext %1) local_unnamed_addr #0 align 2 {
  %3 = icmp slt i16 %1, 0
  %.014 = select i1 %3, i32 127, i32 255
  %.013 = tail call i16 @llvm.abs.i16(i16 %1, i1 false)
  %spec.store.select = tail call i16 @llvm.smin.i16(i16 %.013, i16 8159)
  %narrow = add nsw i16 %spec.store.select, 33
  %4 = tail call noundef signext i16 @_ZNK4sptk6reaper9UlawCodec13SegmentSearchEsPKsi(ptr noundef nonnull align 1 dereferenceable(1) %0, i16 noundef signext %narrow, ptr noundef nonnull @_ZN4sptk6reaper9UlawCodec8seg_uendE, i32 noundef 8)
  %5 = icmp sgt i16 %4, 7
  br i1 %5, label %6, label %9

6:                                                ; preds = %2
  %7 = trunc nuw i32 %.014 to i8
  %8 = and i8 %7, -128
  br label %19

9:                                                ; preds = %2
  %10 = sext i16 %4 to i32
  %.tr = zext i16 %4 to i32
  %11 = shl nuw nsw i32 %.tr, 4
  %12 = sext i16 %narrow to i32
  %13 = add nsw i32 %10, 1
  %14 = ashr i32 %12, %13
  %15 = and i32 %14, 15
  %16 = or disjoint i32 %15, %11
  %17 = xor i32 %16, %.014
  %18 = trunc i32 %17 to i8
  br label %19

19:                                               ; preds = %9, %6
  %.0 = phi i8 [ %8, %6 ], [ %18, %9 ]
  ret i8 %.0
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef signext i16 @_ZNK4sptk6reaper9UlawCodec13SegmentSearchEsPKsi(ptr noundef nonnull align 1 dereferenceable(1), i16 noundef signext, ptr noundef, i32 noundef) local_unnamed_addr #0 align 2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.smin.i16(i16, i16) #1

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

attributes #0 = { mustprogress 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 speculatable 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)"}


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