; LLVM IR from facebook/redex :: libresource/ResourceTypes.ll (ZNK7android12ResXMLParser20getAttributeDataTypeEm)

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

%"class.android::ResXMLParser" = type <{ ptr, i32, [4 x i8], ptr, ptr, i32, [4 x i8] }>
%"struct.android::ResXMLTree_attrExt" = type { %"struct.android::ResStringPool_ref", %"struct.android::ResStringPool_ref", i16, i16, i16, i16, i16, i16 }
%"struct.android::ResStringPool_ref" = type { i32 }
%"struct.android::ResXMLTree_attribute" = type { %"struct.android::ResStringPool_ref", %"struct.android::ResStringPool_ref", %"struct.android::ResStringPool_ref", %"struct.android::Res_value" }
%"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 i32 @_ZNK7android12ResXMLParser20getAttributeDataTypeEm(ptr noundef nonnull align 8 dereferenceable(36) %0, i64 noundef %1) #1 align 2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i8, align 1
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  %10 = load ptr, ptr %4, align 8
  %11 = getelementptr inbounds %"class.android::ResXMLParser", ptr %10, i32 0, i32 1
  %12 = load i32, ptr %11, align 8, !tbaa !11
  %13 = icmp eq i32 %12, 258
  br i1 %13, label %14, label %54

14:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %15 = getelementptr inbounds %"class.android::ResXMLParser", ptr %10, i32 0, i32 4
  %16 = load ptr, ptr %15, align 8, !tbaa !15
  store ptr %16, ptr %6, align 8, !tbaa !5
  %17 = load i64, ptr %5, align 8, !tbaa !9
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = getelementptr inbounds %"struct.android::ResXMLTree_attrExt", ptr %18, i32 0, i32 4
  %20 = load i16, ptr %19, align 4, !tbaa !16
  %21 = zext i16 %20 to i64
  %22 = icmp ult i64 %17, %21
  br i1 %22, label %23, label %50

23:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = getelementptr inbounds %"struct.android::ResXMLTree_attrExt", ptr %25, i32 0, i32 2
  %27 = load i16, ptr %26, align 4, !tbaa !20
  %28 = zext i16 %27 to i32
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i8, ptr %24, i64 %29
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = getelementptr inbounds %"struct.android::ResXMLTree_attrExt", ptr %31, i32 0, i32 3
  %33 = load i16, ptr %32, align 2, !tbaa !21
  %34 = zext i16 %33 to i64
  %35 = load i64, ptr %5, align 8, !tbaa !9
  %36 = mul i64 %34, %35
  %37 = getelementptr inbounds i8, ptr %30, i64 %36
  store ptr %37, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #2
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %"struct.android::ResXMLTree_attribute", ptr %38, i32 0, i32 3
  %40 = getelementptr inbounds %"struct.android::Res_value", ptr %39, i32 0, i32 2
  %41 = load i8, ptr %40, align 1, !tbaa !22
  store i8 %41, ptr %8, align 1, !tbaa !25
  %42 = load i8, ptr %8, align 1, !tbaa !25
  %43 = zext i8 %42 to i32
  %44 = icmp ne i32 %43, 7
  br i1 %44, label %45, label %48

45:                                               ; preds = %23
  %46 = load i8, ptr %8, align 1, !tbaa !25
  %47 = zext i8 %46 to i32
  store i32 %47, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %49

48:                                               ; preds = %23
  store i32 1, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %49

49:                                               ; preds = %48, %45
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  br label %51

50:                                               ; preds = %14
  store i32 0, ptr %9, align 4
  br label %51

51:                                               ; preds = %50, %49
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %52 = load i32, ptr %9, align 4
  switch i32 %52, label %57 [
    i32 0, label %53
    i32 1, label %55
  ]

53:                                               ; preds = %51
  br label %54

54:                                               ; preds = %53, %2
  store i32 0, ptr %3, align 4
  br label %55

55:                                               ; preds = %54, %51
  %56 = load i32, ptr %3, align 4
  ret i32 %56

57:                                               ; preds = %51
  unreachable
}

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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 8}
!12 = !{!"_ZTSN7android12ResXMLParserE", !6, i64 0, !13, i64 8, !6, i64 16, !6, i64 24, !14, i64 32}
!13 = !{!"_ZTSN7android12ResXMLParser12event_code_tE", !7, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!12, !6, i64 24}
!16 = !{!17, !19, i64 12}
!17 = !{!"_ZTSN7android18ResXMLTree_attrExtE", !18, i64 0, !18, i64 4, !19, i64 8, !19, i64 10, !19, i64 12, !19, i64 14, !19, i64 16, !19, i64 18}
!18 = !{!"_ZTSN7android17ResStringPool_refE", !14, i64 0}
!19 = !{!"short", !7, i64 0}
!20 = !{!17, !19, i64 8}
!21 = !{!17, !19, i64 10}
!22 = !{!23, !7, i64 15}
!23 = !{!"_ZTSN7android20ResXMLTree_attributeE", !18, i64 0, !18, i64 4, !18, i64 8, !24, i64 12}
!24 = !{!"_ZTSN7android9Res_valueE", !19, i64 0, !7, i64 2, !7, i64 3, !14, i64 4}
!25 = !{!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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_ZNK7android12ResXMLParser20getAttributeDataTypeEm(ptr nocapture noundef nonnull readonly align 8 dereferenceable(36) %0, i64 noundef %1) local_unnamed_addr #0 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = icmp eq i32 %4, 258
  br i1 %5, label %6, label %.thread

6:                                                ; preds = %2
  %7 = getelementptr inbounds i8, ptr %0, i64 24
  %8 = load ptr, ptr %7, align 8, !tbaa !12
  %9 = getelementptr inbounds i8, ptr %8, i64 12
  %10 = load i16, ptr %9, align 4, !tbaa !13
  %11 = zext i16 %10 to i64
  %12 = icmp ugt i64 %11, %1
  br i1 %12, label %13, label %.thread

13:                                               ; preds = %6
  %14 = getelementptr inbounds i8, ptr %8, i64 8
  %15 = load i16, ptr %14, align 4, !tbaa !17
  %16 = zext i16 %15 to i64
  %17 = getelementptr inbounds i8, ptr %8, i64 %16
  %18 = getelementptr inbounds i8, ptr %8, i64 10
  %19 = load i16, ptr %18, align 2, !tbaa !18
  %20 = zext i16 %19 to i64
  %21 = mul nuw nsw i64 %20, %1
  %22 = getelementptr inbounds i8, ptr %17, i64 %21
  %23 = getelementptr inbounds i8, ptr %22, i64 15
  %24 = load i8, ptr %23, align 1, !tbaa !19
  %.not = icmp eq i8 %24, 7
  %narrow = select i1 %.not, i8 1, i8 %24
  %.011 = zext i8 %narrow to i32
  br label %.thread

.thread:                                          ; preds = %6, %2, %13
  %.2 = phi i32 [ %.011, %13 ], [ 0, %2 ], [ 0, %6 ]
  ret i32 %.2
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) 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, !10, i64 8}
!6 = !{!"_ZTSN7android12ResXMLParserE", !7, i64 0, !10, i64 8, !7, i64 16, !7, i64 24, !11, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"_ZTSN7android12ResXMLParser12event_code_tE", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!6, !7, i64 24}
!13 = !{!14, !16, i64 12}
!14 = !{!"_ZTSN7android18ResXMLTree_attrExtE", !15, i64 0, !15, i64 4, !16, i64 8, !16, i64 10, !16, i64 12, !16, i64 14, !16, i64 16, !16, i64 18}
!15 = !{!"_ZTSN7android17ResStringPool_refE", !11, i64 0}
!16 = !{!"short", !8, i64 0}
!17 = !{!14, !16, i64 8}
!18 = !{!14, !16, i64 10}
!19 = !{!20, !8, i64 15}
!20 = !{!"_ZTSN7android20ResXMLTree_attributeE", !15, i64 0, !15, i64 4, !15, i64 8, !21, i64 12}
!21 = !{!"_ZTSN7android9Res_valueE", !16, i64 0, !8, i64 2, !8, i64 3, !11, i64 4}


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