; LLVM IR from litespeedtech/ls-qpack :: lsqpack (lsqpack_enc_int)

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

@.str.7 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [66 x i8], align 1
@__PRETTY_FUNCTION__.lsqpack_enc_int = external hidden unnamed_addr constant [94 x i8], 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: nounwind uwtable
define dso_local ptr @lsqpack_enc_int(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i64 %2, ptr %8, align 8, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %12, ptr %10, align 8, !tbaa !5
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = load ptr, ptr %7, align 8, !tbaa !5
  %15 = icmp ult ptr %13, %14
  br i1 %15, label %16, label %17

16:                                               ; preds = %4
  br label %18

17:                                               ; preds = %4
  call void @__assert_fail(ptr noundef @.str.7, ptr noundef @.str.8, i32 noundef 792, ptr noundef @__PRETTY_FUNCTION__.lsqpack_enc_int) #4
  unreachable

18:                                               ; preds = %16
  %19 = load i64, ptr %8, align 8, !tbaa !9
  %20 = load i32, ptr %9, align 4, !tbaa !11
  %21 = zext i32 %20 to i64
  %22 = shl i64 1, %21
  %23 = sub i64 %22, 1
  %24 = icmp ult i64 %19, %23
  br i1 %24, label %25, label %33

25:                                               ; preds = %18
  %26 = load i64, ptr %8, align 8, !tbaa !9
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = getelementptr inbounds i8, ptr %27, i32 1
  store ptr %28, ptr %6, align 8, !tbaa !5
  %29 = load i8, ptr %27, align 1, !tbaa !13
  %30 = zext i8 %29 to i64
  %31 = or i64 %30, %26
  %32 = trunc i64 %31 to i8
  store i8 %32, ptr %27, align 1, !tbaa !13
  br label %81

33:                                               ; preds = %18
  %34 = load i32, ptr %9, align 4, !tbaa !11
  %35 = shl i32 1, %34
  %36 = sub nsw i32 %35, 1
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds i8, ptr %37, i32 1
  store ptr %38, ptr %6, align 8, !tbaa !5
  %39 = load i8, ptr %37, align 1, !tbaa !13
  %40 = zext i8 %39 to i32
  %41 = or i32 %40, %36
  %42 = trunc i32 %41 to i8
  store i8 %42, ptr %37, align 1, !tbaa !13
  %43 = load i32, ptr %9, align 4, !tbaa !11
  %44 = shl i32 1, %43
  %45 = sub nsw i32 %44, 1
  %46 = sext i32 %45 to i64
  %47 = load i64, ptr %8, align 8, !tbaa !9
  %48 = sub i64 %47, %46
  store i64 %48, ptr %8, align 8, !tbaa !9
  br label %49

49:                                               ; preds = %68, %33
  %50 = load i64, ptr %8, align 8, !tbaa !9
  %51 = icmp uge i64 %50, 128
  br i1 %51, label %52, label %69

52:                                               ; preds = %49
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  %55 = icmp ult ptr %53, %54
  br i1 %55, label %56, label %66

56:                                               ; preds = %52
  %57 = load i64, ptr %8, align 8, !tbaa !9
  %58 = trunc i64 %57 to i8
  %59 = zext i8 %58 to i32
  %60 = or i32 128, %59
  %61 = trunc i32 %60 to i8
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  %63 = getelementptr inbounds i8, ptr %62, i32 1
  store ptr %63, ptr %6, align 8, !tbaa !5
  store i8 %61, ptr %62, align 1, !tbaa !13
  %64 = load i64, ptr %8, align 8, !tbaa !9
  %65 = lshr i64 %64, 7
  store i64 %65, ptr %8, align 8, !tbaa !9
  br label %68

66:                                               ; preds = %52
  %67 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %67, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %83

68:                                               ; preds = %56
  br label %49, !llvm.loop !14

69:                                               ; preds = %49
  %70 = load ptr, ptr %6, align 8, !tbaa !5
  %71 = load ptr, ptr %7, align 8, !tbaa !5
  %72 = icmp ult ptr %70, %71
  br i1 %72, label %73, label %78

73:                                               ; preds = %69
  %74 = load i64, ptr %8, align 8, !tbaa !9
  %75 = trunc i64 %74 to i8
  %76 = load ptr, ptr %6, align 8, !tbaa !5
  %77 = getelementptr inbounds i8, ptr %76, i32 1
  store ptr %77, ptr %6, align 8, !tbaa !5
  store i8 %75, ptr %76, align 1, !tbaa !13
  br label %80

78:                                               ; preds = %69
  %79 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %79, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %83

80:                                               ; preds = %73
  br label %81

81:                                               ; preds = %80, %25
  %82 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %82, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %83

83:                                               ; preds = %81, %78, %66
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %84 = load ptr, ptr %5, align 8
  ret ptr %84
}

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

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { noreturn nounwind "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 }
attributes #4 = { noreturn 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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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

@.str.7 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [66 x i8], align 1
@__PRETTY_FUNCTION__.lsqpack_enc_int = external hidden unnamed_addr constant [94 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @lsqpack_enc_int(ptr noundef %0, ptr noundef readnone %1, i64 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = icmp ult ptr %0, %1
  br i1 %5, label %7, label %6

6:                                                ; preds = %4
  tail call void @__assert_fail(ptr noundef nonnull @.str.7, ptr noundef nonnull @.str.8, i32 noundef 792, ptr noundef nonnull @__PRETTY_FUNCTION__.lsqpack_enc_int) #2
  unreachable

7:                                                ; preds = %4
  %8 = zext nneg i32 %3 to i64
  %notmask = shl nsw i64 -1, %8
  %9 = xor i64 %notmask, -1
  %10 = icmp ugt i64 %9, %2
  br i1 %10, label %11, label %16

11:                                               ; preds = %7
  %12 = getelementptr inbounds i8, ptr %0, i64 1
  %13 = load i8, ptr %0, align 1, !tbaa !5
  %14 = trunc i64 %2 to i8
  %15 = or i8 %13, %14
  store i8 %15, ptr %0, align 1, !tbaa !5
  br label %.loopexit

16:                                               ; preds = %7
  %notmask30 = shl nsw i32 -1, %3
  %17 = xor i32 %notmask30, -1
  %18 = load i8, ptr %0, align 1, !tbaa !5
  %19 = trunc i32 %17 to i8
  %20 = or i8 %18, %19
  store i8 %20, ptr %0, align 1, !tbaa !5
  %21 = zext nneg i32 %17 to i64
  %22 = sub i64 %2, %21
  %.02434 = getelementptr inbounds i8, ptr %0, i64 1
  %23 = icmp ugt i64 %22, 127
  br i1 %23, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %16, %25
  %.02436 = phi ptr [ %.024, %25 ], [ %.02434, %16 ]
  %.02535 = phi i64 [ %28, %25 ], [ %22, %16 ]
  %24 = icmp ult ptr %.02436, %1
  br i1 %24, label %25, label %.loopexit

25:                                               ; preds = %.lr.ph
  %26 = trunc i64 %.02535 to i8
  %27 = or i8 %26, -128
  store i8 %27, ptr %.02436, align 1, !tbaa !5
  %28 = lshr i64 %.02535, 7
  %.024 = getelementptr inbounds i8, ptr %.02436, i64 1
  %29 = icmp ugt i64 %.02535, 16383
  br i1 %29, label %.lr.ph, label %._crit_edge, !llvm.loop !8

._crit_edge:                                      ; preds = %25, %16
  %.025.lcssa = phi i64 [ %22, %16 ], [ %28, %25 ]
  %.pn.lcssa = phi ptr [ %0, %16 ], [ %.02436, %25 ]
  %.024.lcssa = phi ptr [ %.02434, %16 ], [ %.024, %25 ]
  %30 = icmp ult ptr %.024.lcssa, %1
  br i1 %30, label %31, label %.loopexit

31:                                               ; preds = %._crit_edge
  %32 = trunc nuw nsw i64 %.025.lcssa to i8
  %33 = getelementptr inbounds i8, ptr %.pn.lcssa, i64 2
  store i8 %32, ptr %.024.lcssa, align 1, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %11, %31, %._crit_edge
  %.0 = phi ptr [ %0, %._crit_edge ], [ %12, %11 ], [ %33, %31 ], [ %0, %.lr.ph ]
  ret ptr %.0
}

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

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 = { noreturn nounwind "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 = { noreturn 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 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


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