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

; 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::ResStringPool" = type <{ ptr, i32, [4 x i8], ptr, ptr, i64, %"class.std::mutex", ptr, ptr, ptr, ptr, i32, [4 x i8], ptr, i32, [4 x i8] }>
%"class.std::mutex" = type { %"class.std::__mutex_base" }
%"class.std::__mutex_base" = type { %union.pthread_mutex_t }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%"struct.android::ResStringPool_header" = type { %"struct.android::ResChunk_header", i32, i32, i32, i32, i32 }
%"struct.android::ResChunk_header" = type { i16, i16, i32 }

@stderr = external global ptr, align 8
@.str = external hidden unnamed_addr constant [10 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.24 = external hidden unnamed_addr constant [60 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
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef ptr @_ZNK7android13ResStringPool7styleAtEm(ptr noundef nonnull align 8 dereferenceable(132) %0, i64 noundef %1) #2 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  %8 = load ptr, ptr %4, align 8
  %9 = getelementptr inbounds %"class.android::ResStringPool", ptr %8, i32 0, i32 1
  %10 = load i32, ptr %9, align 8, !tbaa !11
  %11 = icmp eq i32 %10, 0
  br i1 %11, label %12, label %64

12:                                               ; preds = %2
  %13 = load i64, ptr %5, align 8, !tbaa !9
  %14 = getelementptr inbounds %"class.android::ResStringPool", ptr %8, i32 0, i32 4
  %15 = load ptr, ptr %14, align 8, !tbaa !16
  %16 = getelementptr inbounds %"struct.android::ResStringPool_header", ptr %15, i32 0, i32 2
  %17 = load i32, ptr %16, align 4, !tbaa !17
  %18 = zext i32 %17 to i64
  %19 = icmp ult i64 %13, %18
  br i1 %19, label %20, label %64

20:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %21 = getelementptr inbounds %"class.android::ResStringPool", ptr %8, i32 0, i32 8
  %22 = load ptr, ptr %21, align 8, !tbaa !21
  %23 = load i64, ptr %5, align 8, !tbaa !9
  %24 = getelementptr inbounds i32, ptr %22, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !22
  %26 = zext i32 %25 to i64
  %27 = udiv i64 %26, 4
  %28 = trunc i64 %27 to i32
  store i32 %28, ptr %6, align 4, !tbaa !22
  %29 = load i32, ptr %6, align 4, !tbaa !22
  %30 = getelementptr inbounds %"class.android::ResStringPool", ptr %8, i32 0, i32 14
  %31 = load i32, ptr %30, align 8, !tbaa !23
  %32 = icmp ult i32 %29, %31
  br i1 %32, label %33, label %39

33:                                               ; preds = %20
  %34 = getelementptr inbounds %"class.android::ResStringPool", ptr %8, i32 0, i32 13
  %35 = load ptr, ptr %34, align 8, !tbaa !24
  %36 = load i32, ptr %6, align 4, !tbaa !22
  %37 = zext i32 %36 to i64
  %38 = getelementptr inbounds i32, ptr %35, i64 %37
  store ptr %38, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %61

39:                                               ; preds = %20
  br label %40

40:                                               ; preds = %39
  %41 = load ptr, ptr @stderr, align 8, !tbaa !5
  %42 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %41, ptr noundef @.str) #3
  %43 = load ptr, ptr @stderr, align 8, !tbaa !5
  %44 = load i64, ptr %5, align 8, !tbaa !9
  %45 = trunc i64 %44 to i32
  %46 = load i32, ptr %6, align 4, !tbaa !22
  %47 = zext i32 %46 to i64
  %48 = mul i64 %47, 4
  %49 = trunc i64 %48 to i32
  %50 = getelementptr inbounds %"class.android::ResStringPool", ptr %8, i32 0, i32 14
  %51 = load i32, ptr %50, align 8, !tbaa !23
  %52 = zext i32 %51 to i64
  %53 = mul i64 %52, 4
  %54 = trunc i64 %53 to i32
  %55 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %43, ptr noundef @.str.24, i32 noundef %45, i32 noundef %49, i32 noundef %54) #3
  %56 = load ptr, ptr @stderr, align 8, !tbaa !5
  %57 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %56, ptr noundef @.str.2) #3
  br label %58

58:                                               ; preds = %40
  br label %59

59:                                               ; preds = %58
  br label %60

60:                                               ; preds = %59
  store i32 0, ptr %7, align 4
  br label %61

61:                                               ; preds = %60, %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %62 = load i32, ptr %7, align 4
  switch i32 %62, label %67 [
    i32 0, label %63
    i32 1, label %65
  ]

63:                                               ; preds = %61
  br label %64

64:                                               ; preds = %63, %12, %2
  store ptr null, ptr %3, align 8
  br label %65

65:                                               ; preds = %64, %61
  %66 = load ptr, ptr %3, align 8
  ret ptr %66

67:                                               ; preds = %61
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { 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 #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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 8}
!12 = !{!"_ZTSN7android13ResStringPoolE", !13, i64 8, !6, i64 16, !6, i64 24, !10, i64 32, !14, i64 40, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !13, i64 112, !6, i64 120, !13, i64 128}
!13 = !{!"int", !7, i64 0}
!14 = !{!"_ZTSSt5mutex", !15, i64 0}
!15 = !{!"_ZTSSt12__mutex_base", !7, i64 0}
!16 = !{!12, !6, i64 24}
!17 = !{!18, !13, i64 12}
!18 = !{!"_ZTSN7android20ResStringPool_headerE", !19, i64 0, !13, i64 8, !13, i64 12, !13, i64 16, !13, i64 20, !13, i64 24}
!19 = !{!"_ZTSN7android15ResChunk_headerE", !20, i64 0, !20, i64 2, !13, i64 4}
!20 = !{!"short", !7, i64 0}
!21 = !{!12, !6, i64 88}
!22 = !{!13, !13, i64 0}
!23 = !{!12, !13, i64 128}
!24 = !{!12, !6, i64 120}


; 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 [10 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.24 = external hidden unnamed_addr constant [60 x i8], align 1

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

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local noundef ptr @_ZNK7android13ResStringPool7styleAtEm(ptr nocapture noundef nonnull readonly align 8 dereferenceable(132) %0, i64 noundef %1) local_unnamed_addr #1 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %6, label %37

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

13:                                               ; preds = %6
  %14 = getelementptr inbounds i8, ptr %0, i64 88
  %15 = load ptr, ptr %14, align 8, !tbaa !19
  %16 = getelementptr inbounds i32, ptr %15, i64 %1
  %17 = load i32, ptr %16, align 4, !tbaa !20
  %18 = lshr i32 %17, 2
  %19 = getelementptr inbounds i8, ptr %0, i64 128
  %20 = load i32, ptr %19, align 8, !tbaa !21
  %21 = icmp ult i32 %18, %20
  br i1 %21, label %32, label %.thread

.thread:                                          ; preds = %13
  %22 = load ptr, ptr @stderr, align 8, !tbaa !22
  %23 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %22, ptr noundef nonnull @.str) #2
  %24 = load ptr, ptr @stderr, align 8, !tbaa !22
  %25 = trunc nuw i64 %1 to i32
  %26 = and i32 %17, -4
  %27 = load i32, ptr %19, align 8, !tbaa !21
  %28 = shl i32 %27, 2
  %29 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %24, ptr noundef nonnull @.str.24, i32 noundef %25, i32 noundef %26, i32 noundef %28) #2
  %30 = load ptr, ptr @stderr, align 8, !tbaa !22
  %31 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %30, ptr noundef nonnull @.str.2) #2
  br label %37

32:                                               ; preds = %13
  %33 = getelementptr inbounds i8, ptr %0, i64 120
  %34 = load ptr, ptr %33, align 8, !tbaa !23
  %35 = zext nneg i32 %18 to i64
  %36 = getelementptr inbounds i32, ptr %34, i64 %35
  br label %37

37:                                               ; preds = %2, %6, %.thread, %32
  %.1 = phi ptr [ %36, %32 ], [ null, %.thread ], [ null, %6 ], [ null, %2 ]
  ret ptr %.1
}

attributes #0 = { 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 #1 = { 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 #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 8}
!6 = !{!"_ZTSN7android13ResStringPoolE", !7, i64 8, !10, i64 16, !10, i64 24, !11, i64 32, !12, i64 40, !10, i64 80, !10, i64 88, !10, i64 96, !10, i64 104, !7, i64 112, !10, i64 120, !7, i64 128}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!"_ZTSSt5mutex", !13, i64 0}
!13 = !{!"_ZTSSt12__mutex_base", !8, i64 0}
!14 = !{!6, !10, i64 24}
!15 = !{!16, !7, i64 12}
!16 = !{!"_ZTSN7android20ResStringPool_headerE", !17, i64 0, !7, i64 8, !7, i64 12, !7, i64 16, !7, i64 20, !7, i64 24}
!17 = !{!"_ZTSN7android15ResChunk_headerE", !18, i64 0, !18, i64 2, !7, i64 4}
!18 = !{!"short", !8, i64 0}
!19 = !{!6, !10, i64 88}
!20 = !{!7, !7, i64 0}
!21 = !{!6, !7, i64 128}
!22 = !{!10, !10, i64 0}
!23 = !{!6, !10, i64 120}


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