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

; 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.8 = external hidden unnamed_addr constant [66 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [50 x i8], align 1
@__PRETTY_FUNCTION__.lsqpack_enc_enc_str = external hidden unnamed_addr constant [105 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: 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) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @lsqpack_enc_enc_str(i32 noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i32 noundef %4) #2 {
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  store i32 %0, ptr %7, align 4, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !9
  store i64 %2, ptr %9, align 8, !tbaa !11
  store ptr %3, ptr %10, align 8, !tbaa !9
  store i32 %4, ptr %11, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  %16 = load ptr, ptr %10, align 8, !tbaa !9
  %17 = load i32, ptr %11, align 4, !tbaa !5
  %18 = call i32 @qenc_enc_str_size(ptr noundef %16, i32 noundef %17)
  store i32 %18, ptr %13, align 4, !tbaa !5
  %19 = load i32, ptr %13, align 4, !tbaa !5
  %20 = load i32, ptr %11, align 4, !tbaa !5
  %21 = icmp ult i32 %19, %20
  br i1 %21, label %22, label %85

22:                                               ; preds = %5
  %23 = load i32, ptr %13, align 4, !tbaa !5
  %24 = zext i32 %23 to i64
  %25 = load i32, ptr %7, align 4, !tbaa !5
  %26 = call i32 @lsqpack_val2len(i64 noundef %24, i32 noundef %25)
  store i32 %26, ptr %14, align 4, !tbaa !5
  %27 = load i32, ptr %14, align 4, !tbaa !5
  %28 = load i32, ptr %13, align 4, !tbaa !5
  %29 = add i32 %27, %28
  %30 = zext i32 %29 to i64
  %31 = load i64, ptr %9, align 8, !tbaa !11
  %32 = icmp ule i64 %30, %31
  br i1 %32, label %33, label %84

33:                                               ; preds = %22
  %34 = load i32, ptr %7, align 4, !tbaa !5
  %35 = add i32 %34, 1
  %36 = shl i32 1, %35
  %37 = sub nsw i32 %36, 1
  %38 = xor i32 %37, -1
  %39 = load ptr, ptr %8, align 8, !tbaa !9
  %40 = load i8, ptr %39, align 1, !tbaa !13
  %41 = zext i8 %40 to i32
  %42 = and i32 %41, %38
  %43 = trunc i32 %42 to i8
  store i8 %43, ptr %39, align 1, !tbaa !13
  %44 = load i32, ptr %7, align 4, !tbaa !5
  %45 = shl i32 1, %44
  %46 = load ptr, ptr %8, align 8, !tbaa !9
  %47 = load i8, ptr %46, align 1, !tbaa !13
  %48 = zext i8 %47 to i32
  %49 = or i32 %48, %45
  %50 = trunc i32 %49 to i8
  store i8 %50, ptr %46, align 1, !tbaa !13
  %51 = load ptr, ptr %8, align 8, !tbaa !9
  %52 = load i32, ptr %13, align 4, !tbaa !5
  %53 = zext i32 %52 to i64
  %54 = load i32, ptr %7, align 4, !tbaa !5
  call void @lsqpack_enc_int_nocheck(ptr noundef %51, i64 noundef %53, i32 noundef %54)
  %55 = load ptr, ptr %10, align 8, !tbaa !9
  %56 = load ptr, ptr %10, align 8, !tbaa !9
  %57 = load i32, ptr %11, align 4, !tbaa !5
  %58 = zext i32 %57 to i64
  %59 = getelementptr inbounds i8, ptr %56, i64 %58
  %60 = load ptr, ptr %8, align 8, !tbaa !9
  %61 = load i32, ptr %14, align 4, !tbaa !5
  %62 = zext i32 %61 to i64
  %63 = getelementptr inbounds i8, ptr %60, i64 %62
  %64 = call ptr @qenc_huffman_enc(ptr noundef %55, ptr noundef %59, ptr noundef %63)
  store ptr %64, ptr %12, align 8, !tbaa !9
  %65 = load ptr, ptr %12, align 8, !tbaa !9
  %66 = load ptr, ptr %8, align 8, !tbaa !9
  %67 = ptrtoint ptr %65 to i64
  %68 = ptrtoint ptr %66 to i64
  %69 = sub i64 %67, %68
  %70 = trunc i64 %69 to i32
  %71 = load i32, ptr %14, align 4, !tbaa !5
  %72 = load i32, ptr %13, align 4, !tbaa !5
  %73 = add i32 %71, %72
  %74 = icmp eq i32 %70, %73
  br i1 %74, label %75, label %76

75:                                               ; preds = %33
  br label %77

76:                                               ; preds = %33
  call void @__assert_fail(ptr noundef @.str.9, ptr noundef @.str.8, i32 noundef 856, ptr noundef @__PRETTY_FUNCTION__.lsqpack_enc_enc_str) #5
  unreachable

77:                                               ; preds = %75
  %78 = load ptr, ptr %12, align 8, !tbaa !9
  %79 = load ptr, ptr %8, align 8, !tbaa !9
  %80 = ptrtoint ptr %78 to i64
  %81 = ptrtoint ptr %79 to i64
  %82 = sub i64 %80, %81
  %83 = trunc i64 %82 to i32
  store i32 %83, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %122

84:                                               ; preds = %22
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %122

85:                                               ; preds = %5
  %86 = load i32, ptr %11, align 4, !tbaa !5
  %87 = zext i32 %86 to i64
  %88 = load i32, ptr %7, align 4, !tbaa !5
  %89 = call i32 @lsqpack_val2len(i64 noundef %87, i32 noundef %88)
  store i32 %89, ptr %14, align 4, !tbaa !5
  %90 = load i32, ptr %14, align 4, !tbaa !5
  %91 = load i32, ptr %11, align 4, !tbaa !5
  %92 = add i32 %90, %91
  %93 = zext i32 %92 to i64
  %94 = load i64, ptr %9, align 8, !tbaa !11
  %95 = icmp ule i64 %93, %94
  br i1 %95, label %96, label %121

96:                                               ; preds = %85
  %97 = load i32, ptr %7, align 4, !tbaa !5
  %98 = add i32 %97, 1
  %99 = shl i32 1, %98
  %100 = sub nsw i32 %99, 1
  %101 = xor i32 %100, -1
  %102 = load ptr, ptr %8, align 8, !tbaa !9
  %103 = load i8, ptr %102, align 1, !tbaa !13
  %104 = zext i8 %103 to i32
  %105 = and i32 %104, %101
  %106 = trunc i32 %105 to i8
  store i8 %106, ptr %102, align 1, !tbaa !13
  %107 = load ptr, ptr %8, align 8, !tbaa !9
  %108 = load i32, ptr %11, align 4, !tbaa !5
  %109 = zext i32 %108 to i64
  %110 = load i32, ptr %7, align 4, !tbaa !5
  call void @lsqpack_enc_int_nocheck(ptr noundef %107, i64 noundef %109, i32 noundef %110)
  %111 = load ptr, ptr %8, align 8, !tbaa !9
  %112 = load i32, ptr %14, align 4, !tbaa !5
  %113 = zext i32 %112 to i64
  %114 = getelementptr inbounds i8, ptr %111, i64 %113
  %115 = load ptr, ptr %10, align 8, !tbaa !9
  %116 = load i32, ptr %11, align 4, !tbaa !5
  %117 = zext i32 %116 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %114, ptr align 1 %115, i64 %117, i1 false)
  %118 = load i32, ptr %14, align 4, !tbaa !5
  %119 = load i32, ptr %11, align 4, !tbaa !5
  %120 = add i32 %118, %119
  store i32 %120, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %122

121:                                              ; preds = %85
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %122

122:                                              ; preds = %121, %96, %84, %77
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  %123 = load i32, ptr %6, align 4
  ret i32 %123
}

; Function Attrs: nounwind uwtable
declare hidden i32 @qenc_enc_str_size(ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @lsqpack_val2len(i64 noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @lsqpack_enc_int_nocheck(ptr noundef, i64 noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
declare hidden ptr @qenc_huffman_enc(ptr noundef, ptr noundef, ptr noundef) #2

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

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

@.str.8 = external hidden unnamed_addr constant [66 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [50 x i8], align 1
@__PRETTY_FUNCTION__.lsqpack_enc_enc_str = external hidden unnamed_addr constant [105 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @lsqpack_enc_enc_str(i32 noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i32 noundef %4) local_unnamed_addr #1 {
  %6 = tail call i32 @qenc_enc_str_size(ptr noundef %3, i32 noundef %4)
  %7 = icmp ult i32 %6, %4
  br i1 %7, label %8, label %32

8:                                                ; preds = %5
  %9 = zext i32 %6 to i64
  %10 = tail call i32 @lsqpack_val2len(i64 noundef %9, i32 noundef %0)
  %11 = add i32 %10, %6
  %12 = zext i32 %11 to i64
  %.not46 = icmp ugt i64 %12, %2
  br i1 %.not46, label %44, label %13

13:                                               ; preds = %8
  %14 = add i32 %0, 1
  %notmask47 = shl nsw i32 -1, %14
  %15 = load i8, ptr %1, align 1, !tbaa !5
  %16 = trunc i32 %notmask47 to i8
  %17 = and i8 %15, %16
  %18 = shl nuw i32 1, %0
  %19 = trunc i32 %18 to i8
  %20 = or i8 %17, %19
  store i8 %20, ptr %1, align 1, !tbaa !5
  tail call void @lsqpack_enc_int_nocheck(ptr noundef nonnull %1, i64 noundef %9, i32 noundef %0)
  %21 = zext i32 %4 to i64
  %22 = getelementptr inbounds i8, ptr %3, i64 %21
  %23 = zext i32 %10 to i64
  %24 = getelementptr inbounds i8, ptr %1, i64 %23
  %25 = tail call ptr @qenc_huffman_enc(ptr noundef %3, ptr noundef nonnull %22, ptr noundef nonnull %24)
  %26 = ptrtoint ptr %25 to i64
  %27 = ptrtoint ptr %1 to i64
  %28 = sub i64 %26, %27
  %29 = trunc i64 %28 to i32
  %30 = icmp eq i32 %11, %29
  br i1 %30, label %44, label %31

31:                                               ; preds = %13
  tail call void @__assert_fail(ptr noundef nonnull @.str.9, ptr noundef nonnull @.str.8, i32 noundef 856, ptr noundef nonnull @__PRETTY_FUNCTION__.lsqpack_enc_enc_str) #3
  unreachable

32:                                               ; preds = %5
  %33 = zext i32 %4 to i64
  %34 = tail call i32 @lsqpack_val2len(i64 noundef %33, i32 noundef %0)
  %35 = add i32 %34, %4
  %36 = zext i32 %35 to i64
  %.not = icmp ugt i64 %36, %2
  br i1 %.not, label %44, label %37

37:                                               ; preds = %32
  %38 = add i32 %0, 1
  %notmask = shl nsw i32 -1, %38
  %39 = load i8, ptr %1, align 1, !tbaa !5
  %40 = trunc i32 %notmask to i8
  %41 = and i8 %39, %40
  store i8 %41, ptr %1, align 1, !tbaa !5
  tail call void @lsqpack_enc_int_nocheck(ptr noundef nonnull %1, i64 noundef %33, i32 noundef %0)
  %42 = zext i32 %34 to i64
  %43 = getelementptr inbounds i8, ptr %1, i64 %42
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %43, ptr align 1 %3, i64 %33, i1 false)
  br label %44

44:                                               ; preds = %32, %8, %13, %37
  %.0 = phi i32 [ %35, %37 ], [ %11, %13 ], [ -1, %8 ], [ -1, %32 ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare hidden i32 @qenc_enc_str_size(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i32 @lsqpack_val2len(i64 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @lsqpack_enc_int_nocheck(ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden ptr @qenc_huffman_enc(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress 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

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


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