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

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

@.str.2 = external hidden unnamed_addr constant [16 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
define dso_local zeroext i1 @pb_decode_varint(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca i1, align 1
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i8, align 1
  %7 = alloca i8, align 1
  %8 = alloca i64, align 8
  %9 = 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 1, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #2
  store i8 0, ptr %7, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  store i64 0, ptr %8, align 8, !tbaa !10
  br label %10

10:                                               ; preds = %52, %2
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = call zeroext i1 @pb_readbyte(ptr noundef %11, ptr noundef %6)
  br i1 %12, label %14, label %13

13:                                               ; preds = %10
  store i1 false, ptr %3, align 1
  store i32 1, ptr %9, align 4
  br label %60

14:                                               ; preds = %10
  %15 = load i8, ptr %7, align 1, !tbaa !9
  %16 = zext i8 %15 to i32
  %17 = icmp sge i32 %16, 63
  br i1 %17, label %18, label %37

18:                                               ; preds = %14
  %19 = load i8, ptr %6, align 1, !tbaa !9
  %20 = zext i8 %19 to i32
  %21 = and i32 %20, 254
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %37

23:                                               ; preds = %18
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.pb_istream_s, ptr %24, i32 0, i32 3
  %26 = load ptr, ptr %25, align 8, !tbaa !12
  %27 = icmp ne ptr %26, null
  br i1 %27, label %28, label %32

28:                                               ; preds = %23
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.pb_istream_s, ptr %29, i32 0, i32 3
  %31 = load ptr, ptr %30, align 8, !tbaa !12
  br label %33

32:                                               ; preds = %23
  br label %33

33:                                               ; preds = %32, %28
  %34 = phi ptr [ %31, %28 ], [ @.str.2, %32 ]
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.pb_istream_s, ptr %35, i32 0, i32 3
  store ptr %34, ptr %36, align 8, !tbaa !12
  store i1 false, ptr %3, align 1
  store i32 1, ptr %9, align 4
  br label %60

37:                                               ; preds = %18, %14
  %38 = load i8, ptr %6, align 1, !tbaa !9
  %39 = zext i8 %38 to i32
  %40 = and i32 %39, 127
  %41 = sext i32 %40 to i64
  %42 = load i8, ptr %7, align 1, !tbaa !9
  %43 = zext i8 %42 to i32
  %44 = zext i32 %43 to i64
  %45 = shl i64 %41, %44
  %46 = load i64, ptr %8, align 8, !tbaa !10
  %47 = or i64 %46, %45
  store i64 %47, ptr %8, align 8, !tbaa !10
  %48 = load i8, ptr %7, align 1, !tbaa !9
  %49 = zext i8 %48 to i32
  %50 = add nsw i32 %49, 7
  %51 = trunc i32 %50 to i8
  store i8 %51, ptr %7, align 1, !tbaa !9
  br label %52

52:                                               ; preds = %37
  %53 = load i8, ptr %6, align 1, !tbaa !9
  %54 = zext i8 %53 to i32
  %55 = and i32 %54, 128
  %56 = icmp ne i32 %55, 0
  br i1 %56, label %10, label %57, !llvm.loop !14

57:                                               ; preds = %52
  %58 = load i64, ptr %8, align 8, !tbaa !10
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  store i64 %58, ptr %59, align 8, !tbaa !10
  store i1 true, ptr %3, align 1
  store i32 1, ptr %9, align 4
  br label %60

60:                                               ; preds = %57, %33, %13
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #2
  %61 = load i1, ptr %3, align 1
  ret i1 %61
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i1 @pb_readbyte(ptr noundef, ptr noundef) #1

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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !6, i64 24}
!13 = !{!"pb_istream_s", !6, i64 0, !6, i64 8, !11, i64 16, !6, i64 24}
!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.2 = external hidden unnamed_addr constant [16 x i8], align 1

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

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

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @pb_decode_varint(ptr noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #1 {
  %3 = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %3) #2
  br label %4

4:                                                ; preds = %13, %2
  %.010 = phi i32 [ 0, %2 ], [ %19, %13 ]
  %.0 = phi i64 [ 0, %2 ], [ %18, %13 ]
  %5 = call zeroext i1 @pb_readbyte(ptr noundef %0, ptr noundef nonnull %3)
  br i1 %5, label %6, label %.loopexit

6:                                                ; preds = %4
  %7 = and i32 %.010, 255
  %8 = icmp ult i32 %7, 63
  %9 = load i8, ptr %3, align 1
  %.not = icmp ult i8 %9, 2
  %or.cond = select i1 %8, i1 true, i1 %.not
  br i1 %or.cond, label %13, label %10

10:                                               ; preds = %6
  %11 = getelementptr inbounds i8, ptr %0, i64 24
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  %.not14 = icmp eq ptr %12, null
  %spec.select = select i1 %.not14, ptr @.str.2, ptr %12
  store ptr %spec.select, ptr %11, align 8, !tbaa !5
  br label %.loopexit

13:                                               ; preds = %6
  %14 = and i8 %9, 127
  %15 = zext nneg i8 %14 to i64
  %16 = zext nneg i32 %7 to i64
  %17 = shl i64 %15, %16
  %18 = or i64 %17, %.0
  %19 = add nuw nsw i32 %7, 7
  %.not13 = icmp sgt i8 %9, -1
  br i1 %.not13, label %20, label %4, !llvm.loop !11

20:                                               ; preds = %13
  store i64 %18, ptr %1, align 8, !tbaa !13
  br label %.loopexit

.loopexit:                                        ; preds = %4, %20, %10
  %.011 = phi i1 [ false, %10 ], [ true, %20 ], [ false, %4 ]
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %3) #2
  ret i1 %.011
}

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

attributes #0 = { mustprogress 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, !7, i64 24}
!6 = !{!"pb_istream_s", !7, i64 0, !7, i64 8, !10, i64 16, !7, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!10, !10, i64 0}


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