; LLVM IR from lich0821/WeChatFerry :: WeChatFerry/rpc/nanopb (pb_encode)

; 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.pb_field_iter_s = type { ptr, ptr, i16, i16, i16, i16, i16, i16, i16, i8, ptr, ptr, ptr, ptr }
%struct.pb_ostream_s = type { ptr, ptr, i64, i64, ptr }

@.str.2 = external hidden unnamed_addr constant [19 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: nounwind uwtable
declare dso_local zeroext i1 @pb_encode_tag(ptr noundef, i32 noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @pb_encode_tag_for_field(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca i1, align 1
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %8 = load ptr, ptr %5, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.pb_field_iter_s, ptr %8, i32 0, i32 9
  %10 = load i8, ptr %9, align 2, !tbaa !9
  %11 = zext i8 %10 to i32
  %12 = and i32 %11, 15
  switch i32 %12, label %17 [
    i32 0, label %13
    i32 1, label %13
    i32 2, label %13
    i32 3, label %13
    i32 4, label %14
    i32 5, label %15
    i32 6, label %16
    i32 7, label %16
    i32 8, label %16
    i32 9, label %16
    i32 11, label %16
  ]

13:                                               ; preds = %2, %2, %2, %2
  store i32 0, ptr %6, align 4, !tbaa !12
  br label %31

14:                                               ; preds = %2
  store i32 5, ptr %6, align 4, !tbaa !12
  br label %31

15:                                               ; preds = %2
  store i32 1, ptr %6, align 4, !tbaa !12
  br label %31

16:                                               ; preds = %2, %2, %2, %2, %2
  store i32 2, ptr %6, align 4, !tbaa !12
  br label %31

17:                                               ; preds = %2
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.pb_ostream_s, ptr %18, i32 0, i32 4
  %20 = load ptr, ptr %19, align 8, !tbaa !14
  %21 = icmp ne ptr %20, null
  br i1 %21, label %22, label %26

22:                                               ; preds = %17
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.pb_ostream_s, ptr %23, i32 0, i32 4
  %25 = load ptr, ptr %24, align 8, !tbaa !14
  br label %27

26:                                               ; preds = %17
  br label %27

27:                                               ; preds = %26, %22
  %28 = phi ptr [ %25, %22 ], [ @.str.2, %26 ]
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.pb_ostream_s, ptr %29, i32 0, i32 4
  store ptr %28, ptr %30, align 8, !tbaa !14
  store i1 false, ptr %3, align 1
  store i32 1, ptr %7, align 4
  br label %39

31:                                               ; preds = %16, %15, %14, %13
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = load i32, ptr %6, align 4, !tbaa !12
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.pb_field_iter_s, ptr %34, i32 0, i32 6
  %36 = load i16, ptr %35, align 8, !tbaa !17
  %37 = zext i16 %36 to i32
  %38 = call zeroext i1 @pb_encode_tag(ptr noundef %32, i32 noundef %33, i32 noundef %37)
  store i1 %38, ptr %3, align 1
  store i32 1, ptr %7, align 4
  br label %39

39:                                               ; preds = %31, %27
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %40 = load i1, ptr %3, align 1
  ret i1 %40
}

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 = { 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, !7, i64 30}
!10 = !{!"pb_field_iter_s", !6, i64 0, !6, i64 8, !11, i64 16, !11, i64 18, !11, i64 20, !11, i64 22, !11, i64 24, !11, i64 26, !11, i64 28, !7, i64 30, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56}
!11 = !{!"short", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !6, i64 32}
!15 = !{!"pb_ostream_s", !6, i64 0, !6, i64 8, !16, i64 16, !16, i64 24, !6, i64 32}
!16 = !{!"long", !7, i64 0}
!17 = !{!10, !11, i64 24}


; 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.2 = external hidden unnamed_addr constant [19 x i8], align 1
@switch.table.pb_encode_tag_for_field = private unnamed_addr constant [12 x i32] [i32 0, i32 0, i32 0, i32 0, i32 5, i32 1, i32 2, i32 2, i32 2, i32 2, i32 0, i32 2], align 4

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @pb_encode_tag(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @pb_encode_tag_for_field(ptr noundef %0, ptr nocapture noundef readonly %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %1, i64 30
  %4 = load i8, ptr %3, align 2, !tbaa !5
  %5 = and i8 %4, 15
  %6 = icmp ult i8 %5, 12
  br i1 %6, label %switch.hole_check, label %7

7:                                                ; preds = %switch.hole_check, %2
  %8 = getelementptr inbounds i8, ptr %0, i64 32
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %.not = icmp eq ptr %9, null
  %spec.select = select i1 %.not, ptr @.str.2, ptr %9
  store ptr %spec.select, ptr %8, align 8, !tbaa !11
  br label %15

switch.hole_check:                                ; preds = %2
  %switch.maskindex = zext nneg i8 %5 to i16
  %switch.shifted = lshr i16 3071, %switch.maskindex
  %switch.lobit = trunc i16 %switch.shifted to i1
  br i1 %switch.lobit, label %switch.lookup, label %7

switch.lookup:                                    ; preds = %switch.hole_check
  %10 = zext nneg i8 %5 to i64
  %switch.gep = getelementptr inbounds [12 x i32], ptr @switch.table.pb_encode_tag_for_field, i64 0, i64 %10
  %switch.load = load i32, ptr %switch.gep, align 4
  %11 = getelementptr inbounds i8, ptr %1, i64 24
  %12 = load i16, ptr %11, align 8, !tbaa !14
  %13 = zext i16 %12 to i32
  %14 = tail call zeroext i1 @pb_encode_tag(ptr noundef %0, i32 noundef %switch.load, i32 noundef %13)
  br label %15

15:                                               ; preds = %switch.lookup, %7
  %.07 = phi i1 [ false, %7 ], [ %14, %switch.lookup ]
  ret i1 %.07
}

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

!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, !8, i64 30}
!6 = !{!"pb_field_iter_s", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 18, !10, i64 20, !10, i64 22, !10, i64 24, !10, i64 26, !10, i64 28, !8, i64 30, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!12, !7, i64 32}
!12 = !{!"pb_ostream_s", !7, i64 0, !7, i64 8, !13, i64 16, !13, i64 24, !7, i64 32}
!13 = !{!"long", !8, i64 0}
!14 = !{!6, !10, i64 24}


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