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

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

@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.16 = external hidden unnamed_addr constant [52 x i8], align 1
@.str.23 = external hidden unnamed_addr constant [64 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 @_ZNK7android13ResStringPool14stringDecodeAtEmPKhmPm(ptr noundef nonnull align 8 dereferenceable(132) %0, i64 noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4) #2 align 2 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i64, align 8
  %14 = alloca i64, align 8
  %15 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i64 %1, ptr %8, align 8, !tbaa !9
  store ptr %2, ptr %9, align 8, !tbaa !5
  store i64 %3, ptr %10, align 8, !tbaa !9
  store ptr %4, ptr %11, align 8, !tbaa !5
  %16 = load ptr, ptr %7, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %17 = getelementptr inbounds %"class.android::ResStringPool", ptr %16, i32 0, i32 9
  %18 = load ptr, ptr %17, align 8, !tbaa !11
  store ptr %18, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  store i64 0, ptr %13, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #3
  %19 = load i64, ptr %10, align 8, !tbaa !9
  store i64 %19, ptr %14, align 8, !tbaa !9
  br label %20

20:                                               ; preds = %60, %5
  %21 = load ptr, ptr %9, align 8, !tbaa !5
  %22 = load i64, ptr %14, align 8, !tbaa !9
  %23 = getelementptr inbounds i8, ptr %21, i64 %22
  %24 = load ptr, ptr %12, align 8, !tbaa !5
  %25 = ptrtoint ptr %23 to i64
  %26 = ptrtoint ptr %24 to i64
  %27 = sub i64 %25, %26
  %28 = trunc i64 %27 to i32
  %29 = getelementptr inbounds %"class.android::ResStringPool", ptr %16, i32 0, i32 11
  %30 = load i32, ptr %29, align 8, !tbaa !16
  %31 = icmp ult i32 %28, %30
  br i1 %31, label %32, label %66

32:                                               ; preds = %20
  %33 = load ptr, ptr %9, align 8, !tbaa !5
  %34 = load i64, ptr %14, align 8, !tbaa !9
  %35 = getelementptr inbounds i8, ptr %33, i64 %34
  %36 = load i8, ptr %35, align 1, !tbaa !17
  %37 = zext i8 %36 to i32
  %38 = icmp eq i32 %37, 0
  br i1 %38, label %39, label %60

39:                                               ; preds = %32
  %40 = load i64, ptr %13, align 8, !tbaa !9
  %41 = icmp ne i64 %40, 0
  br i1 %41, label %42, label %56

42:                                               ; preds = %39
  br label %43

43:                                               ; preds = %42
  %44 = load ptr, ptr @stderr, align 8, !tbaa !5
  %45 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %44, ptr noundef @.str) #3
  %46 = load ptr, ptr @stderr, align 8, !tbaa !5
  %47 = load i64, ptr %8, align 8, !tbaa !9
  %48 = trunc i64 %47 to i32
  %49 = load i64, ptr %14, align 8, !tbaa !9
  %50 = trunc i64 %49 to i32
  %51 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %46, ptr noundef @.str.23, i32 noundef %48, i32 noundef %50) #3
  %52 = load ptr, ptr @stderr, align 8, !tbaa !5
  %53 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %52, ptr noundef @.str.2) #3
  br label %54

54:                                               ; preds = %43
  br label %55

55:                                               ; preds = %54
  br label %56

56:                                               ; preds = %55, %39
  %57 = load i64, ptr %14, align 8, !tbaa !9
  %58 = load ptr, ptr %11, align 8, !tbaa !5
  store i64 %57, ptr %58, align 8, !tbaa !9
  %59 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %59, ptr %6, align 8
  store i32 1, ptr %15, align 4
  br label %78

60:                                               ; preds = %32
  %61 = load i64, ptr %13, align 8, !tbaa !9
  %62 = add i64 %61, 1
  store i64 %62, ptr %13, align 8, !tbaa !9
  %63 = shl i64 %62, 15
  %64 = load i64, ptr %10, align 8, !tbaa !9
  %65 = or i64 %63, %64
  store i64 %65, ptr %14, align 8, !tbaa !9
  br label %20, !llvm.loop !18

66:                                               ; preds = %20
  br label %67

67:                                               ; preds = %66
  %68 = load ptr, ptr @stderr, align 8, !tbaa !5
  %69 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %68, ptr noundef @.str) #3
  %70 = load ptr, ptr @stderr, align 8, !tbaa !5
  %71 = load i64, ptr %8, align 8, !tbaa !9
  %72 = trunc i64 %71 to i32
  %73 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %70, ptr noundef @.str.16, i32 noundef %72) #3
  %74 = load ptr, ptr @stderr, align 8, !tbaa !5
  %75 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %74, ptr noundef @.str.2) #3
  br label %76

76:                                               ; preds = %67
  br label %77

77:                                               ; preds = %76
  store ptr null, ptr %6, align 8
  store i32 1, ptr %15, align 4
  br label %78

78:                                               ; preds = %77, %56
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  %79 = load ptr, ptr %6, align 8
  ret ptr %79
}

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, !6, i64 96}
!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, !13, i64 112}
!17 = !{!7, !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"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"

@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.16 = external hidden unnamed_addr constant [52 x i8], align 1
@.str.23 = external hidden unnamed_addr constant [64 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 @_ZNK7android13ResStringPool14stringDecodeAtEmPKhmPm(ptr nocapture noundef nonnull readonly align 8 dereferenceable(132) %0, i64 noundef %1, ptr noundef %2, i64 noundef %3, ptr nocapture noundef writeonly %4) local_unnamed_addr #1 align 2 {
  %6 = getelementptr inbounds i8, ptr %0, i64 96
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %8 = ptrtoint ptr %7 to i64
  %9 = getelementptr inbounds i8, ptr %0, i64 112
  %10 = load i32, ptr %9, align 8, !tbaa !14
  %11 = getelementptr inbounds i8, ptr %2, i64 %3
  %12 = ptrtoint ptr %11 to i64
  %13 = sub i64 %12, %8
  %14 = trunc i64 %13 to i32
  %15 = icmp ugt i32 %10, %14
  br i1 %15, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %5
  %16 = load i8, ptr %11, align 1, !tbaa !15
  %17 = icmp eq i8 %16, 0
  br i1 %17, label %21, label %.lr.ph29

.lr.ph:                                           ; preds = %.lr.ph29
  %18 = load i8, ptr %35, align 1, !tbaa !15
  %19 = icmp eq i8 %18, 0
  br i1 %19, label %.lr.ph._crit_edge, label %.lr.ph29, !llvm.loop !16

.lr.ph._crit_edge:                                ; preds = %.lr.ph
  %20 = icmp eq i64 %32, 0
  br label %21

21:                                               ; preds = %.lr.ph._crit_edge, %.lr.ph.preheader
  %.021.lcssa = phi i64 [ %34, %.lr.ph._crit_edge ], [ %3, %.lr.ph.preheader ]
  %.01620.lcssa = phi i1 [ %20, %.lr.ph._crit_edge ], [ true, %.lr.ph.preheader ]
  br i1 %.01620.lcssa, label %31, label %22

22:                                               ; preds = %21
  %23 = load ptr, ptr @stderr, align 8, !tbaa !18
  %24 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %23, ptr noundef nonnull @.str) #2
  %25 = load ptr, ptr @stderr, align 8, !tbaa !18
  %26 = trunc i64 %1 to i32
  %27 = trunc i64 %.021.lcssa to i32
  %28 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %25, ptr noundef nonnull @.str.23, i32 noundef %26, i32 noundef %27) #2
  %29 = load ptr, ptr @stderr, align 8, !tbaa !18
  %30 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %29, ptr noundef nonnull @.str.2) #2
  br label %31

31:                                               ; preds = %22, %21
  store i64 %.021.lcssa, ptr %4, align 8, !tbaa !19
  br label %47

.lr.ph29:                                         ; preds = %.lr.ph.preheader, %.lr.ph
  %.0162028 = phi i64 [ %32, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  %32 = add i64 %.0162028, 1
  %33 = shl i64 %32, 15
  %34 = or i64 %33, %3
  %35 = getelementptr inbounds i8, ptr %2, i64 %34
  %36 = ptrtoint ptr %35 to i64
  %37 = sub i64 %36, %8
  %38 = trunc i64 %37 to i32
  %39 = icmp ugt i32 %10, %38
  br i1 %39, label %.lr.ph, label %._crit_edge, !llvm.loop !16

._crit_edge:                                      ; preds = %.lr.ph29, %5
  %40 = load ptr, ptr @stderr, align 8, !tbaa !18
  %41 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %40, ptr noundef nonnull @.str) #2
  %42 = load ptr, ptr @stderr, align 8, !tbaa !18
  %43 = trunc i64 %1 to i32
  %44 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %42, ptr noundef nonnull @.str.16, i32 noundef %43) #2
  %45 = load ptr, ptr @stderr, align 8, !tbaa !18
  %46 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %45, ptr noundef nonnull @.str.2) #2
  br label %47

47:                                               ; preds = %._crit_edge, %31
  %.017 = phi ptr [ %2, %31 ], [ null, %._crit_edge ]
  ret ptr %.017
}

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, !10, i64 96}
!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, !7, i64 112}
!15 = !{!8, !8, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!10, !10, i64 0}
!19 = !{!11, !11, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass