; LLVM IR from facebook/redex :: libresource/Serialize.ll (ZN4arsc26compute_entry_value_lengthEPN7android14ResTable_entryE)

; 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.android::ResTable_entry" = type { i16, i16, %"struct.android::ResStringPool_ref" }
%"struct.android::ResStringPool_ref" = type { i32 }
%"struct.android::ResTable_map_entry" = type { %"struct.android::ResTable_entry", %"struct.android::ResTable_ref", i32 }
%"struct.android::ResTable_ref" = type { i32 }
%"struct.android::Res_value" = type { i16, i8, i8, i32 }

; 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: mustprogress nounwind uwtable
define dso_local noundef i64 @_ZN4arsc26compute_entry_value_lengthEPN7android14ResTable_entryE(ptr noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i16, align 2
  %5 = alloca i8, align 1
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %12

11:                                               ; preds = %1
  store i64 0, ptr %2, align 8
  br label %50

12:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #2
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %"struct.android::ResTable_entry", ptr %13, i32 0, i32 0
  %15 = load i16, ptr %14, align 4, !tbaa !9
  store i16 %15, ptr %4, align 2, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #2
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %"struct.android::ResTable_entry", ptr %16, i32 0, i32 1
  %18 = load i16, ptr %17, align 2, !tbaa !15
  %19 = zext i16 %18 to i32
  %20 = and i32 %19, 1
  %21 = icmp ne i32 %20, 0
  %22 = zext i1 %21 to i8
  store i8 %22, ptr %5, align 1, !tbaa !16
  %23 = load i8, ptr %5, align 1, !tbaa !16, !range !18, !noundef !19
  %24 = trunc i8 %23 to i1
  br i1 %24, label %25, label %35

25:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %26, ptr %6, align 8, !tbaa !5
  %27 = load i16, ptr %4, align 2, !tbaa !14
  %28 = zext i16 %27 to i64
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds %"struct.android::ResTable_map_entry", ptr %29, i32 0, i32 2
  %31 = load i32, ptr %30, align 4, !tbaa !20
  %32 = zext i32 %31 to i64
  %33 = mul i64 %32, 12
  %34 = add i64 %28, %33
  store i64 %34, ptr %2, align 8
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %49

35:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = load i16, ptr %4, align 2, !tbaa !14
  %38 = zext i16 %37 to i32
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds i8, ptr %36, i64 %39
  store ptr %40, ptr %8, align 8, !tbaa !5
  %41 = load i16, ptr %4, align 2, !tbaa !14
  %42 = zext i16 %41 to i32
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  %44 = getelementptr inbounds %"struct.android::Res_value", ptr %43, i32 0, i32 0
  %45 = load i16, ptr %44, align 4, !tbaa !23
  %46 = zext i16 %45 to i32
  %47 = add nsw i32 %42, %46
  %48 = sext i32 %47 to i64
  store i64 %48, ptr %2, align 8
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  br label %49

49:                                               ; preds = %35, %25
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #2
  br label %50

50:                                               ; preds = %49, %11
  %51 = load i64, ptr %2, align 8
  ret i64 %51
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"_ZTSN7android14ResTable_entryE", !11, i64 0, !11, i64 2, !12, i64 4}
!11 = !{!"short", !7, i64 0}
!12 = !{!"_ZTSN7android17ResStringPool_refE", !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!11, !11, i64 0}
!15 = !{!10, !11, i64 2}
!16 = !{!17, !17, i64 0}
!17 = !{!"bool", !7, i64 0}
!18 = !{i8 0, i8 2}
!19 = !{}
!20 = !{!21, !13, i64 12}
!21 = !{!"_ZTSN7android18ResTable_map_entryE", !10, i64 0, !22, i64 8, !13, i64 12}
!22 = !{!"_ZTSN7android12ResTable_refE", !13, i64 0}
!23 = !{!24, !11, i64 0}
!24 = !{!"_ZTSN7android9Res_valueE", !11, i64 0, !7, i64 2, !7, i64 3, !13, i64 4}


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

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local noundef range(i64 0, 51539673076) i64 @_ZN4arsc26compute_entry_value_lengthEPN7android14ResTable_entryE(ptr noundef readonly %0) local_unnamed_addr #0 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %20, label %3

3:                                                ; preds = %1
  %4 = load i16, ptr %0, align 4, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 2
  %6 = load i16, ptr %5, align 2, !tbaa !12
  %7 = and i16 %6, 1
  %.not = icmp eq i16 %7, 0
  %8 = zext i16 %4 to i64
  br i1 %.not, label %15, label %9

9:                                                ; preds = %3
  %10 = getelementptr inbounds i8, ptr %0, i64 12
  %11 = load i32, ptr %10, align 4, !tbaa !13
  %12 = zext i32 %11 to i64
  %13 = mul nuw nsw i64 %12, 12
  %14 = add nuw nsw i64 %13, %8
  br label %20

15:                                               ; preds = %3
  %16 = getelementptr inbounds i8, ptr %0, i64 %8
  %17 = load i16, ptr %16, align 4, !tbaa !16
  %18 = zext i16 %17 to i64
  %19 = add nuw nsw i64 %18, %8
  br label %20

20:                                               ; preds = %9, %15, %1
  %.1 = phi i64 [ 0, %1 ], [ %14, %9 ], [ %19, %15 ]
  ret i64 %.1
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) 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, !7, i64 0}
!6 = !{!"_ZTSN7android14ResTable_entryE", !7, i64 0, !7, i64 2, !10, i64 4}
!7 = !{!"short", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"_ZTSN7android17ResStringPool_refE", !11, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!6, !7, i64 2}
!13 = !{!14, !11, i64 12}
!14 = !{!"_ZTSN7android18ResTable_map_entryE", !6, i64 0, !15, i64 8, !11, i64 12}
!15 = !{!"_ZTSN7android12ResTable_refE", !11, i64 0}
!16 = !{!17, !7, i64 0}
!17 = !{!"_ZTSN7android9Res_valueE", !7, i64 0, !8, i64 2, !8, i64 3, !11, i64 4}


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