; LLVM IR from facebook/redex :: libresource/TypeWrappers.ll (ZN7android11TypeVariantC2EPKNS_13ResTable_typeE)

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

%"union.android::ResTable_sparseTypeEntry" = type { i32 }
%"struct.android::TypeVariant" = type { ptr, i64 }
%"struct.android::ResTable_type" = type { %"struct.android::ResChunk_header", i8, i8, i16, i32, i32, %"struct.android::ResTable_config" }
%"struct.android::ResChunk_header" = type { i16, i16, i32 }
%"struct.android::ResTable_config" = type { i32, %union.anon, %union.anon.0, %union.anon.2, %union.anon.4, %union.anon.6, %union.anon.8, %union.anon.10, %union.anon.12, [4 x i8], [8 x i8], %union.anon.14, i8, [8 x i8] }
%union.anon = type { i32 }
%union.anon.0 = type { i32 }
%union.anon.2 = type { i32 }
%union.anon.4 = type { i32 }
%union.anon.6 = type { i32 }
%union.anon.8 = type { i32 }
%union.anon.10 = type { i32 }
%union.anon.12 = type { i32 }
%union.anon.14 = type { i32 }
%struct.anon.16 = type { i16, i16 }

@stderr = external global ptr, align 8
@.str = external hidden unnamed_addr constant [8 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [50 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN7android11TypeVariantC2EPKNS_13ResTable_typeE(ptr noundef nonnull align 8 dereferenceable(16) %0, ptr noundef %1) unnamed_addr #0 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca %"union.android::ResTable_sparseTypeEntry", align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %9 = load ptr, ptr %3, align 8
  %10 = getelementptr inbounds %"struct.android::TypeVariant", ptr %9, i32 0, i32 0
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %11, ptr %10, align 8, !tbaa !9
  %12 = getelementptr inbounds %"struct.android::TypeVariant", ptr %9, i32 0, i32 1
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = getelementptr inbounds %"struct.android::ResTable_type", ptr %13, i32 0, i32 4
  %15 = load i32, ptr %14, align 4, !tbaa !12
  %16 = zext i32 %15 to i64
  store i64 %16, ptr %12, align 8, !tbaa !19
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %"struct.android::ResTable_type", ptr %17, i32 0, i32 2
  %19 = load i8, ptr %18, align 1, !tbaa !20
  %20 = zext i8 %19 to i32
  %21 = and i32 %20, 1
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %77

23:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %"struct.android::ResTable_type", ptr %24, i32 0, i32 4
  %26 = load i32, ptr %25, align 4, !tbaa !12
  store i32 %26, ptr %5, align 4, !tbaa !21
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = ptrtoint ptr %27 to i64
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %"struct.android::ResTable_type", ptr %29, i32 0, i32 0
  %31 = getelementptr inbounds %"struct.android::ResChunk_header", ptr %30, i32 0, i32 2
  %32 = load i32, ptr %31, align 4, !tbaa !22
  %33 = zext i32 %32 to i64
  %34 = add i64 %28, %33
  store i64 %34, ptr %6, align 8, !tbaa !23
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = ptrtoint ptr %35 to i64
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds %"struct.android::ResTable_type", ptr %37, i32 0, i32 0
  %39 = getelementptr inbounds %"struct.android::ResChunk_header", ptr %38, i32 0, i32 1
  %40 = load i16, ptr %39, align 2, !tbaa !24
  %41 = zext i16 %40 to i64
  %42 = add i64 %36, %41
  %43 = inttoptr i64 %42 to ptr
  store ptr %43, ptr %7, align 8, !tbaa !5
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = ptrtoint ptr %44 to i64
  %46 = load i32, ptr %5, align 4, !tbaa !21
  %47 = zext i32 %46 to i64
  %48 = mul i64 4, %47
  %49 = add i64 %45, %48
  %50 = load i64, ptr %6, align 8, !tbaa !23
  %51 = icmp ugt i64 %49, %50
  br i1 %51, label %52, label %63

52:                                               ; preds = %23
  br label %53

53:                                               ; preds = %52
  %54 = load ptr, ptr @stderr, align 8, !tbaa !5
  %55 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %54, ptr noundef @.str) #3
  %56 = load ptr, ptr @stderr, align 8, !tbaa !5
  %57 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %56, ptr noundef @.str.1) #3
  %58 = load ptr, ptr @stderr, align 8, !tbaa !5
  %59 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %58, ptr noundef @.str.2) #3
  br label %60

60:                                               ; preds = %53
  br label %61

61:                                               ; preds = %60
  %62 = getelementptr inbounds %"struct.android::TypeVariant", ptr %9, i32 0, i32 1
  store i64 0, ptr %62, align 8, !tbaa !19
  br label %76

63:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %64 = load ptr, ptr %7, align 8, !tbaa !5
  %65 = load i32, ptr %5, align 4, !tbaa !21
  %66 = sub i32 %65, 1
  %67 = zext i32 %66 to i64
  %68 = getelementptr inbounds i32, ptr %64, i64 %67
  %69 = load i32, ptr %68, align 4, !tbaa !21
  store i32 %69, ptr %8, align 4, !tbaa !25
  %70 = getelementptr inbounds %struct.anon.16, ptr %8, i32 0, i32 0
  %71 = load i16, ptr %70, align 4, !tbaa !25
  %72 = zext i16 %71 to i32
  %73 = add nsw i32 %72, 1
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds %"struct.android::TypeVariant", ptr %9, i32 0, i32 1
  store i64 %74, ptr %75, align 8, !tbaa !19
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %76

76:                                               ; preds = %63, %61
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %77

77:                                               ; preds = %76, %2
  ret void
}

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

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #2

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind "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 #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++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"_ZTSN7android11TypeVariantE", !6, i64 0, !11, i64 8}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !16, i64 12}
!13 = !{!"_ZTSN7android13ResTable_typeE", !14, i64 0, !7, i64 8, !7, i64 9, !15, i64 10, !16, i64 12, !16, i64 16, !17, i64 20}
!14 = !{!"_ZTSN7android15ResChunk_headerE", !15, i64 0, !15, i64 2, !16, i64 4}
!15 = !{!"short", !7, i64 0}
!16 = !{!"int", !7, i64 0}
!17 = !{!"_ZTSN7android15ResTable_configE", !16, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !7, i64 20, !7, i64 24, !7, i64 28, !7, i64 32, !7, i64 36, !7, i64 40, !7, i64 48, !18, i64 52, !7, i64 53}
!18 = !{!"bool", !7, i64 0}
!19 = !{!10, !11, i64 8}
!20 = !{!13, !7, i64 9}
!21 = !{!16, !16, i64 0}
!22 = !{!13, !16, i64 4}
!23 = !{!11, !11, i64 0}
!24 = !{!13, !15, i64 2}
!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"

@stderr = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [8 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [50 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local void @_ZN7android11TypeVariantC2EPKNS_13ResTable_typeE(ptr nocapture noundef nonnull writeonly align 8 dereferenceable(16) %0, ptr noundef %1) unnamed_addr #0 align 2 {
  store ptr %1, ptr %0, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = getelementptr inbounds i8, ptr %1, i64 12
  %5 = load i32, ptr %4, align 4, !tbaa !11
  %6 = zext i32 %5 to i64
  store i64 %6, ptr %3, align 8, !tbaa !18
  %7 = getelementptr inbounds i8, ptr %1, i64 9
  %8 = load i8, ptr %7, align 1, !tbaa !19
  %9 = and i8 %8, 1
  %.not = icmp eq i8 %9, 0
  br i1 %.not, label %40, label %10

10:                                               ; preds = %2
  %11 = ptrtoint ptr %1 to i64
  %12 = getelementptr inbounds i8, ptr %1, i64 4
  %13 = load i32, ptr %12, align 4, !tbaa !20
  %14 = zext i32 %13 to i64
  %15 = add i64 %14, %11
  %16 = getelementptr inbounds i8, ptr %1, i64 2
  %17 = load i16, ptr %16, align 2, !tbaa !21
  %18 = zext i16 %17 to i64
  %19 = add i64 %18, %11
  %20 = shl nuw nsw i64 %6, 2
  %21 = add i64 %19, %20
  %22 = icmp ugt i64 %21, %15
  br i1 %22, label %23, label %30

23:                                               ; preds = %10
  %24 = load ptr, ptr @stderr, align 8, !tbaa !22
  %25 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %24, ptr noundef nonnull @.str) #2
  %26 = load ptr, ptr @stderr, align 8, !tbaa !22
  %27 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %26, ptr noundef nonnull @.str.1) #2
  %28 = load ptr, ptr @stderr, align 8, !tbaa !22
  %29 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %28, ptr noundef nonnull @.str.2) #2
  br label %39

30:                                               ; preds = %10
  %31 = inttoptr i64 %19 to ptr
  %32 = add i32 %5, -1
  %33 = zext i32 %32 to i64
  %34 = getelementptr inbounds i32, ptr %31, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !23
  %36 = and i32 %35, 65535
  %37 = add nuw nsw i32 %36, 1
  %38 = zext nneg i32 %37 to i64
  br label %39

39:                                               ; preds = %30, %23
  %storemerge = phi i64 [ %38, %30 ], [ 0, %23 ]
  store i64 %storemerge, ptr %3, align 8, !tbaa !18
  br label %40

40:                                               ; preds = %39, %2
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #1

attributes #0 = { mustprogress nofree 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 = { nofree nounwind "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 = { cold 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 0}
!6 = !{!"_ZTSN7android11TypeVariantE", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!12, !15, i64 12}
!12 = !{!"_ZTSN7android13ResTable_typeE", !13, i64 0, !8, i64 8, !8, i64 9, !14, i64 10, !15, i64 12, !15, i64 16, !16, i64 20}
!13 = !{!"_ZTSN7android15ResChunk_headerE", !14, i64 0, !14, i64 2, !15, i64 4}
!14 = !{!"short", !8, i64 0}
!15 = !{!"int", !8, i64 0}
!16 = !{!"_ZTSN7android15ResTable_configE", !15, i64 0, !8, i64 4, !8, i64 8, !8, i64 12, !8, i64 16, !8, i64 20, !8, i64 24, !8, i64 28, !8, i64 32, !8, i64 36, !8, i64 40, !8, i64 48, !17, i64 52, !8, i64 53}
!17 = !{!"bool", !8, i64 0}
!18 = !{!6, !10, i64 8}
!19 = !{!12, !8, i64 9}
!20 = !{!12, !15, i64 4}
!21 = !{!12, !14, i64 2}
!22 = !{!7, !7, i64 0}
!23 = !{!15, !15, i64 0}


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