; 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_ostream_s = type { ptr, ptr, i64, i64, ptr }

@.str = external hidden unnamed_addr constant [12 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [20 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @pb_write(ptr noundef, ptr noundef, i64 noundef) #0

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

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

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @pb_encode_submessage(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca i1, align 1
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca %struct.pb_ostream_s, align 8
  %9 = alloca i64, align 8
  %10 = alloca i8, align 1
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 40, ptr %8) #3
  call void @llvm.memset.p0.i64(ptr align 8 %8, i8 0, i64 40, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #3
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = load ptr, ptr %7, align 8, !tbaa !5
  %14 = call zeroext i1 @pb_encode(ptr noundef %8, ptr noundef %12, ptr noundef %13)
  br i1 %14, label %20, label %15

15:                                               ; preds = %3
  %16 = getelementptr inbounds %struct.pb_ostream_s, ptr %8, i32 0, i32 4
  %17 = load ptr, ptr %16, align 8, !tbaa !9
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.pb_ostream_s, ptr %18, i32 0, i32 4
  store ptr %17, ptr %19, align 8, !tbaa !9
  store i1 false, ptr %4, align 1
  store i32 1, ptr %11, align 4
  br label %112

20:                                               ; preds = %3
  %21 = getelementptr inbounds %struct.pb_ostream_s, ptr %8, i32 0, i32 3
  %22 = load i64, ptr %21, align 8, !tbaa !12
  store i64 %22, ptr %9, align 8, !tbaa !13
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = load i64, ptr %9, align 8, !tbaa !13
  %25 = call zeroext i1 @pb_encode_varint(ptr noundef %23, i64 noundef %24)
  br i1 %25, label %27, label %26

26:                                               ; preds = %20
  store i1 false, ptr %4, align 1
  store i32 1, ptr %11, align 4
  br label %112

27:                                               ; preds = %20
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.pb_ostream_s, ptr %28, i32 0, i32 0
  %30 = load ptr, ptr %29, align 8, !tbaa !14
  %31 = icmp eq ptr %30, null
  br i1 %31, label %32, label %36

32:                                               ; preds = %27
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = load i64, ptr %9, align 8, !tbaa !13
  %35 = call zeroext i1 @pb_write(ptr noundef %33, ptr noundef null, i64 noundef %34)
  store i1 %35, ptr %4, align 1
  store i32 1, ptr %11, align 4
  br label %112

36:                                               ; preds = %27
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.pb_ostream_s, ptr %37, i32 0, i32 3
  %39 = load i64, ptr %38, align 8, !tbaa !12
  %40 = load i64, ptr %9, align 8, !tbaa !13
  %41 = add i64 %39, %40
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.pb_ostream_s, ptr %42, i32 0, i32 2
  %44 = load i64, ptr %43, align 8, !tbaa !15
  %45 = icmp ugt i64 %41, %44
  br i1 %45, label %46, label %60

46:                                               ; preds = %36
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.pb_ostream_s, ptr %47, i32 0, i32 4
  %49 = load ptr, ptr %48, align 8, !tbaa !9
  %50 = icmp ne ptr %49, null
  br i1 %50, label %51, label %55

51:                                               ; preds = %46
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.pb_ostream_s, ptr %52, i32 0, i32 4
  %54 = load ptr, ptr %53, align 8, !tbaa !9
  br label %56

55:                                               ; preds = %46
  br label %56

56:                                               ; preds = %55, %51
  %57 = phi ptr [ %54, %51 ], [ @.str, %55 ]
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.pb_ostream_s, ptr %58, i32 0, i32 4
  store ptr %57, ptr %59, align 8, !tbaa !9
  store i1 false, ptr %4, align 1
  store i32 1, ptr %11, align 4
  br label %112

60:                                               ; preds = %36
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.pb_ostream_s, ptr %61, i32 0, i32 0
  %63 = load ptr, ptr %62, align 8, !tbaa !14
  %64 = getelementptr inbounds %struct.pb_ostream_s, ptr %8, i32 0, i32 0
  store ptr %63, ptr %64, align 8, !tbaa !14
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.pb_ostream_s, ptr %65, i32 0, i32 1
  %67 = load ptr, ptr %66, align 8, !tbaa !16
  %68 = getelementptr inbounds %struct.pb_ostream_s, ptr %8, i32 0, i32 1
  store ptr %67, ptr %68, align 8, !tbaa !16
  %69 = load i64, ptr %9, align 8, !tbaa !13
  %70 = getelementptr inbounds %struct.pb_ostream_s, ptr %8, i32 0, i32 2
  store i64 %69, ptr %70, align 8, !tbaa !15
  %71 = getelementptr inbounds %struct.pb_ostream_s, ptr %8, i32 0, i32 3
  store i64 0, ptr %71, align 8, !tbaa !12
  %72 = getelementptr inbounds %struct.pb_ostream_s, ptr %8, i32 0, i32 4
  store ptr null, ptr %72, align 8, !tbaa !9
  %73 = load ptr, ptr %6, align 8, !tbaa !5
  %74 = load ptr, ptr %7, align 8, !tbaa !5
  %75 = call zeroext i1 @pb_encode(ptr noundef %8, ptr noundef %73, ptr noundef %74)
  %76 = zext i1 %75 to i8
  store i8 %76, ptr %10, align 1, !tbaa !17
  %77 = getelementptr inbounds %struct.pb_ostream_s, ptr %8, i32 0, i32 3
  %78 = load i64, ptr %77, align 8, !tbaa !12
  %79 = load ptr, ptr %5, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.pb_ostream_s, ptr %79, i32 0, i32 3
  %81 = load i64, ptr %80, align 8, !tbaa !12
  %82 = add i64 %81, %78
  store i64 %82, ptr %80, align 8, !tbaa !12
  %83 = getelementptr inbounds %struct.pb_ostream_s, ptr %8, i32 0, i32 1
  %84 = load ptr, ptr %83, align 8, !tbaa !16
  %85 = load ptr, ptr %5, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.pb_ostream_s, ptr %85, i32 0, i32 1
  store ptr %84, ptr %86, align 8, !tbaa !16
  %87 = getelementptr inbounds %struct.pb_ostream_s, ptr %8, i32 0, i32 4
  %88 = load ptr, ptr %87, align 8, !tbaa !9
  %89 = load ptr, ptr %5, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.pb_ostream_s, ptr %89, i32 0, i32 4
  store ptr %88, ptr %90, align 8, !tbaa !9
  %91 = getelementptr inbounds %struct.pb_ostream_s, ptr %8, i32 0, i32 3
  %92 = load i64, ptr %91, align 8, !tbaa !12
  %93 = load i64, ptr %9, align 8, !tbaa !13
  %94 = icmp ne i64 %92, %93
  br i1 %94, label %95, label %109

95:                                               ; preds = %60
  %96 = load ptr, ptr %5, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.pb_ostream_s, ptr %96, i32 0, i32 4
  %98 = load ptr, ptr %97, align 8, !tbaa !9
  %99 = icmp ne ptr %98, null
  br i1 %99, label %100, label %104

100:                                              ; preds = %95
  %101 = load ptr, ptr %5, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.pb_ostream_s, ptr %101, i32 0, i32 4
  %103 = load ptr, ptr %102, align 8, !tbaa !9
  br label %105

104:                                              ; preds = %95
  br label %105

105:                                              ; preds = %104, %100
  %106 = phi ptr [ %103, %100 ], [ @.str.3, %104 ]
  %107 = load ptr, ptr %5, align 8, !tbaa !5
  %108 = getelementptr inbounds %struct.pb_ostream_s, ptr %107, i32 0, i32 4
  store ptr %106, ptr %108, align 8, !tbaa !9
  store i1 false, ptr %4, align 1
  store i32 1, ptr %11, align 4
  br label %112

109:                                              ; preds = %60
  %110 = load i8, ptr %10, align 1, !tbaa !17, !range !19, !noundef !20
  %111 = trunc i8 %110 to i1
  store i1 %111, ptr %4, align 1
  store i32 1, ptr %11, align 4
  br label %112

112:                                              ; preds = %109, %105, %56, %32, %26, %15
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 40, ptr %8) #3
  %113 = load i1, ptr %4, align 1
  ret i1 %113
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @pb_encode_varint(ptr noundef, i64 noundef) #0

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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/C++ TBAA"}
!9 = !{!10, !6, i64 32}
!10 = !{!"pb_ostream_s", !6, i64 0, !6, i64 8, !11, i64 16, !11, i64 24, !6, i64 32}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 24}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !6, i64 0}
!15 = !{!10, !11, i64 16}
!16 = !{!10, !6, i64 8}
!17 = !{!18, !18, i64 0}
!18 = !{!"_Bool", !7, i64 0}
!19 = !{i8 0, i8 2}
!20 = !{}


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

%struct.pb_ostream_s = type { ptr, ptr, i64, i64, ptr }

@.str = external hidden unnamed_addr constant [12 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [20 x i8], align 1

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @pb_encode_submessage(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = alloca %struct.pb_ostream_s, align 8
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %4) #3
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(40) %4, i8 0, i64 40, i1 false)
  %5 = call zeroext i1 @pb_encode(ptr noundef nonnull %4, ptr noundef %1, ptr noundef %2)
  br i1 %5, label %10, label %6

6:                                                ; preds = %3
  %7 = getelementptr inbounds i8, ptr %4, i64 32
  %8 = load ptr, ptr %7, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %0, i64 32
  store ptr %8, ptr %9, align 8, !tbaa !5
  br label %43

10:                                               ; preds = %3
  %11 = getelementptr inbounds i8, ptr %4, i64 24
  %12 = load i64, ptr %11, align 8, !tbaa !11
  %13 = call zeroext i1 @pb_encode_varint(ptr noundef %0, i64 noundef %12)
  br i1 %13, label %14, label %43

14:                                               ; preds = %10
  %15 = load ptr, ptr %0, align 8, !tbaa !12
  %16 = icmp eq ptr %15, null
  br i1 %16, label %17, label %19

17:                                               ; preds = %14
  %18 = call zeroext i1 @pb_write(ptr noundef nonnull %0, ptr noundef null, i64 noundef %12)
  br label %43

19:                                               ; preds = %14
  %20 = getelementptr inbounds i8, ptr %0, i64 24
  %21 = load i64, ptr %20, align 8, !tbaa !11
  %22 = add i64 %21, %12
  %23 = getelementptr inbounds i8, ptr %0, i64 16
  %24 = load i64, ptr %23, align 8, !tbaa !13
  %25 = icmp ugt i64 %22, %24
  br i1 %25, label %26, label %29

26:                                               ; preds = %19
  %27 = getelementptr inbounds i8, ptr %0, i64 32
  %28 = load ptr, ptr %27, align 8, !tbaa !5
  %.not31 = icmp eq ptr %28, null
  %spec.select = select i1 %.not31, ptr @.str, ptr %28
  store ptr %spec.select, ptr %27, align 8, !tbaa !5
  br label %43

29:                                               ; preds = %19
  store ptr %15, ptr %4, align 8, !tbaa !12
  %30 = getelementptr inbounds i8, ptr %0, i64 8
  %31 = load ptr, ptr %30, align 8, !tbaa !14
  %32 = getelementptr inbounds i8, ptr %4, i64 8
  store ptr %31, ptr %32, align 8, !tbaa !14
  %33 = getelementptr inbounds i8, ptr %4, i64 16
  store i64 %12, ptr %33, align 8, !tbaa !13
  %34 = getelementptr inbounds i8, ptr %4, i64 32
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %11, i8 0, i64 16, i1 false)
  %35 = call zeroext i1 @pb_encode(ptr noundef nonnull %4, ptr noundef %1, ptr noundef %2)
  %36 = load i64, ptr %11, align 8, !tbaa !11
  %37 = load i64, ptr %20, align 8, !tbaa !11
  %38 = add i64 %37, %36
  store i64 %38, ptr %20, align 8, !tbaa !11
  %39 = load ptr, ptr %32, align 8, !tbaa !14
  store ptr %39, ptr %30, align 8, !tbaa !14
  %40 = load ptr, ptr %34, align 8, !tbaa !5
  %41 = getelementptr inbounds i8, ptr %0, i64 32
  store ptr %40, ptr %41, align 8, !tbaa !5
  %.not = icmp eq i64 %36, %12
  br i1 %.not, label %43, label %42

42:                                               ; preds = %29
  %.not30 = icmp eq ptr %40, null
  %spec.select32 = select i1 %.not30, ptr @.str.3, ptr %40
  store ptr %spec.select32, ptr %41, align 8, !tbaa !5
  br label %43

43:                                               ; preds = %29, %10, %42, %26, %17, %6
  %.0 = phi i1 [ %18, %17 ], [ false, %26 ], [ false, %42 ], [ false, %6 ], [ false, %10 ], [ %35, %29 ]
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %4) #3
  ret i1 %.0
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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, !7, i64 32}
!6 = !{!"pb_ostream_s", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 24, !7, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !10, i64 24}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !10, i64 16}
!14 = !{!6, !7, i64 8}


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